Skip to content

Commit aecab4c

Browse files
committed
lib/json.c: names and documentation
1 parent 17182be commit aecab4c

File tree

4 files changed

+117
-13
lines changed

4 files changed

+117
-13
lines changed

include/xbps/json.h

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* SPDX-FileCopyrightText: Copyright 2023 Duncan Overbruck <[email protected]> */
22
/* SPDX-License-Identifier: BSD-2-Clause */
3+
34
#ifndef _XBPS_JSON_H_
45
#define _XBPS_JSON_H_
56

@@ -13,22 +14,126 @@
1314
#include <xbps/xbps_object.h>
1415
#include <xbps/xbps_string.h>
1516

17+
/** @addtogroup json */
18+
/**@{*/
19+
20+
/**
21+
* @struct xbps_json_printer xbps/json.h <xbps/json.h>
22+
* @brief Structure holding state while printing json.
23+
*/
1624
struct xbps_json_printer {
25+
/**
26+
* @var file
27+
* @brief Output file to print to.
28+
*/
1729
FILE *file;
30+
/**
31+
* @var depth
32+
* @brief The current depth inside objects or arrays.
33+
*/
1834
unsigned depth;
35+
/**
36+
* @var indent
37+
* @brief Number of indent spaces per depth.
38+
*/
1939
uint8_t indent;
40+
/**
41+
* @var compact
42+
* @brief Compact mode removes unnecessary spaces.
43+
*/
2044
bool compact;
2145
};
2246

23-
int xbps_json_print_escape(struct xbps_json_printer *p, const char *s);
24-
int xbps_json_print_quote(struct xbps_json_printer *p, const char *s);
47+
/**
48+
* @brief Escape and write the string \a s to the json file.
49+
*
50+
* @param[in] p Json print context.
51+
* @param[in] s The string to write.
52+
*
53+
* @return 0 on success or a negative errno from fprintf(3).
54+
*/
55+
int xbps_json_print_escaped(struct xbps_json_printer *p, const char *s);
56+
57+
/**
58+
* @brief Write the string \a s as quoted string to the json file.
59+
*
60+
* @param[in] p Json print context.
61+
* @param[in] s The string to write.
62+
*
63+
* @return 0 on success or a negative errno from fprintf(3).
64+
*/
65+
int xbps_json_print_quoted(struct xbps_json_printer *p, const char *s);
66+
67+
/**
68+
* @brief Write boolean to the json stream.
69+
*
70+
* @param[in] p Json print context.
71+
* @param[in] b Boolean value.
72+
*
73+
* @return 0 on success or a negative errno from fprintf(3).
74+
*/
2575
int xbps_json_print_bool(struct xbps_json_printer *p, bool b);
2676

77+
/**
78+
* @brief Write a ::xbps_string_t to the json stream.
79+
*
80+
* @param[in] p Json print context.
81+
* @param[in] str String value to print.
82+
*
83+
* @return 0 on success or a negative errno from fprintf(3).
84+
*/
2785
int xbps_json_print_xbps_string(struct xbps_json_printer *p, xbps_string_t str);
86+
87+
/**
88+
* @brief Write a ::xbps_number_t to the json stream.
89+
*
90+
* @param[in] p Json print context.
91+
* @param[in] num Number value to print.
92+
*
93+
* @return 0 on success or a negative errno from fprintf(3).
94+
*/
2895
int xbps_json_print_xbps_number(struct xbps_json_printer *p, xbps_number_t num);
96+
97+
/**
98+
* @brief Write a ::xbps_boolean_t to the json stream.
99+
*
100+
* @param[in] p Json print context.
101+
* @param[in] b Boolean value to print.
102+
*
103+
* @return 0 on success or a negative errno from fprintf(3).
104+
*/
29105
int xbps_json_print_xbps_boolean(struct xbps_json_printer *p, xbps_bool_t b);
106+
107+
/**
108+
* @brief Write a ::xbps_array_t to the json stream.
109+
*
110+
* @param[in] p Json print context.
111+
* @param[in] array Array to print.
112+
*
113+
* @return 0 on success or a negative errno from fprintf(3).
114+
*/
30115
int xbps_json_print_xbps_array(struct xbps_json_printer *p, xbps_array_t array);
116+
117+
/**
118+
* @brief Write a ::xbps_dictionary_t to the json stream.
119+
*
120+
* @param[in] p Json print context.
121+
* @param[in] dict Dictionary to print.
122+
*
123+
* @return 0 on success or a negative errno from fprintf(3).
124+
*/
31125
int xbps_json_print_xbps_dictionary(struct xbps_json_printer *p, xbps_dictionary_t dict);
126+
127+
/**
128+
* @brief Write a ::xbps_object_t to the json stream.
129+
*
130+
* @param[in] p Json print context.
131+
* @param[in] obj Object to print.
132+
*
133+
* @return 0 on success or a negative errno from fprintf(3).
134+
*/
32135
int xbps_json_print_xbps_object(struct xbps_json_printer *p, xbps_object_t obj);
33136

137+
/**@}*/
138+
34139
#endif /* !_XBPS_JSON_H_ */

lib/format.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ xbps_fmt_print_string(const struct xbps_fmt_var *var, const char *str, size_t le
666666

667667
if (var->conv && var->conv->type == JSON) {
668668
struct xbps_json_printer pr = {.file = fp};
669-
return xbps_json_print_quote(&pr, str);
669+
return xbps_json_print_quoted(&pr, str);
670670
}
671671

672672
if (len == 0)

lib/json.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
#include <stdint.h>
1111
#include <stdio.h>
1212

13+
#include "xbps/json.h"
1314
#include "xbps/xbps_array.h"
1415
#include "xbps/xbps_bool.h"
1516
#include "xbps/xbps_dictionary.h"
1617
#include "xbps/xbps_number.h"
1718
#include "xbps/xbps_object.h"
1819
#include "xbps/xbps_string.h"
1920

20-
#include "xbps/json.h"
21-
2221
static int __attribute__ ((format (printf, 2, 3)))
2322
xbps_json_printf(struct xbps_json_printer *p, const char *fmt, ...)
2423
{
@@ -32,7 +31,7 @@ xbps_json_printf(struct xbps_json_printer *p, const char *fmt, ...)
3231
}
3332

3433
int
35-
xbps_json_print_escape(struct xbps_json_printer *p, const char *s)
34+
xbps_json_print_escaped(struct xbps_json_printer *p, const char *s)
3635
{
3736
int r = 0;
3837
for (; r >= 0 && *s; s++) {
@@ -56,12 +55,12 @@ xbps_json_print_escape(struct xbps_json_printer *p, const char *s)
5655
}
5756

5857
int
59-
xbps_json_print_quote(struct xbps_json_printer *p, const char *s)
58+
xbps_json_print_quoted(struct xbps_json_printer *p, const char *s)
6059
{
6160
int r;
6261
if ((r = xbps_json_printf(p, "\"")) < 0)
6362
return r;
64-
if ((r = xbps_json_print_escape(p, s)) < 0)
63+
if ((r = xbps_json_print_escaped(p, s)) < 0)
6564
return r;
6665
return xbps_json_printf(p, "\"");
6766
}
@@ -75,7 +74,7 @@ xbps_json_print_bool(struct xbps_json_printer *p, bool b)
7574
int
7675
xbps_json_print_xbps_string(struct xbps_json_printer *p, xbps_string_t str)
7776
{
78-
return xbps_json_print_quote(p, xbps_string_cstring_nocopy(str));
77+
return xbps_json_print_quoted(p, xbps_string_cstring_nocopy(str));
7978
}
8079

8180
int
@@ -164,7 +163,7 @@ xbps_json_print_xbps_dictionary(struct xbps_json_printer *p, xbps_dictionary_t d
164163
}
165164

166165
key = xbps_dictionary_keysym_cstring_nocopy(keysym);
167-
if ((r = xbps_json_print_quote(p, key)) < 0)
166+
if ((r = xbps_json_print_quoted(p, key)) < 0)
168167
goto err;
169168
if ((r = xbps_json_printf(p, "%s", key_sep)) < 0)
170169
goto err;

tests/xbps/libxbps/json/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ ATF_TC_BODY(xbps_json_print_escape, tc)
2727

2828
ATF_REQUIRE(p.file = open_memstream(&buf, &bufsz));
2929

30-
ATF_CHECK_EQ(0, xbps_json_print_escape(&p, "\"\\\b\f\n\r\t"));
30+
ATF_CHECK_EQ(0, xbps_json_print_escaped(&p, "\"\\\b\f\n\r\t"));
3131
ATF_REQUIRE_EQ(0, fflush(p.file));
3232
ATF_CHECK_STREQ("\\\"\\\\\\b\\f\\n\\r\\t", buf);
3333

3434
memset(buf, '\0', bufsz);
3535
ATF_CHECK_EQ(0, fseek(p.file, 0, SEEK_SET));
36-
ATF_CHECK_EQ(0, xbps_json_print_escape(&p, "09azAZ !$#%^()%"));
36+
ATF_CHECK_EQ(0, xbps_json_print_escaped(&p, "09azAZ !$#%^()%"));
3737
ATF_REQUIRE_EQ(0, fflush(p.file));
3838
ATF_CHECK_STREQ("09azAZ !$#%^()%", buf);
3939

4040
memset(buf, '\0', bufsz);
4141
ATF_CHECK_EQ(0, fseek(p.file, 0, SEEK_SET));
42-
ATF_CHECK_EQ(0, xbps_json_print_escape(&p, "\x01\x1F"));
42+
ATF_CHECK_EQ(0, xbps_json_print_escaped(&p, "\x01\x1F"));
4343
ATF_REQUIRE_EQ(0, fflush(p.file));
4444
ATF_CHECK_STREQ("\\u0001\\u001f", buf);
4545
}

0 commit comments

Comments
 (0)