Devin Jeanpierre | 59e4d35 | 2017-07-21 03:44:36 -0700 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 2 | // Distributed under MIT license, or public domain if desired and |
| 3 | // recognized in your jurisdiction. |
| 4 | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
| 5 | |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 6 | #if defined(__GNUC__) |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 7 | #pragma GCC diagnostic push |
| 8 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 9 | #elif defined(_MSC_VER) |
| 10 | #pragma warning(disable : 4996) |
| 11 | #endif |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 12 | |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 13 | /* This executable is used for testing parser/writer using real JSON files. |
| 14 | */ |
| 15 | |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 16 | #include <algorithm> // sort |
Billy Donahue | dc4a7f9 | 2019-01-17 11:07:53 -0500 | [diff] [blame^] | 17 | #include <cstdio> |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 18 | #include <json/json.h> |
Christopher Dunn | 2160c9a | 2015-01-23 09:02:44 -0600 | [diff] [blame] | 19 | #include <sstream> |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 20 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 21 | struct Options { |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 22 | JSONCPP_STRING path; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 23 | Json::Features features; |
| 24 | bool parseOnly; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 25 | typedef JSONCPP_STRING (*writeFuncType)(Json::Value const&); |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 26 | writeFuncType write; |
| 27 | }; |
| 28 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 29 | static JSONCPP_STRING normalizeFloatingPointStr(double value) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 30 | char buffer[32]; |
Hans Johnson | 5c8e539 | 2018-12-12 13:31:55 -0600 | [diff] [blame] | 31 | jsoncpp_snprintf(buffer, sizeof(buffer), "%.16g", value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 32 | buffer[sizeof(buffer) - 1] = 0; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 33 | JSONCPP_STRING s(buffer); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 34 | JSONCPP_STRING::size_type index = s.find_last_of("eE"); |
| 35 | if (index != JSONCPP_STRING::npos) { |
| 36 | JSONCPP_STRING::size_type hasSign = |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 37 | (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 38 | JSONCPP_STRING::size_type exponentStartIndex = index + 1 + hasSign; |
| 39 | JSONCPP_STRING normalized = s.substr(0, exponentStartIndex); |
| 40 | JSONCPP_STRING::size_type indexDigit = |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 41 | s.find_first_not_of('0', exponentStartIndex); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 42 | JSONCPP_STRING exponent = "0"; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 43 | if (indexDigit != JSONCPP_STRING::npos) // There is an exponent different |
| 44 | // from 0 |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 45 | { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 46 | exponent = s.substr(indexDigit); |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 47 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 48 | return normalized + exponent; |
| 49 | } |
| 50 | return s; |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 53 | static JSONCPP_STRING readInputTestFile(const char* path) { |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 54 | FILE* file = fopen(path, "rb"); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 55 | if (!file) |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 56 | return JSONCPP_STRING(""); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 57 | fseek(file, 0, SEEK_END); |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 58 | long const size = ftell(file); |
Hans Johnson | 1fc3de7 | 2019-01-14 17:09:15 -0600 | [diff] [blame] | 59 | size_t const usize = static_cast<unsigned long>(size); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 60 | fseek(file, 0, SEEK_SET); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 61 | JSONCPP_STRING text; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 62 | char* buffer = new char[size + 1]; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 63 | buffer[size] = 0; |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 64 | if (fread(buffer, 1, usize, file) == usize) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 65 | text = buffer; |
| 66 | fclose(file); |
| 67 | delete[] buffer; |
| 68 | return text; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 71 | static void printValueTree(FILE* fout, |
| 72 | Json::Value& value, |
| 73 | const JSONCPP_STRING& path = ".") { |
Cory Quammen | 4d23492 | 2014-10-09 16:29:47 -0400 | [diff] [blame] | 74 | if (value.hasComment(Json::commentBefore)) { |
| 75 | fprintf(fout, "%s\n", value.getComment(Json::commentBefore).c_str()); |
| 76 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 77 | switch (value.type()) { |
| 78 | case Json::nullValue: |
| 79 | fprintf(fout, "%s=null\n", path.c_str()); |
| 80 | break; |
| 81 | case Json::intValue: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 82 | fprintf(fout, "%s=%s\n", path.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 83 | Json::valueToString(value.asLargestInt()).c_str()); |
| 84 | break; |
| 85 | case Json::uintValue: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 86 | fprintf(fout, "%s=%s\n", path.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 87 | Json::valueToString(value.asLargestUInt()).c_str()); |
| 88 | break; |
| 89 | case Json::realValue: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 90 | fprintf(fout, "%s=%s\n", path.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 91 | normalizeFloatingPointStr(value.asDouble()).c_str()); |
| 92 | break; |
| 93 | case Json::stringValue: |
| 94 | fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str()); |
| 95 | break; |
| 96 | case Json::booleanValue: |
| 97 | fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false"); |
| 98 | break; |
| 99 | case Json::arrayValue: { |
| 100 | fprintf(fout, "%s=[]\n", path.c_str()); |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 101 | Json::ArrayIndex size = value.size(); |
| 102 | for (Json::ArrayIndex index = 0; index < size; ++index) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 103 | static char buffer[16]; |
Hans Johnson | 5c8e539 | 2018-12-12 13:31:55 -0600 | [diff] [blame] | 104 | jsoncpp_snprintf(buffer, sizeof(buffer), "[%u]", index); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 105 | printValueTree(fout, value[index], path + buffer); |
| 106 | } |
| 107 | } break; |
| 108 | case Json::objectValue: { |
| 109 | fprintf(fout, "%s={}\n", path.c_str()); |
| 110 | Json::Value::Members members(value.getMemberNames()); |
| 111 | std::sort(members.begin(), members.end()); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 112 | JSONCPP_STRING suffix = *(path.end() - 1) == '.' ? "" : "."; |
Hans Johnson | cbeed7b | 2019-01-14 17:09:12 -0600 | [diff] [blame] | 113 | for (auto name : members) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 114 | printValueTree(fout, value[name], path + suffix + name); |
| 115 | } |
| 116 | } break; |
| 117 | default: |
| 118 | break; |
| 119 | } |
Cory Quammen | 4d23492 | 2014-10-09 16:29:47 -0400 | [diff] [blame] | 120 | |
| 121 | if (value.hasComment(Json::commentAfter)) { |
| 122 | fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str()); |
| 123 | } |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 126 | static int parseAndSaveValueTree(const JSONCPP_STRING& input, |
| 127 | const JSONCPP_STRING& actual, |
| 128 | const JSONCPP_STRING& kind, |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 129 | const Json::Features& features, |
Christopher Dunn | 632c9b5 | 2015-01-23 11:09:04 -0600 | [diff] [blame] | 130 | bool parseOnly, |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 131 | Json::Value* root) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 132 | Json::Reader reader(features); |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 133 | bool parsingSuccessful = |
| 134 | reader.parse(input.data(), input.data() + input.size(), *root); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 135 | if (!parsingSuccessful) { |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 136 | printf("Failed to parse %s file: \n%s\n", kind.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 137 | reader.getFormattedErrorMessages().c_str()); |
| 138 | return 1; |
| 139 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 140 | if (!parseOnly) { |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 141 | FILE* factual = fopen(actual.c_str(), "wt"); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 142 | if (!factual) { |
| 143 | printf("Failed to create %s actual file.\n", kind.c_str()); |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 144 | return 2; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 145 | } |
Christopher Dunn | 632c9b5 | 2015-01-23 11:09:04 -0600 | [diff] [blame] | 146 | printValueTree(factual, *root); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 147 | fclose(factual); |
| 148 | } |
| 149 | return 0; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 150 | } |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 151 | // static JSONCPP_STRING useFastWriter(Json::Value const& root) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 152 | // Json::FastWriter writer; |
| 153 | // writer.enableYAMLCompatibility(); |
| 154 | // return writer.write(root); |
| 155 | // } |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 156 | static JSONCPP_STRING useStyledWriter(Json::Value const& root) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 157 | Json::StyledWriter writer; |
| 158 | return writer.write(root); |
| 159 | } |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 160 | static JSONCPP_STRING useStyledStreamWriter(Json::Value const& root) { |
Christopher Dunn | 2160c9a | 2015-01-23 09:02:44 -0600 | [diff] [blame] | 161 | Json::StyledStreamWriter writer; |
Christopher Dunn | 38bb491 | 2016-03-06 11:50:00 -0600 | [diff] [blame] | 162 | JSONCPP_OSTRINGSTREAM sout; |
Christopher Dunn | 2160c9a | 2015-01-23 09:02:44 -0600 | [diff] [blame] | 163 | writer.write(sout, root); |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 164 | return sout.str(); |
| 165 | } |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 166 | static JSONCPP_STRING useBuiltStyledStreamWriter(Json::Value const& root) { |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 167 | Json::StreamWriterBuilder builder; |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 168 | return Json::writeString(builder, root); |
Christopher Dunn | 9243d60 | 2015-01-23 08:38:32 -0600 | [diff] [blame] | 169 | } |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 170 | static int rewriteValueTree(const JSONCPP_STRING& rewritePath, |
| 171 | const Json::Value& root, |
| 172 | Options::writeFuncType write, |
| 173 | JSONCPP_STRING* rewrite) { |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 174 | *rewrite = write(root); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 175 | FILE* fout = fopen(rewritePath.c_str(), "wt"); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 176 | if (!fout) { |
| 177 | printf("Failed to create rewrite file: %s\n", rewritePath.c_str()); |
| 178 | return 2; |
| 179 | } |
Christopher Dunn | 632c9b5 | 2015-01-23 11:09:04 -0600 | [diff] [blame] | 180 | fprintf(fout, "%s\n", rewrite->c_str()); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 181 | fclose(fout); |
| 182 | return 0; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 185 | static JSONCPP_STRING removeSuffix(const JSONCPP_STRING& path, |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 186 | const JSONCPP_STRING& extension) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 187 | if (extension.length() >= path.length()) |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 188 | return JSONCPP_STRING(""); |
| 189 | JSONCPP_STRING suffix = path.substr(path.length() - extension.length()); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 190 | if (suffix != extension) |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 191 | return JSONCPP_STRING(""); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 192 | return path.substr(0, path.length() - extension.length()); |
| 193 | } |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 194 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 195 | static void printConfig() { |
| 196 | // Print the configuration used to compile JsonCpp |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 197 | #if defined(JSON_NO_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 198 | printf("JSON_NO_INT64=1\n"); |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 199 | #else |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 200 | printf("JSON_NO_INT64=0\n"); |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 201 | #endif |
| 202 | } |
| 203 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 204 | static int printUsage(const char* argv[]) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 205 | printf("Usage: %s [--strict] input-json-file", argv[0]); |
| 206 | return 3; |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 209 | static int parseCommandLine(int argc, const char* argv[], Options* opts) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 210 | opts->parseOnly = false; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 211 | opts->write = &useStyledWriter; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 212 | if (argc < 2) { |
| 213 | return printUsage(argv); |
| 214 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 215 | int index = 1; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 216 | if (JSONCPP_STRING(argv[index]) == "--json-checker") { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 217 | opts->features = Json::Features::strictMode(); |
| 218 | opts->parseOnly = true; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 219 | ++index; |
| 220 | } |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 221 | if (JSONCPP_STRING(argv[index]) == "--json-config") { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 222 | printConfig(); |
| 223 | return 3; |
| 224 | } |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 225 | if (JSONCPP_STRING(argv[index]) == "--json-writer") { |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 226 | ++index; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 227 | JSONCPP_STRING const writerName(argv[index++]); |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 228 | if (writerName == "StyledWriter") { |
| 229 | opts->write = &useStyledWriter; |
| 230 | } else if (writerName == "StyledStreamWriter") { |
| 231 | opts->write = &useStyledStreamWriter; |
Christopher Dunn | 9243d60 | 2015-01-23 08:38:32 -0600 | [diff] [blame] | 232 | } else if (writerName == "BuiltStyledStreamWriter") { |
| 233 | opts->write = &useBuiltStyledStreamWriter; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 234 | } else { |
| 235 | printf("Unknown '--json-writer %s'\n", writerName.c_str()); |
| 236 | return 4; |
| 237 | } |
| 238 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 239 | if (index == argc || index + 1 < argc) { |
| 240 | return printUsage(argv); |
| 241 | } |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 242 | opts->path = argv[index]; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 243 | return 0; |
| 244 | } |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 245 | static int runTest(Options const& opts) { |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 246 | int exitCode = 0; |
| 247 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 248 | JSONCPP_STRING input = readInputTestFile(opts.path.c_str()); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 249 | if (input.empty()) { |
| 250 | printf("Failed to read input or empty input: %s\n", opts.path.c_str()); |
| 251 | return 3; |
| 252 | } |
| 253 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 254 | JSONCPP_STRING basePath = removeSuffix(opts.path, ".json"); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 255 | if (!opts.parseOnly && basePath.empty()) { |
| 256 | printf("Bad input path. Path does not end with '.expected':\n%s\n", |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 257 | opts.path.c_str()); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 258 | return 3; |
| 259 | } |
| 260 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 261 | JSONCPP_STRING const actualPath = basePath + ".actual"; |
| 262 | JSONCPP_STRING const rewritePath = basePath + ".rewrite"; |
| 263 | JSONCPP_STRING const rewriteActualPath = basePath + ".actual-rewrite"; |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 264 | |
| 265 | Json::Value root; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 266 | exitCode = parseAndSaveValueTree(input, actualPath, "input", opts.features, |
| 267 | opts.parseOnly, &root); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 268 | if (exitCode || opts.parseOnly) { |
| 269 | return exitCode; |
| 270 | } |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 271 | JSONCPP_STRING rewrite; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 272 | exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 273 | if (exitCode) { |
| 274 | return exitCode; |
| 275 | } |
| 276 | Json::Value rewriteRoot; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 277 | exitCode = parseAndSaveValueTree(rewrite, rewriteActualPath, "rewrite", |
| 278 | opts.features, opts.parseOnly, &rewriteRoot); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 279 | if (exitCode) { |
| 280 | return exitCode; |
| 281 | } |
| 282 | return 0; |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 283 | } |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 284 | int main(int argc, const char* argv[]) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 285 | Options opts; |
Gaurav | 6c14548 | 2015-09-22 13:53:19 +0530 | [diff] [blame] | 286 | try { |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 287 | int exitCode = parseCommandLine(argc, argv, &opts); |
| 288 | if (exitCode != 0) { |
| 289 | printf("Failed to parse command-line."); |
| 290 | return exitCode; |
| 291 | } |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 292 | return runTest(opts); |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 293 | } catch (const std::exception& e) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 294 | printf("Unhandled exception:\n%s\n", e.what()); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 295 | return 1; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 296 | } |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 297 | } |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 298 | |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 299 | #if defined(__GNUC__) |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 300 | #pragma GCC diagnostic pop |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 301 | #endif |