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 | 1c2ed7a | 2019-01-17 16:35:29 -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); |
| 40 | Json::String::size_type indexDigit = s.find_first_not_of('0', exponentStartIndex); |
| 41 | Json::String exponent = "0"; |
| 42 | if (indexDigit != Json::String::npos) // There is an exponent different |
| 43 | // from 0 |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 44 | { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 45 | exponent = s.substr(indexDigit); |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 46 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 47 | return normalized + exponent; |
| 48 | } |
| 49 | return s; |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 52 | static Json::String readInputTestFile(const char* path) { |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 53 | FILE* file = fopen(path, "rb"); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 54 | if (!file) |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 55 | return ""; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 56 | fseek(file, 0, SEEK_END); |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 57 | long const size = ftell(file); |
Hans Johnson | 1fc3de7 | 2019-01-14 17:09:15 -0600 | [diff] [blame] | 58 | size_t const usize = static_cast<unsigned long>(size); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 59 | fseek(file, 0, SEEK_SET); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 60 | char* buffer = new char[size + 1]; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 61 | buffer[size] = 0; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 62 | Json::String text; |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 63 | if (fread(buffer, 1, usize, file) == usize) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 64 | text = buffer; |
| 65 | fclose(file); |
| 66 | delete[] buffer; |
| 67 | return text; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 70 | static void |
| 71 | printValueTree(FILE* fout, Json::Value& value, const Json::String& path = ".") { |
Cory Quammen | 4d23492 | 2014-10-09 16:29:47 -0400 | [diff] [blame] | 72 | if (value.hasComment(Json::commentBefore)) { |
| 73 | fprintf(fout, "%s\n", value.getComment(Json::commentBefore).c_str()); |
| 74 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 75 | switch (value.type()) { |
| 76 | case Json::nullValue: |
| 77 | fprintf(fout, "%s=null\n", path.c_str()); |
| 78 | break; |
| 79 | case Json::intValue: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 80 | fprintf(fout, "%s=%s\n", path.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 81 | Json::valueToString(value.asLargestInt()).c_str()); |
| 82 | break; |
| 83 | case Json::uintValue: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 84 | fprintf(fout, "%s=%s\n", path.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 85 | Json::valueToString(value.asLargestUInt()).c_str()); |
| 86 | break; |
| 87 | case Json::realValue: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 88 | fprintf(fout, "%s=%s\n", path.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 89 | normalizeFloatingPointStr(value.asDouble()).c_str()); |
| 90 | break; |
| 91 | case Json::stringValue: |
| 92 | fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str()); |
| 93 | break; |
| 94 | case Json::booleanValue: |
| 95 | fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false"); |
| 96 | break; |
| 97 | case Json::arrayValue: { |
| 98 | fprintf(fout, "%s=[]\n", path.c_str()); |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 99 | Json::ArrayIndex size = value.size(); |
| 100 | for (Json::ArrayIndex index = 0; index < size; ++index) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 101 | static char buffer[16]; |
Hans Johnson | 5c8e539 | 2018-12-12 13:31:55 -0600 | [diff] [blame] | 102 | jsoncpp_snprintf(buffer, sizeof(buffer), "[%u]", index); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 103 | printValueTree(fout, value[index], path + buffer); |
| 104 | } |
| 105 | } break; |
| 106 | case Json::objectValue: { |
| 107 | fprintf(fout, "%s={}\n", path.c_str()); |
| 108 | Json::Value::Members members(value.getMemberNames()); |
| 109 | std::sort(members.begin(), members.end()); |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 110 | Json::String suffix = *(path.end() - 1) == '.' ? "" : "."; |
Hans Johnson | cbeed7b | 2019-01-14 17:09:12 -0600 | [diff] [blame] | 111 | for (auto name : members) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 112 | printValueTree(fout, value[name], path + suffix + name); |
| 113 | } |
| 114 | } break; |
| 115 | default: |
| 116 | break; |
| 117 | } |
Cory Quammen | 4d23492 | 2014-10-09 16:29:47 -0400 | [diff] [blame] | 118 | |
| 119 | if (value.hasComment(Json::commentAfter)) { |
| 120 | fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str()); |
| 121 | } |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 124 | static int parseAndSaveValueTree(const Json::String& input, |
| 125 | const Json::String& actual, |
| 126 | const Json::String& kind, |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 127 | const Json::Features& features, |
Christopher Dunn | 632c9b5 | 2015-01-23 11:09:04 -0600 | [diff] [blame] | 128 | 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 | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 183 | static Json::String removeSuffix(const Json::String& path, const Json::String& extension) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 184 | if (extension.length() >= path.length()) |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 185 | return Json::String(""); |
| 186 | Json::String suffix = path.substr(path.length() - extension.length()); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 187 | if (suffix != extension) |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 188 | return Json::String(""); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 189 | return path.substr(0, path.length() - extension.length()); |
| 190 | } |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 191 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 192 | static void printConfig() { |
| 193 | // Print the configuration used to compile JsonCpp |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 194 | #if defined(JSON_NO_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 195 | printf("JSON_NO_INT64=1\n"); |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 196 | #else |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 197 | printf("JSON_NO_INT64=0\n"); |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 198 | #endif |
| 199 | } |
| 200 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 201 | static int printUsage(const char* argv[]) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 202 | printf("Usage: %s [--strict] input-json-file", argv[0]); |
| 203 | return 3; |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 206 | static int parseCommandLine(int argc, const char* argv[], Options* opts) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 207 | opts->parseOnly = false; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 208 | opts->write = &useStyledWriter; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 209 | if (argc < 2) { |
| 210 | return printUsage(argv); |
| 211 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 212 | int index = 1; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 213 | if (Json::String(argv[index]) == "--json-checker") { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 214 | opts->features = Json::Features::strictMode(); |
| 215 | opts->parseOnly = true; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 216 | ++index; |
| 217 | } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 218 | if (Json::String(argv[index]) == "--json-config") { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 219 | printConfig(); |
| 220 | return 3; |
| 221 | } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 222 | if (Json::String(argv[index]) == "--json-writer") { |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 223 | ++index; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 224 | Json::String const writerName(argv[index++]); |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 225 | if (writerName == "StyledWriter") { |
| 226 | opts->write = &useStyledWriter; |
| 227 | } else if (writerName == "StyledStreamWriter") { |
| 228 | opts->write = &useStyledStreamWriter; |
Christopher Dunn | 9243d60 | 2015-01-23 08:38:32 -0600 | [diff] [blame] | 229 | } else if (writerName == "BuiltStyledStreamWriter") { |
| 230 | opts->write = &useBuiltStyledStreamWriter; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 231 | } else { |
| 232 | printf("Unknown '--json-writer %s'\n", writerName.c_str()); |
| 233 | return 4; |
| 234 | } |
| 235 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 236 | if (index == argc || index + 1 < argc) { |
| 237 | return printUsage(argv); |
| 238 | } |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 239 | opts->path = argv[index]; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 240 | return 0; |
| 241 | } |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 242 | static int runTest(Options const& opts) { |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 243 | int exitCode = 0; |
| 244 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 245 | Json::String input = readInputTestFile(opts.path.c_str()); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 246 | if (input.empty()) { |
| 247 | printf("Failed to read input or empty input: %s\n", opts.path.c_str()); |
| 248 | return 3; |
| 249 | } |
| 250 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 251 | Json::String basePath = removeSuffix(opts.path, ".json"); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 252 | if (!opts.parseOnly && basePath.empty()) { |
| 253 | 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] | 254 | opts.path.c_str()); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 255 | return 3; |
| 256 | } |
| 257 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 258 | Json::String const actualPath = basePath + ".actual"; |
| 259 | Json::String const rewritePath = basePath + ".rewrite"; |
| 260 | Json::String const rewriteActualPath = basePath + ".actual-rewrite"; |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 261 | |
| 262 | Json::Value root; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 263 | exitCode = parseAndSaveValueTree(input, actualPath, "input", opts.features, |
| 264 | opts.parseOnly, &root); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 265 | if (exitCode || opts.parseOnly) { |
| 266 | return exitCode; |
| 267 | } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame^] | 268 | Json::String rewrite; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 269 | exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 270 | if (exitCode) { |
| 271 | return exitCode; |
| 272 | } |
| 273 | Json::Value rewriteRoot; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 274 | exitCode = parseAndSaveValueTree(rewrite, rewriteActualPath, "rewrite", |
| 275 | opts.features, opts.parseOnly, &rewriteRoot); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 276 | if (exitCode) { |
| 277 | return exitCode; |
| 278 | } |
| 279 | return 0; |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 280 | } |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 281 | int main(int argc, const char* argv[]) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 282 | Options opts; |
Gaurav | 6c14548 | 2015-09-22 13:53:19 +0530 | [diff] [blame] | 283 | try { |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 284 | int exitCode = parseCommandLine(argc, argv, &opts); |
| 285 | if (exitCode != 0) { |
| 286 | printf("Failed to parse command-line."); |
| 287 | return exitCode; |
| 288 | } |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 289 | return runTest(opts); |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 290 | } catch (const std::exception& e) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 291 | printf("Unhandled exception:\n%s\n", e.what()); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 292 | return 1; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 293 | } |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 294 | } |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 295 | |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 296 | #if defined(__GNUC__) |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 297 | #pragma GCC diagnostic pop |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 298 | #endif |