-
Notifications
You must be signed in to change notification settings - Fork 0
/
juu.c
111 lines (92 loc) · 1.86 KB
/
juu.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
#include <stdio.h>
#include <stdlib.h> /* EXIT_SUCCESS */
/* Buffer size for reading from stdin */
#define BUFFER_SIZE (1024 * 4)
static void outc(char ch) {
if (EOF == putchar(ch)) {
exit(EXIT_FAILURE);
}
}
static void fputs_repeat(const char *str, size_t count) {
while (count-- > 0) {
int value = fputs(str, stdout);
if (value == EOF) {
exit(EXIT_FAILURE);
}
}
}
int main(int argc, char **argv) {
char buffer[BUFFER_SIZE];
const char placeholder[] = " ";
unsigned int indent = 0;
char is_string = 0;
char escaped = 0;
/* Read stdin until EOF */
while (!feof(stdin)) {
size_t n = fread(&buffer, sizeof(char), BUFFER_SIZE, stdin);
if (n != BUFFER_SIZE && ferror(stdin)) {
/* Error reading stdin */
exit(EXIT_FAILURE);
}
for (size_t k = 0; k < n; k++) {
char ch = buffer[k];
if (is_string) {
/* Inside quoted string */
outc(ch);
if (! escaped) {
if (ch == '"') {
/* Unescaped quote, string just ended */
is_string = 0;
} else if (ch == '\\') {
escaped = 1;
}
} else {
escaped = 0;
}
continue;
}
switch (ch) {
case ' ':
case '\t':
case '\n':
case '\r':
/* Ignoring original formatting */
break;
case '{':
case '[':
outc(ch);
outc('\n');
indent++;
fputs_repeat(placeholder, indent);
break;
case '}':
case ']':
outc('\n');
indent--;
fputs_repeat(placeholder, indent);
outc(ch);
if (indent == 0) outc('\n');
break;
case ',':
outc(',');
outc('\n');
fputs_repeat(placeholder, indent);
break;
case ':':
outc(':');
outc(' ');
break;
case '"':
/* String/property key start, see if clause on top */
outc('"');
is_string = 1;
break;
default:
/* Numbers, true, false, null */
outc(ch);
break;
}
}
}
return EXIT_SUCCESS;
}