Remove unnecessary mutex locks from tst-tls-atexit test case
[glibc.git] / benchtests / json-lib.c
blob823cc32a42adcd670cbcea57610518e7b5d8caf2
1 /* Simple library for printing JSON data.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #include <string.h>
21 #include "json-lib.h"
23 void
24 json_init (json_ctx_t *ctx, unsigned int indent_level, FILE *fp)
26 ctx->indent_level = indent_level;
27 ctx->fp = fp;
28 ctx->first_element = true;
31 static void
32 do_indent (json_ctx_t *ctx)
34 char indent_buf[ctx->indent_level + 1];
36 memset (indent_buf, ' ', ctx->indent_level + 1);
37 indent_buf[ctx->indent_level] = '\0';
39 fputs (indent_buf, ctx->fp);
42 void
43 json_document_begin (json_ctx_t *ctx)
45 do_indent (ctx);
47 fputs ("{\n", ctx->fp);
49 ctx->indent_level++;
50 ctx->first_element = true;
53 void
54 json_document_end (json_ctx_t *ctx)
56 ctx->indent_level--;
58 do_indent (ctx);
60 fputs ("\n}", ctx->fp);
63 void
64 json_attr_object_begin (json_ctx_t *ctx, const char *name)
66 if (!ctx->first_element)
67 fprintf (ctx->fp, ",\n");
69 do_indent (ctx);
71 fprintf (ctx->fp, "\"%s\": {\n", name);
73 ctx->indent_level++;
74 ctx->first_element = true;
77 void
78 json_attr_object_end (json_ctx_t *ctx)
80 ctx->indent_level--;
81 ctx->first_element = false;
83 fputs ("\n", ctx->fp);
85 do_indent (ctx);
87 fputs ("}", ctx->fp);
90 void
91 json_attr_string (json_ctx_t *ctx, const char *name, const char *s)
93 if (!ctx->first_element)
94 fprintf (ctx->fp, ",\n");
95 else
96 ctx->first_element = false;
98 do_indent (ctx);
100 fprintf (ctx->fp, "\"%s\": \"%s\"", name, s);
103 void
104 json_attr_double (json_ctx_t *ctx, const char *name, double d)
106 if (!ctx->first_element)
107 fprintf (ctx->fp, ",\n");
108 else
109 ctx->first_element = false;
111 do_indent (ctx);
113 fprintf (ctx->fp, "\"%s\": %g", name, d);
116 void
117 json_array_begin (json_ctx_t *ctx, const char *name)
119 if (!ctx->first_element)
120 fprintf (ctx->fp, ",\n");
122 do_indent (ctx);
124 fprintf (ctx->fp, "\"%s\": [", name);
126 ctx->indent_level++;
127 ctx->first_element = true;
130 void
131 json_array_end (json_ctx_t *ctx)
133 ctx->indent_level--;
134 ctx->first_element = false;
136 fputs ("]", ctx->fp);
139 void
140 json_element_double (json_ctx_t *ctx, double d)
142 if (!ctx->first_element)
143 fprintf (ctx->fp, ", %g", d);
144 else
146 fprintf (ctx->fp, "%g", d);
147 ctx->first_element = false;
151 void
152 json_element_object_begin (json_ctx_t *ctx)
154 if (!ctx->first_element)
155 fprintf (ctx->fp, ",");
157 fputs ("\n", ctx->fp);
159 do_indent (ctx);
161 fputs ("{\n", ctx->fp);
163 ctx->indent_level++;
164 ctx->first_element = true;
167 void
168 json_element_object_end (json_ctx_t *ctx)
170 ctx->indent_level--;
171 ctx->first_element = false;
173 fputs ("\n", ctx->fp);
175 do_indent (ctx);
177 fputs ("}", ctx->fp);