45 std::ostream & error();
46 int exit_code()
const;
47 void set_input_path(std::string
const & path);
48 void set_output(std::string
const & output);
49 void set_c_file(std::string
const & c);
50 void set_verify(
bool verify);
51 void set_output_json(
bool verify);
52 void set_include_offsets(
bool include_offsets);
61 std::string f_input_path = std::string();
62 std::string f_output = std::string();
63 std::string f_c_file = std::string();
64 bool f_verify =
false;
65 bool f_output_json =
false;
66 bool f_include_offsets =
false;
67 bool f_verbose =
false;
71std::ostream & compiler::error()
78int compiler::exit_code()
const
80 return f_errcnt == 0 ? 0 : 1;
84void compiler::set_input_path(std::string
const & path)
90void compiler::set_output(std::string
const & output)
96void compiler::set_c_file(std::string
const & c_file)
102void compiler::set_verify(
bool verify)
108void compiler::set_output_json(
bool output_json)
110 f_output_json = output_json;
114void compiler::set_include_offsets(
bool include_offsets)
116 f_include_offsets = include_offsets;
120void compiler::set_verbose(
bool verbose)
134 if(f_input_path.empty())
137 std::cerr <<
"error: an input path is required.\n";
144 std::cerr <<
"error: an output filename is required.\n";
148 std::cout <<
"Compiling TLDs from \"" << f_input_path <<
"\"..." << std::endl;
151 c.set_input_folder(f_input_path);
152 c.set_output(f_output);
153 c.set_c_file(f_c_file);
167 << strerror(c.get_errno())
172 std::cout <<
"Number of strings: " << c.get_string_manager().size() <<
"\n";
173 std::cout <<
"Longest string: " << c.get_string_manager().max_length() <<
"\n";
174 std::cout <<
"Total string length: " << c.get_string_manager().total_length() <<
"\n";
175 std::cout <<
"Included strings: " << c.get_string_manager().included_count() <<
" (saved length: " << c.get_string_manager().included_length() <<
")\n";
176 std::cout <<
"Mergeable strings: " << c.get_string_manager().merged_count() <<
" (saved length: " << c.get_string_manager().merged_length() <<
")\n";
177 std::cout <<
"Compressed string length: " << c.get_string_manager().compressed_length() << std::endl;
182 std::string filename;
183 std::string::size_type
const dot(f_output.rfind(
'.'));
184 if(dot != std::string::npos
186 && f_output[dot - 1] !=
'/')
188 filename = f_output.substr(0, dot) +
".json";
192 filename = f_output +
".json";
198 c.output_to_json(out, f_include_offsets);
204 <<
"error: could not open JSON output file: \""
222 tld_file_error err(tld_file_load(f_output.c_str(), &file));
223 if(err != TLD_FILE_ERROR_NONE)
226 std::cerr <<
"error: could not load output file \""
229 << tld_file_errstr(err)
231 <<
static_cast<int>(err)
243 std::cerr <<
"error: conversion of file to JSON failed.\n";
253 std::string filename;
254 std::string::size_type
const dot(f_output.rfind(
'.'));
255 if(dot != std::string::npos
257 && f_output[dot - 1] !=
'/')
259 filename = f_output.substr(0, dot) +
"-verify.json";
263 filename = f_output +
"-verify.json";
269 out.write(json, strlen(json));
275 <<
"error: could not open JSON output file: \""
282 std::stringstream compiler_json;
283 c.output_to_json(compiler_json,
false);
285 if(compiler_json.str() != json)
289 <<
"error: compiler & verification JSON differ."
291 ?
" Check the two .json output files to see the differences."
292 :
" Try using the --output-json command line option to get the .json files to find the differences.")
301void usage(
char * argv0)
303 std::string progname(argv0);
304 std::string::size_type pos(progname.rfind(
'/'));
305 if(pos != std::string::npos)
307 progname = progname.substr(pos + 1);
310 std::cout <<
"Usage: " << progname <<
" [--opts] [<output>]\n";
311 std::cout <<
"Where --opts is one or more of the following:\n";
312 std::cout <<
" --help | -h prints out this help screen and exit\n";
313 std::cout <<
" --c-file path and filename to the \"tld_data.c\" file\n";
314 std::cout <<
" --include-offsets print offset in comment in .json file\n";
315 std::cout <<
" --output-json also save to a .json file\n";
316 std::cout <<
" --source | -s <folder> define the source (input) folder\n";
317 std::cout <<
" --verify verify loading results and compare against sources\n";
318 std::cout <<
" --verbose print out more information about what is happening\n";
319 std::cout <<
" --version | -V print out the version and exit\n";
321 std::cout <<
"The default source is \"/usr/share/libtld/tlds\".\n";
322 std::cout <<
"The default output is \"/var/lib/libtld/tlds.tld\".\n";
323 std::cout << progname <<
" will not output a C-file or JSON by default.\n";
327int main(
int argc,
char * argv[])
331 for(
int i(1); i < argc; ++i)
333 if(argv[i][0] ==
'-')
335 if(strcmp(argv[i],
"-h") == 0
336 || strcmp(argv[i],
"--help") == 0)
341 else if(strcmp(argv[i],
"-V") == 0
342 || strcmp(argv[i],
"--version") == 0)
347 else if(strcmp(argv[i],
"-s") == 0
348 || strcmp(argv[i],
"--source") == 0)
354 <<
"error: argument missing for --source.\n";
358 tldc.set_input_path(argv[i]);
361 else if(strcmp(argv[i],
"--verify") == 0)
363 tldc.set_verify(
true);
365 else if(strcmp(argv[i],
"--c-file") == 0)
371 <<
"error: argument missing for --output-c-file.\n";
375 tldc.set_c_file(argv[i]);
378 else if(strcmp(argv[i],
"--output-json") == 0)
380 tldc.set_output_json(
true);
382 else if(strcmp(argv[i],
"--include-offsets") == 0)
384 tldc.set_include_offsets(
true);
386 else if(strcmp(argv[i],
"--verbose") == 0)
388 tldc.set_verbose(
true);
393 <<
"error: unknown command line option \""
400 tldc.set_output(argv[i]);
406 return tldc.exit_code();
#define LIBTLD_VERSION
The version of the library as a string.
Implementation of the TLD parser library.
char * tld_file_to_json(tld_file const *file)
Transform a tld_file to a JSON string.
Declaration of the TLD file structures.
int verbose
Whether the user asked for verbosity, false by default.
void usage()
Print out the help of the tld tool.