-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindent.cc
More file actions
155 lines (138 loc) · 3.82 KB
/
indent.cc
File metadata and controls
155 lines (138 loc) · 3.82 KB
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
#include <json.h>
#include <fstream>
#include <cstring>
#include <iostream>
#include <unistd.h>
using namespace JSON;
using namespace std;
static int indentLevel = 2;
const char *pad(size_t indent) {
static size_t maxindent = 8192;
static const char *spaces = strdup(string(maxindent, ' ').c_str());
indent = min(indentLevel * indent, maxindent);
return spaces + maxindent - indent;
}
template <typename numtype>
static void pretty(istream &i, ostream &o, size_t indent);
template <typename numtype> void
prettyArray(istream &i, ostream &o, size_t indent)
{
o << "[";
size_t eleCount = 0;
parseArray(i, [=, &eleCount, &o] (istream &i) -> void {
o << (eleCount++ ? "," : "") << "\n" << pad(indent + 1);
pretty<numtype>(i, o, indent+1);
});
if (eleCount)
o << "\n" << pad(indent);
o << "]";
}
template <typename numtype> static void
prettyObject(istream &i, ostream &o, size_t indent)
{
o << "{";
int eleCount = 0;
parseObject(i, [=, &eleCount, &o] (istream &i, string idx) -> void {
if (eleCount++ != 0)
o << ",";
o << "\n" << pad(indent + 1) << "\"" << Escape(idx) << "\": ";
pretty<numtype>(i, o, indent + 1);
});
if (eleCount)
o << "\n" << pad(indent);
o << "}";
}
static void
prettyString(istream &i, ostream &o, size_t indent)
{
o << "\"" << Escape(parseString(i)) << "\"";
}
static void
prettyNull(istream &i, ostream &o, size_t indent)
{
parseNull(i);
o << "null";
}
template <typename numtype> static void
prettyNumber(istream &i, ostream &o, size_t indent)
{
o << parseNumber<numtype>(i);
}
static void
prettyBoolean(istream &i, ostream &o, size_t indent)
{
o << (parseBoolean(i) ? "true" : "false");
}
template <typename numtype> static void
pretty(istream &i, ostream &o, size_t indent)
{
switch (peekType(i)) {
case Array: prettyArray<numtype>(i, o, indent); return;
case Object: prettyObject<numtype>(i, o, indent); return;
case String: prettyString(i, o, indent); return;
case Number: prettyNumber<numtype>(i, o, indent); return;
case Boolean: prettyBoolean(i, o, indent); return;
case Null: prettyNull(i, o, indent); return;
case Eof: return;
}
}
static int
usage() {
clog << "usage: jdent [ -f ] [ files ... ]" << endl;
return -1;
}
static bool doFloat;
static bool
indent(istream &in, ostream &out)
{
static unsigned char bom[] = { 0xef, 0xbb, 0xbf };
// Deal with UTF-8 BOM mark. (Lordy, why would you do that?)
if (in.peek() == bom[0]) {
char s[sizeof bom + 1];
in.get(s, sizeof s);
if (memcmp(s, bom, sizeof bom) != 0)
throw InvalidJSON("invalid BOM/JSON");
}
try {
if (doFloat)
pretty<double> (in, out, 0);
else
pretty<long> (in, out, 0);
cout << endl;
return true;
}
catch (const InvalidJSON &je) {
cerr << "invalid JSON: " << je.what() << endl;
return false;
}
}
int
main(int argc, char *argv[])
{
cin.tie(0);
int c;
while ((c = getopt(argc, argv, "fi:")) != -1) {
switch (c) {
case 'f': doFloat = true; break;
case 'i': indentLevel = strtoul(optarg, 0, 0); break;
default: return usage();
}
}
bool good = true;
for (int i = optind; i < argc; ++i) {
if (strcmp(argv[i], "-") != 0) {
ifstream inFile;
inFile.open(argv[i]);
if (inFile.good())
good = good && indent(inFile, cout);
else
clog << "failed to open " << argv[i]
<< ": " << strerror(errno) << endl;
} else {
good = good && indent(cin, cout);
}
}
if (optind == argc)
good = indent(cin, cout);
return good ? 0 : 1;
}