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 #include <sys/types.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 int
main(int argc,char ** argv)34 main (int argc, char **argv)
35 {
36 ucl_object_t *obj, *cur, *ar;
37 FILE *out;
38 const char *fname_out = NULL;
39 struct ucl_emitter_context *ctx;
40 struct ucl_emitter_functions *f;
41 int ret = 0, opt, json = 0, compact = 0, yaml = 0;
42
43 while ((opt = getopt(argc, argv, "jcy")) != -1) {
44 switch (opt) {
45 case 'j':
46 json = 1;
47 break;
48 case 'c':
49 compact = 1;
50 break;
51 case 'y':
52 yaml = 1;
53 break;
54 default: /* '?' */
55 fprintf (stderr, "Usage: %s [-jcy] [out]\n",
56 argv[0]);
57 exit (EXIT_FAILURE);
58 }
59 }
60
61 argc -= optind;
62 argv += optind;
63
64 switch (argc) {
65 case 2:
66 fname_out = argv[1];
67 break;
68 }
69
70 if (fname_out != NULL) {
71 out = fopen (fname_out, "w");
72 if (out == NULL) {
73 exit (-errno);
74 }
75 }
76 else {
77 out = stdout;
78 }
79
80 obj = ucl_object_typed_new (UCL_OBJECT);
81
82 /* Create some strings */
83 cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM);
84 ucl_object_insert_key (obj, cur, "key1", 0, false);
85 cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE);
86 ucl_object_insert_key (obj, cur, "key2", 0, false);
87 cur = ucl_object_fromstring_common (" test string \n", 0, 0);
88 ucl_object_insert_key (obj, cur, "key3", 0, false);
89
90 f = ucl_object_emit_file_funcs (out);
91
92 if (yaml) {
93 ctx = ucl_object_emit_streamline_new (obj, UCL_EMIT_YAML, f);
94 }
95 else if (json) {
96 if (compact) {
97 ctx = ucl_object_emit_streamline_new (obj, UCL_EMIT_JSON_COMPACT, f);
98 }
99 else {
100 ctx = ucl_object_emit_streamline_new (obj, UCL_EMIT_JSON, f);
101 }
102 }
103 else {
104 ctx = ucl_object_emit_streamline_new (obj, UCL_EMIT_CONFIG, f);
105 }
106
107 assert (ctx != NULL);
108
109 /* Array of numbers */
110 ar = ucl_object_typed_new (UCL_ARRAY);
111 ar->key = "key4";
112 ar->keylen = sizeof ("key4") - 1;
113
114 ucl_object_emit_streamline_start_container (ctx, ar);
115 cur = ucl_object_fromint (10);
116 ucl_object_emit_streamline_add_object (ctx, cur);
117 cur = ucl_object_fromdouble (10.1);
118 ucl_object_emit_streamline_add_object (ctx, cur);
119 cur = ucl_object_fromdouble (9.999);
120 ucl_object_emit_streamline_add_object (ctx, cur);
121
122
123 ucl_object_emit_streamline_end_container (ctx);
124 ucl_object_emit_streamline_finish (ctx);
125 ucl_object_emit_funcs_free (f);
126 ucl_object_unref (obj);
127
128 fclose (out);
129
130 return ret;
131 }
132