1/* Copyright (c) 2013, Dmitriy V. Reshetnikov
2 * Copyright (c) 2013, Vsevolod Stakhov
3 * All rights reserved.
4 *
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.
12 *
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.
23 */
24
25#include <stdio.h>
26#include <errno.h>
27
28#include "ucl.h"
29
30void
31ucl_obj_dump (const ucl_object_t *obj, unsigned int shift)
32{
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;
37
38	pre[--num] = 0x00;
39	while (num--)
40		pre[num] = 0x20;
41
42	tmp = obj;
43
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));
48		}
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);
58			}
59		}
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);
64		}
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));
68		}
69		else if (obj->type == UCL_FLOAT) {
70			printf ("%stype: UCL_FLOAT\n", pre);
71			printf ("%svalue: %f\n", pre, ucl_object_todouble (obj));
72		}
73		else if (obj->type == UCL_STRING) {
74			printf ("%stype: UCL_STRING\n", pre);
75			printf ("%svalue: \"%s\"\n", pre, ucl_object_tostring (obj));
76		}
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));
80		}
81		else if (obj->type == UCL_TIME) {
82			printf ("%stype: UCL_TIME\n", pre);
83			printf ("%svalue: %f\n", pre, ucl_object_todouble (obj));
84		}
85		else if (obj->type == UCL_USERDATA) {
86			printf ("%stype: UCL_USERDATA\n", pre);
87			printf ("%svalue: %p\n", pre, obj->value.ud);
88		}
89	}
90
91	free (pre);
92}
93
94int
95main(int argc, char **argv)
96{
97	const char *fn = NULL;
98	char inbuf[8192];
99	struct ucl_parser *parser;
100	int k, ret = 0, r = 0;
101	ucl_object_t *obj = NULL;
102	const ucl_object_t *par;
103	FILE *in;
104
105	if (argc > 1) {
106		fn = argv[1];
107	}
108
109	if (fn != NULL) {
110		in = fopen (fn, "r");
111		if (in == NULL) {
112			exit (-errno);
113		}
114	}
115	else {
116		in = stdin;
117	}
118
119	parser = ucl_parser_new (0);
120	while (!feof (in) && r < (int)sizeof (inbuf)) {
121		r += fread (inbuf + r, 1, sizeof (inbuf) - r, in);
122	}
123	ucl_parser_add_chunk (parser, inbuf, r);
124	fclose (in);
125	if (ucl_parser_get_error(parser)) {
126		printf ("Error occured: %s\n", ucl_parser_get_error(parser));
127		ret = 1;
128		goto end;
129	}
130
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));
134		ret = 1;
135		goto end;
136	}
137
138	if (argc > 2) {
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);
144		}
145	}
146	else {
147		ucl_obj_dump (obj, 0);
148	}
149
150end:
151	if (parser != NULL) {
152		ucl_parser_free (parser);
153	}
154	if (obj != NULL) {
155		ucl_object_unref (obj);
156	}
157
158	return ret;
159}
160