2 Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
27 #include <atalk/errchk.h>
28 #include <atalk/util.h>
29 #include <atalk/logger.h>
30 #include <atalk/talloc.h>
31 #include <atalk/bstrlib.h>
35 static const char *neststrings[] = {
45 int _dd_add_obj(dd_t *dd, void *talloc_chunk, void *obj, size_t size, int (*destructor)(void *))
47 AFP_ASSERT(talloc_chunk);
50 talloc_set_destructor(talloc_chunk, destructor);
51 if (dd->dd_talloc_array == NULL) {
52 dd->dd_talloc_array = talloc_array(dd, void *, 1);
54 dd->dd_talloc_array = talloc_realloc(dd, dd->dd_talloc_array, void *, talloc_array_length(dd->dd_talloc_array) + 1);
57 memcpy(talloc_chunk, obj, size);
58 dd->dd_talloc_array[talloc_array_length(dd->dd_talloc_array) - 1] = talloc_chunk;
63 static int dd_dump(dd_t *dd, int nestinglevel)
67 printf("%sArray(#%d): {\n", neststrings[nestinglevel], talloc_array_length(dd->dd_talloc_array));
69 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
71 type = talloc_get_name(dd->dd_talloc_array[n]);
73 if (STRCMP(type, ==, "int64_t")) {
75 memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
76 printf("%s%d:\t%" PRId64 "\n", neststrings[nestinglevel + 1], n, i);
77 } else if (STRCMP(type, ==, "bstring")) {
79 memcpy(&b, dd->dd_talloc_array[n], sizeof(bstring));
80 printf("%s%d:\t%s\n", neststrings[nestinglevel + 1], n, bdata(b));
81 } else if (STRCMP(type, ==, "_Bool")) {
83 memcpy(&bl, dd->dd_talloc_array[n], sizeof(bool));
84 printf("%s%d:\t%s\n", neststrings[nestinglevel + 1], n, bl ? "true" : "false");
85 } else if (STRCMP(type, ==, "dd_t")) {
87 memcpy(&nested, dd->dd_talloc_array[n], sizeof(dd_t *));
88 dd_dump(nested, nestinglevel + 1);
91 printf("%s}\n", neststrings[nestinglevel]);
94 static int bstring_destructor(void *bstr)
96 bdestroy(*(bstring *)bstr);
100 #ifdef SPOT_TEST_MAIN
103 int main(int argc, char **argv)
105 TALLOC_CTX *mem_ctx = talloc_new(NULL);
106 dd_t *dd = talloc_zero(mem_ctx, dd_t);
109 set_processname("spot");
110 setuplog("default:info", "/dev/tty");
112 LOG(logtype_default, log_info, "Start");
115 dd_add_obj(dd, &i, int64_t, NULL);
117 bstring str = bfromcstr("hello world");
118 dd_add_obj(dd, &str, bstring, bstring_destructor);
121 dd_add_obj(dd, &b, bool, NULL);
124 dd_add_obj(dd, &b, bool, NULL);
127 dd_add_obj(dd, &i, int64_t, NULL);
129 /* add a nested array */
130 dd_t *nested = talloc_zero(dd, dd_t);
131 dd_add_obj(nested, &i, int64_t, NULL);
132 dd_add_obj(nested, &str, bstring, bstring_destructor);
133 dd_add_obj(dd, &nested, dd_t, NULL);
137 talloc_free(mem_ctx);