-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformatter.c
158 lines (138 loc) · 2.88 KB
/
formatter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* multihash - compute hashes on collections of files
* Copyright (c) 2016 Nicolas George <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "formatter.h"
struct Formatter {
unsigned depth;
unsigned char has_items;
};
int
formatter_alloc(Formatter **rfmt)
{
Formatter *fmt;
fmt = malloc(sizeof(*fmt));
if (fmt == NULL) {
perror("malloc");
return -1;
}
*rfmt = fmt;
return 0;
}
void
formatter_free(Formatter **rfmt)
{
free(*rfmt);
*rfmt = NULL;
}
static void
separator(Formatter *fmt, unsigned final)
{
unsigned i;
if (fmt->has_items && !final)
putc(',', stdout);
putc('\n', stdout);
for (i = fmt->depth * 3; i > 0; i--)
putc(' ', stdout);
fmt->has_items = 1;
}
void
formatter_open(Formatter *fmt)
{
fmt->depth = 0;
}
int
formatter_close(Formatter *fmt)
{
assert(fmt->depth == 0);
putc('\n', stdout);
fflush(stdout);
return ferror(stdout);
}
void
formatter_dict_open(Formatter *fmt)
{
putc('{', stdout);
fmt->has_items = 0;
fmt->depth++;
}
void
formatter_dict_close(Formatter *fmt)
{
fmt->depth--;
separator(fmt, 1);
fprintf(stdout, "}");
}
void
formatter_dict_item(Formatter *fmt, const char *key)
{
separator(fmt, 0);
formatter_string(fmt, key);
fputs(" : ", stdout);
}
void
formatter_array_open(Formatter *fmt)
{
putc('[', stdout);
fmt->has_items = 0;
fmt->depth++;
}
void
formatter_array_close(Formatter *fmt)
{
fmt->depth--;
separator(fmt, 1);
fprintf(stdout, "]");
}
void
formatter_array_item(Formatter *fmt)
{
separator(fmt, 0);
}
void
formatter_string(Formatter *fmt, const unsigned char *str)
{
static const char plain[] = "\"\\\x08\x0C\x0A\x0D\x09";
static const char esc[] = "\"\\bfnrt";
char *match;
assert(strlen(plain) == strlen(esc));
(void)fmt;
putc('"', stdout);
for (; *str; str++) {
match = strchr(plain, *str);
if (match != NULL) {
putc('\\', stdout);
putc(esc[match - plain], stdout);
} else if (*str < 32) {
fprintf(stdout, "\\u%04x", *str);
} else {
putc(*str, stdout);
}
}
putc('"', stdout);
}
void
formatter_integer(Formatter *fmt, intmax_t x)
{
(void)fmt;
fprintf(stdout, "%jd", x);
}
void
formatter_bool(Formatter *fmt, int x)
{
(void)fmt;
fprintf(stdout, "%s", x ? "true" : "false");
}