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