3
3
// Copyright ( c ) 2016 Darrell Wright
4
4
//
5
5
// 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
10
10
// furnished to do so, subject to the following conditions:
11
11
//
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.
14
14
//
15
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
27
#include < curl/curl.h>
28
28
#include < fstream>
29
29
#include < iostream>
30
- #include < optional>
31
30
#include < memory>
31
+ #include < optional>
32
32
#include < string>
33
33
34
34
#include < daw/daw_string_view.h>
35
35
36
36
#include " json_to_cpp.h"
37
37
38
38
namespace {
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 );
40
41
bool is_url ( daw::string_view path );
41
42
} // namespace
42
43
43
44
int main ( int argc, char **argv ) {
44
45
using namespace daw ::json_to_cpp;
45
46
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" ;
47
49
48
50
boost::program_options::options_description desc{" Options" };
49
51
desc.add_options ( )( " help" , " print option descriptions" )(
50
52
" 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" )(
54
59
" 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
+
57
65
" 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 ),
59
72
" User agent to use when downloading via URL" );
60
73
61
74
boost::program_options::variables_map vm;
62
75
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 );
64
78
if ( vm.count ( " help" ) ) {
65
79
std::cout << " Command line options\n " << desc << std::endl;
66
80
return EXIT_SUCCESS;
@@ -81,11 +95,13 @@ int main( int argc, char **argv ) {
81
95
82
96
std::string json_str;
83
97
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>( ) );
85
100
if ( tmp ) {
86
101
json_str = *tmp;
87
102
} 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 " ;
89
105
exit ( EXIT_FAILURE );
90
106
}
91
107
} else {
@@ -98,42 +114,51 @@ int main( int argc, char **argv ) {
98
114
std::ifstream in_file;
99
115
in_file.open ( config.json_path .string ( ) );
100
116
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 " ;
102
119
exit ( EXIT_FAILURE );
103
120
}
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 ) );
105
123
in_file.close ( );
106
124
}
107
125
108
126
config.cpp_stream = &std::cout;
109
127
config.header_stream = &std::cout;
110
128
config.enable_jsonlink = vm[" use_jsonlink" ].as <bool >( );
129
+ config.hide_null_only = vm[" allow_overwrite" ].as <bool >( );
111
130
std::ofstream cpp_file;
112
131
std::ofstream header_file;
113
132
114
133
if ( vm.count ( " cpp_file" ) > 0 ) {
115
134
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>( ) );
117
137
if ( exists ( config.cpp_path ) && !allow_overwrite ) {
118
138
std::cerr << " cpp_file '" << config.cpp_path << " ' already exists\n " ;
119
139
exit ( EXIT_FAILURE );
120
140
}
121
141
cpp_file.open ( config.cpp_path .string ( ), std::ios::out | std::ios::trunc );
122
142
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 " ;
124
145
exit ( EXIT_FAILURE );
125
146
}
126
147
config.cpp_stream = &cpp_file;
127
148
128
149
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>( ) );
130
152
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 " ;
132
155
exit ( EXIT_FAILURE );
133
156
}
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 );
135
159
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 " ;
137
162
exit ( EXIT_FAILURE );
138
163
}
139
164
config.header_stream = &header_file;
@@ -149,14 +174,16 @@ int main( int argc, char **argv ) {
149
174
}
150
175
151
176
namespace {
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 ) {
153
179
assert ( out );
154
180
size_t totalBytes = size * num;
155
181
out->append ( in, totalBytes );
156
182
return totalBytes;
157
183
}
158
184
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 ) {
160
187
struct curl_slist *headers = nullptr ;
161
188
curl_slist_append ( headers, " Accept: application/json" );
162
189
curl_slist_append ( headers, " Content-Type: application/json" );
@@ -206,7 +233,7 @@ namespace {
206
233
}
207
234
208
235
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://" );
210
238
}
211
239
} // namespace
212
-
0 commit comments