You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ucl.h 56KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. /* Copyright (c) 2013-2015, 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. #ifndef UCL_H_
  24. #define UCL_H_
  25. #include <string.h>
  26. #include <stddef.h>
  27. #include <stdlib.h>
  28. #include <stdint.h>
  29. #include <stdbool.h>
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #ifdef _WIN32
  33. # define UCL_EXTERN __declspec(dllexport)
  34. #else
  35. # define UCL_EXTERN
  36. #endif
  37. /**
  38. * @mainpage
  39. * This is a reference manual for UCL API. You may find the description of UCL format by following this
  40. * [github repository](https://github.com/vstakhov/libucl).
  41. *
  42. * This manual has several main sections:
  43. * - @ref structures
  44. * - @ref utils
  45. * - @ref parser
  46. * - @ref emitter
  47. */
  48. /**
  49. * @file ucl.h
  50. * @brief UCL parsing and emitting functions
  51. *
  52. * UCL is universal configuration language, which is a form of
  53. * JSON with less strict rules that make it more comfortable for
  54. * using as a configuration language
  55. */
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /*
  60. * Memory allocation utilities
  61. * UCL_ALLOC(size) - allocate memory for UCL
  62. * UCL_FREE(size, ptr) - free memory of specified size at ptr
  63. * Default: malloc and free
  64. */
  65. #ifndef UCL_ALLOC
  66. #define UCL_ALLOC(size) malloc(size)
  67. #endif
  68. #ifndef UCL_FREE
  69. #define UCL_FREE(size, ptr) free(ptr)
  70. #endif
  71. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  72. #define UCL_WARN_UNUSED_RESULT \
  73. __attribute__((warn_unused_result))
  74. #else
  75. #define UCL_WARN_UNUSED_RESULT
  76. #endif
  77. #ifdef __GNUC__
  78. #define UCL_DEPRECATED(func) func __attribute__ ((deprecated))
  79. #elif defined(_MSC_VER)
  80. #define UCL_DEPRECATED(func) __declspec(deprecated) func
  81. #else
  82. #define UCL_DEPRECATED(func) func
  83. #endif
  84. /**
  85. * @defgroup structures Structures and types
  86. * UCL defines several enumeration types used for error reporting or specifying flags and attributes.
  87. *
  88. * @{
  89. */
  90. /**
  91. * The common error codes returned by ucl parser
  92. */
  93. typedef enum ucl_error {
  94. UCL_EOK = 0, /**< No error */
  95. UCL_ESYNTAX, /**< Syntax error occurred during parsing */
  96. UCL_EIO, /**< IO error occurred during parsing */
  97. UCL_ESTATE, /**< Invalid state machine state */
  98. UCL_ENESTED, /**< Input has too many recursion levels */
  99. UCL_EUNPAIRED, /**< Input has too many recursion levels */
  100. UCL_EMACRO, /**< Error processing a macro */
  101. UCL_EINTERNAL, /**< Internal unclassified error */
  102. UCL_ESSL, /**< SSL error */
  103. UCL_EMERGE /**< A merge error occurred */
  104. } ucl_error_t;
  105. /**
  106. * #ucl_object_t may have one of specified types, some types are compatible with each other and some are not.
  107. * For example, you can always convert #UCL_TIME to #UCL_FLOAT. Also you can convert #UCL_FLOAT to #UCL_INTEGER
  108. * by loosing floating point. Every object may be converted to a string by #ucl_object_tostring_forced() function.
  109. *
  110. */
  111. typedef enum ucl_type {
  112. UCL_OBJECT = 0, /**< UCL object - key/value pairs */
  113. UCL_ARRAY, /**< UCL array */
  114. UCL_INT, /**< Integer number */
  115. UCL_FLOAT, /**< Floating point number */
  116. UCL_STRING, /**< Null terminated string */
  117. UCL_BOOLEAN, /**< Boolean value */
  118. UCL_TIME, /**< Time value (floating point number of seconds) */
  119. UCL_USERDATA, /**< Opaque userdata pointer (may be used in macros) */
  120. UCL_NULL /**< Null value */
  121. } ucl_type_t;
  122. /**
  123. * You can use one of these types to serialise #ucl_object_t by using ucl_object_emit().
  124. */
  125. typedef enum ucl_emitter {
  126. UCL_EMIT_JSON = 0, /**< Emit fine formatted JSON */
  127. UCL_EMIT_JSON_COMPACT, /**< Emit compacted JSON */
  128. UCL_EMIT_CONFIG, /**< Emit human readable config format */
  129. UCL_EMIT_YAML, /**< Emit embedded YAML format */
  130. UCL_EMIT_MSGPACK, /**< Emit msgpack output */
  131. UCL_EMIT_MAX /**< Unsupported emitter type */
  132. } ucl_emitter_t;
  133. /**
  134. * These flags defines parser behaviour. If you specify #UCL_PARSER_ZEROCOPY you must ensure
  135. * that the input memory is not freed if an object is in use. Moreover, if you want to use
  136. * zero-terminated keys and string values then you should not use zero-copy mode, as in this case
  137. * UCL still has to perform copying implicitly.
  138. */
  139. typedef enum ucl_parser_flags {
  140. UCL_PARSER_DEFAULT = 0, /**< No special flags */
  141. UCL_PARSER_KEY_LOWERCASE = (1 << 0), /**< Convert all keys to lower case */
  142. UCL_PARSER_ZEROCOPY = (1 << 1), /**< Parse input in zero-copy mode if possible */
  143. UCL_PARSER_NO_TIME = (1 << 2), /**< Do not parse time and treat time values as strings */
  144. UCL_PARSER_NO_IMPLICIT_ARRAYS = (1 << 3), /** Create explicit arrays instead of implicit ones */
  145. UCL_PARSER_SAVE_COMMENTS = (1 << 4), /** Save comments in the parser context */
  146. UCL_PARSER_DISABLE_MACRO = (1 << 5), /** Treat macros as comments */
  147. UCL_PARSER_NO_FILEVARS = (1 << 6) /** Do not set file vars */
  148. } ucl_parser_flags_t;
  149. /**
  150. * String conversion flags, that are used in #ucl_object_fromstring_common function.
  151. */
  152. typedef enum ucl_string_flags {
  153. UCL_STRING_RAW = 0x0, /**< Treat string as is */
  154. UCL_STRING_ESCAPE = (1 << 0), /**< Perform JSON escape */
  155. UCL_STRING_TRIM = (1 << 1), /**< Trim leading and trailing whitespaces */
  156. UCL_STRING_PARSE_BOOLEAN = (1 << 2), /**< Parse passed string and detect boolean */
  157. UCL_STRING_PARSE_INT = (1 << 3), /**< Parse passed string and detect integer number */
  158. UCL_STRING_PARSE_DOUBLE = (1 << 4), /**< Parse passed string and detect integer or float number */
  159. UCL_STRING_PARSE_TIME = (1 << 5), /**< Parse time strings */
  160. UCL_STRING_PARSE_NUMBER = UCL_STRING_PARSE_INT|UCL_STRING_PARSE_DOUBLE|UCL_STRING_PARSE_TIME, /**<
  161. Parse passed string and detect number */
  162. UCL_STRING_PARSE = UCL_STRING_PARSE_BOOLEAN|UCL_STRING_PARSE_NUMBER, /**<
  163. Parse passed string (and detect booleans and numbers) */
  164. UCL_STRING_PARSE_BYTES = (1 << 6) /**< Treat numbers as bytes */
  165. } ucl_string_flags_t;
  166. /**
  167. * Basic flags for an object (can use up to 12 bits as higher 4 bits are used
  168. * for priorities)
  169. */
  170. typedef enum ucl_object_flags {
  171. UCL_OBJECT_ALLOCATED_KEY = (1 << 0), /**< An object has key allocated internally */
  172. UCL_OBJECT_ALLOCATED_VALUE = (1 << 1), /**< An object has a string value allocated internally */
  173. UCL_OBJECT_NEED_KEY_ESCAPE = (1 << 2), /**< The key of an object need to be escaped on output */
  174. UCL_OBJECT_EPHEMERAL = (1 << 3), /**< Temporary object that does not need to be freed really */
  175. UCL_OBJECT_MULTILINE = (1 << 4), /**< String should be displayed as multiline string */
  176. UCL_OBJECT_MULTIVALUE = (1 << 5), /**< Object is a key with multiple values */
  177. UCL_OBJECT_INHERITED = (1 << 6), /**< Object has been inherited from another */
  178. UCL_OBJECT_BINARY = (1 << 7), /**< Object contains raw binary data */
  179. UCL_OBJECT_SQUOTED = (1 << 8) /**< Object has been enclosed in single quotes */
  180. } ucl_object_flags_t;
  181. /**
  182. * Duplicate policy types
  183. */
  184. enum ucl_duplicate_strategy {
  185. UCL_DUPLICATE_APPEND = 0, /**< Default policy to merge based on priorities */
  186. UCL_DUPLICATE_MERGE, /**< Merge new object with old one */
  187. UCL_DUPLICATE_REWRITE, /**< Rewrite old keys */
  188. UCL_DUPLICATE_ERROR /**< Stop parsing on duplicate found */
  189. };
  190. /**
  191. * Input format type
  192. */
  193. enum ucl_parse_type {
  194. UCL_PARSE_UCL = 0, /**< Default ucl format */
  195. UCL_PARSE_MSGPACK, /**< Message pack input format */
  196. UCL_PARSE_CSEXP, /**< Canonical S-expressions */
  197. UCL_PARSE_AUTO /**< Try to detect parse type */
  198. };
  199. /**
  200. * UCL object structure. Please mention that the most of fields should not be touched by
  201. * UCL users. In future, this structure may be converted to private one.
  202. */
  203. typedef struct ucl_object_s {
  204. /**
  205. * Variant value type
  206. */
  207. union {
  208. int64_t iv; /**< Int value of an object */
  209. const char *sv; /**< String value of an object */
  210. double dv; /**< Double value of an object */
  211. void *av; /**< Array */
  212. void *ov; /**< Object */
  213. void* ud; /**< Opaque user data */
  214. } value;
  215. const char *key; /**< Key of an object */
  216. struct ucl_object_s *next; /**< Array handle */
  217. struct ucl_object_s *prev; /**< Array handle */
  218. uint32_t keylen; /**< Length of a key */
  219. uint32_t len; /**< Size of an object */
  220. uint32_t ref; /**< Reference count */
  221. uint16_t flags; /**< Object flags */
  222. uint16_t type; /**< Real type */
  223. unsigned char* trash_stack[2]; /**< Pointer to allocated chunks */
  224. } ucl_object_t;
  225. /**
  226. * Destructor type for userdata objects
  227. * @param ud user specified data pointer
  228. */
  229. typedef void (*ucl_userdata_dtor)(void *ud);
  230. typedef const char* (*ucl_userdata_emitter)(void *ud);
  231. /** @} */
  232. /**
  233. * @defgroup utils Utility functions
  234. * A number of utility functions simplify handling of UCL objects
  235. *
  236. * @{
  237. */
  238. /**
  239. * Copy and return a key of an object, returned key is zero-terminated
  240. * @param obj CL object
  241. * @return zero terminated key
  242. */
  243. UCL_EXTERN char* ucl_copy_key_trash (const ucl_object_t *obj);
  244. /**
  245. * Copy and return a string value of an object, returned key is zero-terminated
  246. * @param obj CL object
  247. * @return zero terminated string representation of object value
  248. */
  249. UCL_EXTERN char* ucl_copy_value_trash (const ucl_object_t *obj);
  250. /**
  251. * Creates a new object
  252. * @return new object
  253. */
  254. UCL_EXTERN ucl_object_t* ucl_object_new (void) UCL_WARN_UNUSED_RESULT;
  255. /**
  256. * Create new object with type specified
  257. * @param type type of a new object
  258. * @return new object
  259. */
  260. UCL_EXTERN ucl_object_t* ucl_object_typed_new (ucl_type_t type) UCL_WARN_UNUSED_RESULT;
  261. /**
  262. * Create new object with type and priority specified
  263. * @param type type of a new object
  264. * @param priority priority of an object
  265. * @return new object
  266. */
  267. UCL_EXTERN ucl_object_t* ucl_object_new_full (ucl_type_t type, unsigned priority)
  268. UCL_WARN_UNUSED_RESULT;
  269. /**
  270. * Create new object with userdata dtor
  271. * @param dtor destructor function
  272. * @param emitter emitter for userdata
  273. * @param ptr opaque pointer
  274. * @return new object
  275. */
  276. UCL_EXTERN ucl_object_t* ucl_object_new_userdata (ucl_userdata_dtor dtor,
  277. ucl_userdata_emitter emitter, void *ptr) UCL_WARN_UNUSED_RESULT;
  278. /**
  279. * Perform deep copy of an object copying everything
  280. * @param other object to copy
  281. * @return new object with refcount equal to 1
  282. */
  283. UCL_EXTERN ucl_object_t * ucl_object_copy (const ucl_object_t *other)
  284. UCL_WARN_UNUSED_RESULT;
  285. /**
  286. * Return the type of an object
  287. * @return the object type
  288. */
  289. UCL_EXTERN ucl_type_t ucl_object_type (const ucl_object_t *obj);
  290. /**
  291. * Converts ucl object type to its string representation
  292. * @param type type of object
  293. * @return constant string describing type
  294. */
  295. UCL_EXTERN const char * ucl_object_type_to_string (ucl_type_t type);
  296. /**
  297. * Converts string that represents ucl type to real ucl type enum
  298. * @param input C string with name of type
  299. * @param res resulting target
  300. * @return true if `input` is a name of type stored in `res`
  301. */
  302. UCL_EXTERN bool ucl_object_string_to_type (const char *input, ucl_type_t *res);
  303. /**
  304. * Convert any string to an ucl object making the specified transformations
  305. * @param str fixed size or NULL terminated string
  306. * @param len length (if len is zero, than str is treated as NULL terminated)
  307. * @param flags conversion flags
  308. * @return new object
  309. */
  310. UCL_EXTERN ucl_object_t * ucl_object_fromstring_common (const char *str, size_t len,
  311. enum ucl_string_flags flags) UCL_WARN_UNUSED_RESULT;
  312. /**
  313. * Create a UCL object from the specified string
  314. * @param str NULL terminated string, will be json escaped
  315. * @return new object
  316. */
  317. UCL_EXTERN ucl_object_t *ucl_object_fromstring (const char *str) UCL_WARN_UNUSED_RESULT;
  318. /**
  319. * Create a UCL object from the specified string
  320. * @param str fixed size string, will be json escaped
  321. * @param len length of a string
  322. * @return new object
  323. */
  324. UCL_EXTERN ucl_object_t *ucl_object_fromlstring (const char *str,
  325. size_t len) UCL_WARN_UNUSED_RESULT;
  326. /**
  327. * Create an object from an integer number
  328. * @param iv number
  329. * @return new object
  330. */
  331. UCL_EXTERN ucl_object_t* ucl_object_fromint (int64_t iv) UCL_WARN_UNUSED_RESULT;
  332. /**
  333. * Create an object from a float number
  334. * @param dv number
  335. * @return new object
  336. */
  337. UCL_EXTERN ucl_object_t* ucl_object_fromdouble (double dv) UCL_WARN_UNUSED_RESULT;
  338. /**
  339. * Create an object from a boolean
  340. * @param bv bool value
  341. * @return new object
  342. */
  343. UCL_EXTERN ucl_object_t* ucl_object_frombool (bool bv) UCL_WARN_UNUSED_RESULT;
  344. /**
  345. * Insert a object 'elt' to the hash 'top' and associate it with key 'key'
  346. * @param top destination object (must be of type UCL_OBJECT)
  347. * @param elt element to insert (must NOT be NULL)
  348. * @param key key to associate with this object (either const or preallocated)
  349. * @param keylen length of the key (or 0 for NULL terminated keys)
  350. * @param copy_key make an internal copy of key
  351. * @return true if key has been inserted
  352. */
  353. UCL_EXTERN bool ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
  354. const char *key, size_t keylen, bool copy_key);
  355. /**
  356. * Replace a object 'elt' to the hash 'top' and associate it with key 'key', old object will be unrefed,
  357. * if no object has been found this function works like ucl_object_insert_key()
  358. * @param top destination object (must be of type UCL_OBJECT)
  359. * @param elt element to insert (must NOT be NULL)
  360. * @param key key to associate with this object (either const or preallocated)
  361. * @param keylen length of the key (or 0 for NULL terminated keys)
  362. * @param copy_key make an internal copy of key
  363. * @return true if key has been inserted
  364. */
  365. UCL_EXTERN bool ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt,
  366. const char *key, size_t keylen, bool copy_key);
  367. /**
  368. * Merge the keys from one object to another object. Overwrite on conflict
  369. * @param top destination object (must be of type UCL_OBJECT)
  370. * @param elt element to insert (must be of type UCL_OBJECT)
  371. * @param copy copy rather than reference the elements
  372. * @return true if all keys have been merged
  373. */
  374. UCL_EXTERN bool ucl_object_merge (ucl_object_t *top, ucl_object_t *elt, bool copy);
  375. /**
  376. * Delete a object associated with key 'key', old object will be unrefered,
  377. * @param top object
  378. * @param key key associated to the object to remove
  379. * @param keylen length of the key (or 0 for NULL terminated keys)
  380. */
  381. UCL_EXTERN bool ucl_object_delete_keyl (ucl_object_t *top,
  382. const char *key, size_t keylen);
  383. /**
  384. * Delete a object associated with key 'key', old object will be unrefered,
  385. * @param top object
  386. * @param key key associated to the object to remove
  387. */
  388. UCL_EXTERN bool ucl_object_delete_key (ucl_object_t *top,
  389. const char *key);
  390. /**
  391. * Removes `key` from `top` object, returning the object that was removed. This
  392. * object is not released, caller must unref the returned object when it is no
  393. * longer needed.
  394. * @param top object
  395. * @param key key to remove
  396. * @param keylen length of the key (or 0 for NULL terminated keys)
  397. * @return removed object or NULL if object has not been found
  398. */
  399. UCL_EXTERN ucl_object_t* ucl_object_pop_keyl (ucl_object_t *top, const char *key,
  400. size_t keylen) UCL_WARN_UNUSED_RESULT;
  401. /**
  402. * Removes `key` from `top` object returning the object that was removed. This
  403. * object is not released, caller must unref the returned object when it is no
  404. * longer needed.
  405. * @param top object
  406. * @param key key to remove
  407. * @return removed object or NULL if object has not been found
  408. */
  409. UCL_EXTERN ucl_object_t* ucl_object_pop_key (ucl_object_t *top, const char *key)
  410. UCL_WARN_UNUSED_RESULT;
  411. /**
  412. * Insert a object 'elt' to the hash 'top' and associate it with key 'key', if
  413. * the specified key exist, try to merge its content
  414. * @param top destination object (must be of type UCL_OBJECT)
  415. * @param elt element to insert (must NOT be NULL)
  416. * @param key key to associate with this object (either const or preallocated)
  417. * @param keylen length of the key (or 0 for NULL terminated keys)
  418. * @param copy_key make an internal copy of key
  419. * @return true if key has been inserted
  420. */
  421. UCL_EXTERN bool ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt,
  422. const char *key, size_t keylen, bool copy_key);
  423. /**
  424. * Reserve space in ucl array or object for `elt` elements
  425. * @param obj object to reserve
  426. * @param reserved size to reserve in an object
  427. * @return 0 on success, -1 on failure (i.e. ENOMEM)
  428. */
  429. UCL_EXTERN bool ucl_object_reserve (ucl_object_t *obj, size_t reserved);
  430. /**
  431. * Append an element to the end of array object
  432. * @param top destination object (must NOT be NULL)
  433. * @param elt element to append (must NOT be NULL)
  434. * @return true if value has been inserted
  435. */
  436. UCL_EXTERN bool ucl_array_append (ucl_object_t *top,
  437. ucl_object_t *elt);
  438. /**
  439. * Append an element to the start of array object
  440. * @param top destination object (must NOT be NULL)
  441. * @param elt element to append (must NOT be NULL)
  442. * @return true if value has been inserted
  443. */
  444. UCL_EXTERN bool ucl_array_prepend (ucl_object_t *top,
  445. ucl_object_t *elt);
  446. /**
  447. * Merge all elements of second array into the first array
  448. * @param top destination array (must be of type UCL_ARRAY)
  449. * @param elt array to copy elements from (must be of type UCL_ARRAY)
  450. * @param copy copy elements instead of referencing them
  451. * @return true if arrays were merged
  452. */
  453. UCL_EXTERN bool ucl_array_merge (ucl_object_t *top, ucl_object_t *elt,
  454. bool copy);
  455. /**
  456. * Removes an element `elt` from the array `top`, returning the object that was
  457. * removed. This object is not released, caller must unref the returned object
  458. * when it is no longer needed.
  459. * @param top array ucl object
  460. * @param elt element to remove
  461. * @return removed element or NULL if `top` is NULL or not an array
  462. */
  463. UCL_EXTERN ucl_object_t* ucl_array_delete (ucl_object_t *top,
  464. ucl_object_t *elt);
  465. /**
  466. * Returns the first element of the array `top`
  467. * @param top array ucl object
  468. * @return element or NULL if `top` is NULL or not an array
  469. */
  470. UCL_EXTERN const ucl_object_t* ucl_array_head (const ucl_object_t *top);
  471. /**
  472. * Returns the last element of the array `top`
  473. * @param top array ucl object
  474. * @return element or NULL if `top` is NULL or not an array
  475. */
  476. UCL_EXTERN const ucl_object_t* ucl_array_tail (const ucl_object_t *top);
  477. /**
  478. * Removes the last element from the array `top`, returning the object that was
  479. * removed. This object is not released, caller must unref the returned object
  480. * when it is no longer needed.
  481. * @param top array ucl object
  482. * @return removed element or NULL if `top` is NULL or not an array
  483. */
  484. UCL_EXTERN ucl_object_t* ucl_array_pop_last (ucl_object_t *top);
  485. /**
  486. * Removes the first element from the array `top`, returning the object that was
  487. * removed. This object is not released, caller must unref the returned object
  488. * when it is no longer needed.
  489. * @param top array ucl object
  490. * @return removed element or NULL if `top` is NULL or not an array
  491. */
  492. UCL_EXTERN ucl_object_t* ucl_array_pop_first (ucl_object_t *top);
  493. /**
  494. * Return size of the array `top`
  495. * @param top object to get size from (must be of type UCL_ARRAY)
  496. * @return size of the array
  497. */
  498. UCL_EXTERN unsigned int ucl_array_size (const ucl_object_t *top);
  499. /**
  500. * Return object identified by index of the array `top`
  501. * @param top object to get a key from (must be of type UCL_ARRAY)
  502. * @param index array index to return
  503. * @return object at the specified index or NULL if index is not found
  504. */
  505. UCL_EXTERN const ucl_object_t* ucl_array_find_index (const ucl_object_t *top,
  506. unsigned int index);
  507. /**
  508. * Return the index of `elt` in the array `top`
  509. * @param top object to get a key from (must be of type UCL_ARRAY)
  510. * @param elt element to find index of (must NOT be NULL)
  511. * @return index of `elt` in the array `top or (unsigned int)-1 if `elt` is not found
  512. */
  513. UCL_EXTERN unsigned int ucl_array_index_of (ucl_object_t *top,
  514. ucl_object_t *elt);
  515. /**
  516. * Replace an element in an array with a different element, returning the object
  517. * that was replaced. This object is not released, caller must unref the
  518. * returned object when it is no longer needed.
  519. * @param top destination object (must be of type UCL_ARRAY)
  520. * @param elt element to append (must NOT be NULL)
  521. * @param index array index in destination to overwrite with elt
  522. * @return object that was replaced or NULL if index is not found
  523. */
  524. ucl_object_t *
  525. ucl_array_replace_index (ucl_object_t *top, ucl_object_t *elt,
  526. unsigned int index);
  527. /**
  528. * Append a element to another element forming an implicit array
  529. * @param head head to append (may be NULL)
  530. * @param elt new element
  531. * @return the new implicit array
  532. */
  533. UCL_EXTERN ucl_object_t * ucl_elt_append (ucl_object_t *head,
  534. ucl_object_t *elt);
  535. /**
  536. * Converts an object to double value
  537. * @param obj CL object
  538. * @param target target double variable
  539. * @return true if conversion was successful
  540. */
  541. UCL_EXTERN bool ucl_object_todouble_safe (const ucl_object_t *obj, double *target);
  542. /**
  543. * Unsafe version of \ref ucl_obj_todouble_safe
  544. * @param obj CL object
  545. * @return double value
  546. */
  547. UCL_EXTERN double ucl_object_todouble (const ucl_object_t *obj);
  548. /**
  549. * Converts an object to integer value
  550. * @param obj CL object
  551. * @param target target integer variable
  552. * @return true if conversion was successful
  553. */
  554. UCL_EXTERN bool ucl_object_toint_safe (const ucl_object_t *obj, int64_t *target);
  555. /**
  556. * Unsafe version of \ref ucl_obj_toint_safe
  557. * @param obj CL object
  558. * @return int value
  559. */
  560. UCL_EXTERN int64_t ucl_object_toint (const ucl_object_t *obj);
  561. /**
  562. * Converts an object to boolean value
  563. * @param obj CL object
  564. * @param target target boolean variable
  565. * @return true if conversion was successful
  566. */
  567. UCL_EXTERN bool ucl_object_toboolean_safe (const ucl_object_t *obj, bool *target);
  568. /**
  569. * Unsafe version of \ref ucl_obj_toboolean_safe
  570. * @param obj CL object
  571. * @return boolean value
  572. */
  573. UCL_EXTERN bool ucl_object_toboolean (const ucl_object_t *obj);
  574. /**
  575. * Converts an object to string value
  576. * @param obj CL object
  577. * @param target target string variable, no need to free value
  578. * @return true if conversion was successful
  579. */
  580. UCL_EXTERN bool ucl_object_tostring_safe (const ucl_object_t *obj, const char **target);
  581. /**
  582. * Unsafe version of \ref ucl_obj_tostring_safe
  583. * @param obj CL object
  584. * @return string value
  585. */
  586. UCL_EXTERN const char* ucl_object_tostring (const ucl_object_t *obj);
  587. /**
  588. * Convert any object to a string in JSON notation if needed
  589. * @param obj CL object
  590. * @return string value
  591. */
  592. UCL_EXTERN const char* ucl_object_tostring_forced (const ucl_object_t *obj);
  593. /**
  594. * Return string as char * and len, string may be not zero terminated, more efficient that \ref ucl_obj_tostring as it
  595. * allows zero-copy (if #UCL_PARSER_ZEROCOPY has been used during parsing)
  596. * @param obj CL object
  597. * @param target target string variable, no need to free value
  598. * @param tlen target length
  599. * @return true if conversion was successful
  600. */
  601. UCL_EXTERN bool ucl_object_tolstring_safe (const ucl_object_t *obj,
  602. const char **target, size_t *tlen);
  603. /**
  604. * Unsafe version of \ref ucl_obj_tolstring_safe
  605. * @param obj CL object
  606. * @return string value
  607. */
  608. UCL_EXTERN const char* ucl_object_tolstring (const ucl_object_t *obj, size_t *tlen);
  609. /**
  610. * Return object identified by a key in the specified object
  611. * @param obj object to get a key from (must be of type UCL_OBJECT)
  612. * @param key key to search
  613. * @return object matching the specified key or NULL if key was not found
  614. */
  615. UCL_EXTERN const ucl_object_t* ucl_object_lookup (const ucl_object_t *obj,
  616. const char *key);
  617. #define ucl_object_find_key ucl_object_lookup
  618. /**
  619. * Return object identified by a key in the specified object, if the first key is
  620. * not found then look for the next one. This process is repeated unless
  621. * the next argument in the list is not NULL. So, `ucl_object_find_any_key(obj, key, NULL)`
  622. * is equal to `ucl_object_find_key(obj, key)`
  623. * @param obj object to get a key from (must be of type UCL_OBJECT)
  624. * @param key key to search
  625. * @param ... list of alternative keys to search (NULL terminated)
  626. * @return object matching the specified key or NULL if key was not found
  627. */
  628. UCL_EXTERN const ucl_object_t* ucl_object_lookup_any (const ucl_object_t *obj,
  629. const char *key, ...);
  630. #define ucl_object_find_any_key ucl_object_lookup_any
  631. /**
  632. * Return object identified by a fixed size key in the specified object
  633. * @param obj object to get a key from (must be of type UCL_OBJECT)
  634. * @param key key to search
  635. * @param klen length of a key
  636. * @return object matching the specified key or NULL if key was not found
  637. */
  638. UCL_EXTERN const ucl_object_t* ucl_object_lookup_len (const ucl_object_t *obj,
  639. const char *key, size_t klen);
  640. #define ucl_object_find_keyl ucl_object_lookup_len
  641. /**
  642. * Return object identified by dot notation string
  643. * @param obj object to search in
  644. * @param path dot.notation.path to the path to lookup. May use numeric .index on arrays
  645. * @return object matched the specified path or NULL if path is not found
  646. */
  647. UCL_EXTERN const ucl_object_t *ucl_object_lookup_path (const ucl_object_t *obj,
  648. const char *path);
  649. #define ucl_lookup_path ucl_object_lookup_path
  650. /**
  651. * Return object identified by object notation string using arbitrary delimiter
  652. * @param obj object to search in
  653. * @param path dot.notation.path to the path to lookup. May use numeric .index on arrays
  654. * @param sep the sepatorator to use in place of . (incase keys have . in them)
  655. * @return object matched the specified path or NULL if path is not found
  656. */
  657. UCL_EXTERN const ucl_object_t *ucl_object_lookup_path_char (const ucl_object_t *obj,
  658. const char *path, char sep);
  659. #define ucl_lookup_path_char ucl_object_lookup_path_char
  660. /**
  661. * Returns a key of an object as a NULL terminated string
  662. * @param obj CL object
  663. * @return key or NULL if there is no key
  664. */
  665. UCL_EXTERN const char* ucl_object_key (const ucl_object_t *obj);
  666. /**
  667. * Returns a key of an object as a fixed size string (may be more efficient)
  668. * @param obj CL object
  669. * @param len target key length
  670. * @return key pointer
  671. */
  672. UCL_EXTERN const char* ucl_object_keyl (const ucl_object_t *obj, size_t *len);
  673. /**
  674. * Increase reference count for an object
  675. * @param obj object to ref
  676. * @return the referenced object
  677. */
  678. UCL_EXTERN ucl_object_t* ucl_object_ref (const ucl_object_t *obj);
  679. /**
  680. * Free ucl object
  681. * @param obj ucl object to free
  682. */
  683. UCL_DEPRECATED(UCL_EXTERN void ucl_object_free (ucl_object_t *obj));
  684. /**
  685. * Decrease reference count for an object
  686. * @param obj object to unref
  687. */
  688. UCL_EXTERN void ucl_object_unref (ucl_object_t *obj);
  689. /**
  690. * Compare objects `o1` and `o2`
  691. * @param o1 the first object
  692. * @param o2 the second object
  693. * @return values >0, 0 and <0 if `o1` is more than, equal and less than `o2`.
  694. * The order of comparison:
  695. * 1) Type of objects
  696. * 2) Size of objects
  697. * 3) Content of objects
  698. */
  699. UCL_EXTERN int ucl_object_compare (const ucl_object_t *o1,
  700. const ucl_object_t *o2);
  701. /**
  702. * Compare objects `o1` and `o2` useful for sorting
  703. * @param o1 the first object
  704. * @param o2 the second object
  705. * @return values >0, 0 and <0 if `o1` is more than, equal and less than `o2`.
  706. * The order of comparison:
  707. * 1) Type of objects
  708. * 2) Size of objects
  709. * 3) Content of objects
  710. */
  711. UCL_EXTERN int ucl_object_compare_qsort (const ucl_object_t **o1,
  712. const ucl_object_t **o2);
  713. /**
  714. * Sort UCL array using `cmp` compare function
  715. * @param ar
  716. * @param cmp
  717. */
  718. UCL_EXTERN void ucl_object_array_sort (ucl_object_t *ar,
  719. int (*cmp)(const ucl_object_t **o1, const ucl_object_t **o2));
  720. enum ucl_object_keys_sort_flags {
  721. UCL_SORT_KEYS_DEFAULT = 0,
  722. UCL_SORT_KEYS_ICASE = (1u << 0u),
  723. UCL_SORT_KEYS_RECURSIVE = (1u << 1u),
  724. };
  725. /***
  726. * Sorts keys in object in place
  727. * @param obj
  728. * @param how
  729. */
  730. UCL_EXTERN void ucl_object_sort_keys (ucl_object_t *obj,
  731. enum ucl_object_keys_sort_flags how);
  732. /**
  733. * Get the priority for specific UCL object
  734. * @param obj any ucl object
  735. * @return priority of an object
  736. */
  737. UCL_EXTERN unsigned int ucl_object_get_priority (const ucl_object_t *obj);
  738. /**
  739. * Set explicit priority of an object.
  740. * @param obj any ucl object
  741. * @param priority new priroity value (only 4 least significant bits are considred)
  742. */
  743. UCL_EXTERN void ucl_object_set_priority (ucl_object_t *obj,
  744. unsigned int priority);
  745. /**
  746. * Opaque iterator object
  747. */
  748. typedef void* ucl_object_iter_t;
  749. /**
  750. * Get next key from an object
  751. * @param obj object to iterate
  752. * @param iter opaque iterator, must be set to NULL on the first call:
  753. * ucl_object_iter_t it = NULL;
  754. * while ((cur = ucl_iterate_object (obj, &it)) != NULL) ...
  755. * @param ep pointer record exception (such as ENOMEM), could be NULL
  756. * @return the next object or NULL
  757. */
  758. UCL_EXTERN const ucl_object_t* ucl_object_iterate_with_error (const ucl_object_t *obj,
  759. ucl_object_iter_t *iter, bool expand_values, int *ep);
  760. #define ucl_iterate_object ucl_object_iterate
  761. #define ucl_object_iterate(ob, it, ev) ucl_object_iterate_with_error((ob), (it), (ev), NULL)
  762. /**
  763. * Create new safe iterator for the specified object
  764. * @param obj object to iterate
  765. * @return new iterator object that should be used with safe iterators API only
  766. */
  767. UCL_EXTERN ucl_object_iter_t ucl_object_iterate_new (const ucl_object_t *obj)
  768. UCL_WARN_UNUSED_RESULT;
  769. /**
  770. * Check safe iterator object after performing some operations on it
  771. * (such as ucl_object_iterate_safe()) to see if operation has encountered
  772. * fatal exception while performing that operation (e.g. ENOMEM).
  773. * @param iter opaque iterator
  774. * @return true if exception has occured, false otherwise
  775. */
  776. UCL_EXTERN bool ucl_object_iter_chk_excpn(ucl_object_iter_t *it);
  777. /**
  778. * Reset initialized iterator to a new object
  779. * @param obj new object to iterate
  780. * @return modified iterator object
  781. */
  782. UCL_EXTERN ucl_object_iter_t ucl_object_iterate_reset (ucl_object_iter_t it,
  783. const ucl_object_t *obj);
  784. /**
  785. * Get the next object from the `obj`. This function iterates over arrays, objects
  786. * and implicit arrays
  787. * @param iter safe iterator
  788. * @param expand_values expand explicit arrays and objects
  789. * @return the next object in sequence
  790. */
  791. UCL_EXTERN const ucl_object_t* ucl_object_iterate_safe (ucl_object_iter_t iter,
  792. bool expand_values);
  793. /**
  794. * Iteration type enumerator
  795. */
  796. enum ucl_iterate_type {
  797. UCL_ITERATE_EXPLICIT = 1 << 0, /**< Iterate just explicit arrays and objects */
  798. UCL_ITERATE_IMPLICIT = 1 << 1, /**< Iterate just implicit arrays */
  799. UCL_ITERATE_BOTH = (1 << 0) | (1 << 1), /**< Iterate both explicit and implicit arrays*/
  800. };
  801. /**
  802. * Get the next object from the `obj`. This function iterates over arrays, objects
  803. * and implicit arrays if needed
  804. * @param iter safe iterator
  805. * @param
  806. * @return the next object in sequence
  807. */
  808. UCL_EXTERN const ucl_object_t* ucl_object_iterate_full (ucl_object_iter_t iter,
  809. enum ucl_iterate_type type);
  810. /**
  811. * Free memory associated with the safe iterator
  812. * @param it safe iterator object
  813. */
  814. UCL_EXTERN void ucl_object_iterate_free (ucl_object_iter_t it);
  815. /** @} */
  816. /**
  817. * @defgroup parser Parsing functions
  818. * These functions are used to parse UCL objects
  819. *
  820. * @{
  821. */
  822. /**
  823. * Macro handler for a parser
  824. * @param data the content of macro
  825. * @param len the length of content
  826. * @param arguments arguments object
  827. * @param ud opaque user data
  828. * @param err error pointer
  829. * @return true if macro has been parsed
  830. */
  831. typedef bool (*ucl_macro_handler) (const unsigned char *data, size_t len,
  832. const ucl_object_t *arguments,
  833. void* ud);
  834. /**
  835. * Context dependent macro handler for a parser
  836. * @param data the content of macro
  837. * @param len the length of content
  838. * @param arguments arguments object
  839. * @param context previously parsed context
  840. * @param ud opaque user data
  841. * @param err error pointer
  842. * @return true if macro has been parsed
  843. */
  844. typedef bool (*ucl_context_macro_handler) (const unsigned char *data, size_t len,
  845. const ucl_object_t *arguments,
  846. const ucl_object_t *context,
  847. void* ud);
  848. /* Opaque parser */
  849. struct ucl_parser;
  850. /**
  851. * Creates new parser object
  852. * @param pool pool to allocate memory from
  853. * @return new parser object
  854. */
  855. UCL_EXTERN struct ucl_parser* ucl_parser_new (int flags);
  856. /**
  857. * Sets the default priority for the parser applied to chunks that do not
  858. * specify priority explicitly
  859. * @param parser parser object
  860. * @param prio default priority (0 .. 16)
  861. * @return true if parser's default priority was set
  862. */
  863. UCL_EXTERN bool ucl_parser_set_default_priority (struct ucl_parser *parser,
  864. unsigned prio);
  865. /**
  866. * Gets the default priority for the parser applied to chunks that do not
  867. * specify priority explicitly
  868. * @param parser parser object
  869. * @return true default priority (0 .. 16), -1 for failure
  870. */
  871. UCL_EXTERN int ucl_parser_get_default_priority (struct ucl_parser *parser);
  872. /**
  873. * Register new handler for a macro
  874. * @param parser parser object
  875. * @param macro macro name (without leading dot)
  876. * @param handler handler (it is called immediately after macro is parsed)
  877. * @param ud opaque user data for a handler
  878. * @return true on success, false on failure (i.e. ENOMEM)
  879. */
  880. UCL_EXTERN bool ucl_parser_register_macro (struct ucl_parser *parser,
  881. const char *macro,
  882. ucl_macro_handler handler, void* ud);
  883. /**
  884. * Register new context dependent handler for a macro
  885. * @param parser parser object
  886. * @param macro macro name (without leading dot)
  887. * @param handler handler (it is called immediately after macro is parsed)
  888. * @param ud opaque user data for a handler
  889. * @return true on success, false on failure (i.e. ENOMEM)
  890. */
  891. UCL_EXTERN bool ucl_parser_register_context_macro (struct ucl_parser *parser,
  892. const char *macro,
  893. ucl_context_macro_handler handler,
  894. void* ud);
  895. /**
  896. * Handler to detect unregistered variables
  897. * @param data variable data
  898. * @param len length of variable
  899. * @param replace (out) replace value for variable
  900. * @param replace_len (out) replace length for variable
  901. * @param need_free (out) UCL will free `dest` after usage
  902. * @param ud opaque userdata
  903. * @return true if variable
  904. */
  905. typedef bool (*ucl_variable_handler) (const unsigned char *data, size_t len,
  906. unsigned char **replace, size_t *replace_len, bool *need_free, void* ud);
  907. /**
  908. * Register new parser variable
  909. * @param parser parser object
  910. * @param var variable name
  911. * @param value variable value
  912. */
  913. UCL_EXTERN void ucl_parser_register_variable (struct ucl_parser *parser, const char *var,
  914. const char *value);
  915. /**
  916. * Set handler for unknown variables
  917. * @param parser parser structure
  918. * @param handler desired handler
  919. * @param ud opaque data for the handler
  920. */
  921. UCL_EXTERN void ucl_parser_set_variables_handler (struct ucl_parser *parser,
  922. ucl_variable_handler handler, void *ud);
  923. /**
  924. * Load new chunk to a parser
  925. * @param parser parser structure
  926. * @param data the pointer to the beginning of a chunk
  927. * @param len the length of a chunk
  928. * @return true if chunk has been added and false in case of error
  929. */
  930. UCL_EXTERN bool ucl_parser_add_chunk (struct ucl_parser *parser,
  931. const unsigned char *data, size_t len);
  932. /**
  933. * Load new chunk to a parser with the specified priority
  934. * @param parser parser structure
  935. * @param data the pointer to the beginning of a chunk
  936. * @param len the length of a chunk
  937. * @param priority the desired priority of a chunk (only 4 least significant bits
  938. * are considered for this parameter)
  939. * @return true if chunk has been added and false in case of error
  940. */
  941. UCL_EXTERN bool ucl_parser_add_chunk_priority (struct ucl_parser *parser,
  942. const unsigned char *data, size_t len, unsigned priority);
  943. /**
  944. * Insert new chunk to a parser (must have previously processed data with an existing top object)
  945. * @param parser parser structure
  946. * @param data the pointer to the beginning of a chunk
  947. * @param len the length of a chunk
  948. * @return true if chunk has been added and false in case of error
  949. */
  950. UCL_EXTERN bool ucl_parser_insert_chunk (struct ucl_parser *parser,
  951. const unsigned char *data, size_t len);
  952. /**
  953. * Full version of ucl_add_chunk with priority and duplicate strategy
  954. * @param parser parser structure
  955. * @param data the pointer to the beginning of a chunk
  956. * @param len the length of a chunk
  957. * @param priority the desired priority of a chunk (only 4 least significant bits
  958. * are considered for this parameter)
  959. * @param strat duplicates merging strategy
  960. * @param parse_type input format
  961. * @return true if chunk has been added and false in case of error
  962. */
  963. UCL_EXTERN bool ucl_parser_add_chunk_full (struct ucl_parser *parser,
  964. const unsigned char *data, size_t len, unsigned priority,
  965. enum ucl_duplicate_strategy strat, enum ucl_parse_type parse_type);
  966. /**
  967. * Load ucl object from a string
  968. * @param parser parser structure
  969. * @param data the pointer to the string
  970. * @param len the length of the string, if `len` is 0 then `data` must be zero-terminated string
  971. * @return true if string has been added and false in case of error
  972. */
  973. UCL_EXTERN bool ucl_parser_add_string (struct ucl_parser *parser,
  974. const char *data, size_t len);
  975. /**
  976. * Load ucl object from a string
  977. * @param parser parser structure
  978. * @param data the pointer to the string
  979. * @param len the length of the string, if `len` is 0 then `data` must be zero-terminated string
  980. * @param priority the desired priority of a chunk (only 4 least significant bits
  981. * are considered for this parameter)
  982. * @return true if string has been added and false in case of error
  983. */
  984. UCL_EXTERN bool ucl_parser_add_string_priority (struct ucl_parser *parser,
  985. const char *data, size_t len, unsigned priority);
  986. /**
  987. * Load and add data from a file
  988. * @param parser parser structure
  989. * @param filename the name of file
  990. * @param err if *err is NULL it is set to parser error
  991. * @return true if chunk has been added and false in case of error
  992. */
  993. UCL_EXTERN bool ucl_parser_add_file (struct ucl_parser *parser,
  994. const char *filename);
  995. /**
  996. * Load and add data from a file
  997. * @param parser parser structure
  998. * @param filename the name of file
  999. * @param err if *err is NULL it is set to parser error
  1000. * @param priority the desired priority of a chunk (only 4 least significant bits
  1001. * are considered for this parameter)
  1002. * @return true if chunk has been added and false in case of error
  1003. */
  1004. UCL_EXTERN bool ucl_parser_add_file_priority (struct ucl_parser *parser,
  1005. const char *filename, unsigned priority);
  1006. /**
  1007. * Load and add data from a file
  1008. * @param parser parser structure
  1009. * @param filename the name of file
  1010. * @param priority the desired priority of a chunk (only 4 least significant bits
  1011. * are considered for this parameter)
  1012. * @param strat Merge strategy to use while parsing this file
  1013. * @param parse_type Parser type to use while parsing this file
  1014. * @return true if chunk has been added and false in case of error
  1015. */
  1016. UCL_EXTERN bool ucl_parser_add_file_full (struct ucl_parser *parser, const char *filename,
  1017. unsigned priority, enum ucl_duplicate_strategy strat,
  1018. enum ucl_parse_type parse_type);
  1019. /**
  1020. * Load and add data from a file descriptor
  1021. * @param parser parser structure
  1022. * @param filename the name of file
  1023. * @param err if *err is NULL it is set to parser error
  1024. * @return true if chunk has been added and false in case of error
  1025. */
  1026. UCL_EXTERN bool ucl_parser_add_fd (struct ucl_parser *parser,
  1027. int fd);
  1028. /**
  1029. * Load and add data from a file descriptor
  1030. * @param parser parser structure
  1031. * @param filename the name of file
  1032. * @param err if *err is NULL it is set to parser error
  1033. * @param priority the desired priority of a chunk (only 4 least significant bits
  1034. * are considered for this parameter)
  1035. * @return true if chunk has been added and false in case of error
  1036. */
  1037. UCL_EXTERN bool ucl_parser_add_fd_priority (struct ucl_parser *parser,
  1038. int fd, unsigned priority);
  1039. /**
  1040. * Load and add data from a file descriptor
  1041. * @param parser parser structure
  1042. * @param filename the name of file
  1043. * @param err if *err is NULL it is set to parser error
  1044. * @param priority the desired priority of a chunk (only 4 least significant bits
  1045. * are considered for this parameter)
  1046. * @param strat Merge strategy to use while parsing this file
  1047. * @param parse_type Parser type to use while parsing this file
  1048. * @return true if chunk has been added and false in case of error
  1049. */
  1050. UCL_EXTERN bool ucl_parser_add_fd_full (struct ucl_parser *parser, int fd,
  1051. unsigned priority, enum ucl_duplicate_strategy strat,
  1052. enum ucl_parse_type parse_type);
  1053. /**
  1054. * Provide a UCL_ARRAY of paths to search for include files. The object is
  1055. * copied so caller must unref the object.
  1056. * @param parser parser structure
  1057. * @param paths UCL_ARRAY of paths to search
  1058. * @return true if the path search array was replaced in the parser
  1059. */
  1060. UCL_EXTERN bool ucl_set_include_path (struct ucl_parser *parser,
  1061. ucl_object_t *paths);
  1062. /**
  1063. * Get a top object for a parser (refcount is increased)
  1064. * @param parser parser structure
  1065. * @param err if *err is NULL it is set to parser error
  1066. * @return top parser object or NULL
  1067. */
  1068. UCL_EXTERN ucl_object_t* ucl_parser_get_object (struct ucl_parser *parser);
  1069. /**
  1070. * Get the current stack object as stack accessor function for use in macro
  1071. * functions (refcount is increased)
  1072. * @param parser parser object
  1073. * @param depth depth of stack to retrieve (top is 0)
  1074. * @return current stack object or NULL
  1075. */
  1076. UCL_EXTERN ucl_object_t* ucl_parser_get_current_stack_object (struct ucl_parser *parser, unsigned int depth);
  1077. /**
  1078. * Peek at the character at the current chunk position
  1079. * @param parser parser structure
  1080. * @return current chunk position character
  1081. */
  1082. UCL_EXTERN unsigned char ucl_parser_chunk_peek (struct ucl_parser *parser);
  1083. /**
  1084. * Skip the character at the current chunk position
  1085. * @param parser parser structure
  1086. * @return success boolean
  1087. */
  1088. UCL_EXTERN bool ucl_parser_chunk_skip (struct ucl_parser *parser);
  1089. /**
  1090. * Get the error string if parsing has been failed
  1091. * @param parser parser object
  1092. * @return error description
  1093. */
  1094. UCL_EXTERN const char *ucl_parser_get_error (struct ucl_parser *parser);
  1095. /**
  1096. * Get the code of the last error
  1097. * @param parser parser object
  1098. * @return error code
  1099. */
  1100. UCL_EXTERN int ucl_parser_get_error_code (struct ucl_parser *parser);
  1101. /**
  1102. * Get the current column number within parser
  1103. * @param parser parser object
  1104. * @return current column number
  1105. */
  1106. UCL_EXTERN unsigned ucl_parser_get_column (struct ucl_parser *parser);
  1107. /**
  1108. * Get the current line number within parser
  1109. * @param parser parser object
  1110. * @return current line number
  1111. */
  1112. UCL_EXTERN unsigned ucl_parser_get_linenum (struct ucl_parser *parser);
  1113. /**
  1114. * Clear the error in the parser
  1115. * @param parser parser object
  1116. */
  1117. UCL_EXTERN void ucl_parser_clear_error (struct ucl_parser *parser);
  1118. /**
  1119. * Free ucl parser object
  1120. * @param parser parser object
  1121. */
  1122. UCL_EXTERN void ucl_parser_free (struct ucl_parser *parser);
  1123. /**
  1124. * Get constant opaque pointer to comments structure for this parser. Increase
  1125. * refcount to prevent this object to be destroyed on parser's destruction
  1126. * @param parser parser structure
  1127. * @return ucl comments pointer or NULL
  1128. */
  1129. UCL_EXTERN const ucl_object_t * ucl_parser_get_comments (struct ucl_parser *parser);
  1130. /**
  1131. * Utility function to find a comment object for the specified object in the input
  1132. * @param comments comments object
  1133. * @param srch search object
  1134. * @return string comment enclosed in ucl_object_t
  1135. */
  1136. UCL_EXTERN const ucl_object_t * ucl_comments_find (const ucl_object_t *comments,
  1137. const ucl_object_t *srch);
  1138. /**
  1139. * Move comment from `from` object to `to` object
  1140. * @param comments comments object
  1141. * @param what source object
  1142. * @param with destination object
  1143. * @return `true` if `from` has comment and it has been moved to `to`
  1144. */
  1145. UCL_EXTERN bool ucl_comments_move (ucl_object_t *comments,
  1146. const ucl_object_t *from, const ucl_object_t *to);
  1147. /**
  1148. * Adds a new comment for an object
  1149. * @param comments comments object
  1150. * @param obj object to add comment to
  1151. * @param comment string representation of a comment
  1152. */
  1153. UCL_EXTERN void ucl_comments_add (ucl_object_t *comments,
  1154. const ucl_object_t *obj, const char *comment);
  1155. /**
  1156. * Add new public key to parser for signatures check
  1157. * @param parser parser object
  1158. * @param key PEM representation of a key
  1159. * @param len length of the key
  1160. * @param err if *err is NULL it is set to parser error
  1161. * @return true if a key has been successfully added
  1162. */
  1163. UCL_EXTERN bool ucl_parser_pubkey_add (struct ucl_parser *parser,
  1164. const unsigned char *key, size_t len);
  1165. /**
  1166. * Set FILENAME and CURDIR variables in parser
  1167. * @param parser parser object
  1168. * @param filename filename to set or NULL to set FILENAME to "undef" and CURDIR to getcwd()
  1169. * @param need_expand perform realpath() if this variable is true and filename is not NULL
  1170. * @return true if variables has been set
  1171. */
  1172. UCL_EXTERN bool ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename,
  1173. bool need_expand);
  1174. /**
  1175. * Returns current file for the parser
  1176. * @param parser parser object
  1177. * @return current file or NULL if parsing memory
  1178. */
  1179. UCL_EXTERN const char *ucl_parser_get_cur_file (struct ucl_parser *parser);
  1180. /**
  1181. * Defines special handler for certain types of data (identified by magic)
  1182. */
  1183. typedef bool (*ucl_parser_special_handler_t) (struct ucl_parser *parser,
  1184. const unsigned char *source, size_t source_len,
  1185. unsigned char **destination, size_t *dest_len,
  1186. void *user_data);
  1187. /**
  1188. * Special handler flags
  1189. */
  1190. enum ucl_special_handler_flags {
  1191. UCL_SPECIAL_HANDLER_DEFAULT = 0,
  1192. UCL_SPECIAL_HANDLER_PREPROCESS_ALL = (1u << 0),
  1193. };
  1194. /**
  1195. * Special handler structure
  1196. */
  1197. struct ucl_parser_special_handler {
  1198. const unsigned char *magic;
  1199. size_t magic_len;
  1200. enum ucl_special_handler_flags flags;
  1201. ucl_parser_special_handler_t handler;
  1202. void (*free_function) (unsigned char *data, size_t len, void *user_data);
  1203. void *user_data;
  1204. struct ucl_parser_special_handler *next; /* Used internally */
  1205. };
  1206. /**
  1207. * Add special handler for a parser, handles special sequences identified by magic
  1208. * @param parser parser structure
  1209. * @param handler handler structure
  1210. */
  1211. UCL_EXTERN void ucl_parser_add_special_handler (struct ucl_parser *parser,
  1212. struct ucl_parser_special_handler *handler);
  1213. /**
  1214. * Handler for include traces:
  1215. * @param parser parser object
  1216. * @param parent where include is done from
  1217. * @param args arguments to an include
  1218. * @param path path of the include
  1219. * @param pathlen length of the path
  1220. * @param user_data opaque userdata
  1221. */
  1222. typedef void (ucl_include_trace_func_t) (struct ucl_parser *parser,
  1223. const ucl_object_t *parent,
  1224. const ucl_object_t *args,
  1225. const char *path,
  1226. size_t pathlen,
  1227. void *user_data);
  1228. /**
  1229. * Register trace function for an include handler
  1230. * @param parser parser object
  1231. * @param func function to trace includes
  1232. * @param user_data opaque data
  1233. */
  1234. UCL_EXTERN void ucl_parser_set_include_tracer (struct ucl_parser *parser,
  1235. ucl_include_trace_func_t func,
  1236. void *user_data);
  1237. /** @} */
  1238. /**
  1239. * @defgroup emitter Emitting functions
  1240. * These functions are used to serialise UCL objects to some string representation.
  1241. *
  1242. * @{
  1243. */
  1244. struct ucl_emitter_context;
  1245. /**
  1246. * Structure using for emitter callbacks
  1247. */
  1248. struct ucl_emitter_functions {
  1249. /** Append a single character */
  1250. int (*ucl_emitter_append_character) (unsigned char c, size_t nchars, void *ud);
  1251. /** Append a string of a specified length */
  1252. int (*ucl_emitter_append_len) (unsigned const char *str, size_t len, void *ud);
  1253. /** Append a 64 bit integer */
  1254. int (*ucl_emitter_append_int) (int64_t elt, void *ud);
  1255. /** Append floating point element */
  1256. int (*ucl_emitter_append_double) (double elt, void *ud);
  1257. /** Free userdata */
  1258. void (*ucl_emitter_free_func)(void *ud);
  1259. /** Opaque userdata pointer */
  1260. void *ud;
  1261. };
  1262. struct ucl_emitter_operations {
  1263. /** Write a primitive element */
  1264. void (*ucl_emitter_write_elt) (struct ucl_emitter_context *ctx,
  1265. const ucl_object_t *obj, bool first, bool print_key);
  1266. /** Start ucl object */
  1267. void (*ucl_emitter_start_object) (struct ucl_emitter_context *ctx,
  1268. const ucl_object_t *obj, bool print_key);
  1269. /** End ucl object */
  1270. void (*ucl_emitter_end_object) (struct ucl_emitter_context *ctx,
  1271. const ucl_object_t *obj);
  1272. /** Start ucl array */
  1273. void (*ucl_emitter_start_array) (struct ucl_emitter_context *ctx,
  1274. const ucl_object_t *obj, bool print_key);
  1275. void (*ucl_emitter_end_array) (struct ucl_emitter_context *ctx,
  1276. const ucl_object_t *obj);
  1277. };
  1278. /**
  1279. * Structure that defines emitter functions
  1280. */
  1281. struct ucl_emitter_context {
  1282. /** Name of emitter (e.g. json, compact_json) */
  1283. const char *name;
  1284. /** Unique id (e.g. UCL_EMIT_JSON for standard emitters */
  1285. int id;
  1286. /** A set of output functions */
  1287. const struct ucl_emitter_functions *func;
  1288. /** A set of output operations */
  1289. const struct ucl_emitter_operations *ops;
  1290. /** Current amount of indent tabs */
  1291. unsigned int indent;
  1292. /** Top level object */
  1293. const ucl_object_t *top;
  1294. /** Optional comments */
  1295. const ucl_object_t *comments;
  1296. };
  1297. /**
  1298. * Emit object to a string
  1299. * @param obj object
  1300. * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is
  1301. * #UCL_EMIT_CONFIG then emit config like object
  1302. * @return dump of an object (must be freed after using) or NULL in case of error
  1303. */
  1304. UCL_EXTERN unsigned char *ucl_object_emit (const ucl_object_t *obj,
  1305. enum ucl_emitter emit_type);
  1306. /**
  1307. * Emit object to a string that can contain `\0` inside
  1308. * @param obj object
  1309. * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is
  1310. * #UCL_EMIT_CONFIG then emit config like object
  1311. * @param len the resulting length
  1312. * @return dump of an object (must be freed after using) or NULL in case of error
  1313. */
  1314. UCL_EXTERN unsigned char *ucl_object_emit_len (const ucl_object_t *obj,
  1315. enum ucl_emitter emit_type, size_t *len);
  1316. /**
  1317. * Emit object to a string
  1318. * @param obj object
  1319. * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is
  1320. * #UCL_EMIT_CONFIG then emit config like object
  1321. * @param emitter a set of emitter functions
  1322. * @param comments optional comments for the parser
  1323. * @return dump of an object (must be freed after using) or NULL in case of error
  1324. */
  1325. UCL_EXTERN bool ucl_object_emit_full (const ucl_object_t *obj,
  1326. enum ucl_emitter emit_type,
  1327. struct ucl_emitter_functions *emitter,
  1328. const ucl_object_t *comments);
  1329. /**
  1330. * Start streamlined UCL object emitter
  1331. * @param obj top UCL object
  1332. * @param emit_type emit type
  1333. * @param emitter a set of emitter functions
  1334. * @return new streamlined context that should be freed by
  1335. * `ucl_object_emit_streamline_finish`
  1336. */
  1337. UCL_EXTERN struct ucl_emitter_context* ucl_object_emit_streamline_new (
  1338. const ucl_object_t *obj, enum ucl_emitter emit_type,
  1339. struct ucl_emitter_functions *emitter);
  1340. /**
  1341. * Start object or array container for the streamlined output
  1342. * @param ctx streamlined context
  1343. * @param obj container object
  1344. */
  1345. UCL_EXTERN void ucl_object_emit_streamline_start_container (
  1346. struct ucl_emitter_context *ctx, const ucl_object_t *obj);
  1347. /**
  1348. * Add a complete UCL object to streamlined output
  1349. * @param ctx streamlined context
  1350. * @param obj object to output
  1351. */
  1352. UCL_EXTERN void ucl_object_emit_streamline_add_object (
  1353. struct ucl_emitter_context *ctx, const ucl_object_t *obj);
  1354. /**
  1355. * End previously added container
  1356. * @param ctx streamlined context
  1357. */
  1358. UCL_EXTERN void ucl_object_emit_streamline_end_container (
  1359. struct ucl_emitter_context *ctx);
  1360. /**
  1361. * Terminate streamlined container finishing all containers in it
  1362. * @param ctx streamlined context
  1363. */
  1364. UCL_EXTERN void ucl_object_emit_streamline_finish (
  1365. struct ucl_emitter_context *ctx);
  1366. /**
  1367. * Returns functions to emit object to memory
  1368. * @param pmem target pointer (should be freed by caller)
  1369. * @return emitter functions structure
  1370. */
  1371. UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_memory_funcs (
  1372. void **pmem);
  1373. /**
  1374. * Returns functions to emit object to FILE *
  1375. * @param fp FILE * object
  1376. * @return emitter functions structure
  1377. */
  1378. UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_file_funcs (
  1379. FILE *fp);
  1380. /**
  1381. * Returns functions to emit object to a file descriptor
  1382. * @param fd file descriptor
  1383. * @return emitter functions structure
  1384. */
  1385. UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_fd_funcs (
  1386. int fd);
  1387. /**
  1388. * Free emitter functions
  1389. * @param f pointer to functions
  1390. */
  1391. UCL_EXTERN void ucl_object_emit_funcs_free (struct ucl_emitter_functions *f);
  1392. /** @} */
  1393. /**
  1394. * @defgroup schema Schema functions
  1395. * These functions are used to validate UCL objects using json schema format
  1396. *
  1397. * @{
  1398. */
  1399. /**
  1400. * Used to define UCL schema error
  1401. */
  1402. enum ucl_schema_error_code {
  1403. UCL_SCHEMA_OK = 0, /**< no error */
  1404. UCL_SCHEMA_TYPE_MISMATCH, /**< type of object is incorrect */
  1405. UCL_SCHEMA_INVALID_SCHEMA, /**< schema is invalid */
  1406. UCL_SCHEMA_MISSING_PROPERTY,/**< one or more missing properties */
  1407. UCL_SCHEMA_CONSTRAINT, /**< constraint found */
  1408. UCL_SCHEMA_MISSING_DEPENDENCY, /**< missing dependency */
  1409. UCL_SCHEMA_EXTERNAL_REF_MISSING, /**< cannot fetch external ref */
  1410. UCL_SCHEMA_EXTERNAL_REF_INVALID, /**< invalid external ref */
  1411. UCL_SCHEMA_INTERNAL_ERROR, /**< something bad happened */
  1412. UCL_SCHEMA_UNKNOWN /**< generic error */
  1413. };
  1414. /**
  1415. * Generic ucl schema error
  1416. */
  1417. struct ucl_schema_error {
  1418. enum ucl_schema_error_code code; /**< error code */
  1419. char msg[128]; /**< error message */
  1420. const ucl_object_t *obj; /**< object where error occurred */
  1421. };
  1422. /**
  1423. * Validate object `obj` using schema object `schema`.
  1424. * @param schema schema object
  1425. * @param obj object to validate
  1426. * @param err error pointer, if this parameter is not NULL and error has been
  1427. * occurred, then `err` is filled with the exact error definition.
  1428. * @return true if `obj` is valid using `schema`
  1429. */
  1430. UCL_EXTERN bool ucl_object_validate (const ucl_object_t *schema,
  1431. const ucl_object_t *obj, struct ucl_schema_error *err);
  1432. /**
  1433. * Validate object `obj` using schema object `schema` and root schema at `root`.
  1434. * @param schema schema object
  1435. * @param obj object to validate
  1436. * @param root root schema object
  1437. * @param err error pointer, if this parameter is not NULL and error has been
  1438. * occurred, then `err` is filled with the exact error definition.
  1439. * @return true if `obj` is valid using `schema`
  1440. */
  1441. UCL_EXTERN bool ucl_object_validate_root (const ucl_object_t *schema,
  1442. const ucl_object_t *obj,
  1443. const ucl_object_t *root,
  1444. struct ucl_schema_error *err);
  1445. /**
  1446. * Validate object `obj` using schema object `schema` and root schema at `root`
  1447. * using some external references provided.
  1448. * @param schema schema object
  1449. * @param obj object to validate
  1450. * @param root root schema object
  1451. * @param ext_refs external references (might be modified during validation)
  1452. * @param err error pointer, if this parameter is not NULL and error has been
  1453. * occurred, then `err` is filled with the exact error definition.
  1454. * @return true if `obj` is valid using `schema`
  1455. */
  1456. UCL_EXTERN bool ucl_object_validate_root_ext (const ucl_object_t *schema,
  1457. const ucl_object_t *obj,
  1458. const ucl_object_t *root,
  1459. ucl_object_t *ext_refs,
  1460. struct ucl_schema_error *err);
  1461. /** @} */
  1462. #ifdef __cplusplus
  1463. }
  1464. #endif
  1465. /*
  1466. * XXX: Poorly named API functions, need to replace them with the appropriate
  1467. * named function. All API functions *must* use naming ucl_object_*. Usage of
  1468. * ucl_obj* should be avoided.
  1469. */
  1470. #define ucl_obj_todouble_safe ucl_object_todouble_safe
  1471. #define ucl_obj_todouble ucl_object_todouble
  1472. #define ucl_obj_tostring ucl_object_tostring
  1473. #define ucl_obj_tostring_safe ucl_object_tostring_safe
  1474. #define ucl_obj_tolstring ucl_object_tolstring
  1475. #define ucl_obj_tolstring_safe ucl_object_tolstring_safe
  1476. #define ucl_obj_toint ucl_object_toint
  1477. #define ucl_obj_toint_safe ucl_object_toint_safe
  1478. #define ucl_obj_toboolean ucl_object_toboolean
  1479. #define ucl_obj_toboolean_safe ucl_object_toboolean_safe
  1480. #define ucl_obj_get_key ucl_object_find_key
  1481. #define ucl_obj_get_keyl ucl_object_find_keyl
  1482. #define ucl_obj_unref ucl_object_unref
  1483. #define ucl_obj_ref ucl_object_ref
  1484. #define ucl_obj_free ucl_object_free
  1485. #endif /* UCL_H_ */