benchtests: Add pthread-mutex-locks bench
[platform/upstream/glibc.git] / benchtests / json-lib.c
1 /* Simple library for printing JSON data.
2    Copyright (C) 2014-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18
19 #include <string.h>
20
21 #include "json-lib.h"
22
23 void
24 json_init (json_ctx_t *ctx, unsigned int indent_level, FILE *fp)
25 {
26   ctx->indent_level = indent_level;
27   ctx->fp = fp;
28   ctx->first_element = true;
29 }
30
31 static void
32 do_indent (json_ctx_t *ctx)
33 {
34   char indent_buf[ctx->indent_level + 1];
35
36   memset (indent_buf, ' ', ctx->indent_level + 1);
37   indent_buf[ctx->indent_level] = '\0';
38
39   fputs (indent_buf, ctx->fp);
40 }
41
42 void
43 json_document_begin (json_ctx_t *ctx)
44 {
45   do_indent (ctx);
46
47   fputs ("{\n", ctx->fp);
48
49   ctx->indent_level++;
50   ctx->first_element = true;
51 }
52
53 void
54 json_document_end (json_ctx_t *ctx)
55 {
56   ctx->indent_level--;
57
58   do_indent (ctx);
59
60   fputs ("\n}", ctx->fp);
61 }
62
63 void
64 json_attr_object_begin (json_ctx_t *ctx, const char *name)
65 {
66   if (!ctx->first_element)
67     fprintf (ctx->fp, ",\n");
68
69   do_indent (ctx);
70
71   fprintf (ctx->fp, "\"%s\": {\n", name);
72
73   ctx->indent_level++;
74   ctx->first_element = true;
75 }
76
77 void
78 json_attr_object_end (json_ctx_t *ctx)
79 {
80   ctx->indent_level--;
81   ctx->first_element = false;
82
83   fputs ("\n", ctx->fp);
84
85   do_indent (ctx);
86
87   fputs ("}", ctx->fp);
88 }
89
90 void
91 json_attr_string (json_ctx_t *ctx, const char *name, const char *s)
92 {
93   if (!ctx->first_element)
94     fprintf (ctx->fp, ",\n");
95   else
96     ctx->first_element = false;
97
98   do_indent (ctx);
99
100   fprintf (ctx->fp, "\"%s\": \"%s\"", name, s);
101 }
102
103 void
104 json_attr_uint (json_ctx_t *ctx, const char *name, uint64_t d)
105 {
106   if (!ctx->first_element)
107     fprintf (ctx->fp, ",\n");
108   else
109     ctx->first_element = false;
110
111   do_indent (ctx);
112
113   fprintf (ctx->fp, "\"%s\": %" PRIu64 , name, d);
114 }
115
116 void
117 json_attr_int (json_ctx_t *ctx, const char *name, int64_t d)
118 {
119   if (!ctx->first_element)
120     fprintf (ctx->fp, ",\n");
121   else
122     ctx->first_element = false;
123
124   do_indent (ctx);
125
126   fprintf (ctx->fp, "\"%s\": %" PRId64 , name, d);
127 }
128
129 void
130 json_attr_double (json_ctx_t *ctx, const char *name, double d)
131 {
132   if (!ctx->first_element)
133     fprintf (ctx->fp, ",\n");
134   else
135     ctx->first_element = false;
136
137   do_indent (ctx);
138
139   fprintf (ctx->fp, "\"%s\": %g", name, d);
140 }
141
142 void
143 json_array_begin (json_ctx_t *ctx, const char *name)
144 {
145   if (!ctx->first_element)
146     fprintf (ctx->fp, ",\n");
147
148   do_indent (ctx);
149
150   fprintf (ctx->fp, "\"%s\": [", name);
151
152   ctx->indent_level++;
153   ctx->first_element = true;
154 }
155
156 void
157 json_array_end (json_ctx_t *ctx)
158 {
159   ctx->indent_level--;
160   ctx->first_element = false;
161
162   fputs ("]", ctx->fp);
163 }
164
165 void
166 json_element_string (json_ctx_t *ctx, const char *s)
167 {
168   if (!ctx->first_element)
169     fprintf (ctx->fp, ", \"%s\"", s);
170   else
171     {
172       fprintf (ctx->fp, "\"%s\"", s);
173       ctx->first_element = false;
174     }
175 }
176
177 void
178 json_element_uint (json_ctx_t *ctx, uint64_t d)
179 {
180   if (!ctx->first_element)
181     fprintf (ctx->fp, ", %" PRIu64, d);
182   else
183     {
184       fprintf (ctx->fp, "%" PRIu64, d);
185       ctx->first_element = false;
186     }
187 }
188
189 void
190 json_element_int (json_ctx_t *ctx, int64_t d)
191 {
192   if (!ctx->first_element)
193     fprintf (ctx->fp, ", %" PRId64, d);
194   else
195     {
196       fprintf (ctx->fp, "%" PRId64, d);
197       ctx->first_element = false;
198     }
199 }
200
201 void
202 json_element_double (json_ctx_t *ctx, double d)
203 {
204   if (!ctx->first_element)
205     fprintf (ctx->fp, ", %g", d);
206   else
207     {
208       fprintf (ctx->fp, "%g", d);
209       ctx->first_element = false;
210     }
211 }
212
213 void
214 json_element_object_begin (json_ctx_t *ctx)
215 {
216   if (!ctx->first_element)
217     fprintf (ctx->fp, ",");
218
219   fputs ("\n", ctx->fp);
220
221   do_indent (ctx);
222
223   fputs ("{\n", ctx->fp);
224
225   ctx->indent_level++;
226   ctx->first_element = true;
227 }
228
229 void
230 json_element_object_end (json_ctx_t *ctx)
231 {
232   ctx->indent_level--;
233   ctx->first_element = false;
234
235   fputs ("\n", ctx->fp);
236
237   do_indent (ctx);
238
239   fputs ("}", ctx->fp);
240 }