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 */
43 const struct ucl_emitter_functions *func;
44 const struct ucl_emitter_operations *ops;
46 const ucl_object_t *top;
48 /* Streamline specific fields */
49 struct ucl_emitter_streamline_stack *containers;
52 #define TO_STREAMLINE(ctx) (struct ucl_emitter_context_streamline *)(ctx)
54 struct ucl_emitter_context*
55 ucl_object_emit_streamline_new (const ucl_object_t *obj,
56 enum ucl_emitter emit_type,
57 struct ucl_emitter_functions *emitter)
59 const struct ucl_emitter_context *ctx;
60 struct ucl_emitter_context_streamline *sctx;
62 ctx = ucl_emit_get_standard_context (emit_type);
67 sctx = calloc (1, sizeof (*sctx));
72 memcpy (sctx, ctx, sizeof (*ctx));
76 ucl_object_emit_streamline_start_container ((struct ucl_emitter_context *)sctx,
79 return (struct ucl_emitter_context *)sctx;
83 ucl_object_emit_streamline_start_container (struct ucl_emitter_context *ctx,
84 const ucl_object_t *obj)
86 struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
87 struct ucl_emitter_streamline_stack *st, *top;
88 bool print_key = false;
90 /* Check top object presence */
91 if (sctx->top == NULL) {
95 top = sctx->containers;
96 st = malloc (sizeof (*st));
98 if (top != NULL && !top->is_array) {
103 if (obj != NULL && obj->type == UCL_ARRAY) {
105 sctx->ops->ucl_emitter_start_array (ctx, obj, print_key);
108 st->is_array = false;
109 sctx->ops->ucl_emitter_start_object (ctx, obj, print_key);
111 LL_PREPEND (sctx->containers, st);
116 ucl_object_emit_streamline_add_object (
117 struct ucl_emitter_context *ctx, const ucl_object_t *obj)
119 struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
120 bool is_array = false, is_first = false;
122 if (sctx->containers != NULL) {
123 if (sctx->containers->is_array) {
126 if (sctx->containers->empty) {
128 sctx->containers->empty = false;
132 sctx->ops->ucl_emitter_write_elt (ctx, obj, is_first, !is_array);
136 ucl_object_emit_streamline_end_container (struct ucl_emitter_context *ctx)
138 struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
139 struct ucl_emitter_streamline_stack *st;
141 if (sctx->containers != NULL) {
142 st = sctx->containers;
145 sctx->ops->ucl_emitter_end_array (ctx, st->obj);
148 sctx->ops->ucl_emitter_end_object (ctx, st->obj);
150 sctx->containers = st->next;
156 ucl_object_emit_streamline_finish (struct ucl_emitter_context *ctx)
158 struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
160 while (sctx->containers != NULL) {
161 ucl_object_emit_streamline_end_container (ctx);