Path: blob/main/contrib/libucl/tests/test_streamline.c
39586 views
/* Copyright (c) 2013, Vsevolod Stakhov1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10*11* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY12* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED13* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE14* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY15* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES16* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;17* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND18* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT19* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS20* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.21*/2223#include <stdio.h>24#include <errno.h>25#include <assert.h>26#include "ucl.h"2728#include <sys/types.h>29#include <fcntl.h>30#include <unistd.h>3132int33main (int argc, char **argv)34{35ucl_object_t *obj, *cur, *ar;36FILE *out;37const char *fname_out = NULL;38struct ucl_emitter_context *ctx;39struct ucl_emitter_functions *f;40int ret = 0, opt, json = 0, compact = 0, yaml = 0;4142while ((opt = getopt(argc, argv, "jcy")) != -1) {43switch (opt) {44case 'j':45json = 1;46break;47case 'c':48compact = 1;49break;50case 'y':51yaml = 1;52break;53default: /* '?' */54fprintf (stderr, "Usage: %s [-jcy] [out]\n",55argv[0]);56exit (EXIT_FAILURE);57}58}5960argc -= optind;61argv += optind;6263switch (argc) {64case 2:65fname_out = argv[1];66break;67}6869if (fname_out != NULL) {70out = fopen (fname_out, "w");71if (out == NULL) {72exit (-errno);73}74}75else {76out = stdout;77}7879obj = ucl_object_typed_new (UCL_OBJECT);8081/* Create some strings */82cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM);83ucl_object_insert_key (obj, cur, "key1", 0, false);84cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE);85ucl_object_insert_key (obj, cur, "key2", 0, false);86cur = ucl_object_fromstring_common (" test string \n", 0, 0);87ucl_object_insert_key (obj, cur, "key3", 0, false);8889f = ucl_object_emit_file_funcs (out);9091if (yaml) {92ctx = ucl_object_emit_streamline_new (obj, UCL_EMIT_YAML, f);93}94else if (json) {95if (compact) {96ctx = ucl_object_emit_streamline_new (obj, UCL_EMIT_JSON_COMPACT, f);97}98else {99ctx = ucl_object_emit_streamline_new (obj, UCL_EMIT_JSON, f);100}101}102else {103ctx = ucl_object_emit_streamline_new (obj, UCL_EMIT_CONFIG, f);104}105106assert (ctx != NULL);107108/* Array of numbers */109ar = ucl_object_typed_new (UCL_ARRAY);110ar->key = "key4";111ar->keylen = sizeof ("key4") - 1;112113ucl_object_emit_streamline_start_container (ctx, ar);114cur = ucl_object_fromint (10);115ucl_object_emit_streamline_add_object (ctx, cur);116cur = ucl_object_fromdouble (10.1);117ucl_object_emit_streamline_add_object (ctx, cur);118cur = ucl_object_fromdouble (9.999);119ucl_object_emit_streamline_add_object (ctx, cur);120121122ucl_object_emit_streamline_end_container (ctx);123ucl_object_emit_streamline_finish (ctx);124ucl_object_emit_funcs_free (f);125ucl_object_unref (obj);126127fclose (out);128129return ret;130}131132133