Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/libucl/src/ucl_emitter_streamline.c
2066 views
1
/* Copyright (c) 2014, Vsevolod Stakhov
2
* All rights reserved.
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
6
* * Redistributions of source code must retain the above copyright
7
* notice, this list of conditions and the following disclaimer.
8
* * Redistributions in binary form must reproduce the above copyright
9
* notice, this list of conditions and the following disclaimer in the
10
* documentation and/or other materials provided with the distribution.
11
*
12
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
*/
23
24
#ifdef HAVE_CONFIG_H
25
#include "config.h"
26
#endif
27
28
#include "ucl.h"
29
#include "ucl_internal.h"
30
#include "ucl_chartable.h"
31
32
struct ucl_emitter_streamline_stack {
33
bool is_array;
34
bool empty;
35
const ucl_object_t *obj;
36
struct ucl_emitter_streamline_stack *next;
37
};
38
39
struct ucl_emitter_context_streamline {
40
/* Inherited from the main context */
41
/** Name of emitter (e.g. json, compact_json) */
42
const char *name;
43
/** Unique id (e.g. UCL_EMIT_JSON for standard emitters */
44
int id;
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 */
50
unsigned int indent;
51
/** Top level object */
52
const ucl_object_t *top;
53
/** Optional comments */
54
const ucl_object_t *comments;
55
56
/* Streamline specific fields */
57
struct ucl_emitter_streamline_stack *containers;
58
};
59
60
#define TO_STREAMLINE(ctx) (struct ucl_emitter_context_streamline *)(ctx)
61
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)
66
{
67
const struct ucl_emitter_context *ctx;
68
struct ucl_emitter_context_streamline *sctx;
69
70
ctx = ucl_emit_get_standard_context (emit_type);
71
if (ctx == NULL) {
72
return NULL;
73
}
74
75
sctx = calloc (1, sizeof (*sctx));
76
if (sctx == NULL) {
77
return NULL;
78
}
79
80
memcpy (sctx, ctx, sizeof (*ctx));
81
sctx->func = emitter;
82
sctx->top = obj;
83
84
ucl_object_emit_streamline_start_container ((struct ucl_emitter_context *)sctx,
85
obj);
86
87
return (struct ucl_emitter_context *)sctx;
88
}
89
90
void
91
ucl_object_emit_streamline_start_container (struct ucl_emitter_context *ctx,
92
const ucl_object_t *obj)
93
{
94
struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
95
struct ucl_emitter_streamline_stack *st, *top;
96
bool print_key = false;
97
98
/* Check top object presence */
99
if (sctx->top == NULL) {
100
sctx->top = obj;
101
}
102
103
top = sctx->containers;
104
st = malloc (sizeof (*st));
105
if (st != NULL) {
106
st->empty = true;
107
if (top && !top->is_array) {
108
print_key = true;
109
}
110
111
st->obj = obj;
112
if (obj != NULL && obj->type == UCL_ARRAY) {
113
st->is_array = true;
114
sctx->ops->ucl_emitter_start_array (ctx, obj, top == NULL, print_key);
115
}
116
else {
117
st->is_array = false;
118
sctx->ops->ucl_emitter_start_object (ctx, obj, top == NULL, print_key);
119
}
120
LL_PREPEND (sctx->containers, st);
121
}
122
}
123
124
void
125
ucl_object_emit_streamline_add_object (
126
struct ucl_emitter_context *ctx, const ucl_object_t *obj)
127
{
128
struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
129
bool is_array = false, is_first = false;
130
131
if (sctx->containers != NULL) {
132
if (sctx->containers->is_array) {
133
is_array = true;
134
}
135
if (sctx->containers->empty) {
136
is_first = true;
137
sctx->containers->empty = false;
138
}
139
}
140
141
sctx->ops->ucl_emitter_write_elt (ctx, obj, is_first, !is_array);
142
}
143
144
void
145
ucl_object_emit_streamline_end_container (struct ucl_emitter_context *ctx)
146
{
147
struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
148
struct ucl_emitter_streamline_stack *st;
149
150
if (sctx->containers != NULL) {
151
st = sctx->containers;
152
153
if (st->is_array) {
154
sctx->ops->ucl_emitter_end_array (ctx, st->obj);
155
}
156
else {
157
sctx->ops->ucl_emitter_end_object (ctx, st->obj);
158
}
159
sctx->containers = st->next;
160
free (st);
161
}
162
}
163
164
void
165
ucl_object_emit_streamline_finish (struct ucl_emitter_context *ctx)
166
{
167
struct ucl_emitter_context_streamline *sctx = TO_STREAMLINE(ctx);
168
169
while (sctx->containers != NULL) {
170
ucl_object_emit_streamline_end_container (ctx);
171
}
172
173
free (sctx);
174
}
175
176