1268828Sbapt/* Copyright (c) 2013, Vsevolod Stakhov
2268828Sbapt * All rights reserved.
3268828Sbapt *
4268828Sbapt * Redistribution and use in source and binary forms, with or without
5268828Sbapt * modification, are permitted provided that the following conditions are met:
6268828Sbapt *       * Redistributions of source code must retain the above copyright
7268828Sbapt *         notice, this list of conditions and the following disclaimer.
8268828Sbapt *       * Redistributions in binary form must reproduce the above copyright
9268828Sbapt *         notice, this list of conditions and the following disclaimer in the
10268828Sbapt *         documentation and/or other materials provided with the distribution.
11268828Sbapt *
12268828Sbapt * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13268828Sbapt * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14268828Sbapt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15268828Sbapt * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16268828Sbapt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17268828Sbapt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18268828Sbapt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19268828Sbapt * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20268828Sbapt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21268828Sbapt * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22268828Sbapt */
23268828Sbapt
24268828Sbapt#include <stdio.h>
25268828Sbapt#include <errno.h>
26268828Sbapt#include <assert.h>
27268828Sbapt#include "ucl.h"
28268828Sbapt
29268828Sbaptint
30268828Sbaptmain (int argc, char **argv)
31268828Sbapt{
32268828Sbapt	ucl_object_t *obj, *cur, *ar;
33268828Sbapt	FILE *out;
34268828Sbapt	const char *fname_out = NULL;
35268828Sbapt	struct ucl_emitter_context *ctx;
36268828Sbapt	struct ucl_emitter_functions *f;
37268828Sbapt	int ret = 0;
38268828Sbapt
39268828Sbapt	switch (argc) {
40268828Sbapt	case 2:
41268828Sbapt		fname_out = argv[1];
42268828Sbapt		break;
43268828Sbapt	}
44268828Sbapt
45268828Sbapt	if (fname_out != NULL) {
46268828Sbapt		out = fopen (fname_out, "w");
47268828Sbapt		if (out == NULL) {
48268828Sbapt			exit (-errno);
49268828Sbapt		}
50268828Sbapt	}
51268828Sbapt	else {
52268828Sbapt		out = stdout;
53268828Sbapt	}
54268828Sbapt
55268828Sbapt	obj = ucl_object_typed_new (UCL_OBJECT);
56268828Sbapt
57268828Sbapt	/* Create some strings */
58268828Sbapt	cur = ucl_object_fromstring_common ("  test string    ", 0, UCL_STRING_TRIM);
59268828Sbapt	ucl_object_insert_key (obj, cur, "key1", 0, false);
60268828Sbapt	cur = ucl_object_fromstring_common ("  test \nstring\n    ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE);
61268828Sbapt	ucl_object_insert_key (obj, cur, "key2", 0, false);
62268828Sbapt	cur = ucl_object_fromstring_common ("  test string    \n", 0, 0);
63268828Sbapt	ucl_object_insert_key (obj, cur, "key3", 0, false);
64268828Sbapt
65268828Sbapt	f = ucl_object_emit_file_funcs (out);
66268828Sbapt	ctx = ucl_object_emit_streamline_new (obj, UCL_EMIT_CONFIG, f);
67268828Sbapt
68268828Sbapt	assert (ctx != NULL);
69268828Sbapt
70268828Sbapt	/* Array of numbers */
71268828Sbapt	ar = ucl_object_typed_new (UCL_ARRAY);
72268828Sbapt	ar->key = "key4";
73268828Sbapt	ar->keylen = sizeof ("key4") - 1;
74268828Sbapt
75268828Sbapt	ucl_object_emit_streamline_start_container (ctx, ar);
76268828Sbapt	cur = ucl_object_fromint (10);
77268828Sbapt	ucl_object_emit_streamline_add_object (ctx, cur);
78268828Sbapt	cur = ucl_object_fromdouble (10.1);
79268828Sbapt	ucl_object_emit_streamline_add_object (ctx, cur);
80268828Sbapt	cur = ucl_object_fromdouble (9.999);
81268828Sbapt	ucl_object_emit_streamline_add_object (ctx, cur);
82268828Sbapt
83268828Sbapt
84268828Sbapt	ucl_object_emit_streamline_end_container (ctx);
85268828Sbapt	ucl_object_emit_streamline_finish (ctx);
86268828Sbapt	ucl_object_emit_funcs_free (f);
87268828Sbapt	ucl_object_unref (obj);
88268828Sbapt
89268828Sbapt	fclose (out);
90268828Sbapt
91268828Sbapt	return ret;
92268828Sbapt}
93