1717#include < cstdio>
1818#include < json/json.h>
1919#include < memory>
20+ #include < iostream>
2021#include < sstream>
2122
2223struct Options {
@@ -143,7 +144,8 @@ static int parseAndSaveValueTree(const Json::String& input,
143144 reader->parse (input.data (), input.data () + input.size (), root, &errors);
144145
145146 if (!parsingSuccessful) {
146- printf (" Failed to parse %s file: \n %s\n " , kind.c_str (), errors.c_str ());
147+ std::cerr << " Failed to parse " << kind << " file: " << std::endl
148+ << errors << std::endl;
147149 return 1 ;
148150 }
149151
@@ -154,16 +156,16 @@ static int parseAndSaveValueTree(const Json::String& input,
154156 const bool parsingSuccessful =
155157 reader.parse (input.data (), input.data () + input.size (), *root);
156158 if (!parsingSuccessful) {
157- printf ( " Failed to parse %s file: \n %s \n " , kind. c_str (),
158- reader.getFormattedErrorMessages (). c_str ()) ;
159+ std::cerr << " Failed to parse " << kind << " file: " << std::endl
160+ << reader.getFormatedErrorMessages () << std::endl ;
159161 return 1 ;
160162 }
161163 }
162164
163165 if (!parseOnly) {
164166 FILE* factual = fopen (actual.c_str (), " wt" );
165167 if (!factual) {
166- printf ( " Failed to create %s actual file.\n " , kind. c_str ()) ;
168+ std::cerr << " Failed to create ' " << kind << " ' actual file." << std::endl ;
167169 return 2 ;
168170 }
169171 printValueTree (factual, *root);
@@ -197,7 +199,7 @@ static int rewriteValueTree(const Json::String& rewritePath,
197199 *rewrite = write (root);
198200 FILE* fout = fopen (rewritePath.c_str (), " wt" );
199201 if (!fout) {
200- printf ( " Failed to create rewrite file: %s \n " , rewritePath. c_str ()) ;
202+ std::cerr << " Failed to create rewrite file: " << rewritePath << std::endl ;
201203 return 2 ;
202204 }
203205 fprintf (fout, " %s\n " , rewrite->c_str ());
@@ -218,14 +220,14 @@ static Json::String removeSuffix(const Json::String& path,
218220static void printConfig () {
219221// Print the configuration used to compile JsonCpp
220222#if defined(JSON_NO_INT64)
221- printf ( " JSON_NO_INT64=1\n " ) ;
223+ std::cout << " JSON_NO_INT64=1" << std::endl ;
222224#else
223- printf ( " JSON_NO_INT64=0\n " ) ;
225+ std::cout << " JSON_NO_INT64=0" << std::endl ;
224226#endif
225227}
226228
227229static int printUsage (const char * argv[]) {
228- printf ( " Usage: %s [ --strict] input-json-file" , argv[ 0 ]) ;
230+ std::cout << " Usage: " << argv[ 0 ] << " [ --strict] input-json-file" << std::endl ;
229231 return 3 ;
230232}
231233
@@ -255,7 +257,7 @@ static int parseCommandLine(int argc, const char* argv[], Options* opts) {
255257 } else if (writerName == " BuiltStyledStreamWriter" ) {
256258 opts->write = &useBuiltStyledStreamWriter;
257259 } else {
258- printf ( " Unknown '--json-writer %s' \n " , writerName. c_str ()) ;
260+ std::cerr << " Unknown '--json-writer' " << writerName << std::endl ;
259261 return 4 ;
260262 }
261263 }
@@ -271,14 +273,13 @@ static int runTest(Options const& opts, bool use_legacy) {
271273
272274 Json::String input = readInputTestFile (opts.path .c_str ());
273275 if (input.empty ()) {
274- printf ( " Failed to read input or empty input: %s \n " , opts.path . c_str ()) ;
276+ std::cerr << " Invalid input file: " << opts.path << std::endl ;
275277 return 3 ;
276278 }
277279
278280 Json::String basePath = removeSuffix (opts.path , " .json" );
279281 if (!opts.parseOnly && basePath.empty ()) {
280- printf (" Bad input path. Path does not end with '.expected':\n %s\n " ,
281- opts.path .c_str ());
282+ std::cerr << " Bad input path '" << opts.path << " '. Must end with '.expected'" << std::endl;
282283 return 3 ;
283284 }
284285
@@ -312,21 +313,22 @@ int main(int argc, const char* argv[]) {
312313 try {
313314 int exitCode = parseCommandLine (argc, argv, &opts);
314315 if (exitCode != 0 ) {
315- printf ( " Failed to parse command-line." ) ;
316+ std::cerr << " Failed to parse command-line." << std::endl ;
316317 return exitCode;
317318 }
318319
319- // TODO(baylesj): replace this with proper calls to both. Right now
320- // we only check the legacy if the modern one is not broken.
321- const int legacy_return_code = runTest (opts, true );
322320 const int modern_return_code = runTest (opts, false );
323321 if (modern_return_code) {
324322 return modern_return_code;
325- } else {
326- return legacy_return_code;
323+ }
324+
325+ const std::string filename = opts.path .substr (opts.path .find_last_of (" \\ /" ) + 1 );
326+ const bool should_run_legacy = (filename.rfind (" legacy_" , 0 ) == 0 );
327+ if (should_run_legacy) {
328+ return runTest (opts, true );
327329 }
328330 } catch (const std::exception& e) {
329- printf ( " Unhandled exception:\n %s \n " , e.what ()) ;
331+ std::cerr << " Unhandled exception:" << std::endl << e.what () << std::endl ;
330332 return 1 ;
331333 }
332334}
0 commit comments