]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/dd.c
1bb95896431a36535907c844b82426002489292e
[netatalk.git] / etc / afpd / dd.c
1 /*
2   Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
3
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.
8
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.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <string.h>
20 #include <strings.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <inttypes.h>
26
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>
32
33 #include "dd.h"
34
35 static const char *neststrings[] = {
36     "",
37     "    ",
38     "        ",
39     "            ",
40     "                ",
41     "                    ",
42     "                        "
43 };
44
45 int _dd_add_obj(dd_t *dd, void *talloc_chunk, void *obj, size_t size, int (*destructor)(void *))
46 {
47     AFP_ASSERT(talloc_chunk);
48
49     if (destructor)
50         talloc_set_destructor(talloc_chunk, destructor);
51     if (dd->dd_talloc_array == NULL) {
52         dd->dd_talloc_array = talloc_array(dd, void *, 1);
53     } else {
54         dd->dd_talloc_array = talloc_realloc(dd, dd->dd_talloc_array, void *, talloc_array_length(dd->dd_talloc_array) + 1);
55     }
56
57     memcpy(talloc_chunk, obj, size);
58     dd->dd_talloc_array[talloc_array_length(dd->dd_talloc_array) - 1] = talloc_chunk;
59
60     return 0;
61 }
62
63 static int dd_dump(dd_t *dd, int nestinglevel)
64 {
65     const char *type;
66
67     printf("%sArray(#%d): {\n", neststrings[nestinglevel], talloc_array_length(dd->dd_talloc_array));
68
69     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
70
71         type = talloc_get_name(dd->dd_talloc_array[n]);
72
73         if (STRCMP(type, ==, "int64_t")) {
74             int64_t i;
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")) {
78             bstring b;
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")) {
82             bool bl;
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")) {
86             dd_t *nested;
87             memcpy(&nested, dd->dd_talloc_array[n], sizeof(dd_t *));
88             dd_dump(nested, nestinglevel + 1);
89         }
90     }
91     printf("%s}\n", neststrings[nestinglevel]);
92 }
93
94 static int bstring_destructor(void *bstr)
95 {
96     bdestroy(*(bstring *)bstr);
97     return 0;
98 }
99
100 #ifdef SPOT_TEST_MAIN
101 #include <stdarg.h>
102
103 int main(int argc, char **argv)
104 {
105     TALLOC_CTX *mem_ctx = talloc_new(NULL);
106     dd_t *dd = talloc_zero(mem_ctx, dd_t);
107     int i;
108
109     set_processname("spot");
110     setuplog("default:info", "/dev/tty");
111
112     LOG(logtype_default, log_info, "Start");
113
114     i = 2;
115     dd_add_obj(dd, &i, int64_t, NULL);
116
117     bstring str = bfromcstr("hello world");
118     dd_add_obj(dd, &str, bstring, bstring_destructor);
119
120     bool b = true;
121     dd_add_obj(dd, &b, bool, NULL);
122
123     b = false;
124     dd_add_obj(dd, &b, bool, NULL);
125
126     i = 1;
127     dd_add_obj(dd, &i, int64_t, NULL);
128
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);
134
135     dd_dump(dd, 0);
136
137     talloc_free(mem_ctx);
138     return 0;
139 }
140 #endif