1 /* Copyright (c) 2013, Vsevolod Stakhov
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *       * Redistributions of source code must retain the above copyright
7  *         notice, this list of conditions and the following disclaimer.
8  *       * Redistributions in binary form must reproduce the above copyright
9  *         notice, this list of conditions and the following disclaimer in the
10  *         documentation and/or other materials provided with the distribution.
11  *
12  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  */
23 
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include "ucl.h"
28 
29 int
30 main (int argc, char **argv)
31 {
32 	ucl_object_t *obj, *cur, *ar, *ref;
33 	ucl_object_iter_t it;
34 	const ucl_object_t *found, *it_obj;
35 	FILE *out;
36 	unsigned char *emitted;
37 	const char *fname_out = NULL;
38 	int ret = 0;
39 
40 	switch (argc) {
41 	case 2:
42 		fname_out = argv[1];
43 		break;
44 	}
45 
46 
47 	if (fname_out != NULL) {
48 		out = fopen (fname_out, "w");
49 		if (out == NULL) {
50 			exit (-errno);
51 		}
52 	}
53 	else {
54 		out = stdout;
55 	}
56 
57 	obj = ucl_object_typed_new (UCL_OBJECT);
58 
59 	/* Keys replacing */
60 	cur = ucl_object_fromstring_common ("value1", 0, UCL_STRING_TRIM);
61 	ucl_object_insert_key (obj, cur, "key0", 0, false);
62 	cur = ucl_object_fromdouble (0.1);
63 	assert (ucl_object_replace_key (obj, cur, "key0", 0, false));
64 
65 	/* Create some strings */
66 	cur = ucl_object_fromstring_common ("  test string    ", 0, UCL_STRING_TRIM);
67 	ucl_object_insert_key (obj, cur, "key1", 0, false);
68 	cur = ucl_object_fromstring_common ("  test \nstring\n    ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE);
69 	ucl_object_insert_key (obj, cur, "key2", 0, false);
70 	cur = ucl_object_fromstring_common ("  test string    \n", 0, 0);
71 	ucl_object_insert_key (obj, cur, "key3", 0, false);
72 	/* Array of numbers */
73 	ar = ucl_object_typed_new (UCL_ARRAY);
74 	cur = ucl_object_fromint (10);
75 	ucl_array_append (ar, cur);
76 	cur = ucl_object_fromdouble (10.1);
77 	ucl_array_append (ar, cur);
78 	cur = ucl_object_fromdouble (9.999);
79 	ucl_array_prepend (ar, cur);
80 
81 	/* Removing from an array */
82 	cur = ucl_object_fromdouble (1.0);
83 	ucl_array_append (ar, cur);
84 	cur = ucl_array_delete (ar, cur);
85 	assert (ucl_object_todouble (cur) == 1.0);
86 	ucl_object_unref (cur);
87 	cur = ucl_object_fromdouble (2.0);
88 	ucl_array_append (ar, cur);
89 	cur = ucl_array_pop_last (ar);
90 	assert (ucl_object_todouble (cur) == 2.0);
91 	ucl_object_unref (cur);
92 	cur = ucl_object_fromdouble (3.0);
93 	ucl_array_prepend (ar, cur);
94 	cur = ucl_array_pop_first (ar);
95 	assert (ucl_object_todouble (cur) == 3.0);
96 	ucl_object_unref (cur);
97 
98 	ucl_object_insert_key (obj, ar, "key4", 0, false);
99 	cur = ucl_object_frombool (true);
100 	/* Ref object to test refcounts */
101 	ref = ucl_object_ref (cur);
102 	ucl_object_insert_key (obj, cur, "key4", 0, false);
103 	/* Empty strings */
104 	cur = ucl_object_fromstring_common ("      ", 0, UCL_STRING_TRIM);
105 	ucl_object_insert_key (obj, cur, "key5", 0, false);
106 	cur = ucl_object_fromstring_common ("", 0, UCL_STRING_ESCAPE);
107 	ucl_object_insert_key (obj, cur, "key6", 0, false);
108 	cur = ucl_object_fromstring_common ("   \n", 0, UCL_STRING_ESCAPE);
109 	ucl_object_insert_key (obj, cur, "key7", 0, false);
110 	/* Numbers and booleans */
111 	cur = ucl_object_fromstring_common ("1mb", 0, UCL_STRING_ESCAPE | UCL_STRING_PARSE);
112 	ucl_object_insert_key (obj, cur, "key8", 0, false);
113 	cur = ucl_object_fromstring_common ("3.14", 0, UCL_STRING_PARSE);
114 	ucl_object_insert_key (obj, cur, "key9", 0, false);
115 	cur = ucl_object_fromstring_common ("true", 0, UCL_STRING_PARSE);
116 	ucl_object_insert_key (obj, cur, "key10", 0, false);
117 	cur = ucl_object_fromstring_common ("  off  ", 0, UCL_STRING_PARSE | UCL_STRING_TRIM);
118 	ucl_object_insert_key (obj, cur, "key11", 0, false);
119 	cur = ucl_object_fromstring_common ("gslin@gslin.org", 0, UCL_STRING_PARSE_INT);
120 	ucl_object_insert_key (obj, cur, "key12", 0, false);
121 	cur = ucl_object_fromstring_common ("#test", 0, UCL_STRING_PARSE_INT);
122 	ucl_object_insert_key (obj, cur, "key13", 0, false);
123 	cur = ucl_object_frombool (true);
124 	ucl_object_insert_key (obj, cur, "k=3", 0, false);
125 
126 	/* Try to find using path */
127 	/* Should exist */
128 	found = ucl_lookup_path (obj, "key4.1");
129 	assert (found != NULL && ucl_object_toint (found) == 10);
130 	/* . should be ignored */
131 	found = ucl_lookup_path (obj, ".key4.1");
132 	assert (found != NULL && ucl_object_toint (found) == 10);
133 	/* moar dots... */
134 	found = ucl_lookup_path (obj, ".key4........1...");
135 	assert (found != NULL && ucl_object_toint (found) == 10);
136 	/* No such index */
137 	found = ucl_lookup_path (obj, ".key4.3");
138 	assert (found == NULL);
139 	/* No such key */
140 	found = ucl_lookup_path (obj, "key9..key1");
141 	assert (found == NULL);
142 
143 	/* Test iteration */
144 	it = ucl_object_iterate_new (obj);
145 	it_obj = ucl_object_iterate_safe (it, true);
146 	/* key0 = 0.1 */
147 	assert (ucl_object_type (it_obj) == UCL_FLOAT);
148 	it_obj = ucl_object_iterate_safe (it, true);
149 	/* key1 = "" */
150 	assert (ucl_object_type (it_obj) == UCL_STRING);
151 	it_obj = ucl_object_iterate_safe (it, true);
152 	/* key2 = "" */
153 	assert (ucl_object_type (it_obj) == UCL_STRING);
154 	it_obj = ucl_object_iterate_safe (it, true);
155 	/* key3 = "" */
156 	assert (ucl_object_type (it_obj) == UCL_STRING);
157 	it_obj = ucl_object_iterate_safe (it, true);
158 	/* key4 = ([float, int, float], boolean) */
159 	ucl_object_iterate_reset (it, it_obj);
160 	it_obj = ucl_object_iterate_safe (it, true);
161 	assert (ucl_object_type (it_obj) == UCL_FLOAT);
162 	it_obj = ucl_object_iterate_safe (it, true);
163 	assert (ucl_object_type (it_obj) == UCL_INT);
164 	it_obj = ucl_object_iterate_safe (it, true);
165 	assert (ucl_object_type (it_obj) == UCL_FLOAT);
166 	it_obj = ucl_object_iterate_safe (it, true);
167 	assert (ucl_object_type (it_obj) == UCL_BOOLEAN);
168 	ucl_object_iterate_free (it);
169 
170 	emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG);
171 
172 	fprintf (out, "%s\n", emitted);
173 	ucl_object_unref (obj);
174 
175 	if (emitted != NULL) {
176 		free (emitted);
177 	}
178 	fclose (out);
179 
180 	/* Ref should still be accessible */
181 	ref->value.iv = 100500;
182 	ucl_object_unref (ref);
183 
184 	return ret;
185 }
186