1 /* Copyright (c) 2014, Vsevolod Stakhov
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "ucl_internal.h"
30 #include "ucl_chartable.h"
32 struct ucl_emitter_streamline_stack {
35 const ucl_object_t *obj;
36 struct ucl_emitter_streamline_stack *next;
39 struct ucl_emitter_context_streamline {
40 /* Inherited from the main context */
41 /** Name of emitter (e.g. json, compact_json) */
43 /** Unique id (e.g. UCL_EMIT_JSON for standard emitters */
45 /** A set of output functions */
46 const struct ucl_emitter_functions *func;
47 /** A set of output operations */
48 const struct ucl_emitter_operations *ops;
49 /** Current amount of indent tabs */
51 /** Top level object */
52 const ucl_object_t *top;
53 /** Optional comments */
54 const ucl_object_t *comments;
56 /* Streamline specific fields */
57 struct ucl_emitter_streamline_stack *containers;
60 #define TO_STREAMLINE(ctx) (struct ucl_emitter_context_streamline *)(ctx)
62 struct ucl_emitter_context*
63 ucl_object_emit_streamline_new (const ucl_object_t *obj,
64 enum ucl_emitter emit_type,
65 struct ucl_emitter_functions *emitter)
67 const struct ucl_emitter_context *ctx;
68 struct ucl_emitter_context_streamline *sctx;
70 ctx = ucl_emit_get_standard_context (emit_type);
75 sctx = calloc (1, sizeof (*sctx));
80 memcpy (sctx, ctx, sizeof (*ctx));
84 ucl_object_emit_streamline_start_container ((struct ucl_emitter_context *)sctx,
87 return (struct ucl_emitter_context *)sctx;
91 ucl_object_emit_streamline_start_container (struct ucl_emitter_context *ctx,
92 const ucl_object_t *obj)
94 struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
95 struct ucl_emitter_streamline_stack *st, *top;
96 bool print_key = false;
98 /* Check top object presence */
99 if (sctx->top == NULL) {
103 top = sctx->containers;
104 st = malloc (sizeof (*st));
106 if (top != NULL && !top->is_array) {
111 if (obj != NULL && obj->type == UCL_ARRAY) {
113 sctx->ops->ucl_emitter_start_array (ctx, obj, print_key);
116 st->is_array = false;
117 sctx->ops->ucl_emitter_start_object (ctx, obj, print_key);
119 LL_PREPEND (sctx->containers, st);
124 ucl_object_emit_streamline_add_object (
125 struct ucl_emitter_context *ctx, const ucl_object_t *obj)
127 struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
128 bool is_array = false, is_first = false;
130 if (sctx->containers != NULL) {
131 if (sctx->containers->is_array) {
134 if (sctx->containers->empty) {
136 sctx->containers->empty = false;
140 sctx->ops->ucl_emitter_write_elt (ctx, obj, is_first, !is_array);
144 ucl_object_emit_streamline_end_container (struct ucl_emitter_context *ctx)
146 struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
147 struct ucl_emitter_streamline_stack *st;
149 if (sctx->containers != NULL) {
150 st = sctx->containers;
153 sctx->ops->ucl_emitter_end_array (ctx, st->obj);
156 sctx->ops->ucl_emitter_end_object (ctx, st->obj);
158 sctx->containers = st->next;
164 ucl_object_emit_streamline_finish (struct ucl_emitter_context *ctx)
166 struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
168 while (sctx->containers != NULL) {
169 ucl_object_emit_streamline_end_container (ctx);