-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
139 lines (122 loc) · 5.04 KB
/
main.cpp
File metadata and controls
139 lines (122 loc) · 5.04 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include "jsonparser.h"
#include "jsonformatter.h"
#include "jsonlinter.h"
const std::string APP_VERSION = "0.0.1";
void printUsage() {
std::cout <<
"Usage: jsonify [options] <file.json>\n"
"Options:\n"
" --lint Lint the JSON file\n"
" --format Pretty-print the JSON file\n"
" --fix, -f Attempt to auto-correct the JSON\n"
" --quiet, -q. Suppress success messages\n"
" --compact Compact output (no newlines/indent)\n"
" --indent N Indent width (default 2)\n"
" --jsonc Allow comments (JSONC)\n"
" --color Enable color output (default)\n"
" --no-color Disable color output\n"
" -v, --version Show version information\n"
" --help Show this help\n";
}
int main(int argc, char* argv[]) {
if (argc < 2) { printUsage(); return 1; }
bool doLint = false, doFormat = false, compact = false, jsonc = false, doFix = false, doQuiet = false;
bool useColor = true;
// What is the purpose of this variable? It is not used and causes compile error
// bool colorSpecified = false;
int indent = 2;
std::string filename;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--version" || arg == "-version" || arg == "-v" || arg == "-V") {
std::cout << "jsonify v" << APP_VERSION << " (Semantic Versioning)\n";
return 0;
}
if (arg == "--lint") doLint = true;
else if (arg == "--format") doFormat = true;
else if (arg == "--compact") compact = true;
else if (arg == "--jsonc") jsonc = true;
else if (arg == "--fix" || arg == "-f" ) {
doFix = true;
}
else if (arg == "--quiet" || arg == "-q") {
doQuiet = true;
}
else if (arg == "--color") { useColor = true; /*colorSpecified = true;*/ }
else if (arg == "--no-color") { useColor = false; /*colorSpecified = true;*/ }
else if (arg == "--indent" && i+1 < argc) { indent = std::stoi(argv[++i]); }
else if (arg == "--help") { printUsage(); return 0; }
else if (arg[0] != '-') filename = arg;
else { std::cerr << "Unknown option: " << arg << '\n'; return 1; }
}
if (filename.empty()) { std::cerr << "No input file.\n"; return 1; }
try {
std::ifstream f(filename);
if (!f) throw std::runtime_error("Cannot open " + filename);
std::stringstream buf; buf << f.rdbuf();
std::string src = buf.str();
// ---- JSONC comment stripping ----
if (jsonc) {
std::string clean;
bool inStr = false, esc = false;
for (size_t i = 0; i < src.size(); ++i) {
char c = src[i];
if (esc) { clean += c; esc = false; continue; }
if (c == '\\') { esc = true; clean += c; continue; }
if (c == '"' && !esc) inStr = !inStr;
if (!inStr && i+1 < src.size()) {
if (src[i] == '/' && src[i+1] == '/') { // // comment
i = src.find('\n', i) ; if (i == std::string::npos) i = src.size()-1;
continue;
}
if (src[i] == '/' && src[i+1] == '*') { // /* */
size_t end = src.find("*/", i+2);
i = (end == std::string::npos) ? src.size()-1 : end+1;
continue;
}
}
clean += c;
}
src = clean;
}
if (doFix) {
src = correctJson(src);
}
// ---- Parse ----
auto root = JsonParser::parse(src);
// ---- Lint ----
if (doLint) {
auto issues = lintJson(root, src);
if (issues.empty()) {
if (!doQuiet) std::cout << "No lint issues.\n";
} else {
for (const auto& iss : issues) {
std::string sev;
switch (iss.severity) {
case JsonLintIssue::Severity::Error: sev = "Error"; break;
case JsonLintIssue::Severity::Warning: sev = "Warning"; break;
case JsonLintIssue::Severity::Info: sev = "Info"; break;
}
std::cout << sev << ": " << iss.message;
if (iss.line != -1)
std::cout << " (line " << iss.line << ", col " << iss.column << ")";
std::cout << '\n';
}
}
}
// ---- Format ----
if (doFormat) {
printJson(root, std::cout, 0, indent, compact, useColor);
std::cout << '\n';
}
if (!doLint && !doFormat && doQuiet) std::cout << "Parsed successfully.\n";
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << '\n';
return 1;
}
return 0;
}