1 /* Copyright (c) 2013, Dmitriy V. Reshetnikov 2 * Copyright (c) 2013, Vsevolod Stakhov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include <stdio.h> 26 #include <errno.h> 27 28 #include "ucl.h" 29 30 void 31 ucl_obj_dump (const ucl_object_t *obj, unsigned int shift) 32 { 33 int num = shift * 4 + 5; 34 char *pre = (char *) malloc (num * sizeof(char)); 35 const ucl_object_t *cur, *tmp; 36 ucl_object_iter_t it = NULL, it_obj = NULL; 37 38 pre[--num] = 0x00; 39 while (num--) 40 pre[num] = 0x20; 41 42 tmp = obj; 43 44 while ((obj = ucl_iterate_object (tmp, &it, false))) { 45 printf ("%sucl object address: %p\n", pre + 4, obj); 46 if (obj->key != NULL) { 47 printf ("%skey: \"%s\"\n", pre, ucl_object_key (obj)); 48 } 49 printf ("%sref: %u\n", pre, obj->ref); 50 printf ("%slen: %u\n", pre, obj->len); 51 printf ("%sprev: %p\n", pre, obj->prev); 52 printf ("%snext: %p\n", pre, obj->next); 53 if (obj->type == UCL_OBJECT) { 54 printf ("%stype: UCL_OBJECT\n", pre); 55 printf ("%svalue: %p\n", pre, obj->value.ov); 56 it_obj = NULL; 57 while ((cur = ucl_iterate_object (obj, &it_obj, true))) { 58 ucl_obj_dump (cur, shift + 2); 59 } 60 } 61 else if (obj->type == UCL_ARRAY) { 62 printf ("%stype: UCL_ARRAY\n", pre); 63 printf ("%svalue: %p\n", pre, obj->value.av); 64 it_obj = NULL; 65 while ((cur = ucl_iterate_object (obj, &it_obj, true))) { 66 ucl_obj_dump (cur, shift + 2); 67 } 68 } 69 else if (obj->type == UCL_INT) { 70 printf ("%stype: UCL_INT\n", pre); 71 printf ("%svalue: %jd\n", pre, (intmax_t)ucl_object_toint (obj)); 72 } 73 else if (obj->type == UCL_FLOAT) { 74 printf ("%stype: UCL_FLOAT\n", pre); 75 printf ("%svalue: %f\n", pre, ucl_object_todouble (obj)); 76 } 77 else if (obj->type == UCL_STRING) { 78 printf ("%stype: UCL_STRING\n", pre); 79 printf ("%svalue: \"%s\"\n", pre, ucl_object_tostring (obj)); 80 } 81 else if (obj->type == UCL_BOOLEAN) { 82 printf ("%stype: UCL_BOOLEAN\n", pre); 83 printf ("%svalue: %s\n", pre, ucl_object_tostring_forced (obj)); 84 } 85 else if (obj->type == UCL_TIME) { 86 printf ("%stype: UCL_TIME\n", pre); 87 printf ("%svalue: %f\n", pre, ucl_object_todouble (obj)); 88 } 89 else if (obj->type == UCL_USERDATA) { 90 printf ("%stype: UCL_USERDATA\n", pre); 91 printf ("%svalue: %p\n", pre, obj->value.ud); 92 } 93 } 94 95 free (pre); 96 } 97 98 int 99 main(int argc, char **argv) 100 { 101 const char *fn = NULL; 102 unsigned char inbuf[8192]; 103 struct ucl_parser *parser; 104 int k, ret = 0, r = 0; 105 ucl_object_t *obj = NULL; 106 const ucl_object_t *par; 107 FILE *in; 108 109 if (argc > 1) { 110 fn = argv[1]; 111 } 112 113 if (fn != NULL) { 114 in = fopen (fn, "r"); 115 if (in == NULL) { 116 exit (-errno); 117 } 118 } 119 else { 120 in = stdin; 121 } 122 123 parser = ucl_parser_new (0); 124 while (!feof (in) && r < (int)sizeof (inbuf)) { 125 r += fread (inbuf + r, 1, sizeof (inbuf) - r, in); 126 } 127 ucl_parser_add_chunk (parser, inbuf, r); 128 fclose (in); 129 if (ucl_parser_get_error(parser)) { 130 printf ("Error occured: %s\n", ucl_parser_get_error(parser)); 131 ret = 1; 132 goto end; 133 } 134 135 obj = ucl_parser_get_object (parser); 136 if (ucl_parser_get_error (parser)) { 137 printf ("Error occured: %s\n", ucl_parser_get_error(parser)); 138 ret = 1; 139 goto end; 140 } 141 142 if (argc > 2) { 143 for (k = 2; k < argc; k++) { 144 printf ("search for \"%s\"... ", argv[k]); 145 par = ucl_object_find_key (obj, argv[k]); 146 printf ("%sfound\n", (par == NULL )?"not ":""); 147 ucl_obj_dump (par, 0); 148 } 149 } 150 else { 151 ucl_obj_dump (obj, 0); 152 } 153 154 end: 155 if (parser != NULL) { 156 ucl_parser_free (parser); 157 } 158 if (obj != NULL) { 159 ucl_object_unref (obj); 160 } 161 162 return ret; 163 } 164