33// Copyright ( c ) 2016 Darrell Wright
44//
55// Permission is hereby granted, free of charge, to any person obtaining a copy
6- // of this software and associated documentation files( the "Software" ), to deal
7- // in the Software without restriction, including without limitation the rights
8- // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
9- // copies of the Software, and to permit persons to whom the Software is
6+ // of this software and associated documentation files( the "Software" ), to
7+ // deal in the Software without restriction, including without limitation the
8+ // rights to use, copy, modify, merge, publish, distribute, sublicense, and / or
9+ // sell copies of the Software, and to permit persons to whom the Software is
1010// furnished to do so, subject to the following conditions:
1111//
12- // The above copyright notice and this permission notice shall be included in all
13- // copies or substantial portions of the Software.
12+ // The above copyright notice and this permission notice shall be included in
13+ // all copies or substantial portions of the Software.
1414//
1515// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2727#include < curl/curl.h>
2828#include < fstream>
2929#include < iostream>
30- #include < optional>
3130#include < memory>
31+ #include < optional>
3232#include < string>
3333
3434#include < daw/daw_string_view.h>
3535
3636#include " json_to_cpp.h"
3737
3838namespace {
39- std::optional<std::string> download ( daw::string_view url, daw::string_view user_agent );
39+ std::optional<std::string> download ( daw::string_view url,
40+ daw::string_view user_agent );
4041 bool is_url ( daw::string_view path );
4142} // namespace
4243
4344int main ( int argc, char **argv ) {
4445 using namespace daw ::json_to_cpp;
4546 static std::string const default_user_agent =
46- " Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36" ;
47+ " Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) "
48+ " Chrome/54.0.2840.100 Safari/537.36" ;
4749
4850 boost::program_options::options_description desc{" Options" };
4951 desc.add_options ( )( " help" , " print option descriptions" )(
5052 " in_file" , boost::program_options::value<boost::filesystem::path>( ),
51- " json source file path or url" )( " use_jsonlink" , boost::program_options::value<bool >( )->default_value ( true ),
52- " Use JsonLink serializaion/deserialization" )(
53- " cpp_file" , boost::program_options::value<boost::filesystem::path>( ), " output c++ file" )(
53+ " json source file path or url" )(
54+ " use_jsonlink" ,
55+ boost::program_options::value<bool >( )->default_value ( true ),
56+ " Use JsonLink serializaion/deserialization" )(
57+ " cpp_file" , boost::program_options::value<boost::filesystem::path>( ),
58+ " output c++ file" )(
5459 " header_file" , boost::program_options::value<boost::filesystem::path>( ),
55- " output c++ header file. If not specified uses cpp_file. Only valid when use_jsonlink=true" )(
56- " allow_overwrite" , boost::program_options::value<bool >( )->default_value ( true ),
60+ " output c++ header file. If not specified uses cpp_file. Only valid when "
61+ " use_jsonlink=true" )(
62+ " allow_overwrite" ,
63+ boost::program_options::value<bool >( )->default_value ( true ),
64+
5765 " Overwrite existing output files" )(
58- " user_agent" , boost::program_options::value<std::string>( )->default_value ( default_user_agent ),
66+ " hide_null_only" ,
67+ boost::program_options::value<bool >( )->default_value ( true ),
68+ " Do not output json entries that are only ever null" )(
69+ " user_agent" ,
70+ boost::program_options::value<std::string>( )->default_value (
71+ default_user_agent ),
5972 " User agent to use when downloading via URL" );
6073
6174 boost::program_options::variables_map vm;
6275 try {
63- boost::program_options::store ( boost::program_options::parse_command_line ( argc, argv, desc ), vm );
76+ boost::program_options::store (
77+ boost::program_options::parse_command_line ( argc, argv, desc ), vm );
6478 if ( vm.count ( " help" ) ) {
6579 std::cout << " Command line options\n " << desc << std::endl;
6680 return EXIT_SUCCESS;
@@ -81,11 +95,13 @@ int main( int argc, char **argv ) {
8195
8296 std::string json_str;
8397 if ( is_url ( config.json_path .string ( ) ) ) {
84- auto tmp = download ( config.json_path .string ( ), vm[" user_agent" ].as <std::string>( ) );
98+ auto tmp = download ( config.json_path .string ( ),
99+ vm[" user_agent" ].as <std::string>( ) );
85100 if ( tmp ) {
86101 json_str = *tmp;
87102 } else {
88- std::cerr << " Could not download json data from '" << canonical ( config.json_path ) << " '\n " ;
103+ std::cerr << " Could not download json data from '"
104+ << canonical ( config.json_path ) << " '\n " ;
89105 exit ( EXIT_FAILURE );
90106 }
91107 } else {
@@ -98,42 +114,51 @@ int main( int argc, char **argv ) {
98114 std::ifstream in_file;
99115 in_file.open ( config.json_path .string ( ) );
100116 if ( !in_file ) {
101- std::cerr << " Could not open json in_file '" << canonical ( config.json_path ) << " '\n " ;
117+ std::cerr << " Could not open json in_file '"
118+ << canonical ( config.json_path ) << " '\n " ;
102119 exit ( EXIT_FAILURE );
103120 }
104- std::copy ( std::istream_iterator<char >{in_file}, std::istream_iterator<char >{}, std::back_inserter ( json_str ) );
121+ std::copy ( std::istream_iterator<char >{in_file},
122+ std::istream_iterator<char >{}, std::back_inserter ( json_str ) );
105123 in_file.close ( );
106124 }
107125
108126 config.cpp_stream = &std::cout;
109127 config.header_stream = &std::cout;
110128 config.enable_jsonlink = vm[" use_jsonlink" ].as <bool >( );
129+ config.hide_null_only = vm[" allow_overwrite" ].as <bool >( );
111130 std::ofstream cpp_file;
112131 std::ofstream header_file;
113132
114133 if ( vm.count ( " cpp_file" ) > 0 ) {
115134 bool const allow_overwrite = vm[" allow_overwrite" ].as <bool >( );
116- config.cpp_path = canonical ( vm[" cpp_file" ].as <boost::filesystem::path>( ) );
135+ config.cpp_path =
136+ canonical ( vm[" cpp_file" ].as <boost::filesystem::path>( ) );
117137 if ( exists ( config.cpp_path ) && !allow_overwrite ) {
118138 std::cerr << " cpp_file '" << config.cpp_path << " ' already exists\n " ;
119139 exit ( EXIT_FAILURE );
120140 }
121141 cpp_file.open ( config.cpp_path .string ( ), std::ios::out | std::ios::trunc );
122142 if ( !cpp_file ) {
123- std::cerr << " Could not open cpp_file '" << config.cpp_path << " ' for writing\n " ;
143+ std::cerr << " Could not open cpp_file '" << config.cpp_path
144+ << " ' for writing\n " ;
124145 exit ( EXIT_FAILURE );
125146 }
126147 config.cpp_stream = &cpp_file;
127148
128149 if ( config.enable_jsonlink && vm.count ( " header_file" ) > 0 ) {
129- config.header_path = canonical ( vm[" header_file" ].as <boost::filesystem::path>( ) );
150+ config.header_path =
151+ canonical ( vm[" header_file" ].as <boost::filesystem::path>( ) );
130152 if ( exists ( config.header_path ) && !allow_overwrite ) {
131- std::cerr << " header_file '" << config.header_path << " ' already exists\n " ;
153+ std::cerr << " header_file '" << config.header_path
154+ << " ' already exists\n " ;
132155 exit ( EXIT_FAILURE );
133156 }
134- header_file.open ( config.header_path .string ( ), std::ios::out | std::ios::trunc );
157+ header_file.open ( config.header_path .string ( ),
158+ std::ios::out | std::ios::trunc );
135159 if ( !header_file ) {
136- std::cerr << " Could not open header_file '" << config.header_path << " ' for writing\n " ;
160+ std::cerr << " Could not open header_file '" << config.header_path
161+ << " ' for writing\n " ;
137162 exit ( EXIT_FAILURE );
138163 }
139164 config.header_stream = &header_file;
@@ -149,14 +174,16 @@ int main( int argc, char **argv ) {
149174}
150175
151176namespace {
152- size_t callback ( char const *in, size_t const size, size_t const num, std::string *const out ) {
177+ size_t callback ( char const *in, size_t const size, size_t const num,
178+ std::string *const out ) {
153179 assert ( out );
154180 size_t totalBytes = size * num;
155181 out->append ( in, totalBytes );
156182 return totalBytes;
157183 }
158184
159- std::optional<std::string> download ( daw::string_view url, daw::string_view user_agent ) {
185+ std::optional<std::string> download ( daw::string_view url,
186+ daw::string_view user_agent ) {
160187 struct curl_slist *headers = nullptr ;
161188 curl_slist_append ( headers, " Accept: application/json" );
162189 curl_slist_append ( headers, " Content-Type: application/json" );
@@ -206,7 +233,7 @@ namespace {
206233 }
207234
208235 bool is_url ( daw::string_view path ) {
209- return boost::starts_with ( path.data ( ), " http://" ) || boost::starts_with ( path.data ( ), " https://" );
236+ return boost::starts_with ( path.data ( ), " http://" ) ||
237+ boost::starts_with ( path.data ( ), " https://" );
210238 }
211239} // namespace
212-
0 commit comments