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 const ucl_object_t *found; 34 FILE *out; 35 unsigned char *emitted; 36 const char *fname_out = NULL; 37 int ret = 0; 38 39 switch (argc) { 40 case 2: 41 fname_out = argv[1]; 42 break; 43 } 44 45 46 if (fname_out != NULL) { 47 out = fopen (fname_out, "w"); 48 if (out == NULL) { 49 exit (-errno); 50 } 51 } 52 else { 53 out = stdout; 54 } 55 56 obj = ucl_object_typed_new (UCL_OBJECT); 57 58 /* Keys replacing */ 59 cur = ucl_object_fromstring_common ("value1", 0, UCL_STRING_TRIM); 60 ucl_object_insert_key (obj, cur, "key0", 0, false); 61 cur = ucl_object_fromdouble (0.1); 62 ucl_object_replace_key (obj, cur, "key0", 0, false); 63 64 /* Create some strings */ 65 cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM); 66 ucl_object_insert_key (obj, cur, "key1", 0, false); 67 cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE); 68 ucl_object_insert_key (obj, cur, "key2", 0, false); 69 cur = ucl_object_fromstring_common (" test string \n", 0, 0); 70 ucl_object_insert_key (obj, cur, "key3", 0, false); 71 /* Array of numbers */ 72 ar = ucl_object_typed_new (UCL_ARRAY); 73 cur = ucl_object_fromint (10); 74 ucl_array_append (ar, cur); 75 cur = ucl_object_fromdouble (10.1); 76 ucl_array_append (ar, cur); 77 cur = ucl_object_fromdouble (9.999); 78 ucl_array_prepend (ar, cur); 79 80 /* Removing from an array */ 81 cur = ucl_object_fromdouble (1.0); 82 ucl_array_append (ar, cur); 83 cur = ucl_array_delete (ar, cur); 84 assert (ucl_object_todouble (cur) == 1.0); 85 ucl_object_unref (cur); 86 cur = ucl_object_fromdouble (2.0); 87 ucl_array_append (ar, cur); 88 cur = ucl_array_pop_last (ar); 89 assert (ucl_object_todouble (cur) == 2.0); 90 ucl_object_unref (cur); 91 cur = ucl_object_fromdouble (3.0); 92 ucl_array_prepend (ar, cur); 93 cur = ucl_array_pop_first (ar); 94 assert (ucl_object_todouble (cur) == 3.0); 95 ucl_object_unref (cur); 96 97 ucl_object_insert_key (obj, ar, "key4", 0, false); 98 cur = ucl_object_frombool (true); 99 /* Ref object to test refcounts */ 100 ref = ucl_object_ref (cur); 101 ucl_object_insert_key (obj, cur, "key4", 0, false); 102 /* Empty strings */ 103 cur = ucl_object_fromstring_common (" ", 0, UCL_STRING_TRIM); 104 ucl_object_insert_key (obj, cur, "key5", 0, false); 105 cur = ucl_object_fromstring_common ("", 0, UCL_STRING_ESCAPE); 106 ucl_object_insert_key (obj, cur, "key6", 0, false); 107 cur = ucl_object_fromstring_common (" \n", 0, UCL_STRING_ESCAPE); 108 ucl_object_insert_key (obj, cur, "key7", 0, false); 109 /* Numbers and booleans */ 110 cur = ucl_object_fromstring_common ("1mb", 0, UCL_STRING_ESCAPE | UCL_STRING_PARSE); 111 ucl_object_insert_key (obj, cur, "key8", 0, false); 112 cur = ucl_object_fromstring_common ("3.14", 0, UCL_STRING_PARSE); 113 ucl_object_insert_key (obj, cur, "key9", 0, false); 114 cur = ucl_object_fromstring_common ("true", 0, UCL_STRING_PARSE); 115 ucl_object_insert_key (obj, cur, "key10", 0, false); 116 cur = ucl_object_fromstring_common (" off ", 0, UCL_STRING_PARSE | UCL_STRING_TRIM); 117 ucl_object_insert_key (obj, cur, "key11", 0, false); 118 cur = ucl_object_fromstring_common ("gslin@gslin.org", 0, UCL_STRING_PARSE_INT); 119 ucl_object_insert_key (obj, cur, "key12", 0, false); 120 cur = ucl_object_fromstring_common ("#test", 0, UCL_STRING_PARSE_INT); 121 ucl_object_insert_key (obj, cur, "key13", 0, false); 122 cur = ucl_object_frombool (true); 123 ucl_object_insert_key (obj, cur, "k=3", 0, false); 124 125 /* Try to find using path */ 126 /* Should exist */ 127 found = ucl_lookup_path (obj, "key4.1"); 128 assert (found != NULL && ucl_object_toint (found) == 10); 129 /* . should be ignored */ 130 found = ucl_lookup_path (obj, ".key4.1"); 131 assert (found != NULL && ucl_object_toint (found) == 10); 132 /* moar dots... */ 133 found = ucl_lookup_path (obj, ".key4........1..."); 134 assert (found != NULL && ucl_object_toint (found) == 10); 135 /* No such index */ 136 found = ucl_lookup_path (obj, ".key4.3"); 137 assert (found == NULL); 138 /* No such key */ 139 found = ucl_lookup_path (obj, "key9..key1"); 140 assert (found == NULL); 141 142 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); 143 144 fprintf (out, "%s\n", emitted); 145 ucl_object_unref (obj); 146 147 if (emitted != NULL) { 148 free (emitted); 149 } 150 fclose (out); 151 152 /* Ref should still be accessible */ 153 ref->value.iv = 100500; 154 ucl_object_unref (ref); 155 156 return ret; 157 } 158