xref: /freebsd/contrib/libucl/include/ucl.h (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
139ee7a7aSBaptiste Daroussin /* Copyright (c) 2013-2015, Vsevolod Stakhov
2c99fb5f9SBaptiste Daroussin  * All rights reserved.
3c99fb5f9SBaptiste Daroussin  *
4c99fb5f9SBaptiste Daroussin  * Redistribution and use in source and binary forms, with or without
5c99fb5f9SBaptiste Daroussin  * modification, are permitted provided that the following conditions are met:
6c99fb5f9SBaptiste Daroussin  *       * Redistributions of source code must retain the above copyright
7c99fb5f9SBaptiste Daroussin  *         notice, this list of conditions and the following disclaimer.
8c99fb5f9SBaptiste Daroussin  *       * Redistributions in binary form must reproduce the above copyright
9c99fb5f9SBaptiste Daroussin  *         notice, this list of conditions and the following disclaimer in the
10c99fb5f9SBaptiste Daroussin  *         documentation and/or other materials provided with the distribution.
11c99fb5f9SBaptiste Daroussin  *
12c99fb5f9SBaptiste Daroussin  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13c99fb5f9SBaptiste Daroussin  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14c99fb5f9SBaptiste Daroussin  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15c99fb5f9SBaptiste Daroussin  * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16c99fb5f9SBaptiste Daroussin  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17c99fb5f9SBaptiste Daroussin  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18c99fb5f9SBaptiste Daroussin  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19c99fb5f9SBaptiste Daroussin  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20c99fb5f9SBaptiste Daroussin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21c99fb5f9SBaptiste Daroussin  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22c99fb5f9SBaptiste Daroussin  */
23c99fb5f9SBaptiste Daroussin 
24c99fb5f9SBaptiste Daroussin #ifndef UCL_H_
25c99fb5f9SBaptiste Daroussin #define UCL_H_
26c99fb5f9SBaptiste Daroussin 
27c99fb5f9SBaptiste Daroussin #include <string.h>
28c99fb5f9SBaptiste Daroussin #include <stddef.h>
29c99fb5f9SBaptiste Daroussin #include <stdlib.h>
30c99fb5f9SBaptiste Daroussin #include <stdint.h>
31c99fb5f9SBaptiste Daroussin #include <stdbool.h>
32c99fb5f9SBaptiste Daroussin #include <stdarg.h>
33c99fb5f9SBaptiste Daroussin #include <stdio.h>
34c99fb5f9SBaptiste Daroussin 
3536c53d67SBaptiste Daroussin #ifdef _WIN32
3636c53d67SBaptiste Daroussin # define UCL_EXTERN __declspec(dllexport)
3736c53d67SBaptiste Daroussin #else
3836c53d67SBaptiste Daroussin # define UCL_EXTERN
3936c53d67SBaptiste Daroussin #endif
4036c53d67SBaptiste Daroussin 
41c99fb5f9SBaptiste Daroussin /**
42c99fb5f9SBaptiste Daroussin  * @mainpage
43c99fb5f9SBaptiste Daroussin  * This is a reference manual for UCL API. You may find the description of UCL format by following this
44c99fb5f9SBaptiste Daroussin  * [github repository](https://github.com/vstakhov/libucl).
45c99fb5f9SBaptiste Daroussin  *
46c99fb5f9SBaptiste Daroussin  * This manual has several main sections:
47c99fb5f9SBaptiste Daroussin  *  - @ref structures
48c99fb5f9SBaptiste Daroussin  *  - @ref utils
49c99fb5f9SBaptiste Daroussin  *  - @ref parser
50c99fb5f9SBaptiste Daroussin  *  - @ref emitter
51c99fb5f9SBaptiste Daroussin  */
52c99fb5f9SBaptiste Daroussin 
53c99fb5f9SBaptiste Daroussin /**
54c99fb5f9SBaptiste Daroussin  * @file ucl.h
55c99fb5f9SBaptiste Daroussin  * @brief UCL parsing and emitting functions
56c99fb5f9SBaptiste Daroussin  *
57c99fb5f9SBaptiste Daroussin  * UCL is universal configuration language, which is a form of
58c99fb5f9SBaptiste Daroussin  * JSON with less strict rules that make it more comfortable for
59c99fb5f9SBaptiste Daroussin  * using as a configuration language
60c99fb5f9SBaptiste Daroussin  */
61c99fb5f9SBaptiste Daroussin #ifdef  __cplusplus
62c99fb5f9SBaptiste Daroussin extern "C" {
63c99fb5f9SBaptiste Daroussin #endif
64c99fb5f9SBaptiste Daroussin /*
65c99fb5f9SBaptiste Daroussin  * Memory allocation utilities
66c99fb5f9SBaptiste Daroussin  * UCL_ALLOC(size) - allocate memory for UCL
67c99fb5f9SBaptiste Daroussin  * UCL_FREE(size, ptr) - free memory of specified size at ptr
68c99fb5f9SBaptiste Daroussin  * Default: malloc and free
69c99fb5f9SBaptiste Daroussin  */
70c99fb5f9SBaptiste Daroussin #ifndef UCL_ALLOC
71c99fb5f9SBaptiste Daroussin #define UCL_ALLOC(size) malloc(size)
72c99fb5f9SBaptiste Daroussin #endif
73c99fb5f9SBaptiste Daroussin #ifndef UCL_FREE
74c99fb5f9SBaptiste Daroussin #define UCL_FREE(size, ptr) free(ptr)
75c99fb5f9SBaptiste Daroussin #endif
76c99fb5f9SBaptiste Daroussin 
77c99fb5f9SBaptiste Daroussin #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
78c99fb5f9SBaptiste Daroussin #define UCL_WARN_UNUSED_RESULT               \
79c99fb5f9SBaptiste Daroussin   __attribute__((warn_unused_result))
80c99fb5f9SBaptiste Daroussin #else
81c99fb5f9SBaptiste Daroussin #define UCL_WARN_UNUSED_RESULT
82c99fb5f9SBaptiste Daroussin #endif
83c99fb5f9SBaptiste Daroussin 
84b04a7a0bSBaptiste Daroussin #ifdef __GNUC__
85b04a7a0bSBaptiste Daroussin #define UCL_DEPRECATED(func) func __attribute__ ((deprecated))
86b04a7a0bSBaptiste Daroussin #elif defined(_MSC_VER)
87b04a7a0bSBaptiste Daroussin #define UCL_DEPRECATED(func) __declspec(deprecated) func
88b04a7a0bSBaptiste Daroussin #else
89b04a7a0bSBaptiste Daroussin #define UCL_DEPRECATED(func) func
90b04a7a0bSBaptiste Daroussin #endif
91b04a7a0bSBaptiste Daroussin 
92c99fb5f9SBaptiste Daroussin /**
93c99fb5f9SBaptiste Daroussin  * @defgroup structures Structures and types
94c99fb5f9SBaptiste Daroussin  * UCL defines several enumeration types used for error reporting or specifying flags and attributes.
95c99fb5f9SBaptiste Daroussin  *
96c99fb5f9SBaptiste Daroussin  * @{
97c99fb5f9SBaptiste Daroussin  */
98c99fb5f9SBaptiste Daroussin 
99c99fb5f9SBaptiste Daroussin /**
100c99fb5f9SBaptiste Daroussin  * The common error codes returned by ucl parser
101c99fb5f9SBaptiste Daroussin  */
102c99fb5f9SBaptiste Daroussin typedef enum ucl_error {
103c99fb5f9SBaptiste Daroussin 	UCL_EOK = 0, /**< No error */
104c99fb5f9SBaptiste Daroussin 	UCL_ESYNTAX, /**< Syntax error occurred during parsing */
105c99fb5f9SBaptiste Daroussin 	UCL_EIO, /**< IO error occurred during parsing */
106c99fb5f9SBaptiste Daroussin 	UCL_ESTATE, /**< Invalid state machine state */
107c99fb5f9SBaptiste Daroussin 	UCL_ENESTED, /**< Input has too many recursion levels */
108c99fb5f9SBaptiste Daroussin 	UCL_EMACRO, /**< Error processing a macro */
109c99fb5f9SBaptiste Daroussin 	UCL_EINTERNAL, /**< Internal unclassified error */
110*d9f0ce31SBaptiste Daroussin 	UCL_ESSL, /**< SSL error */
111*d9f0ce31SBaptiste Daroussin 	UCL_EMERGE /**< A merge error occured */
112c99fb5f9SBaptiste Daroussin } ucl_error_t;
113c99fb5f9SBaptiste Daroussin 
114c99fb5f9SBaptiste Daroussin /**
115c99fb5f9SBaptiste Daroussin  * #ucl_object_t may have one of specified types, some types are compatible with each other and some are not.
116c99fb5f9SBaptiste Daroussin  * For example, you can always convert #UCL_TIME to #UCL_FLOAT. Also you can convert #UCL_FLOAT to #UCL_INTEGER
117c99fb5f9SBaptiste Daroussin  * by loosing floating point. Every object may be converted to a string by #ucl_object_tostring_forced() function.
118c99fb5f9SBaptiste Daroussin  *
119c99fb5f9SBaptiste Daroussin  */
120c99fb5f9SBaptiste Daroussin typedef enum ucl_type {
121c99fb5f9SBaptiste Daroussin 	UCL_OBJECT = 0, /**< UCL object - key/value pairs */
122c99fb5f9SBaptiste Daroussin 	UCL_ARRAY, /**< UCL array */
123c99fb5f9SBaptiste Daroussin 	UCL_INT, /**< Integer number */
124c99fb5f9SBaptiste Daroussin 	UCL_FLOAT, /**< Floating point number */
125c99fb5f9SBaptiste Daroussin 	UCL_STRING, /**< Null terminated string */
126c99fb5f9SBaptiste Daroussin 	UCL_BOOLEAN, /**< Boolean value */
127c99fb5f9SBaptiste Daroussin 	UCL_TIME, /**< Time value (floating point number of seconds) */
128c99fb5f9SBaptiste Daroussin 	UCL_USERDATA, /**< Opaque userdata pointer (may be used in macros) */
129c99fb5f9SBaptiste Daroussin 	UCL_NULL /**< Null value */
130c99fb5f9SBaptiste Daroussin } ucl_type_t;
131c99fb5f9SBaptiste Daroussin 
132c99fb5f9SBaptiste Daroussin /**
133c99fb5f9SBaptiste Daroussin  * You can use one of these types to serialise #ucl_object_t by using ucl_object_emit().
134c99fb5f9SBaptiste Daroussin  */
135c99fb5f9SBaptiste Daroussin typedef enum ucl_emitter {
136c99fb5f9SBaptiste Daroussin 	UCL_EMIT_JSON = 0, /**< Emit fine formatted JSON */
137c99fb5f9SBaptiste Daroussin 	UCL_EMIT_JSON_COMPACT, /**< Emit compacted JSON */
138c99fb5f9SBaptiste Daroussin 	UCL_EMIT_CONFIG, /**< Emit human readable config format */
13939ee7a7aSBaptiste Daroussin 	UCL_EMIT_YAML, /**< Emit embedded YAML format */
14039ee7a7aSBaptiste Daroussin 	UCL_EMIT_MSGPACK, /**< Emit msgpack output */
14139ee7a7aSBaptiste Daroussin 	UCL_EMIT_MAX /**< Unsupported emitter type */
142c99fb5f9SBaptiste Daroussin } ucl_emitter_t;
143c99fb5f9SBaptiste Daroussin 
144c99fb5f9SBaptiste Daroussin /**
145c99fb5f9SBaptiste Daroussin  * These flags defines parser behaviour. If you specify #UCL_PARSER_ZEROCOPY you must ensure
146c99fb5f9SBaptiste Daroussin  * that the input memory is not freed if an object is in use. Moreover, if you want to use
147c99fb5f9SBaptiste Daroussin  * zero-terminated keys and string values then you should not use zero-copy mode, as in this case
148c99fb5f9SBaptiste Daroussin  * UCL still has to perform copying implicitly.
149c99fb5f9SBaptiste Daroussin  */
150c99fb5f9SBaptiste Daroussin typedef enum ucl_parser_flags {
151*d9f0ce31SBaptiste Daroussin 	UCL_PARSER_DEFAULT = 0,       /**< No special flags */
152*d9f0ce31SBaptiste Daroussin 	UCL_PARSER_KEY_LOWERCASE = (1 << 0), /**< Convert all keys to lower case */
153*d9f0ce31SBaptiste Daroussin 	UCL_PARSER_ZEROCOPY = (1 << 1), /**< Parse input in zero-copy mode if possible */
154*d9f0ce31SBaptiste Daroussin 	UCL_PARSER_NO_TIME = (1 << 2), /**< Do not parse time and treat time values as strings */
155*d9f0ce31SBaptiste Daroussin 	UCL_PARSER_NO_IMPLICIT_ARRAYS = (1 << 3), /** Create explicit arrays instead of implicit ones */
156*d9f0ce31SBaptiste Daroussin 	UCL_PARSER_SAVE_COMMENTS = (1 << 4), /** Save comments in the parser context */
157*d9f0ce31SBaptiste Daroussin 	UCL_PARSER_DISABLE_MACRO = (1 << 5) /** Treat macros as comments */
158c99fb5f9SBaptiste Daroussin } ucl_parser_flags_t;
159c99fb5f9SBaptiste Daroussin 
160c99fb5f9SBaptiste Daroussin /**
161c99fb5f9SBaptiste Daroussin  * String conversion flags, that are used in #ucl_object_fromstring_common function.
162c99fb5f9SBaptiste Daroussin  */
163c99fb5f9SBaptiste Daroussin typedef enum ucl_string_flags {
16439ee7a7aSBaptiste Daroussin 	UCL_STRING_RAW = 0x0,     /**< Treat string as is */
165*d9f0ce31SBaptiste Daroussin 	UCL_STRING_ESCAPE = (1 << 0),  /**< Perform JSON escape */
166*d9f0ce31SBaptiste Daroussin 	UCL_STRING_TRIM = (1 << 1),    /**< Trim leading and trailing whitespaces */
167*d9f0ce31SBaptiste Daroussin 	UCL_STRING_PARSE_BOOLEAN = (1 << 2),    /**< Parse passed string and detect boolean */
168*d9f0ce31SBaptiste Daroussin 	UCL_STRING_PARSE_INT = (1 << 3),    /**< Parse passed string and detect integer number */
169*d9f0ce31SBaptiste Daroussin 	UCL_STRING_PARSE_DOUBLE = (1 << 4),    /**< Parse passed string and detect integer or float number */
170*d9f0ce31SBaptiste Daroussin 	UCL_STRING_PARSE_TIME = (1 << 5), /**< Parse time strings */
17197bd480fSBaptiste Daroussin 	UCL_STRING_PARSE_NUMBER =  UCL_STRING_PARSE_INT|UCL_STRING_PARSE_DOUBLE|UCL_STRING_PARSE_TIME,  /**<
172c99fb5f9SBaptiste Daroussin 									Parse passed string and detect number */
173c99fb5f9SBaptiste Daroussin 	UCL_STRING_PARSE =  UCL_STRING_PARSE_BOOLEAN|UCL_STRING_PARSE_NUMBER,   /**<
174c99fb5f9SBaptiste Daroussin 									Parse passed string (and detect booleans and numbers) */
175*d9f0ce31SBaptiste Daroussin 	UCL_STRING_PARSE_BYTES = (1 << 6)  /**< Treat numbers as bytes */
176c99fb5f9SBaptiste Daroussin } ucl_string_flags_t;
177c99fb5f9SBaptiste Daroussin 
178c99fb5f9SBaptiste Daroussin /**
179c99fb5f9SBaptiste Daroussin  * Basic flags for an object
180c99fb5f9SBaptiste Daroussin  */
181c99fb5f9SBaptiste Daroussin typedef enum ucl_object_flags {
18239ee7a7aSBaptiste Daroussin 	UCL_OBJECT_ALLOCATED_KEY = (1 << 0), /**< An object has key allocated internally */
18339ee7a7aSBaptiste Daroussin 	UCL_OBJECT_ALLOCATED_VALUE = (1 << 1), /**< An object has a string value allocated internally */
18439ee7a7aSBaptiste Daroussin 	UCL_OBJECT_NEED_KEY_ESCAPE = (1 << 2), /**< The key of an object need to be escaped on output */
18539ee7a7aSBaptiste Daroussin 	UCL_OBJECT_EPHEMERAL = (1 << 3), /**< Temporary object that does not need to be freed really */
18639ee7a7aSBaptiste Daroussin 	UCL_OBJECT_MULTILINE = (1 << 4), /**< String should be displayed as multiline string */
18739ee7a7aSBaptiste Daroussin 	UCL_OBJECT_MULTIVALUE = (1 << 5), /**< Object is a key with multiple values */
18839ee7a7aSBaptiste Daroussin 	UCL_OBJECT_INHERITED = (1 << 6), /**< Object has been inherited from another */
18939ee7a7aSBaptiste Daroussin 	UCL_OBJECT_BINARY = (1 << 7) /**< Object contains raw binary data */
190c99fb5f9SBaptiste Daroussin } ucl_object_flags_t;
191c99fb5f9SBaptiste Daroussin 
192c99fb5f9SBaptiste Daroussin /**
19339ee7a7aSBaptiste Daroussin  * Duplicate policy types
19439ee7a7aSBaptiste Daroussin  */
19539ee7a7aSBaptiste Daroussin enum ucl_duplicate_strategy {
19639ee7a7aSBaptiste Daroussin 	UCL_DUPLICATE_APPEND = 0, /**< Default policy to merge based on priorities */
19739ee7a7aSBaptiste Daroussin 	UCL_DUPLICATE_MERGE,     /**< Merge new object with old one */
19839ee7a7aSBaptiste Daroussin 	UCL_DUPLICATE_REWRITE,   /**< Rewrite old keys */
19939ee7a7aSBaptiste Daroussin 	UCL_DUPLICATE_ERROR      /**< Stop parsing on duplicate found */
20039ee7a7aSBaptiste Daroussin };
20139ee7a7aSBaptiste Daroussin 
20239ee7a7aSBaptiste Daroussin /**
20339ee7a7aSBaptiste Daroussin  * Input format type
20439ee7a7aSBaptiste Daroussin  */
20539ee7a7aSBaptiste Daroussin enum ucl_parse_type {
20639ee7a7aSBaptiste Daroussin 	UCL_PARSE_UCL = 0, /**< Default ucl format */
20739ee7a7aSBaptiste Daroussin 	UCL_PARSE_MSGPACK, /**< Message pack input format */
20839ee7a7aSBaptiste Daroussin 	UCL_PARSE_CSEXP /**< Canonical S-expressions */
20939ee7a7aSBaptiste Daroussin };
21039ee7a7aSBaptiste Daroussin 
21139ee7a7aSBaptiste Daroussin /**
212c99fb5f9SBaptiste Daroussin  * UCL object structure. Please mention that the most of fields should not be touched by
213c99fb5f9SBaptiste Daroussin  * UCL users. In future, this structure may be converted to private one.
214c99fb5f9SBaptiste Daroussin  */
215c99fb5f9SBaptiste Daroussin typedef struct ucl_object_s {
216c99fb5f9SBaptiste Daroussin 	/**
217c99fb5f9SBaptiste Daroussin 	 * Variant value type
218c99fb5f9SBaptiste Daroussin 	 */
219c99fb5f9SBaptiste Daroussin 	union {
220c99fb5f9SBaptiste Daroussin 		int64_t iv;							/**< Int value of an object */
221c99fb5f9SBaptiste Daroussin 		const char *sv;						/**< String value of an object */
222c99fb5f9SBaptiste Daroussin 		double dv;							/**< Double value of an object */
2238e3b1ab2SBaptiste Daroussin 		void *av;							/**< Array					*/
224c99fb5f9SBaptiste Daroussin 		void *ov;							/**< Object					*/
225c99fb5f9SBaptiste Daroussin 		void* ud;							/**< Opaque user data		*/
226c99fb5f9SBaptiste Daroussin 	} value;
227c99fb5f9SBaptiste Daroussin 	const char *key;						/**< Key of an object		*/
228c99fb5f9SBaptiste Daroussin 	struct ucl_object_s *next;				/**< Array handle			*/
229c99fb5f9SBaptiste Daroussin 	struct ucl_object_s *prev;				/**< Array handle			*/
2304bf54857SBaptiste Daroussin 	uint32_t keylen;						/**< Lenght of a key		*/
2314bf54857SBaptiste Daroussin 	uint32_t len;							/**< Size of an object		*/
2324bf54857SBaptiste Daroussin 	uint32_t ref;							/**< Reference count		*/
233c99fb5f9SBaptiste Daroussin 	uint16_t flags;							/**< Object flags			*/
2344bf54857SBaptiste Daroussin 	uint16_t type;							/**< Real type				*/
2354bf54857SBaptiste Daroussin 	unsigned char* trash_stack[2];			/**< Pointer to allocated chunks */
236c99fb5f9SBaptiste Daroussin } ucl_object_t;
237c99fb5f9SBaptiste Daroussin 
2384bf54857SBaptiste Daroussin /**
2394bf54857SBaptiste Daroussin  * Destructor type for userdata objects
2404bf54857SBaptiste Daroussin  * @param ud user specified data pointer
2414bf54857SBaptiste Daroussin  */
2424bf54857SBaptiste Daroussin typedef void (*ucl_userdata_dtor)(void *ud);
2434bf54857SBaptiste Daroussin typedef const char* (*ucl_userdata_emitter)(void *ud);
2444bf54857SBaptiste Daroussin 
245c99fb5f9SBaptiste Daroussin /** @} */
246c99fb5f9SBaptiste Daroussin 
247c99fb5f9SBaptiste Daroussin /**
248c99fb5f9SBaptiste Daroussin  * @defgroup utils Utility functions
249c99fb5f9SBaptiste Daroussin  * A number of utility functions simplify handling of UCL objects
250c99fb5f9SBaptiste Daroussin  *
251c99fb5f9SBaptiste Daroussin  * @{
252c99fb5f9SBaptiste Daroussin  */
253c99fb5f9SBaptiste Daroussin /**
254c99fb5f9SBaptiste Daroussin  * Copy and return a key of an object, returned key is zero-terminated
255c99fb5f9SBaptiste Daroussin  * @param obj CL object
256c99fb5f9SBaptiste Daroussin  * @return zero terminated key
257c99fb5f9SBaptiste Daroussin  */
258b04a7a0bSBaptiste Daroussin UCL_EXTERN char* ucl_copy_key_trash (const ucl_object_t *obj);
259c99fb5f9SBaptiste Daroussin 
260c99fb5f9SBaptiste Daroussin /**
261c99fb5f9SBaptiste Daroussin  * Copy and return a string value of an object, returned key is zero-terminated
262c99fb5f9SBaptiste Daroussin  * @param obj CL object
263c99fb5f9SBaptiste Daroussin  * @return zero terminated string representation of object value
264c99fb5f9SBaptiste Daroussin  */
265b04a7a0bSBaptiste Daroussin UCL_EXTERN char* ucl_copy_value_trash (const ucl_object_t *obj);
266c99fb5f9SBaptiste Daroussin 
267c99fb5f9SBaptiste Daroussin /**
268c99fb5f9SBaptiste Daroussin  * Creates a new object
269c99fb5f9SBaptiste Daroussin  * @return new object
270c99fb5f9SBaptiste Daroussin  */
27197bd480fSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_new (void) UCL_WARN_UNUSED_RESULT;
272c99fb5f9SBaptiste Daroussin 
273c99fb5f9SBaptiste Daroussin /**
274c99fb5f9SBaptiste Daroussin  * Create new object with type specified
275c99fb5f9SBaptiste Daroussin  * @param type type of a new object
276c99fb5f9SBaptiste Daroussin  * @return new object
277c99fb5f9SBaptiste Daroussin  */
2782e8ed2b8SBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_typed_new (ucl_type_t type) UCL_WARN_UNUSED_RESULT;
2792e8ed2b8SBaptiste Daroussin 
2802e8ed2b8SBaptiste Daroussin /**
2814bf54857SBaptiste Daroussin  * Create new object with type and priority specified
2824bf54857SBaptiste Daroussin  * @param type type of a new object
2834bf54857SBaptiste Daroussin  * @param priority priority of an object
2844bf54857SBaptiste Daroussin  * @return new object
2854bf54857SBaptiste Daroussin  */
2864bf54857SBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_new_full (ucl_type_t type, unsigned priority)
2874bf54857SBaptiste Daroussin 	UCL_WARN_UNUSED_RESULT;
2884bf54857SBaptiste Daroussin 
2894bf54857SBaptiste Daroussin /**
2904bf54857SBaptiste Daroussin  * Create new object with userdata dtor
2914bf54857SBaptiste Daroussin  * @param dtor destructor function
292*d9f0ce31SBaptiste Daroussin  * @param emitter emitter for userdata
293*d9f0ce31SBaptiste Daroussin  * @param ptr opaque pointer
2944bf54857SBaptiste Daroussin  * @return new object
2954bf54857SBaptiste Daroussin  */
2964bf54857SBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_new_userdata (ucl_userdata_dtor dtor,
297*d9f0ce31SBaptiste Daroussin 		ucl_userdata_emitter emitter, void *ptr) UCL_WARN_UNUSED_RESULT;
2984bf54857SBaptiste Daroussin 
2994bf54857SBaptiste Daroussin /**
3004bf54857SBaptiste Daroussin  * Perform deep copy of an object copying everything
3014bf54857SBaptiste Daroussin  * @param other object to copy
3024bf54857SBaptiste Daroussin  * @return new object with refcount equal to 1
3034bf54857SBaptiste Daroussin  */
3044bf54857SBaptiste Daroussin UCL_EXTERN ucl_object_t * ucl_object_copy (const ucl_object_t *other)
3054bf54857SBaptiste Daroussin 	UCL_WARN_UNUSED_RESULT;
3064bf54857SBaptiste Daroussin 
3074bf54857SBaptiste Daroussin /**
3082e8ed2b8SBaptiste Daroussin  * Return the type of an object
3092e8ed2b8SBaptiste Daroussin  * @return the object type
3102e8ed2b8SBaptiste Daroussin  */
3112e8ed2b8SBaptiste Daroussin UCL_EXTERN ucl_type_t ucl_object_type (const ucl_object_t *obj);
312c99fb5f9SBaptiste Daroussin 
313c99fb5f9SBaptiste Daroussin /**
314*d9f0ce31SBaptiste Daroussin  * Converts ucl object type to its string representation
315*d9f0ce31SBaptiste Daroussin  * @param type type of object
316*d9f0ce31SBaptiste Daroussin  * @return constant string describing type
317*d9f0ce31SBaptiste Daroussin  */
318*d9f0ce31SBaptiste Daroussin UCL_EXTERN const char * ucl_object_type_to_string (ucl_type_t type);
319*d9f0ce31SBaptiste Daroussin 
320*d9f0ce31SBaptiste Daroussin /**
321*d9f0ce31SBaptiste Daroussin  * Converts string that represents ucl type to real ucl type enum
322*d9f0ce31SBaptiste Daroussin  * @param input C string with name of type
323*d9f0ce31SBaptiste Daroussin  * @param res resulting target
324*d9f0ce31SBaptiste Daroussin  * @return true if `input` is a name of type stored in `res`
325*d9f0ce31SBaptiste Daroussin  */
326*d9f0ce31SBaptiste Daroussin UCL_EXTERN bool ucl_object_string_to_type (const char *input, ucl_type_t *res);
327*d9f0ce31SBaptiste Daroussin 
328*d9f0ce31SBaptiste Daroussin /**
329c99fb5f9SBaptiste Daroussin  * Convert any string to an ucl object making the specified transformations
330c99fb5f9SBaptiste Daroussin  * @param str fixed size or NULL terminated string
331c99fb5f9SBaptiste Daroussin  * @param len length (if len is zero, than str is treated as NULL terminated)
332c99fb5f9SBaptiste Daroussin  * @param flags conversion flags
333c99fb5f9SBaptiste Daroussin  * @return new object
334c99fb5f9SBaptiste Daroussin  */
33536c53d67SBaptiste Daroussin UCL_EXTERN ucl_object_t * ucl_object_fromstring_common (const char *str, size_t len,
336c99fb5f9SBaptiste Daroussin 		enum ucl_string_flags flags) UCL_WARN_UNUSED_RESULT;
337c99fb5f9SBaptiste Daroussin 
338c99fb5f9SBaptiste Daroussin /**
339c99fb5f9SBaptiste Daroussin  * Create a UCL object from the specified string
340c99fb5f9SBaptiste Daroussin  * @param str NULL terminated string, will be json escaped
341c99fb5f9SBaptiste Daroussin  * @return new object
342c99fb5f9SBaptiste Daroussin  */
343b04a7a0bSBaptiste Daroussin UCL_EXTERN ucl_object_t *ucl_object_fromstring (const char *str) UCL_WARN_UNUSED_RESULT;
344c99fb5f9SBaptiste Daroussin 
345c99fb5f9SBaptiste Daroussin /**
346c99fb5f9SBaptiste Daroussin  * Create a UCL object from the specified string
347c99fb5f9SBaptiste Daroussin  * @param str fixed size string, will be json escaped
348c99fb5f9SBaptiste Daroussin  * @param len length of a string
349c99fb5f9SBaptiste Daroussin  * @return new object
350c99fb5f9SBaptiste Daroussin  */
351b04a7a0bSBaptiste Daroussin UCL_EXTERN ucl_object_t *ucl_object_fromlstring (const char *str,
352b04a7a0bSBaptiste Daroussin 		size_t len) UCL_WARN_UNUSED_RESULT;
353c99fb5f9SBaptiste Daroussin 
354c99fb5f9SBaptiste Daroussin /**
355c99fb5f9SBaptiste Daroussin  * Create an object from an integer number
356c99fb5f9SBaptiste Daroussin  * @param iv number
357c99fb5f9SBaptiste Daroussin  * @return new object
358c99fb5f9SBaptiste Daroussin  */
359b04a7a0bSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_fromint (int64_t iv) UCL_WARN_UNUSED_RESULT;
360c99fb5f9SBaptiste Daroussin 
361c99fb5f9SBaptiste Daroussin /**
362c99fb5f9SBaptiste Daroussin  * Create an object from a float number
363c99fb5f9SBaptiste Daroussin  * @param dv number
364c99fb5f9SBaptiste Daroussin  * @return new object
365c99fb5f9SBaptiste Daroussin  */
366b04a7a0bSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_fromdouble (double dv) UCL_WARN_UNUSED_RESULT;
367c99fb5f9SBaptiste Daroussin 
368c99fb5f9SBaptiste Daroussin /**
369c99fb5f9SBaptiste Daroussin  * Create an object from a boolean
370c99fb5f9SBaptiste Daroussin  * @param bv bool value
371c99fb5f9SBaptiste Daroussin  * @return new object
372c99fb5f9SBaptiste Daroussin  */
373b04a7a0bSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_frombool (bool bv) UCL_WARN_UNUSED_RESULT;
374c99fb5f9SBaptiste Daroussin 
375c99fb5f9SBaptiste Daroussin /**
376c99fb5f9SBaptiste Daroussin  * Insert a object 'elt' to the hash 'top' and associate it with key 'key'
3774bf54857SBaptiste Daroussin  * @param top destination object (must be of type UCL_OBJECT)
378c99fb5f9SBaptiste Daroussin  * @param elt element to insert (must NOT be NULL)
379c99fb5f9SBaptiste Daroussin  * @param key key to associate with this object (either const or preallocated)
380c99fb5f9SBaptiste Daroussin  * @param keylen length of the key (or 0 for NULL terminated keys)
381c99fb5f9SBaptiste Daroussin  * @param copy_key make an internal copy of key
382b04a7a0bSBaptiste Daroussin  * @return true if key has been inserted
383c99fb5f9SBaptiste Daroussin  */
384b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
385b04a7a0bSBaptiste Daroussin 		const char *key, size_t keylen, bool copy_key);
386c99fb5f9SBaptiste Daroussin 
387c99fb5f9SBaptiste Daroussin /**
388c99fb5f9SBaptiste Daroussin  * Replace a object 'elt' to the hash 'top' and associate it with key 'key', old object will be unrefed,
389c99fb5f9SBaptiste Daroussin  * if no object has been found this function works like ucl_object_insert_key()
3904bf54857SBaptiste Daroussin  * @param top destination object (must be of type UCL_OBJECT)
391c99fb5f9SBaptiste Daroussin  * @param elt element to insert (must NOT be NULL)
392c99fb5f9SBaptiste Daroussin  * @param key key to associate with this object (either const or preallocated)
393c99fb5f9SBaptiste Daroussin  * @param keylen length of the key (or 0 for NULL terminated keys)
394c99fb5f9SBaptiste Daroussin  * @param copy_key make an internal copy of key
395b04a7a0bSBaptiste Daroussin  * @return true if key has been inserted
396c99fb5f9SBaptiste Daroussin  */
397b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt,
398b04a7a0bSBaptiste Daroussin 		const char *key, size_t keylen, bool copy_key);
399c99fb5f9SBaptiste Daroussin 
400c99fb5f9SBaptiste Daroussin /**
4014bf54857SBaptiste Daroussin  * Merge the keys from one object to another object. Overwrite on conflict
4024bf54857SBaptiste Daroussin  * @param top destination object (must be of type UCL_OBJECT)
4034bf54857SBaptiste Daroussin  * @param elt element to insert (must be of type UCL_OBJECT)
4044bf54857SBaptiste Daroussin  * @param copy copy rather than reference the elements
4054bf54857SBaptiste Daroussin  * @return true if all keys have been merged
4064bf54857SBaptiste Daroussin  */
4074bf54857SBaptiste Daroussin UCL_EXTERN bool ucl_object_merge (ucl_object_t *top, ucl_object_t *elt, bool copy);
4084bf54857SBaptiste Daroussin 
4094bf54857SBaptiste Daroussin /**
41036c53d67SBaptiste Daroussin  * Delete a object associated with key 'key', old object will be unrefered,
41136c53d67SBaptiste Daroussin  * @param top object
41236c53d67SBaptiste Daroussin  * @param key key associated to the object to remove
41336c53d67SBaptiste Daroussin  * @param keylen length of the key (or 0 for NULL terminated keys)
41436c53d67SBaptiste Daroussin  */
415b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_delete_keyl (ucl_object_t *top,
416b04a7a0bSBaptiste Daroussin 		const char *key, size_t keylen);
41736c53d67SBaptiste Daroussin 
41836c53d67SBaptiste Daroussin /**
41936c53d67SBaptiste Daroussin  * Delete a object associated with key 'key', old object will be unrefered,
42036c53d67SBaptiste Daroussin  * @param top object
42136c53d67SBaptiste Daroussin  * @param key key associated to the object to remove
42236c53d67SBaptiste Daroussin  */
423b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_delete_key (ucl_object_t *top,
424b04a7a0bSBaptiste Daroussin 		const char *key);
42536c53d67SBaptiste Daroussin 
42697bd480fSBaptiste Daroussin 
42797bd480fSBaptiste Daroussin /**
4284bf54857SBaptiste Daroussin  * Removes `key` from `top` object, returning the object that was removed. This
4294bf54857SBaptiste Daroussin  * object is not released, caller must unref the returned object when it is no
4304bf54857SBaptiste Daroussin  * longer needed.
43197bd480fSBaptiste Daroussin  * @param top object
43297bd480fSBaptiste Daroussin  * @param key key to remove
43397bd480fSBaptiste Daroussin  * @param keylen length of the key (or 0 for NULL terminated keys)
43497bd480fSBaptiste Daroussin  * @return removed object or NULL if object has not been found
43597bd480fSBaptiste Daroussin  */
43697bd480fSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_pop_keyl (ucl_object_t *top, const char *key,
43797bd480fSBaptiste Daroussin 		size_t keylen) UCL_WARN_UNUSED_RESULT;
43897bd480fSBaptiste Daroussin 
43997bd480fSBaptiste Daroussin /**
4404bf54857SBaptiste Daroussin  * Removes `key` from `top` object returning the object that was removed. This
4414bf54857SBaptiste Daroussin  * object is not released, caller must unref the returned object when it is no
4424bf54857SBaptiste Daroussin  * longer needed.
44397bd480fSBaptiste Daroussin  * @param top object
44497bd480fSBaptiste Daroussin  * @param key key to remove
44597bd480fSBaptiste Daroussin  * @return removed object or NULL if object has not been found
44697bd480fSBaptiste Daroussin  */
44797bd480fSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_pop_key (ucl_object_t *top, const char *key)
44897bd480fSBaptiste Daroussin 	UCL_WARN_UNUSED_RESULT;
44997bd480fSBaptiste Daroussin 
45036c53d67SBaptiste Daroussin /**
4514bf54857SBaptiste Daroussin  * Insert a object 'elt' to the hash 'top' and associate it with key 'key', if
4524bf54857SBaptiste Daroussin  * the specified key exist, try to merge its content
4534bf54857SBaptiste Daroussin  * @param top destination object (must be of type UCL_OBJECT)
454c99fb5f9SBaptiste Daroussin  * @param elt element to insert (must NOT be NULL)
455c99fb5f9SBaptiste Daroussin  * @param key key to associate with this object (either const or preallocated)
456c99fb5f9SBaptiste Daroussin  * @param keylen length of the key (or 0 for NULL terminated keys)
457c99fb5f9SBaptiste Daroussin  * @param copy_key make an internal copy of key
458b04a7a0bSBaptiste Daroussin  * @return true if key has been inserted
459c99fb5f9SBaptiste Daroussin  */
460b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt,
461b04a7a0bSBaptiste Daroussin 		const char *key, size_t keylen, bool copy_key);
462c99fb5f9SBaptiste Daroussin 
463c99fb5f9SBaptiste Daroussin /**
4644bf54857SBaptiste Daroussin  * Append an element to the end of array object
4654bf54857SBaptiste Daroussin  * @param top destination object (must NOT be NULL)
466c99fb5f9SBaptiste Daroussin  * @param elt element to append (must NOT be NULL)
467b04a7a0bSBaptiste Daroussin  * @return true if value has been inserted
468c99fb5f9SBaptiste Daroussin  */
469b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_array_append (ucl_object_t *top,
470b04a7a0bSBaptiste Daroussin 		ucl_object_t *elt);
471c99fb5f9SBaptiste Daroussin 
472c99fb5f9SBaptiste Daroussin /**
473c99fb5f9SBaptiste Daroussin  * Append an element to the start of array object
4744bf54857SBaptiste Daroussin  * @param top destination object (must NOT be NULL)
475c99fb5f9SBaptiste Daroussin  * @param elt element to append (must NOT be NULL)
476b04a7a0bSBaptiste Daroussin  * @return true if value has been inserted
477c99fb5f9SBaptiste Daroussin  */
478b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_array_prepend (ucl_object_t *top,
479b04a7a0bSBaptiste Daroussin 		ucl_object_t *elt);
480c99fb5f9SBaptiste Daroussin 
481c99fb5f9SBaptiste Daroussin /**
4824bf54857SBaptiste Daroussin  * Merge all elements of second array into the first array
4834bf54857SBaptiste Daroussin  * @param top destination array (must be of type UCL_ARRAY)
4844bf54857SBaptiste Daroussin  * @param elt array to copy elements from (must be of type UCL_ARRAY)
4854bf54857SBaptiste Daroussin  * @param copy copy elements instead of referencing them
4864bf54857SBaptiste Daroussin  * @return true if arrays were merged
4874bf54857SBaptiste Daroussin  */
4884bf54857SBaptiste Daroussin UCL_EXTERN bool ucl_array_merge (ucl_object_t *top, ucl_object_t *elt,
4894bf54857SBaptiste Daroussin 		bool copy);
4904bf54857SBaptiste Daroussin 
4914bf54857SBaptiste Daroussin /**
4924bf54857SBaptiste Daroussin  * Removes an element `elt` from the array `top`, returning the object that was
4934bf54857SBaptiste Daroussin  * removed. This object is not released, caller must unref the returned object
4944bf54857SBaptiste Daroussin  * when it is no longer needed.
495c99fb5f9SBaptiste Daroussin  * @param top array ucl object
496c99fb5f9SBaptiste Daroussin  * @param elt element to remove
497c99fb5f9SBaptiste Daroussin  * @return removed element or NULL if `top` is NULL or not an array
498c99fb5f9SBaptiste Daroussin  */
499b04a7a0bSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_array_delete (ucl_object_t *top,
500b04a7a0bSBaptiste Daroussin 		ucl_object_t *elt);
501c99fb5f9SBaptiste Daroussin 
502c99fb5f9SBaptiste Daroussin /**
503c99fb5f9SBaptiste Daroussin  * Returns the first element of the array `top`
504c99fb5f9SBaptiste Daroussin  * @param top array ucl object
505c99fb5f9SBaptiste Daroussin  * @return element or NULL if `top` is NULL or not an array
506c99fb5f9SBaptiste Daroussin  */
507b04a7a0bSBaptiste Daroussin UCL_EXTERN const ucl_object_t* ucl_array_head (const ucl_object_t *top);
508c99fb5f9SBaptiste Daroussin 
509c99fb5f9SBaptiste Daroussin /**
510c99fb5f9SBaptiste Daroussin  * Returns the last element of the array `top`
511c99fb5f9SBaptiste Daroussin  * @param top array ucl object
512c99fb5f9SBaptiste Daroussin  * @return element or NULL if `top` is NULL or not an array
513c99fb5f9SBaptiste Daroussin  */
514b04a7a0bSBaptiste Daroussin UCL_EXTERN const ucl_object_t* ucl_array_tail (const ucl_object_t *top);
515c99fb5f9SBaptiste Daroussin 
516c99fb5f9SBaptiste Daroussin /**
5174bf54857SBaptiste Daroussin  * Removes the last element from the array `top`, returning the object that was
5184bf54857SBaptiste Daroussin  * removed. This object is not released, caller must unref the returned object
5194bf54857SBaptiste Daroussin  * when it is no longer needed.
520c99fb5f9SBaptiste Daroussin  * @param top array ucl object
521c99fb5f9SBaptiste Daroussin  * @return removed element or NULL if `top` is NULL or not an array
522c99fb5f9SBaptiste Daroussin  */
52397bd480fSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_array_pop_last (ucl_object_t *top);
524c99fb5f9SBaptiste Daroussin 
525c99fb5f9SBaptiste Daroussin /**
5264bf54857SBaptiste Daroussin  * Removes the first element from the array `top`, returning the object that was
5274bf54857SBaptiste Daroussin  * removed. This object is not released, caller must unref the returned object
5284bf54857SBaptiste Daroussin  * when it is no longer needed.
529c99fb5f9SBaptiste Daroussin  * @param top array ucl object
530c99fb5f9SBaptiste Daroussin  * @return removed element or NULL if `top` is NULL or not an array
531c99fb5f9SBaptiste Daroussin  */
53297bd480fSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_array_pop_first (ucl_object_t *top);
533c99fb5f9SBaptiste Daroussin 
534c99fb5f9SBaptiste Daroussin /**
5354bf54857SBaptiste Daroussin  * Return object identified by index of the array `top`
5364bf54857SBaptiste Daroussin  * @param top object to get a key from (must be of type UCL_ARRAY)
5374bf54857SBaptiste Daroussin  * @param index array index to return
5384bf54857SBaptiste Daroussin  * @return object at the specified index or NULL if index is not found
5394bf54857SBaptiste Daroussin  */
5404bf54857SBaptiste Daroussin UCL_EXTERN const ucl_object_t* ucl_array_find_index (const ucl_object_t *top,
5414bf54857SBaptiste Daroussin 		unsigned int index);
5424bf54857SBaptiste Daroussin 
5434bf54857SBaptiste Daroussin /**
54439ee7a7aSBaptiste Daroussin  * Return the index of `elt` in the array `top`
54539ee7a7aSBaptiste Daroussin  * @param top object to get a key from (must be of type UCL_ARRAY)
54639ee7a7aSBaptiste Daroussin  * @param elt element to find index of (must NOT be NULL)
54739ee7a7aSBaptiste Daroussin  * @return index of `elt` in the array `top or (unsigned int)-1 if `elt` is not found
54839ee7a7aSBaptiste Daroussin  */
54939ee7a7aSBaptiste Daroussin UCL_EXTERN unsigned int ucl_array_index_of (ucl_object_t *top,
55039ee7a7aSBaptiste Daroussin 		ucl_object_t *elt);
55139ee7a7aSBaptiste Daroussin 
55239ee7a7aSBaptiste Daroussin /**
5534bf54857SBaptiste Daroussin  * Replace an element in an array with a different element, returning the object
5544bf54857SBaptiste Daroussin  * that was replaced. This object is not released, caller must unref the
5554bf54857SBaptiste Daroussin  * returned object when it is no longer needed.
5564bf54857SBaptiste Daroussin  * @param top destination object (must be of type UCL_ARRAY)
5574bf54857SBaptiste Daroussin  * @param elt element to append (must NOT be NULL)
5584bf54857SBaptiste Daroussin  * @param index array index in destination to overwrite with elt
5594bf54857SBaptiste Daroussin  * @return object that was replaced or NULL if index is not found
5604bf54857SBaptiste Daroussin  */
5614bf54857SBaptiste Daroussin ucl_object_t *
5624bf54857SBaptiste Daroussin ucl_array_replace_index (ucl_object_t *top, ucl_object_t *elt,
5634bf54857SBaptiste Daroussin 	unsigned int index);
5644bf54857SBaptiste Daroussin 
5654bf54857SBaptiste Daroussin /**
566c99fb5f9SBaptiste Daroussin  * Append a element to another element forming an implicit array
567c99fb5f9SBaptiste Daroussin  * @param head head to append (may be NULL)
568c99fb5f9SBaptiste Daroussin  * @param elt new element
5694bf54857SBaptiste Daroussin  * @return the new implicit array
570c99fb5f9SBaptiste Daroussin  */
57197bd480fSBaptiste Daroussin UCL_EXTERN ucl_object_t * ucl_elt_append (ucl_object_t *head,
572b04a7a0bSBaptiste Daroussin 		ucl_object_t *elt);
573c99fb5f9SBaptiste Daroussin 
574c99fb5f9SBaptiste Daroussin /**
575c99fb5f9SBaptiste Daroussin  * Converts an object to double value
576c99fb5f9SBaptiste Daroussin  * @param obj CL object
577c99fb5f9SBaptiste Daroussin  * @param target target double variable
578c99fb5f9SBaptiste Daroussin  * @return true if conversion was successful
579c99fb5f9SBaptiste Daroussin  */
580b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_todouble_safe (const ucl_object_t *obj, double *target);
581c99fb5f9SBaptiste Daroussin 
582c99fb5f9SBaptiste Daroussin /**
583c99fb5f9SBaptiste Daroussin  * Unsafe version of \ref ucl_obj_todouble_safe
584c99fb5f9SBaptiste Daroussin  * @param obj CL object
585c99fb5f9SBaptiste Daroussin  * @return double value
586c99fb5f9SBaptiste Daroussin  */
587b04a7a0bSBaptiste Daroussin UCL_EXTERN double ucl_object_todouble (const ucl_object_t *obj);
588c99fb5f9SBaptiste Daroussin 
589c99fb5f9SBaptiste Daroussin /**
590c99fb5f9SBaptiste Daroussin  * Converts an object to integer value
591c99fb5f9SBaptiste Daroussin  * @param obj CL object
592c99fb5f9SBaptiste Daroussin  * @param target target integer variable
593c99fb5f9SBaptiste Daroussin  * @return true if conversion was successful
594c99fb5f9SBaptiste Daroussin  */
595b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_toint_safe (const ucl_object_t *obj, int64_t *target);
596c99fb5f9SBaptiste Daroussin 
597c99fb5f9SBaptiste Daroussin /**
598c99fb5f9SBaptiste Daroussin  * Unsafe version of \ref ucl_obj_toint_safe
599c99fb5f9SBaptiste Daroussin  * @param obj CL object
600c99fb5f9SBaptiste Daroussin  * @return int value
601c99fb5f9SBaptiste Daroussin  */
602b04a7a0bSBaptiste Daroussin UCL_EXTERN int64_t ucl_object_toint (const ucl_object_t *obj);
603c99fb5f9SBaptiste Daroussin 
604c99fb5f9SBaptiste Daroussin /**
605c99fb5f9SBaptiste Daroussin  * Converts an object to boolean value
606c99fb5f9SBaptiste Daroussin  * @param obj CL object
607c99fb5f9SBaptiste Daroussin  * @param target target boolean variable
608c99fb5f9SBaptiste Daroussin  * @return true if conversion was successful
609c99fb5f9SBaptiste Daroussin  */
610b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_toboolean_safe (const ucl_object_t *obj, bool *target);
611c99fb5f9SBaptiste Daroussin 
612c99fb5f9SBaptiste Daroussin /**
613c99fb5f9SBaptiste Daroussin  * Unsafe version of \ref ucl_obj_toboolean_safe
614c99fb5f9SBaptiste Daroussin  * @param obj CL object
615c99fb5f9SBaptiste Daroussin  * @return boolean value
616c99fb5f9SBaptiste Daroussin  */
617b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_toboolean (const ucl_object_t *obj);
618c99fb5f9SBaptiste Daroussin 
619c99fb5f9SBaptiste Daroussin /**
620c99fb5f9SBaptiste Daroussin  * Converts an object to string value
621c99fb5f9SBaptiste Daroussin  * @param obj CL object
622c99fb5f9SBaptiste Daroussin  * @param target target string variable, no need to free value
623c99fb5f9SBaptiste Daroussin  * @return true if conversion was successful
624c99fb5f9SBaptiste Daroussin  */
625b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_tostring_safe (const ucl_object_t *obj, const char **target);
626c99fb5f9SBaptiste Daroussin 
627c99fb5f9SBaptiste Daroussin /**
628c99fb5f9SBaptiste Daroussin  * Unsafe version of \ref ucl_obj_tostring_safe
629c99fb5f9SBaptiste Daroussin  * @param obj CL object
630c99fb5f9SBaptiste Daroussin  * @return string value
631c99fb5f9SBaptiste Daroussin  */
632b04a7a0bSBaptiste Daroussin UCL_EXTERN const char* ucl_object_tostring (const ucl_object_t *obj);
633c99fb5f9SBaptiste Daroussin 
634c99fb5f9SBaptiste Daroussin /**
635c99fb5f9SBaptiste Daroussin  * Convert any object to a string in JSON notation if needed
636c99fb5f9SBaptiste Daroussin  * @param obj CL object
637c99fb5f9SBaptiste Daroussin  * @return string value
638c99fb5f9SBaptiste Daroussin  */
639b04a7a0bSBaptiste Daroussin UCL_EXTERN const char* ucl_object_tostring_forced (const ucl_object_t *obj);
640c99fb5f9SBaptiste Daroussin 
641c99fb5f9SBaptiste Daroussin /**
642c99fb5f9SBaptiste Daroussin  * Return string as char * and len, string may be not zero terminated, more efficient that \ref ucl_obj_tostring as it
643c99fb5f9SBaptiste Daroussin  * allows zero-copy (if #UCL_PARSER_ZEROCOPY has been used during parsing)
644c99fb5f9SBaptiste Daroussin  * @param obj CL object
645c99fb5f9SBaptiste Daroussin  * @param target target string variable, no need to free value
646c99fb5f9SBaptiste Daroussin  * @param tlen target length
647c99fb5f9SBaptiste Daroussin  * @return true if conversion was successful
648c99fb5f9SBaptiste Daroussin  */
649b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_tolstring_safe (const ucl_object_t *obj,
65097bd480fSBaptiste Daroussin 		const char **target, size_t *tlen);
651c99fb5f9SBaptiste Daroussin 
652c99fb5f9SBaptiste Daroussin /**
653c99fb5f9SBaptiste Daroussin  * Unsafe version of \ref ucl_obj_tolstring_safe
654c99fb5f9SBaptiste Daroussin  * @param obj CL object
655c99fb5f9SBaptiste Daroussin  * @return string value
656c99fb5f9SBaptiste Daroussin  */
657b04a7a0bSBaptiste Daroussin UCL_EXTERN const char* ucl_object_tolstring (const ucl_object_t *obj, size_t *tlen);
658c99fb5f9SBaptiste Daroussin 
659c99fb5f9SBaptiste Daroussin /**
660c99fb5f9SBaptiste Daroussin  * Return object identified by a key in the specified object
661c99fb5f9SBaptiste Daroussin  * @param obj object to get a key from (must be of type UCL_OBJECT)
662c99fb5f9SBaptiste Daroussin  * @param key key to search
6634bf54857SBaptiste Daroussin  * @return object matching the specified key or NULL if key was not found
664c99fb5f9SBaptiste Daroussin  */
665*d9f0ce31SBaptiste Daroussin UCL_EXTERN const ucl_object_t* ucl_object_lookup (const ucl_object_t *obj,
666b04a7a0bSBaptiste Daroussin 		const char *key);
667*d9f0ce31SBaptiste Daroussin #define ucl_object_find_key ucl_object_lookup
668c99fb5f9SBaptiste Daroussin 
669c99fb5f9SBaptiste Daroussin /**
67039ee7a7aSBaptiste Daroussin  * Return object identified by a key in the specified object, if the first key is
67139ee7a7aSBaptiste Daroussin  * not found then look for the next one. This process is repeated unless
67239ee7a7aSBaptiste Daroussin  * the next argument in the list is not NULL. So, `ucl_object_find_any_key(obj, key, NULL)`
67339ee7a7aSBaptiste Daroussin  * is equal to `ucl_object_find_key(obj, key)`
67439ee7a7aSBaptiste Daroussin  * @param obj object to get a key from (must be of type UCL_OBJECT)
67539ee7a7aSBaptiste Daroussin  * @param key key to search
67639ee7a7aSBaptiste Daroussin  * @param ... list of alternative keys to search (NULL terminated)
67739ee7a7aSBaptiste Daroussin  * @return object matching the specified key or NULL if key was not found
67839ee7a7aSBaptiste Daroussin  */
679*d9f0ce31SBaptiste Daroussin UCL_EXTERN const ucl_object_t* ucl_object_lookup_any (const ucl_object_t *obj,
68039ee7a7aSBaptiste Daroussin 		const char *key, ...);
681*d9f0ce31SBaptiste Daroussin #define ucl_object_find_any_key ucl_object_lookup_any
68239ee7a7aSBaptiste Daroussin 
68339ee7a7aSBaptiste Daroussin /**
684c99fb5f9SBaptiste Daroussin  * Return object identified by a fixed size key in the specified object
685c99fb5f9SBaptiste Daroussin  * @param obj object to get a key from (must be of type UCL_OBJECT)
686c99fb5f9SBaptiste Daroussin  * @param key key to search
687c99fb5f9SBaptiste Daroussin  * @param klen length of a key
6884bf54857SBaptiste Daroussin  * @return object matching the specified key or NULL if key was not found
689c99fb5f9SBaptiste Daroussin  */
690*d9f0ce31SBaptiste Daroussin UCL_EXTERN const ucl_object_t* ucl_object_lookup_len (const ucl_object_t *obj,
691b04a7a0bSBaptiste Daroussin 		const char *key, size_t klen);
692*d9f0ce31SBaptiste Daroussin #define ucl_object_find_keyl ucl_object_lookup_len
693c99fb5f9SBaptiste Daroussin 
694c99fb5f9SBaptiste Daroussin /**
6952e8ed2b8SBaptiste Daroussin  * Return object identified by dot notation string
6962e8ed2b8SBaptiste Daroussin  * @param obj object to search in
6972e8ed2b8SBaptiste Daroussin  * @param path dot.notation.path to the path to lookup. May use numeric .index on arrays
6982e8ed2b8SBaptiste Daroussin  * @return object matched the specified path or NULL if path is not found
6992e8ed2b8SBaptiste Daroussin  */
700*d9f0ce31SBaptiste Daroussin UCL_EXTERN const ucl_object_t *ucl_object_lookup_path (const ucl_object_t *obj,
7012e8ed2b8SBaptiste Daroussin 		const char *path);
702*d9f0ce31SBaptiste Daroussin #define ucl_lookup_path ucl_object_lookup_path
7032e8ed2b8SBaptiste Daroussin 
7042e8ed2b8SBaptiste Daroussin /**
70539ee7a7aSBaptiste Daroussin  * Return object identified by object notation string using arbitrary delimiter
70639ee7a7aSBaptiste Daroussin  * @param obj object to search in
70739ee7a7aSBaptiste Daroussin  * @param path dot.notation.path to the path to lookup. May use numeric .index on arrays
70839ee7a7aSBaptiste Daroussin  * @param sep the sepatorator to use in place of . (incase keys have . in them)
70939ee7a7aSBaptiste Daroussin  * @return object matched the specified path or NULL if path is not found
71039ee7a7aSBaptiste Daroussin  */
711*d9f0ce31SBaptiste Daroussin UCL_EXTERN const ucl_object_t *ucl_object_lookup_path_char (const ucl_object_t *obj,
71239ee7a7aSBaptiste Daroussin 		const char *path, char sep);
713*d9f0ce31SBaptiste Daroussin #define ucl_lookup_path_char ucl_object_lookup_path_char
71439ee7a7aSBaptiste Daroussin 
71539ee7a7aSBaptiste Daroussin /**
716c99fb5f9SBaptiste Daroussin  * Returns a key of an object as a NULL terminated string
717c99fb5f9SBaptiste Daroussin  * @param obj CL object
718c99fb5f9SBaptiste Daroussin  * @return key or NULL if there is no key
719c99fb5f9SBaptiste Daroussin  */
720b04a7a0bSBaptiste Daroussin UCL_EXTERN const char* ucl_object_key (const ucl_object_t *obj);
721c99fb5f9SBaptiste Daroussin 
722c99fb5f9SBaptiste Daroussin /**
723c99fb5f9SBaptiste Daroussin  * Returns a key of an object as a fixed size string (may be more efficient)
724c99fb5f9SBaptiste Daroussin  * @param obj CL object
725c99fb5f9SBaptiste Daroussin  * @param len target key length
726c99fb5f9SBaptiste Daroussin  * @return key pointer
727c99fb5f9SBaptiste Daroussin  */
728b04a7a0bSBaptiste Daroussin UCL_EXTERN const char* ucl_object_keyl (const ucl_object_t *obj, size_t *len);
729c99fb5f9SBaptiste Daroussin 
730c99fb5f9SBaptiste Daroussin /**
731c99fb5f9SBaptiste Daroussin  * Increase reference count for an object
732c99fb5f9SBaptiste Daroussin  * @param obj object to ref
7334bf54857SBaptiste Daroussin  * @return the referenced object
734c99fb5f9SBaptiste Daroussin  */
735b04a7a0bSBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_object_ref (const ucl_object_t *obj);
736b04a7a0bSBaptiste Daroussin 
737b04a7a0bSBaptiste Daroussin /**
738b04a7a0bSBaptiste Daroussin  * Free ucl object
739b04a7a0bSBaptiste Daroussin  * @param obj ucl object to free
740b04a7a0bSBaptiste Daroussin  */
741b04a7a0bSBaptiste Daroussin UCL_DEPRECATED(UCL_EXTERN void ucl_object_free (ucl_object_t *obj));
742c99fb5f9SBaptiste Daroussin 
743c99fb5f9SBaptiste Daroussin /**
744c99fb5f9SBaptiste Daroussin  * Decrease reference count for an object
745c99fb5f9SBaptiste Daroussin  * @param obj object to unref
746c99fb5f9SBaptiste Daroussin  */
74797bd480fSBaptiste Daroussin UCL_EXTERN void ucl_object_unref (ucl_object_t *obj);
74897bd480fSBaptiste Daroussin 
74997bd480fSBaptiste Daroussin /**
75097bd480fSBaptiste Daroussin  * Compare objects `o1` and `o2`
75197bd480fSBaptiste Daroussin  * @param o1 the first object
75297bd480fSBaptiste Daroussin  * @param o2 the second object
75397bd480fSBaptiste Daroussin  * @return values >0, 0 and <0 if `o1` is more than, equal and less than `o2`.
75497bd480fSBaptiste Daroussin  * The order of comparison:
75597bd480fSBaptiste Daroussin  * 1) Type of objects
75697bd480fSBaptiste Daroussin  * 2) Size of objects
75797bd480fSBaptiste Daroussin  * 3) Content of objects
75897bd480fSBaptiste Daroussin  */
759b04a7a0bSBaptiste Daroussin UCL_EXTERN int ucl_object_compare (const ucl_object_t *o1,
760b04a7a0bSBaptiste Daroussin 		const ucl_object_t *o2);
76197bd480fSBaptiste Daroussin 
76297bd480fSBaptiste Daroussin /**
763*d9f0ce31SBaptiste Daroussin  * Compare objects `o1` and `o2` useful for sorting
764*d9f0ce31SBaptiste Daroussin  * @param o1 the first object
765*d9f0ce31SBaptiste Daroussin  * @param o2 the second object
766*d9f0ce31SBaptiste Daroussin  * @return values >0, 0 and <0 if `o1` is more than, equal and less than `o2`.
767*d9f0ce31SBaptiste Daroussin  * The order of comparison:
768*d9f0ce31SBaptiste Daroussin  * 1) Type of objects
769*d9f0ce31SBaptiste Daroussin  * 2) Size of objects
770*d9f0ce31SBaptiste Daroussin  * 3) Content of objects
771*d9f0ce31SBaptiste Daroussin  */
772*d9f0ce31SBaptiste Daroussin UCL_EXTERN int ucl_object_compare_qsort (const ucl_object_t **o1,
773*d9f0ce31SBaptiste Daroussin 		const ucl_object_t **o2);
774*d9f0ce31SBaptiste Daroussin 
775*d9f0ce31SBaptiste Daroussin /**
77697bd480fSBaptiste Daroussin  * Sort UCL array using `cmp` compare function
77797bd480fSBaptiste Daroussin  * @param ar
77897bd480fSBaptiste Daroussin  * @param cmp
77997bd480fSBaptiste Daroussin  */
78097bd480fSBaptiste Daroussin UCL_EXTERN void ucl_object_array_sort (ucl_object_t *ar,
78139ee7a7aSBaptiste Daroussin 		int (*cmp)(const ucl_object_t **o1, const ucl_object_t **o2));
78297bd480fSBaptiste Daroussin 
783c99fb5f9SBaptiste Daroussin /**
7844bf54857SBaptiste Daroussin  * Get the priority for specific UCL object
7854bf54857SBaptiste Daroussin  * @param obj any ucl object
7864bf54857SBaptiste Daroussin  * @return priority of an object
7874bf54857SBaptiste Daroussin  */
7884bf54857SBaptiste Daroussin UCL_EXTERN unsigned int ucl_object_get_priority (const ucl_object_t *obj);
7894bf54857SBaptiste Daroussin 
7904bf54857SBaptiste Daroussin /**
7914bf54857SBaptiste Daroussin  * Set explicit priority of an object.
7924bf54857SBaptiste Daroussin  * @param obj any ucl object
7934bf54857SBaptiste Daroussin  * @param priority new priroity value (only 4 least significant bits are considred)
7944bf54857SBaptiste Daroussin  */
7954bf54857SBaptiste Daroussin UCL_EXTERN void ucl_object_set_priority (ucl_object_t *obj,
7964bf54857SBaptiste Daroussin 		unsigned int priority);
7974bf54857SBaptiste Daroussin 
7984bf54857SBaptiste Daroussin /**
799c99fb5f9SBaptiste Daroussin  * Opaque iterator object
800c99fb5f9SBaptiste Daroussin  */
801c99fb5f9SBaptiste Daroussin typedef void* ucl_object_iter_t;
802c99fb5f9SBaptiste Daroussin 
803c99fb5f9SBaptiste Daroussin /**
804c99fb5f9SBaptiste Daroussin  * Get next key from an object
805c99fb5f9SBaptiste Daroussin  * @param obj object to iterate
806c99fb5f9SBaptiste Daroussin  * @param iter opaque iterator, must be set to NULL on the first call:
807c99fb5f9SBaptiste Daroussin  * ucl_object_iter_t it = NULL;
808c99fb5f9SBaptiste Daroussin  * while ((cur = ucl_iterate_object (obj, &it)) != NULL) ...
809c99fb5f9SBaptiste Daroussin  * @return the next object or NULL
810c99fb5f9SBaptiste Daroussin  */
811*d9f0ce31SBaptiste Daroussin UCL_EXTERN const ucl_object_t* ucl_object_iterate (const ucl_object_t *obj,
812b04a7a0bSBaptiste Daroussin 		ucl_object_iter_t *iter, bool expand_values);
813*d9f0ce31SBaptiste Daroussin #define ucl_iterate_object ucl_object_iterate
8148e3b1ab2SBaptiste Daroussin 
8158e3b1ab2SBaptiste Daroussin /**
8168e3b1ab2SBaptiste Daroussin  * Create new safe iterator for the specified object
8178e3b1ab2SBaptiste Daroussin  * @param obj object to iterate
8188e3b1ab2SBaptiste Daroussin  * @return new iterator object that should be used with safe iterators API only
8198e3b1ab2SBaptiste Daroussin  */
8208e3b1ab2SBaptiste Daroussin UCL_EXTERN ucl_object_iter_t ucl_object_iterate_new (const ucl_object_t *obj)
8218e3b1ab2SBaptiste Daroussin 	UCL_WARN_UNUSED_RESULT;
8228e3b1ab2SBaptiste Daroussin /**
8238e3b1ab2SBaptiste Daroussin  * Reset initialized iterator to a new object
8248e3b1ab2SBaptiste Daroussin  * @param obj new object to iterate
8258e3b1ab2SBaptiste Daroussin  * @return modified iterator object
8268e3b1ab2SBaptiste Daroussin  */
8278e3b1ab2SBaptiste Daroussin UCL_EXTERN ucl_object_iter_t ucl_object_iterate_reset (ucl_object_iter_t it,
8288e3b1ab2SBaptiste Daroussin 		const ucl_object_t *obj);
8298e3b1ab2SBaptiste Daroussin 
8308e3b1ab2SBaptiste Daroussin /**
8318e3b1ab2SBaptiste Daroussin  * Get the next object from the `obj`. This fucntion iterates over arrays, objects
8328e3b1ab2SBaptiste Daroussin  * and implicit arrays
8338e3b1ab2SBaptiste Daroussin  * @param iter safe iterator
8348e3b1ab2SBaptiste Daroussin  * @return the next object in sequence
8358e3b1ab2SBaptiste Daroussin  */
8368e3b1ab2SBaptiste Daroussin UCL_EXTERN const ucl_object_t* ucl_object_iterate_safe (ucl_object_iter_t iter,
8378e3b1ab2SBaptiste Daroussin 		bool expand_values);
8388e3b1ab2SBaptiste Daroussin 
8398e3b1ab2SBaptiste Daroussin /**
8408e3b1ab2SBaptiste Daroussin  * Free memory associated with the safe iterator
8418e3b1ab2SBaptiste Daroussin  * @param it safe iterator object
8428e3b1ab2SBaptiste Daroussin  */
8438e3b1ab2SBaptiste Daroussin UCL_EXTERN void ucl_object_iterate_free (ucl_object_iter_t it);
8448e3b1ab2SBaptiste Daroussin 
845c99fb5f9SBaptiste Daroussin /** @} */
846c99fb5f9SBaptiste Daroussin 
847c99fb5f9SBaptiste Daroussin 
848c99fb5f9SBaptiste Daroussin /**
849c99fb5f9SBaptiste Daroussin  * @defgroup parser Parsing functions
850c99fb5f9SBaptiste Daroussin  * These functions are used to parse UCL objects
851c99fb5f9SBaptiste Daroussin  *
852c99fb5f9SBaptiste Daroussin  * @{
853c99fb5f9SBaptiste Daroussin  */
854c99fb5f9SBaptiste Daroussin 
855c99fb5f9SBaptiste Daroussin /**
856c99fb5f9SBaptiste Daroussin  * Macro handler for a parser
857c99fb5f9SBaptiste Daroussin  * @param data the content of macro
858c99fb5f9SBaptiste Daroussin  * @param len the length of content
8594bf54857SBaptiste Daroussin  * @param arguments arguments object
860c99fb5f9SBaptiste Daroussin  * @param ud opaque user data
861c99fb5f9SBaptiste Daroussin  * @param err error pointer
862c99fb5f9SBaptiste Daroussin  * @return true if macro has been parsed
863c99fb5f9SBaptiste Daroussin  */
8644bf54857SBaptiste Daroussin typedef bool (*ucl_macro_handler) (const unsigned char *data, size_t len,
8654bf54857SBaptiste Daroussin 		const ucl_object_t *arguments,
8664bf54857SBaptiste Daroussin 		void* ud);
867c99fb5f9SBaptiste Daroussin 
86839ee7a7aSBaptiste Daroussin /**
86939ee7a7aSBaptiste Daroussin  * Context dependent macro handler for a parser
87039ee7a7aSBaptiste Daroussin  * @param data the content of macro
87139ee7a7aSBaptiste Daroussin  * @param len the length of content
87239ee7a7aSBaptiste Daroussin  * @param arguments arguments object
87339ee7a7aSBaptiste Daroussin  * @param context previously parsed context
87439ee7a7aSBaptiste Daroussin  * @param ud opaque user data
87539ee7a7aSBaptiste Daroussin  * @param err error pointer
87639ee7a7aSBaptiste Daroussin  * @return true if macro has been parsed
87739ee7a7aSBaptiste Daroussin  */
87839ee7a7aSBaptiste Daroussin typedef bool (*ucl_context_macro_handler) (const unsigned char *data, size_t len,
87939ee7a7aSBaptiste Daroussin 		const ucl_object_t *arguments,
88039ee7a7aSBaptiste Daroussin 		const ucl_object_t *context,
88139ee7a7aSBaptiste Daroussin 		void* ud);
88239ee7a7aSBaptiste Daroussin 
883c99fb5f9SBaptiste Daroussin /* Opaque parser */
884c99fb5f9SBaptiste Daroussin struct ucl_parser;
885c99fb5f9SBaptiste Daroussin 
886c99fb5f9SBaptiste Daroussin /**
887c99fb5f9SBaptiste Daroussin  * Creates new parser object
888c99fb5f9SBaptiste Daroussin  * @param pool pool to allocate memory from
889c99fb5f9SBaptiste Daroussin  * @return new parser object
890c99fb5f9SBaptiste Daroussin  */
89136c53d67SBaptiste Daroussin UCL_EXTERN struct ucl_parser* ucl_parser_new (int flags);
892c99fb5f9SBaptiste Daroussin 
893c99fb5f9SBaptiste Daroussin /**
89439ee7a7aSBaptiste Daroussin  * Sets the default priority for the parser applied to chunks that does not
89539ee7a7aSBaptiste Daroussin  * specify priority explicitly
89639ee7a7aSBaptiste Daroussin  * @param parser parser object
89739ee7a7aSBaptiste Daroussin  * @param prio default priority (0 .. 16)
89839ee7a7aSBaptiste Daroussin  * @return true if parser's default priority was set
89939ee7a7aSBaptiste Daroussin  */
90039ee7a7aSBaptiste Daroussin UCL_EXTERN bool ucl_parser_set_default_priority (struct ucl_parser *parser,
90139ee7a7aSBaptiste Daroussin 		unsigned prio);
90239ee7a7aSBaptiste Daroussin /**
903c99fb5f9SBaptiste Daroussin  * Register new handler for a macro
904c99fb5f9SBaptiste Daroussin  * @param parser parser object
905c99fb5f9SBaptiste Daroussin  * @param macro macro name (without leading dot)
906c99fb5f9SBaptiste Daroussin  * @param handler handler (it is called immediately after macro is parsed)
907c99fb5f9SBaptiste Daroussin  * @param ud opaque user data for a handler
908c99fb5f9SBaptiste Daroussin  */
90939ee7a7aSBaptiste Daroussin UCL_EXTERN void ucl_parser_register_macro (struct ucl_parser *parser,
91039ee7a7aSBaptiste Daroussin 		const char *macro,
911c99fb5f9SBaptiste Daroussin 		ucl_macro_handler handler, void* ud);
912c99fb5f9SBaptiste Daroussin 
913c99fb5f9SBaptiste Daroussin /**
91439ee7a7aSBaptiste Daroussin  * Register new context dependent handler for a macro
91539ee7a7aSBaptiste Daroussin  * @param parser parser object
91639ee7a7aSBaptiste Daroussin  * @param macro macro name (without leading dot)
91739ee7a7aSBaptiste Daroussin  * @param handler handler (it is called immediately after macro is parsed)
91839ee7a7aSBaptiste Daroussin  * @param ud opaque user data for a handler
91939ee7a7aSBaptiste Daroussin  */
92039ee7a7aSBaptiste Daroussin UCL_EXTERN void ucl_parser_register_context_macro (struct ucl_parser *parser,
92139ee7a7aSBaptiste Daroussin 		const char *macro,
92239ee7a7aSBaptiste Daroussin 		ucl_context_macro_handler handler,
92339ee7a7aSBaptiste Daroussin 		void* ud);
92439ee7a7aSBaptiste Daroussin 
92539ee7a7aSBaptiste Daroussin /**
9262e8ed2b8SBaptiste Daroussin  * Handler to detect unregistered variables
9272e8ed2b8SBaptiste Daroussin  * @param data variable data
9282e8ed2b8SBaptiste Daroussin  * @param len length of variable
9292e8ed2b8SBaptiste Daroussin  * @param replace (out) replace value for variable
9302e8ed2b8SBaptiste Daroussin  * @param replace_len (out) replace length for variable
9312e8ed2b8SBaptiste Daroussin  * @param need_free (out) UCL will free `dest` after usage
9322e8ed2b8SBaptiste Daroussin  * @param ud opaque userdata
9332e8ed2b8SBaptiste Daroussin  * @return true if variable
9342e8ed2b8SBaptiste Daroussin  */
9352e8ed2b8SBaptiste Daroussin typedef bool (*ucl_variable_handler) (const unsigned char *data, size_t len,
9362e8ed2b8SBaptiste Daroussin 		unsigned char **replace, size_t *replace_len, bool *need_free, void* ud);
9372e8ed2b8SBaptiste Daroussin 
9382e8ed2b8SBaptiste Daroussin /**
939c99fb5f9SBaptiste Daroussin  * Register new parser variable
940c99fb5f9SBaptiste Daroussin  * @param parser parser object
941c99fb5f9SBaptiste Daroussin  * @param var variable name
942c99fb5f9SBaptiste Daroussin  * @param value variable value
943c99fb5f9SBaptiste Daroussin  */
94436c53d67SBaptiste Daroussin UCL_EXTERN void ucl_parser_register_variable (struct ucl_parser *parser, const char *var,
945c99fb5f9SBaptiste Daroussin 		const char *value);
946c99fb5f9SBaptiste Daroussin 
947c99fb5f9SBaptiste Daroussin /**
9482e8ed2b8SBaptiste Daroussin  * Set handler for unknown variables
9492e8ed2b8SBaptiste Daroussin  * @param parser parser structure
9502e8ed2b8SBaptiste Daroussin  * @param handler desired handler
9512e8ed2b8SBaptiste Daroussin  * @param ud opaque data for the handler
9522e8ed2b8SBaptiste Daroussin  */
9532e8ed2b8SBaptiste Daroussin UCL_EXTERN void ucl_parser_set_variables_handler (struct ucl_parser *parser,
9542e8ed2b8SBaptiste Daroussin 		ucl_variable_handler handler, void *ud);
9552e8ed2b8SBaptiste Daroussin 
9562e8ed2b8SBaptiste Daroussin /**
957c99fb5f9SBaptiste Daroussin  * Load new chunk to a parser
958c99fb5f9SBaptiste Daroussin  * @param parser parser structure
959c99fb5f9SBaptiste Daroussin  * @param data the pointer to the beginning of a chunk
960c99fb5f9SBaptiste Daroussin  * @param len the length of a chunk
961c99fb5f9SBaptiste Daroussin  * @return true if chunk has been added and false in case of error
962c99fb5f9SBaptiste Daroussin  */
96397bd480fSBaptiste Daroussin UCL_EXTERN bool ucl_parser_add_chunk (struct ucl_parser *parser,
96497bd480fSBaptiste Daroussin 		const unsigned char *data, size_t len);
96597bd480fSBaptiste Daroussin 
96697bd480fSBaptiste Daroussin /**
9674bf54857SBaptiste Daroussin  * Load new chunk to a parser with the specified priority
9684bf54857SBaptiste Daroussin  * @param parser parser structure
9694bf54857SBaptiste Daroussin  * @param data the pointer to the beginning of a chunk
9704bf54857SBaptiste Daroussin  * @param len the length of a chunk
9714bf54857SBaptiste Daroussin  * @param priority the desired priority of a chunk (only 4 least significant bits
9724bf54857SBaptiste Daroussin  * are considered for this parameter)
9734bf54857SBaptiste Daroussin  * @return true if chunk has been added and false in case of error
9744bf54857SBaptiste Daroussin  */
9754bf54857SBaptiste Daroussin UCL_EXTERN bool ucl_parser_add_chunk_priority (struct ucl_parser *parser,
9764bf54857SBaptiste Daroussin 		const unsigned char *data, size_t len, unsigned priority);
9774bf54857SBaptiste Daroussin 
9784bf54857SBaptiste Daroussin /**
97939ee7a7aSBaptiste Daroussin  * Full version of ucl_add_chunk with priority and duplicate strategy
98039ee7a7aSBaptiste Daroussin  * @param parser parser structure
98139ee7a7aSBaptiste Daroussin  * @param data the pointer to the beginning of a chunk
98239ee7a7aSBaptiste Daroussin  * @param len the length of a chunk
98339ee7a7aSBaptiste Daroussin  * @param priority the desired priority of a chunk (only 4 least significant bits
98439ee7a7aSBaptiste Daroussin  * are considered for this parameter)
98539ee7a7aSBaptiste Daroussin  * @param strat duplicates merging strategy
98639ee7a7aSBaptiste Daroussin  * @param parse_type input format
98739ee7a7aSBaptiste Daroussin  * @return true if chunk has been added and false in case of error
98839ee7a7aSBaptiste Daroussin  */
98939ee7a7aSBaptiste Daroussin UCL_EXTERN bool ucl_parser_add_chunk_full (struct ucl_parser *parser,
99039ee7a7aSBaptiste Daroussin 		const unsigned char *data, size_t len, unsigned priority,
99139ee7a7aSBaptiste Daroussin 		enum ucl_duplicate_strategy strat, enum ucl_parse_type parse_type);
99239ee7a7aSBaptiste Daroussin 
99339ee7a7aSBaptiste Daroussin /**
99497bd480fSBaptiste Daroussin  * Load ucl object from a string
99597bd480fSBaptiste Daroussin  * @param parser parser structure
99697bd480fSBaptiste Daroussin  * @param data the pointer to the string
99797bd480fSBaptiste Daroussin  * @param len the length of the string, if `len` is 0 then `data` must be zero-terminated string
99897bd480fSBaptiste Daroussin  * @return true if string has been added and false in case of error
99997bd480fSBaptiste Daroussin  */
100097bd480fSBaptiste Daroussin UCL_EXTERN bool ucl_parser_add_string (struct ucl_parser *parser,
100197bd480fSBaptiste Daroussin 		const char *data,size_t len);
1002c99fb5f9SBaptiste Daroussin 
1003c99fb5f9SBaptiste Daroussin /**
100439ee7a7aSBaptiste Daroussin  * Load ucl object from a string
100539ee7a7aSBaptiste Daroussin  * @param parser parser structure
100639ee7a7aSBaptiste Daroussin  * @param data the pointer to the string
100739ee7a7aSBaptiste Daroussin  * @param len the length of the string, if `len` is 0 then `data` must be zero-terminated string
100839ee7a7aSBaptiste Daroussin  * @param priority the desired priority of a chunk (only 4 least significant bits
100939ee7a7aSBaptiste Daroussin  * are considered for this parameter)
101039ee7a7aSBaptiste Daroussin  * @return true if string has been added and false in case of error
101139ee7a7aSBaptiste Daroussin  */
101239ee7a7aSBaptiste Daroussin UCL_EXTERN bool ucl_parser_add_string_priority (struct ucl_parser *parser,
101339ee7a7aSBaptiste Daroussin 		const char *data, size_t len, unsigned priority);
101439ee7a7aSBaptiste Daroussin 
101539ee7a7aSBaptiste Daroussin /**
1016c99fb5f9SBaptiste Daroussin  * Load and add data from a file
1017c99fb5f9SBaptiste Daroussin  * @param parser parser structure
1018c99fb5f9SBaptiste Daroussin  * @param filename the name of file
1019c99fb5f9SBaptiste Daroussin  * @param err if *err is NULL it is set to parser error
1020c99fb5f9SBaptiste Daroussin  * @return true if chunk has been added and false in case of error
1021c99fb5f9SBaptiste Daroussin  */
1022b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_parser_add_file (struct ucl_parser *parser,
1023b04a7a0bSBaptiste Daroussin 		const char *filename);
1024c99fb5f9SBaptiste Daroussin 
1025c99fb5f9SBaptiste Daroussin /**
102639ee7a7aSBaptiste Daroussin  * Load and add data from a file
102739ee7a7aSBaptiste Daroussin  * @param parser parser structure
102839ee7a7aSBaptiste Daroussin  * @param filename the name of file
102939ee7a7aSBaptiste Daroussin  * @param err if *err is NULL it is set to parser error
103039ee7a7aSBaptiste Daroussin  * @param priority the desired priority of a chunk (only 4 least significant bits
103139ee7a7aSBaptiste Daroussin  * are considered for this parameter)
103239ee7a7aSBaptiste Daroussin  * @return true if chunk has been added and false in case of error
103339ee7a7aSBaptiste Daroussin  */
103439ee7a7aSBaptiste Daroussin UCL_EXTERN bool ucl_parser_add_file_priority (struct ucl_parser *parser,
103539ee7a7aSBaptiste Daroussin 		const char *filename, unsigned priority);
103639ee7a7aSBaptiste Daroussin 
103739ee7a7aSBaptiste Daroussin /**
1038b04a7a0bSBaptiste Daroussin  * Load and add data from a file descriptor
1039b04a7a0bSBaptiste Daroussin  * @param parser parser structure
1040b04a7a0bSBaptiste Daroussin  * @param filename the name of file
1041b04a7a0bSBaptiste Daroussin  * @param err if *err is NULL it is set to parser error
1042b04a7a0bSBaptiste Daroussin  * @return true if chunk has been added and false in case of error
1043b04a7a0bSBaptiste Daroussin  */
1044b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_parser_add_fd (struct ucl_parser *parser,
1045b04a7a0bSBaptiste Daroussin 		int fd);
1046b04a7a0bSBaptiste Daroussin 
1047b04a7a0bSBaptiste Daroussin /**
104839ee7a7aSBaptiste Daroussin  * Load and add data from a file descriptor
104939ee7a7aSBaptiste Daroussin  * @param parser parser structure
105039ee7a7aSBaptiste Daroussin  * @param filename the name of file
105139ee7a7aSBaptiste Daroussin  * @param err if *err is NULL it is set to parser error
105239ee7a7aSBaptiste Daroussin  * @param priority the desired priority of a chunk (only 4 least significant bits
105339ee7a7aSBaptiste Daroussin  * are considered for this parameter)
105439ee7a7aSBaptiste Daroussin  * @return true if chunk has been added and false in case of error
105539ee7a7aSBaptiste Daroussin  */
105639ee7a7aSBaptiste Daroussin UCL_EXTERN bool ucl_parser_add_fd_priority (struct ucl_parser *parser,
105739ee7a7aSBaptiste Daroussin 		int fd, unsigned priority);
105839ee7a7aSBaptiste Daroussin 
105939ee7a7aSBaptiste Daroussin /**
106039ee7a7aSBaptiste Daroussin  * Provide a UCL_ARRAY of paths to search for include files. The object is
106139ee7a7aSBaptiste Daroussin  * copied so caller must unref the object.
106239ee7a7aSBaptiste Daroussin  * @param parser parser structure
106339ee7a7aSBaptiste Daroussin  * @param paths UCL_ARRAY of paths to search
106439ee7a7aSBaptiste Daroussin  * @return true if the path search array was replaced in the parser
106539ee7a7aSBaptiste Daroussin  */
106639ee7a7aSBaptiste Daroussin UCL_EXTERN bool ucl_set_include_path (struct ucl_parser *parser,
106739ee7a7aSBaptiste Daroussin 		ucl_object_t *paths);
106839ee7a7aSBaptiste Daroussin 
106939ee7a7aSBaptiste Daroussin /**
1070b04a7a0bSBaptiste Daroussin  * Get a top object for a parser (refcount is increased)
1071c99fb5f9SBaptiste Daroussin  * @param parser parser structure
1072c99fb5f9SBaptiste Daroussin  * @param err if *err is NULL it is set to parser error
1073c99fb5f9SBaptiste Daroussin  * @return top parser object or NULL
1074c99fb5f9SBaptiste Daroussin  */
107536c53d67SBaptiste Daroussin UCL_EXTERN ucl_object_t* ucl_parser_get_object (struct ucl_parser *parser);
1076c99fb5f9SBaptiste Daroussin 
1077c99fb5f9SBaptiste Daroussin /**
107839ee7a7aSBaptiste Daroussin  * Get the error string if parsing has been failed
1079c99fb5f9SBaptiste Daroussin  * @param parser parser object
108039ee7a7aSBaptiste Daroussin  * @return error description
1081c99fb5f9SBaptiste Daroussin  */
108236c53d67SBaptiste Daroussin UCL_EXTERN const char *ucl_parser_get_error (struct ucl_parser *parser);
10838e3b1ab2SBaptiste Daroussin 
10848e3b1ab2SBaptiste Daroussin /**
108539ee7a7aSBaptiste Daroussin  * Get the code of the last error
108639ee7a7aSBaptiste Daroussin  * @param parser parser object
108739ee7a7aSBaptiste Daroussin  * @return error code
108839ee7a7aSBaptiste Daroussin  */
108939ee7a7aSBaptiste Daroussin UCL_EXTERN int ucl_parser_get_error_code (struct ucl_parser *parser);
109039ee7a7aSBaptiste Daroussin 
109139ee7a7aSBaptiste Daroussin /**
109239ee7a7aSBaptiste Daroussin  * Get the current column number within parser
109339ee7a7aSBaptiste Daroussin  * @param parser parser object
109439ee7a7aSBaptiste Daroussin  * @return current column number
109539ee7a7aSBaptiste Daroussin  */
109639ee7a7aSBaptiste Daroussin UCL_EXTERN unsigned ucl_parser_get_column (struct ucl_parser *parser);
109739ee7a7aSBaptiste Daroussin 
109839ee7a7aSBaptiste Daroussin /**
109939ee7a7aSBaptiste Daroussin  * Get the current line number within parser
110039ee7a7aSBaptiste Daroussin  * @param parser parser object
110139ee7a7aSBaptiste Daroussin  * @return current line number
110239ee7a7aSBaptiste Daroussin  */
110339ee7a7aSBaptiste Daroussin UCL_EXTERN unsigned ucl_parser_get_linenum (struct ucl_parser *parser);
110439ee7a7aSBaptiste Daroussin 
110539ee7a7aSBaptiste Daroussin /**
11068e3b1ab2SBaptiste Daroussin  * Clear the error in the parser
11078e3b1ab2SBaptiste Daroussin  * @param parser parser object
11088e3b1ab2SBaptiste Daroussin  */
11098e3b1ab2SBaptiste Daroussin UCL_EXTERN void ucl_parser_clear_error (struct ucl_parser *parser);
11108e3b1ab2SBaptiste Daroussin 
1111c99fb5f9SBaptiste Daroussin /**
1112c99fb5f9SBaptiste Daroussin  * Free ucl parser object
1113c99fb5f9SBaptiste Daroussin  * @param parser parser object
1114c99fb5f9SBaptiste Daroussin  */
111536c53d67SBaptiste Daroussin UCL_EXTERN void ucl_parser_free (struct ucl_parser *parser);
1116c99fb5f9SBaptiste Daroussin 
1117c99fb5f9SBaptiste Daroussin /**
1118*d9f0ce31SBaptiste Daroussin  * Get constant opaque pointer to comments structure for this parser. Increase
1119*d9f0ce31SBaptiste Daroussin  * refcount to prevent this object to be destroyed on parser's destruction
1120*d9f0ce31SBaptiste Daroussin  * @param parser parser structure
1121*d9f0ce31SBaptiste Daroussin  * @return ucl comments pointer or NULL
1122*d9f0ce31SBaptiste Daroussin  */
1123*d9f0ce31SBaptiste Daroussin UCL_EXTERN const ucl_object_t * ucl_parser_get_comments (struct ucl_parser *parser);
1124*d9f0ce31SBaptiste Daroussin 
1125*d9f0ce31SBaptiste Daroussin /**
1126*d9f0ce31SBaptiste Daroussin  * Utility function to find a comment object for the specified object in the input
1127*d9f0ce31SBaptiste Daroussin  * @param comments comments object
1128*d9f0ce31SBaptiste Daroussin  * @param srch search object
1129*d9f0ce31SBaptiste Daroussin  * @return string comment enclosed in ucl_object_t
1130*d9f0ce31SBaptiste Daroussin  */
1131*d9f0ce31SBaptiste Daroussin UCL_EXTERN const ucl_object_t * ucl_comments_find (const ucl_object_t *comments,
1132*d9f0ce31SBaptiste Daroussin 		const ucl_object_t *srch);
1133*d9f0ce31SBaptiste Daroussin 
1134*d9f0ce31SBaptiste Daroussin /**
1135*d9f0ce31SBaptiste Daroussin  * Move comment from `from` object to `to` object
1136*d9f0ce31SBaptiste Daroussin  * @param comments comments object
1137*d9f0ce31SBaptiste Daroussin  * @param what source object
1138*d9f0ce31SBaptiste Daroussin  * @param whith destination object
1139*d9f0ce31SBaptiste Daroussin  * @return `true` if `from` has comment and it has been moved to `to`
1140*d9f0ce31SBaptiste Daroussin  */
1141*d9f0ce31SBaptiste Daroussin UCL_EXTERN bool ucl_comments_move (ucl_object_t *comments,
1142*d9f0ce31SBaptiste Daroussin 		const ucl_object_t *from, const ucl_object_t *to);
1143*d9f0ce31SBaptiste Daroussin 
1144*d9f0ce31SBaptiste Daroussin /**
1145*d9f0ce31SBaptiste Daroussin  * Adds a new comment for an object
1146*d9f0ce31SBaptiste Daroussin  * @param comments comments object
1147*d9f0ce31SBaptiste Daroussin  * @param obj object to add comment to
1148*d9f0ce31SBaptiste Daroussin  * @param comment string representation of a comment
1149*d9f0ce31SBaptiste Daroussin  */
1150*d9f0ce31SBaptiste Daroussin UCL_EXTERN void ucl_comments_add (ucl_object_t *comments,
1151*d9f0ce31SBaptiste Daroussin 		const ucl_object_t *obj, const char *comment);
1152*d9f0ce31SBaptiste Daroussin 
1153*d9f0ce31SBaptiste Daroussin /**
1154c99fb5f9SBaptiste Daroussin  * Add new public key to parser for signatures check
1155c99fb5f9SBaptiste Daroussin  * @param parser parser object
1156c99fb5f9SBaptiste Daroussin  * @param key PEM representation of a key
1157c99fb5f9SBaptiste Daroussin  * @param len length of the key
1158c99fb5f9SBaptiste Daroussin  * @param err if *err is NULL it is set to parser error
1159c99fb5f9SBaptiste Daroussin  * @return true if a key has been successfully added
1160c99fb5f9SBaptiste Daroussin  */
1161*d9f0ce31SBaptiste Daroussin UCL_EXTERN bool ucl_parser_pubkey_add (struct ucl_parser *parser,
1162*d9f0ce31SBaptiste Daroussin 		const unsigned char *key, size_t len);
1163c99fb5f9SBaptiste Daroussin 
1164c99fb5f9SBaptiste Daroussin /**
1165c99fb5f9SBaptiste Daroussin  * Set FILENAME and CURDIR variables in parser
1166c99fb5f9SBaptiste Daroussin  * @param parser parser object
1167c99fb5f9SBaptiste Daroussin  * @param filename filename to set or NULL to set FILENAME to "undef" and CURDIR to getcwd()
1168c99fb5f9SBaptiste Daroussin  * @param need_expand perform realpath() if this variable is true and filename is not NULL
1169c99fb5f9SBaptiste Daroussin  * @return true if variables has been set
1170c99fb5f9SBaptiste Daroussin  */
117136c53d67SBaptiste Daroussin UCL_EXTERN bool ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename,
1172c99fb5f9SBaptiste Daroussin 		bool need_expand);
1173c99fb5f9SBaptiste Daroussin 
1174c99fb5f9SBaptiste Daroussin /** @} */
1175c99fb5f9SBaptiste Daroussin 
1176c99fb5f9SBaptiste Daroussin /**
1177c99fb5f9SBaptiste Daroussin  * @defgroup emitter Emitting functions
1178c99fb5f9SBaptiste Daroussin  * These functions are used to serialise UCL objects to some string representation.
1179c99fb5f9SBaptiste Daroussin  *
1180c99fb5f9SBaptiste Daroussin  * @{
1181c99fb5f9SBaptiste Daroussin  */
1182c99fb5f9SBaptiste Daroussin 
11833dcf5eb7SBaptiste Daroussin struct ucl_emitter_context;
1184c99fb5f9SBaptiste Daroussin /**
1185c99fb5f9SBaptiste Daroussin  * Structure using for emitter callbacks
1186c99fb5f9SBaptiste Daroussin  */
1187c99fb5f9SBaptiste Daroussin struct ucl_emitter_functions {
1188c99fb5f9SBaptiste Daroussin 	/** Append a single character */
1189c99fb5f9SBaptiste Daroussin 	int (*ucl_emitter_append_character) (unsigned char c, size_t nchars, void *ud);
1190c99fb5f9SBaptiste Daroussin 	/** Append a string of a specified length */
1191c99fb5f9SBaptiste Daroussin 	int (*ucl_emitter_append_len) (unsigned const char *str, size_t len, void *ud);
1192c99fb5f9SBaptiste Daroussin 	/** Append a 64 bit integer */
1193c99fb5f9SBaptiste Daroussin 	int (*ucl_emitter_append_int) (int64_t elt, void *ud);
1194c99fb5f9SBaptiste Daroussin 	/** Append floating point element */
1195c99fb5f9SBaptiste Daroussin 	int (*ucl_emitter_append_double) (double elt, void *ud);
11963dcf5eb7SBaptiste Daroussin 	/** Free userdata */
11973dcf5eb7SBaptiste Daroussin 	void (*ucl_emitter_free_func)(void *ud);
1198c99fb5f9SBaptiste Daroussin 	/** Opaque userdata pointer */
1199c99fb5f9SBaptiste Daroussin 	void *ud;
1200c99fb5f9SBaptiste Daroussin };
1201c99fb5f9SBaptiste Daroussin 
12023dcf5eb7SBaptiste Daroussin struct ucl_emitter_operations {
12033dcf5eb7SBaptiste Daroussin 	/** Write a primitive element */
12043dcf5eb7SBaptiste Daroussin 	void (*ucl_emitter_write_elt) (struct ucl_emitter_context *ctx,
12053dcf5eb7SBaptiste Daroussin 		const ucl_object_t *obj, bool first, bool print_key);
12063dcf5eb7SBaptiste Daroussin 	/** Start ucl object */
12073dcf5eb7SBaptiste Daroussin 	void (*ucl_emitter_start_object) (struct ucl_emitter_context *ctx,
12083dcf5eb7SBaptiste Daroussin 		const ucl_object_t *obj, bool print_key);
12093dcf5eb7SBaptiste Daroussin 	/** End ucl object */
12103dcf5eb7SBaptiste Daroussin 	void (*ucl_emitter_end_object) (struct ucl_emitter_context *ctx,
12113dcf5eb7SBaptiste Daroussin 		const ucl_object_t *obj);
12123dcf5eb7SBaptiste Daroussin 	/** Start ucl array */
12133dcf5eb7SBaptiste Daroussin 	void (*ucl_emitter_start_array) (struct ucl_emitter_context *ctx,
12143dcf5eb7SBaptiste Daroussin 		const ucl_object_t *obj, bool print_key);
12153dcf5eb7SBaptiste Daroussin 	void (*ucl_emitter_end_array) (struct ucl_emitter_context *ctx,
12163dcf5eb7SBaptiste Daroussin 		const ucl_object_t *obj);
12173dcf5eb7SBaptiste Daroussin };
12183dcf5eb7SBaptiste Daroussin 
12193dcf5eb7SBaptiste Daroussin /**
12203dcf5eb7SBaptiste Daroussin  * Structure that defines emitter functions
12213dcf5eb7SBaptiste Daroussin  */
12223dcf5eb7SBaptiste Daroussin struct ucl_emitter_context {
12233dcf5eb7SBaptiste Daroussin 	/** Name of emitter (e.g. json, compact_json) */
12243dcf5eb7SBaptiste Daroussin 	const char *name;
12253dcf5eb7SBaptiste Daroussin 	/** Unique id (e.g. UCL_EMIT_JSON for standard emitters */
12263dcf5eb7SBaptiste Daroussin 	int id;
12273dcf5eb7SBaptiste Daroussin 	/** A set of output functions */
12283dcf5eb7SBaptiste Daroussin 	const struct ucl_emitter_functions *func;
12293dcf5eb7SBaptiste Daroussin 	/** A set of output operations */
12303dcf5eb7SBaptiste Daroussin 	const struct ucl_emitter_operations *ops;
12313dcf5eb7SBaptiste Daroussin 	/** Current amount of indent tabs */
12324bf54857SBaptiste Daroussin 	unsigned int indent;
12333dcf5eb7SBaptiste Daroussin 	/** Top level object */
12343dcf5eb7SBaptiste Daroussin 	const ucl_object_t *top;
1235*d9f0ce31SBaptiste Daroussin 	/** Optional comments */
1236*d9f0ce31SBaptiste Daroussin 	const ucl_object_t *comments;
12373dcf5eb7SBaptiste Daroussin };
12383dcf5eb7SBaptiste Daroussin 
1239c99fb5f9SBaptiste Daroussin /**
1240c99fb5f9SBaptiste Daroussin  * Emit object to a string
1241c99fb5f9SBaptiste Daroussin  * @param obj object
1242c99fb5f9SBaptiste Daroussin  * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is
1243c99fb5f9SBaptiste Daroussin  * #UCL_EMIT_CONFIG then emit config like object
1244c99fb5f9SBaptiste Daroussin  * @return dump of an object (must be freed after using) or NULL in case of error
1245c99fb5f9SBaptiste Daroussin  */
1246b04a7a0bSBaptiste Daroussin UCL_EXTERN unsigned char *ucl_object_emit (const ucl_object_t *obj,
1247b04a7a0bSBaptiste Daroussin 		enum ucl_emitter emit_type);
1248c99fb5f9SBaptiste Daroussin 
1249c99fb5f9SBaptiste Daroussin /**
125039ee7a7aSBaptiste Daroussin  * Emit object to a string that can contain `\0` inside
125139ee7a7aSBaptiste Daroussin  * @param obj object
125239ee7a7aSBaptiste Daroussin  * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is
125339ee7a7aSBaptiste Daroussin  * #UCL_EMIT_CONFIG then emit config like object
125439ee7a7aSBaptiste Daroussin  * @param len the resulting length
125539ee7a7aSBaptiste Daroussin  * @return dump of an object (must be freed after using) or NULL in case of error
125639ee7a7aSBaptiste Daroussin  */
125739ee7a7aSBaptiste Daroussin UCL_EXTERN unsigned char *ucl_object_emit_len (const ucl_object_t *obj,
125839ee7a7aSBaptiste Daroussin 		enum ucl_emitter emit_type, size_t *len);
125939ee7a7aSBaptiste Daroussin 
126039ee7a7aSBaptiste Daroussin /**
1261c99fb5f9SBaptiste Daroussin  * Emit object to a string
1262c99fb5f9SBaptiste Daroussin  * @param obj object
1263c99fb5f9SBaptiste Daroussin  * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is
1264c99fb5f9SBaptiste Daroussin  * #UCL_EMIT_CONFIG then emit config like object
12653dcf5eb7SBaptiste Daroussin  * @param emitter a set of emitter functions
1266*d9f0ce31SBaptiste Daroussin  * @param comments optional comments for the parser
1267c99fb5f9SBaptiste Daroussin  * @return dump of an object (must be freed after using) or NULL in case of error
1268c99fb5f9SBaptiste Daroussin  */
1269b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_emit_full (const ucl_object_t *obj,
1270b04a7a0bSBaptiste Daroussin 		enum ucl_emitter emit_type,
1271*d9f0ce31SBaptiste Daroussin 		struct ucl_emitter_functions *emitter,
1272*d9f0ce31SBaptiste Daroussin 		const ucl_object_t *comments);
12733dcf5eb7SBaptiste Daroussin 
12743dcf5eb7SBaptiste Daroussin /**
12753dcf5eb7SBaptiste Daroussin  * Start streamlined UCL object emitter
12763dcf5eb7SBaptiste Daroussin  * @param obj top UCL object
12773dcf5eb7SBaptiste Daroussin  * @param emit_type emit type
12783dcf5eb7SBaptiste Daroussin  * @param emitter a set of emitter functions
12793dcf5eb7SBaptiste Daroussin  * @return new streamlined context that should be freed by
12803dcf5eb7SBaptiste Daroussin  * `ucl_object_emit_streamline_finish`
12813dcf5eb7SBaptiste Daroussin  */
12823dcf5eb7SBaptiste Daroussin UCL_EXTERN struct ucl_emitter_context* ucl_object_emit_streamline_new (
12833dcf5eb7SBaptiste Daroussin 		const ucl_object_t *obj, enum ucl_emitter emit_type,
12843dcf5eb7SBaptiste Daroussin 		struct ucl_emitter_functions *emitter);
12853dcf5eb7SBaptiste Daroussin 
12863dcf5eb7SBaptiste Daroussin /**
12873dcf5eb7SBaptiste Daroussin  * Start object or array container for the streamlined output
12883dcf5eb7SBaptiste Daroussin  * @param ctx streamlined context
12893dcf5eb7SBaptiste Daroussin  * @param obj container object
12903dcf5eb7SBaptiste Daroussin  */
12913dcf5eb7SBaptiste Daroussin UCL_EXTERN void ucl_object_emit_streamline_start_container (
12923dcf5eb7SBaptiste Daroussin 		struct ucl_emitter_context *ctx, const ucl_object_t *obj);
12933dcf5eb7SBaptiste Daroussin /**
12943dcf5eb7SBaptiste Daroussin  * Add a complete UCL object to streamlined output
12953dcf5eb7SBaptiste Daroussin  * @param ctx streamlined context
12963dcf5eb7SBaptiste Daroussin  * @param obj object to output
12973dcf5eb7SBaptiste Daroussin  */
12983dcf5eb7SBaptiste Daroussin UCL_EXTERN void ucl_object_emit_streamline_add_object (
12993dcf5eb7SBaptiste Daroussin 		struct ucl_emitter_context *ctx, const ucl_object_t *obj);
13003dcf5eb7SBaptiste Daroussin /**
13013dcf5eb7SBaptiste Daroussin  * End previously added container
13023dcf5eb7SBaptiste Daroussin  * @param ctx streamlined context
13033dcf5eb7SBaptiste Daroussin  */
13043dcf5eb7SBaptiste Daroussin UCL_EXTERN void ucl_object_emit_streamline_end_container (
13053dcf5eb7SBaptiste Daroussin 		struct ucl_emitter_context *ctx);
13063dcf5eb7SBaptiste Daroussin /**
13073dcf5eb7SBaptiste Daroussin  * Terminate streamlined container finishing all containers in it
13083dcf5eb7SBaptiste Daroussin  * @param ctx streamlined context
13093dcf5eb7SBaptiste Daroussin  */
13103dcf5eb7SBaptiste Daroussin UCL_EXTERN void ucl_object_emit_streamline_finish (
13113dcf5eb7SBaptiste Daroussin 		struct ucl_emitter_context *ctx);
13123dcf5eb7SBaptiste Daroussin 
13133dcf5eb7SBaptiste Daroussin /**
13143dcf5eb7SBaptiste Daroussin  * Returns functions to emit object to memory
13153dcf5eb7SBaptiste Daroussin  * @param pmem target pointer (should be freed by caller)
13163dcf5eb7SBaptiste Daroussin  * @return emitter functions structure
13173dcf5eb7SBaptiste Daroussin  */
13183dcf5eb7SBaptiste Daroussin UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_memory_funcs (
13193dcf5eb7SBaptiste Daroussin 		void **pmem);
13203dcf5eb7SBaptiste Daroussin 
13213dcf5eb7SBaptiste Daroussin /**
13223dcf5eb7SBaptiste Daroussin  * Returns functions to emit object to FILE *
13233dcf5eb7SBaptiste Daroussin  * @param fp FILE * object
13243dcf5eb7SBaptiste Daroussin  * @return emitter functions structure
13253dcf5eb7SBaptiste Daroussin  */
13263dcf5eb7SBaptiste Daroussin UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_file_funcs (
13273dcf5eb7SBaptiste Daroussin 		FILE *fp);
13283dcf5eb7SBaptiste Daroussin /**
13293dcf5eb7SBaptiste Daroussin  * Returns functions to emit object to a file descriptor
13303dcf5eb7SBaptiste Daroussin  * @param fd file descriptor
13313dcf5eb7SBaptiste Daroussin  * @return emitter functions structure
13323dcf5eb7SBaptiste Daroussin  */
13333dcf5eb7SBaptiste Daroussin UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_fd_funcs (
13343dcf5eb7SBaptiste Daroussin 		int fd);
13353dcf5eb7SBaptiste Daroussin 
13363dcf5eb7SBaptiste Daroussin /**
13373dcf5eb7SBaptiste Daroussin  * Free emitter functions
13383dcf5eb7SBaptiste Daroussin  * @param f pointer to functions
13393dcf5eb7SBaptiste Daroussin  */
13403dcf5eb7SBaptiste Daroussin UCL_EXTERN void ucl_object_emit_funcs_free (struct ucl_emitter_functions *f);
13413dcf5eb7SBaptiste Daroussin 
1342c99fb5f9SBaptiste Daroussin /** @} */
1343c99fb5f9SBaptiste Daroussin 
134497bd480fSBaptiste Daroussin /**
134597bd480fSBaptiste Daroussin  * @defgroup schema Schema functions
134697bd480fSBaptiste Daroussin  * These functions are used to validate UCL objects using json schema format
134797bd480fSBaptiste Daroussin  *
134897bd480fSBaptiste Daroussin  * @{
134997bd480fSBaptiste Daroussin  */
135097bd480fSBaptiste Daroussin 
135197bd480fSBaptiste Daroussin /**
135297bd480fSBaptiste Daroussin  * Used to define UCL schema error
135397bd480fSBaptiste Daroussin  */
135497bd480fSBaptiste Daroussin enum ucl_schema_error_code {
135597bd480fSBaptiste Daroussin 	UCL_SCHEMA_OK = 0,          /**< no error */
135697bd480fSBaptiste Daroussin 	UCL_SCHEMA_TYPE_MISMATCH,   /**< type of object is incorrect */
135797bd480fSBaptiste Daroussin 	UCL_SCHEMA_INVALID_SCHEMA,  /**< schema is invalid */
135897bd480fSBaptiste Daroussin 	UCL_SCHEMA_MISSING_PROPERTY,/**< one or more missing properties */
135997bd480fSBaptiste Daroussin 	UCL_SCHEMA_CONSTRAINT,      /**< constraint found */
136097bd480fSBaptiste Daroussin 	UCL_SCHEMA_MISSING_DEPENDENCY, /**< missing dependency */
1361*d9f0ce31SBaptiste Daroussin 	UCL_SCHEMA_EXTERNAL_REF_MISSING, /**< cannot fetch external ref */
1362*d9f0ce31SBaptiste Daroussin 	UCL_SCHEMA_EXTERNAL_REF_INVALID, /**< invalid external ref */
1363*d9f0ce31SBaptiste Daroussin 	UCL_SCHEMA_INTERNAL_ERROR, /**< something bad happened */
136497bd480fSBaptiste Daroussin 	UCL_SCHEMA_UNKNOWN          /**< generic error */
136597bd480fSBaptiste Daroussin };
136697bd480fSBaptiste Daroussin 
136797bd480fSBaptiste Daroussin /**
136897bd480fSBaptiste Daroussin  * Generic ucl schema error
136997bd480fSBaptiste Daroussin  */
137097bd480fSBaptiste Daroussin struct ucl_schema_error {
137197bd480fSBaptiste Daroussin 	enum ucl_schema_error_code code;	/**< error code */
137297bd480fSBaptiste Daroussin 	char msg[128];						/**< error message */
1373b04a7a0bSBaptiste Daroussin 	const ucl_object_t *obj;			/**< object where error occured */
137497bd480fSBaptiste Daroussin };
137597bd480fSBaptiste Daroussin 
137697bd480fSBaptiste Daroussin /**
137797bd480fSBaptiste Daroussin  * Validate object `obj` using schema object `schema`.
137897bd480fSBaptiste Daroussin  * @param schema schema object
137997bd480fSBaptiste Daroussin  * @param obj object to validate
138097bd480fSBaptiste Daroussin  * @param err error pointer, if this parameter is not NULL and error has been
138197bd480fSBaptiste Daroussin  * occured, then `err` is filled with the exact error definition.
138297bd480fSBaptiste Daroussin  * @return true if `obj` is valid using `schema`
138397bd480fSBaptiste Daroussin  */
1384b04a7a0bSBaptiste Daroussin UCL_EXTERN bool ucl_object_validate (const ucl_object_t *schema,
1385b04a7a0bSBaptiste Daroussin 		const ucl_object_t *obj, struct ucl_schema_error *err);
138697bd480fSBaptiste Daroussin 
1387*d9f0ce31SBaptiste Daroussin /**
1388*d9f0ce31SBaptiste Daroussin  * Validate object `obj` using schema object `schema` and root schema at `root`.
1389*d9f0ce31SBaptiste Daroussin  * @param schema schema object
1390*d9f0ce31SBaptiste Daroussin  * @param obj object to validate
1391*d9f0ce31SBaptiste Daroussin  * @param root root schema object
1392*d9f0ce31SBaptiste Daroussin  * @param err error pointer, if this parameter is not NULL and error has been
1393*d9f0ce31SBaptiste Daroussin  * occured, then `err` is filled with the exact error definition.
1394*d9f0ce31SBaptiste Daroussin  * @return true if `obj` is valid using `schema`
1395*d9f0ce31SBaptiste Daroussin  */
1396*d9f0ce31SBaptiste Daroussin UCL_EXTERN bool ucl_object_validate_root (const ucl_object_t *schema,
1397*d9f0ce31SBaptiste Daroussin 		const ucl_object_t *obj,
1398*d9f0ce31SBaptiste Daroussin 		const ucl_object_t *root,
1399*d9f0ce31SBaptiste Daroussin 		struct ucl_schema_error *err);
1400*d9f0ce31SBaptiste Daroussin 
1401*d9f0ce31SBaptiste Daroussin /**
1402*d9f0ce31SBaptiste Daroussin  * Validate object `obj` using schema object `schema` and root schema at `root`
1403*d9f0ce31SBaptiste Daroussin  * using some external references provided.
1404*d9f0ce31SBaptiste Daroussin  * @param schema schema object
1405*d9f0ce31SBaptiste Daroussin  * @param obj object to validate
1406*d9f0ce31SBaptiste Daroussin  * @param root root schema object
1407*d9f0ce31SBaptiste Daroussin  * @param ext_refs external references (might be modified during validation)
1408*d9f0ce31SBaptiste Daroussin  * @param err error pointer, if this parameter is not NULL and error has been
1409*d9f0ce31SBaptiste Daroussin  * occured, then `err` is filled with the exact error definition.
1410*d9f0ce31SBaptiste Daroussin  * @return true if `obj` is valid using `schema`
1411*d9f0ce31SBaptiste Daroussin  */
1412*d9f0ce31SBaptiste Daroussin UCL_EXTERN bool ucl_object_validate_root_ext (const ucl_object_t *schema,
1413*d9f0ce31SBaptiste Daroussin 		const ucl_object_t *obj,
1414*d9f0ce31SBaptiste Daroussin 		const ucl_object_t *root,
1415*d9f0ce31SBaptiste Daroussin 		ucl_object_t *ext_refs,
1416*d9f0ce31SBaptiste Daroussin 		struct ucl_schema_error *err);
1417*d9f0ce31SBaptiste Daroussin 
141897bd480fSBaptiste Daroussin /** @} */
141997bd480fSBaptiste Daroussin 
1420c99fb5f9SBaptiste Daroussin #ifdef  __cplusplus
1421c99fb5f9SBaptiste Daroussin }
1422c99fb5f9SBaptiste Daroussin #endif
1423c99fb5f9SBaptiste Daroussin /*
1424c99fb5f9SBaptiste Daroussin  * XXX: Poorly named API functions, need to replace them with the appropriate
1425c99fb5f9SBaptiste Daroussin  * named function. All API functions *must* use naming ucl_object_*. Usage of
1426c99fb5f9SBaptiste Daroussin  * ucl_obj* should be avoided.
1427c99fb5f9SBaptiste Daroussin  */
1428c99fb5f9SBaptiste Daroussin #define ucl_obj_todouble_safe ucl_object_todouble_safe
1429c99fb5f9SBaptiste Daroussin #define ucl_obj_todouble ucl_object_todouble
1430c99fb5f9SBaptiste Daroussin #define ucl_obj_tostring ucl_object_tostring
1431c99fb5f9SBaptiste Daroussin #define ucl_obj_tostring_safe ucl_object_tostring_safe
1432c99fb5f9SBaptiste Daroussin #define ucl_obj_tolstring ucl_object_tolstring
1433c99fb5f9SBaptiste Daroussin #define ucl_obj_tolstring_safe ucl_object_tolstring_safe
1434c99fb5f9SBaptiste Daroussin #define ucl_obj_toint ucl_object_toint
1435c99fb5f9SBaptiste Daroussin #define ucl_obj_toint_safe ucl_object_toint_safe
1436c99fb5f9SBaptiste Daroussin #define ucl_obj_toboolean ucl_object_toboolean
1437c99fb5f9SBaptiste Daroussin #define ucl_obj_toboolean_safe ucl_object_toboolean_safe
1438c99fb5f9SBaptiste Daroussin #define ucl_obj_get_key ucl_object_find_key
1439c99fb5f9SBaptiste Daroussin #define ucl_obj_get_keyl ucl_object_find_keyl
1440c99fb5f9SBaptiste Daroussin #define ucl_obj_unref ucl_object_unref
1441c99fb5f9SBaptiste Daroussin #define ucl_obj_ref ucl_object_ref
1442c99fb5f9SBaptiste Daroussin #define ucl_obj_free ucl_object_free
1443c99fb5f9SBaptiste Daroussin 
1444c99fb5f9SBaptiste Daroussin #endif /* UCL_H_ */
1445