1262395Sbapt/* Copyright (c) 2013, Vsevolod Stakhov 2262395Sbapt * All rights reserved. 3262395Sbapt * 4262395Sbapt * Redistribution and use in source and binary forms, with or without 5262395Sbapt * modification, are permitted provided that the following conditions are met: 6262395Sbapt * * Redistributions of source code must retain the above copyright 7262395Sbapt * notice, this list of conditions and the following disclaimer. 8262395Sbapt * * Redistributions in binary form must reproduce the above copyright 9262395Sbapt * notice, this list of conditions and the following disclaimer in the 10262395Sbapt * documentation and/or other materials provided with the distribution. 11262395Sbapt * 12262395Sbapt * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 13262395Sbapt * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14262395Sbapt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15262395Sbapt * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY 16262395Sbapt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17262395Sbapt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18262395Sbapt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 19262395Sbapt * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20262395Sbapt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21262395Sbapt * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22262395Sbapt */ 23262395Sbapt 24262395Sbapt#include <stdio.h> 25262395Sbapt#include <errno.h> 26262395Sbapt#include <assert.h> 27262395Sbapt#include "ucl.h" 28262395Sbapt 29262395Sbaptint 30262395Sbaptmain (int argc, char **argv) 31262395Sbapt{ 32268896Sbapt ucl_object_t *obj, *cur, *ar, *ref; 33268896Sbapt const ucl_object_t *found; 34262395Sbapt FILE *out; 35262395Sbapt unsigned char *emitted; 36262395Sbapt const char *fname_out = NULL; 37262395Sbapt int ret = 0; 38262395Sbapt 39262395Sbapt switch (argc) { 40262395Sbapt case 2: 41262395Sbapt fname_out = argv[1]; 42262395Sbapt break; 43262395Sbapt } 44262395Sbapt 45262395Sbapt 46262395Sbapt if (fname_out != NULL) { 47262395Sbapt out = fopen (fname_out, "w"); 48262395Sbapt if (out == NULL) { 49262395Sbapt exit (-errno); 50262395Sbapt } 51262395Sbapt } 52262395Sbapt else { 53262395Sbapt out = stdout; 54262395Sbapt } 55262395Sbapt 56262395Sbapt obj = ucl_object_typed_new (UCL_OBJECT); 57262395Sbapt /* Create some strings */ 58262395Sbapt cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM); 59268896Sbapt ucl_object_insert_key (obj, cur, "key1", 0, false); 60262395Sbapt cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE); 61268896Sbapt ucl_object_insert_key (obj, cur, "key2", 0, false); 62262395Sbapt cur = ucl_object_fromstring_common (" test string \n", 0, 0); 63268896Sbapt ucl_object_insert_key (obj, cur, "key3", 0, false); 64262395Sbapt /* Array of numbers */ 65268896Sbapt ar = ucl_object_typed_new (UCL_ARRAY); 66262395Sbapt cur = ucl_object_fromint (10); 67268896Sbapt ucl_array_append (ar, cur); 68262395Sbapt cur = ucl_object_fromdouble (10.1); 69268896Sbapt ucl_array_append (ar, cur); 70262395Sbapt cur = ucl_object_fromdouble (9.999); 71268896Sbapt ucl_array_prepend (ar, cur); 72262395Sbapt 73262395Sbapt /* Removing from an array */ 74262395Sbapt cur = ucl_object_fromdouble (1.0); 75268896Sbapt ucl_array_append (ar, cur); 76262395Sbapt cur = ucl_array_delete (ar, cur); 77262395Sbapt assert (ucl_object_todouble (cur) == 1.0); 78262395Sbapt ucl_object_unref (cur); 79262395Sbapt cur = ucl_object_fromdouble (2.0); 80268896Sbapt ucl_array_append (ar, cur); 81262395Sbapt cur = ucl_array_pop_last (ar); 82262395Sbapt assert (ucl_object_todouble (cur) == 2.0); 83262395Sbapt ucl_object_unref (cur); 84262395Sbapt cur = ucl_object_fromdouble (3.0); 85268896Sbapt ucl_array_prepend (ar, cur); 86262395Sbapt cur = ucl_array_pop_first (ar); 87262395Sbapt assert (ucl_object_todouble (cur) == 3.0); 88262395Sbapt ucl_object_unref (cur); 89262395Sbapt 90268896Sbapt ucl_object_insert_key (obj, ar, "key4", 0, false); 91262395Sbapt cur = ucl_object_frombool (true); 92268896Sbapt /* Ref object to test refcounts */ 93268896Sbapt ref = ucl_object_ref (cur); 94268896Sbapt ucl_object_insert_key (obj, cur, "key4", 0, false); 95262395Sbapt /* Empty strings */ 96262395Sbapt cur = ucl_object_fromstring_common (" ", 0, UCL_STRING_TRIM); 97268896Sbapt ucl_object_insert_key (obj, cur, "key5", 0, false); 98262395Sbapt cur = ucl_object_fromstring_common ("", 0, UCL_STRING_ESCAPE); 99268896Sbapt ucl_object_insert_key (obj, cur, "key6", 0, false); 100262395Sbapt cur = ucl_object_fromstring_common (" \n", 0, UCL_STRING_ESCAPE); 101268896Sbapt ucl_object_insert_key (obj, cur, "key7", 0, false); 102262395Sbapt /* Numbers and booleans */ 103262395Sbapt cur = ucl_object_fromstring_common ("1mb", 0, UCL_STRING_ESCAPE | UCL_STRING_PARSE); 104268896Sbapt ucl_object_insert_key (obj, cur, "key8", 0, false); 105262395Sbapt cur = ucl_object_fromstring_common ("3.14", 0, UCL_STRING_PARSE); 106268896Sbapt ucl_object_insert_key (obj, cur, "key9", 0, false); 107262395Sbapt cur = ucl_object_fromstring_common ("true", 0, UCL_STRING_PARSE); 108268896Sbapt ucl_object_insert_key (obj, cur, "key10", 0, false); 109262395Sbapt cur = ucl_object_fromstring_common (" off ", 0, UCL_STRING_PARSE | UCL_STRING_TRIM); 110268896Sbapt ucl_object_insert_key (obj, cur, "key11", 0, false); 111262395Sbapt cur = ucl_object_fromstring_common ("gslin@gslin.org", 0, UCL_STRING_PARSE_INT); 112268896Sbapt ucl_object_insert_key (obj, cur, "key12", 0, false); 113262395Sbapt cur = ucl_object_fromstring_common ("#test", 0, UCL_STRING_PARSE_INT); 114268896Sbapt ucl_object_insert_key (obj, cur, "key13", 0, false); 115262395Sbapt cur = ucl_object_frombool (true); 116268896Sbapt ucl_object_insert_key (obj, cur, "k=3", 0, false); 117262395Sbapt 118268896Sbapt /* Try to find using path */ 119268896Sbapt /* Should exist */ 120268896Sbapt found = ucl_lookup_path (obj, "key4.1"); 121268896Sbapt assert (found != NULL && ucl_object_toint (found) == 10); 122268896Sbapt /* . should be ignored */ 123268896Sbapt found = ucl_lookup_path (obj, ".key4.1"); 124268896Sbapt assert (found != NULL && ucl_object_toint (found) == 10); 125268896Sbapt /* moar dots... */ 126268896Sbapt found = ucl_lookup_path (obj, ".key4........1..."); 127268896Sbapt assert (found != NULL && ucl_object_toint (found) == 10); 128268896Sbapt /* No such index */ 129268896Sbapt found = ucl_lookup_path (obj, ".key4.3"); 130268896Sbapt assert (found == NULL); 131268896Sbapt /* No such key */ 132268896Sbapt found = ucl_lookup_path (obj, "key9..key1"); 133268896Sbapt assert (found == NULL); 134262395Sbapt 135262395Sbapt emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); 136262395Sbapt 137262395Sbapt fprintf (out, "%s\n", emitted); 138262395Sbapt ucl_object_unref (obj); 139262395Sbapt 140262395Sbapt if (emitted != NULL) { 141262395Sbapt free (emitted); 142262395Sbapt } 143262395Sbapt fclose (out); 144262395Sbapt 145268896Sbapt /* Ref should still be accessible */ 146268896Sbapt ref->value.iv = 100500; 147268896Sbapt ucl_object_unref (ref); 148268896Sbapt 149262395Sbapt return ret; 150262395Sbapt} 151