1 /* Copyright (c) 2013, Dmitriy V. Reshetnikov
2 * Copyright (c) 2013, Vsevolod Stakhov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ucl_obj_dump (const ucl_object_t *obj, unsigned int shift)
33 int num = shift * 4 + 5;
34 char *pre = (char *) malloc (num * sizeof(char));
35 const ucl_object_t *cur, *tmp;
36 ucl_object_iter_t it = NULL, it_obj = NULL;
44 while ((obj = ucl_iterate_object (tmp, &it, false))) {
45 printf ("%sucl object address: %p\n", pre + 4, obj);
46 if (obj->key != NULL) {
47 printf ("%skey: \"%s\"\n", pre, ucl_object_key (obj));
49 printf ("%sref: %hd\n", pre, obj->ref);
50 printf ("%slen: %u\n", pre, obj->len);
51 printf ("%sprev: %p\n", pre, obj->prev);
52 printf ("%snext: %p\n", pre, obj->next);
53 if (obj->type == UCL_OBJECT) {
54 printf ("%stype: UCL_OBJECT\n", pre);
55 printf ("%svalue: %p\n", pre, obj->value.ov);
56 while ((cur = ucl_iterate_object (obj, &it_obj, true))) {
57 ucl_obj_dump (cur, shift + 2);
60 else if (obj->type == UCL_ARRAY) {
61 printf ("%stype: UCL_ARRAY\n", pre);
62 printf ("%svalue: %p\n", pre, obj->value.av);
63 ucl_obj_dump (obj->value.av, shift + 2);
65 else if (obj->type == UCL_INT) {
66 printf ("%stype: UCL_INT\n", pre);
67 printf ("%svalue: %jd\n", pre, (intmax_t)ucl_object_toint (obj));
69 else if (obj->type == UCL_FLOAT) {
70 printf ("%stype: UCL_FLOAT\n", pre);
71 printf ("%svalue: %f\n", pre, ucl_object_todouble (obj));
73 else if (obj->type == UCL_STRING) {
74 printf ("%stype: UCL_STRING\n", pre);
75 printf ("%svalue: \"%s\"\n", pre, ucl_object_tostring (obj));
77 else if (obj->type == UCL_BOOLEAN) {
78 printf ("%stype: UCL_BOOLEAN\n", pre);
79 printf ("%svalue: %s\n", pre, ucl_object_tostring_forced (obj));
81 else if (obj->type == UCL_TIME) {
82 printf ("%stype: UCL_TIME\n", pre);
83 printf ("%svalue: %f\n", pre, ucl_object_todouble (obj));
85 else if (obj->type == UCL_USERDATA) {
86 printf ("%stype: UCL_USERDATA\n", pre);
87 printf ("%svalue: %p\n", pre, obj->value.ud);
95 main(int argc, char **argv)
97 const char *fn = NULL;
99 struct ucl_parser *parser;
100 int k, ret = 0, r = 0;
101 ucl_object_t *obj = NULL;
102 const ucl_object_t *par;
110 in = fopen (fn, "r");
119 parser = ucl_parser_new (0);
120 while (!feof (in) && r < (int)sizeof (inbuf)) {
121 r += fread (inbuf + r, 1, sizeof (inbuf) - r, in);
123 ucl_parser_add_chunk (parser, inbuf, r);
125 if (ucl_parser_get_error(parser)) {
126 printf ("Error occured: %s\n", ucl_parser_get_error(parser));
131 obj = ucl_parser_get_object (parser);
132 if (ucl_parser_get_error (parser)) {
133 printf ("Error occured: %s\n", ucl_parser_get_error(parser));
139 for (k = 2; k < argc; k++) {
140 printf ("search for \"%s\"... ", argv[k]);
141 par = ucl_object_find_key (obj, argv[k]);
142 printf ("%sfound\n", (par == NULL )?"not ":"");
143 ucl_obj_dump (par, 0);
147 ucl_obj_dump (obj, 0);
151 if (parser != NULL) {
152 ucl_parser_free (parser);
155 ucl_object_unref (obj);