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 void *_dd_add_obj(dd_t *dd, void *talloc_chunk, void *obj, size_t size)
47 AFP_ASSERT(talloc_chunk);
49 if (dd->dd_talloc_array == NULL) {
50 dd->dd_talloc_array = talloc_array(dd, void *, 1);
52 dd->dd_talloc_array = talloc_realloc(dd, dd->dd_talloc_array, void *, talloc_array_length(dd->dd_talloc_array) + 1);
55 memcpy(talloc_chunk, obj, size);
56 dd->dd_talloc_array[talloc_array_length(dd->dd_talloc_array) - 1] = talloc_chunk;
61 static int dd_dump(dd_t *dd, int nestinglevel)
65 printf("%sArray(#%d): {\n", neststrings[nestinglevel], talloc_array_length(dd->dd_talloc_array));
67 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
69 type = talloc_get_name(dd->dd_talloc_array[n]);
71 if (STRCMP(type, ==, "int64_t")) {
73 memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
74 printf("%s%d:\t%" PRId64 "\n", neststrings[nestinglevel + 1], n, i);
75 } else if (STRCMP(type, ==, "bstring")) {
77 memcpy(&b, dd->dd_talloc_array[n], sizeof(bstring));
78 printf("%s%d:\t%s\n", neststrings[nestinglevel + 1], n, bdata(b));
79 } else if (STRCMP(type, ==, "_Bool")) {
81 memcpy(&bl, dd->dd_talloc_array[n], sizeof(bool));
82 printf("%s%d:\t%s\n", neststrings[nestinglevel + 1], n, bl ? "true" : "false");
83 } else if (STRCMP(type, ==, "dd_t")) {
85 memcpy(&nested, dd->dd_talloc_array[n], sizeof(dd_t *));
86 dd_dump(nested, nestinglevel + 1);
89 printf("%s}\n", neststrings[nestinglevel]);
95 int main(int argc, char **argv)
97 TALLOC_CTX *mem_ctx = talloc_new(NULL);
98 dd_t *dd = talloc_zero(mem_ctx, dd_t);
101 set_processname("spot");
102 setuplog("default:info", "/dev/tty");
104 LOG(logtype_default, log_info, "Start");
107 dd_add_obj(dd, &i, int64_t);
109 bstring str = bfromcstr("hello world");
110 dd_add_obj(dd, &str, bstring);
113 dd_add_obj(dd, &b, bool);
116 dd_add_obj(dd, &b, bool);
119 dd_add_obj(dd, &i, int64_t);
121 /* add a nested array */
122 dd_t *nested = talloc_zero(dd, dd_t);
123 dd_add_obj(nested, &i, int64_t);
124 dd_add_obj(nested, &str, bstring);
125 dd_add_obj(dd, &nested, dd_t);
129 talloc_free(mem_ctx);