]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
Give the baby the name dalloc, haha
[netatalk.git] / etc / afpd / spotlight.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 "spotlight.h"
34
35 #ifdef SPOT_TEST_MAIN
36
37 static const char *neststrings[] = {
38     "",
39     "    ",
40     "        ",
41     "            ",
42     "                ",
43     "                    ",
44     "                        "
45 };
46
47 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
48 {
49     const char *type;
50
51     printf("%sArray(#%d): {\n", neststrings[nestinglevel], talloc_array_length(dd->dd_talloc_array));
52
53     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
54
55         type = talloc_get_name(dd->dd_talloc_array[n]);
56
57         if (STRCMP(type, ==, "int64_t")) {
58             int64_t i;
59             memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
60             printf("%s%d:\t%" PRId64 "\n", neststrings[nestinglevel + 1], n, i);
61         } else if (STRCMP(type, ==, "uint32_t")) {
62             uint32_t i;
63             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t));
64             printf("%s%d:\t%" PRIu32 "\n", neststrings[nestinglevel + 1], n, i);
65         } else if (STRCMP(type, ==, "char *")) {
66             char *s;
67             memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
68             printf("%s%d:\t%s\n", neststrings[nestinglevel + 1], n, s);
69         } else if (STRCMP(type, ==, "_Bool")) {
70             bool bl;
71             memcpy(&bl, dd->dd_talloc_array[n], sizeof(bool));
72             printf("%s%d:\t%s\n", neststrings[nestinglevel + 1], n, bl ? "true" : "false");
73         } else if (STRCMP(type, ==, "dd_t")) {
74             DALLOC_CTX *nested;
75             memcpy(&nested, dd->dd_talloc_array[n], sizeof(DALLOC_CTX *));
76             dd_dump(nested, nestinglevel + 1);
77         } else if (STRCMP(type, ==, "cnid_array_t")) {
78             cnid_array_t *cnids;
79             memcpy(&cnids, dd->dd_talloc_array[n], sizeof(cnid_array_t *));
80             printf("%s%d:\tunkn1: %" PRIu16 ", unkn2: %" PRIu32,
81                    neststrings[nestinglevel + 1], n, cnids->ca_unkn1, cnids->ca_unkn2);
82             if (cnids->ca_cnids)
83                 dd_dump(cnids->ca_cnids, nestinglevel + 1);
84         }
85     }
86     printf("%s}\n", neststrings[nestinglevel]);
87 }
88
89 #include <stdarg.h>
90
91 int main(int argc, char **argv)
92 {
93     TALLOC_CTX *mem_ctx = talloc_new(NULL);
94     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
95     int64_t i;
96
97     set_processname("spot");
98     setuplog("default:info", "/dev/tty");
99
100     LOG(logtype_default, log_info, "Start");
101
102     i = 2;
103     dalloc_add(dd, &i, int64_t);
104
105     i = 1;
106     dalloc_add(dd, &i, int64_t);
107
108
109     char *str = talloc_strdup(dd, "hello world");
110     dalloc_add(dd, &str, char *);
111
112     bool b = true;
113     dalloc_add(dd, &b, bool);
114
115     b = false;
116     dalloc_add(dd, &b, bool);
117
118
119     /* add a nested array */
120     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
121     i = 3;
122     dalloc_add(nested, &i, int64_t);
123     dalloc_add(dd, &nested, DALLOC_CTX);
124
125     /* test a CNID array */
126     uint32_t id = 16;
127     cnid_array_t *cnids = talloc_zero(dd, cnid_array_t);
128
129     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
130
131     cnids->ca_unkn1 = 1;
132     cnids->ca_unkn2 = 2;
133
134     dalloc_add(cnids->ca_cnids, &id, uint32_t);
135     dalloc_add(dd, &cnids, cnid_array_t);
136
137     dd_dump(dd, 0);
138
139     talloc_free(mem_ctx);
140     return 0;
141 }
142 #endif