]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/dd.c
8c3d13502f9cfdfe151b6155bfc81f77f275e157
[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 void *_dd_add_obj(dd_t *dd, void *talloc_chunk, void *obj, size_t size)
46 {
47     AFP_ASSERT(talloc_chunk);
48     
49     if (dd->dd_talloc_array == NULL) {
50         dd->dd_talloc_array = talloc_array(dd, void *, 1);
51     } else {
52         dd->dd_talloc_array = talloc_realloc(dd, dd->dd_talloc_array, void *, talloc_array_length(dd->dd_talloc_array) + 1);
53     }
54
55     memcpy(talloc_chunk, obj, size);
56     dd->dd_talloc_array[talloc_array_length(dd->dd_talloc_array) - 1] = talloc_chunk;
57
58     return 0;
59 }
60
61 static int dd_dump(dd_t *dd, int nestinglevel)
62 {
63     const char *type;
64
65     printf("%sArray(#%d): {\n", neststrings[nestinglevel], talloc_array_length(dd->dd_talloc_array));
66
67     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
68
69         type = talloc_get_name(dd->dd_talloc_array[n]);
70
71         if (STRCMP(type, ==, "int64_t")) {
72             int64_t i;
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")) {
76             bstring b;
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")) {
80             bool bl;
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")) {
84             dd_t *nested;
85             memcpy(&nested, dd->dd_talloc_array[n], sizeof(dd_t *));
86             dd_dump(nested, nestinglevel + 1);
87         }
88     }
89     printf("%s}\n", neststrings[nestinglevel]);
90 }
91
92 #ifdef SPOT_TEST_MAIN
93 #include <stdarg.h>
94
95 int main(int argc, char **argv)
96 {
97     TALLOC_CTX *mem_ctx = talloc_new(NULL);
98     dd_t *dd = talloc_zero(mem_ctx, dd_t);
99     int i;
100
101     set_processname("spot");
102     setuplog("default:info", "/dev/tty");
103
104     LOG(logtype_default, log_info, "Start");
105
106     i = 2;
107     dd_add_obj(dd, &i, int64_t);
108
109     bstring str = bfromcstr("hello world");
110     dd_add_obj(dd, &str, bstring);
111
112     bool b = true;
113     dd_add_obj(dd, &b, bool);
114
115     b = false;
116     dd_add_obj(dd, &b, bool);
117
118     i = 1;
119     dd_add_obj(dd, &i, int64_t);
120
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);
126
127     dd_dump(dd, 0);
128
129     talloc_free(mem_ctx);
130     return 0;
131 }
132 #endif