57#include <cppthread/thread.h>
58#include <cppthread/version.h>
63#include <libexcept/exception.h>
64#include <libexcept/file_inheritance.h>
65#include <libexcept/report_signal.h>
70#include <snapdev/file_contents.h>
71#include <snapdev/pathinfo.h>
76#include <boost/preprocessor/stringize.hpp>
87#include <snapdev/poison.h>
119 ,
advgetopt::Help(
"filename with atomic name definitions; the same name is used to generate the output, only the extension gets changed.")
141 "/etc/cppthread/atomic-names.conf",
149#pragma GCC diagnostic push
150#pragma GCC diagnostic ignored "-Wpedantic"
154 .f_group_name =
"atomic-names",
156 .f_options_files_directory =
nullptr,
158 .f_environment_variable_intro =
"ATOMIC_NAMES_",
159 .f_section_variables_name =
nullptr,
161 .f_configuration_filename =
nullptr,
162 .f_configuration_directories =
nullptr,
164 .f_help_header =
"Usage: %p [-<opt>]\n"
165 "where -<opt> is one or more of:",
166 .f_help_footer =
"Try `man atomic-names` for more info.\n%c",
167 .f_version = CPPTHREAD_VERSION_STRING,
168 .f_license =
"GPL v2 or newer",
169 .f_copyright =
"Copyright (c) 2022-"
170 BOOST_PP_STRINGIZE(UTC_BUILD_YEAR)
171 " Made to Order Software Corporation",
172 .f_build_date = UTC_BUILD_DATE,
173 .f_build_time = UTC_BUILD_TIME,
176#pragma GCC diagnostic pop
194 std::string
const & what
196 ,
bool start_end_underscore =
false);
198 std::string
const & name
199 , std::string
const & value
201 , std::string & result);
202 int generate_files();
205 bool f_verbose =
false;
206 std::string f_filename = std::string();
207 std::string f_basename = std::string();
208 std::string f_output_path = std::string();
211 std::string f_introducer =
"atomic_name";
212 std::string f_project = std::string();
213 std::string f_sub_project = std::string();
254 std::cout <<
"info: get filename.\n";
259 std::cerr <<
"error: a <filename> is required.\n";
265 std::cerr <<
"error: <filename> requires a non-empty name.\n";
273 <<
"error: somehow the basename of \""
275 <<
"\" is an empty string.\n";
282 std::cerr <<
"error: the --output-path command line option requires a non-empty name.\n";
295 <<
"info: load input \""
305 <<
"error: could not read input file \""
316 std::string
const & what
318 ,
bool start_end_underscore)
322 std::cerr <<
"error: " << what <<
" cannot be empty.\n";
326 for(
char const * n(name.c_str()); *n !=
'\0'; ++n)
330 name[n - name.c_str()] =
'_';
332 else if((*n <
'A' || *n >
'Z')
333 && (*n <
'a' || *n >
'z')
334 && (*n <
'0' || *n >
'9')
340 <<
" includes unexpected characters in \""
347 if(!start_end_underscore
348 && (name.front() ==
'_' || name.back() ==
'_'))
353 <<
" cannot start and/or end with an underscore in \""
364 std::string
const & name
365 , std::string
const & value
367 , std::string & result)
374 char const * v(value.c_str());
375 int64_t identifier(0);
376 bool found_digits(
false);
377 for(; *v !=
'\0'; ++v)
379 if(*v <
'0' || *v >
'9')
384 identifier += *v -
'0';
404 <<
"error: empty values are not currently allowed (parameter \""
410 for(
auto const & c : str)
416 <<
"error: found a NUL character in \""
445 result += ((c >> 6) & 0x03) +
'0';
446 result += ((c >> 3) & 0x07) +
'0';
447 result += ((c >> 6) & 0x07) +
'0';
468 std::cout <<
"info: generate files.\n";
471 if(
f_names->has_parameter(
"introducer"))
481 if(!
f_names->has_parameter(
"project"))
483 std::cerr <<
"error: the \"project=...\" parameter is mandatory.\n";
493 if(
f_names->has_parameter(
"sub_project"))
507 std::stringstream cpp;
511 <<
"// DO NOT EDIT, see `man atomic-names` for details\n"
519 <<
"// DO NOT EDIT, see `man atomic-names` for details\n"
527 for(
auto const & p : parameters)
529 if(p.first.length() < 7)
533 std::string::size_type
const pos(p.first.find(
"::"));
534 if(pos == std::string::npos)
538 std::string
const scope(p.first.substr(0, pos));
539 bool is_public(
true);
540 if(scope !=
"public")
542 if(scope !=
"private")
548 std::string name(p.first.substr(pos + 2));
554 <<
"error: empty names are not allowed.\n";
568 <<
"extern char const * g_"
576 cpp <<
"char const * g_"
598 <<
"info: save to \""
602 <<
"{.cpp,.h,_private.h}\".\n";
606 header_public.contents(h.str());
607 if(!header_public.write_all())
609 std::cerr <<
"error: could not save public header file to \""
610 << header_public.filename()
616 cpp_file.contents(cpp.str());
617 if(!cpp_file.write_all())
619 std::cerr <<
"error: could not save C++ implementation file to \""
620 << cpp_file.filename()
636int main(
int argc,
char * argv[])
638 libexcept::init_report_signal();
639 libexcept::verify_inherited_files();
643 atomic_names n(argc, argv);
650 catch(libexcept::exception_t
const & e)
653 <<
"error: a libexcept exception occurred: \""
657 catch(std::exception
const & e)
660 <<
"error: a standard exception occurred: \""
666 std::cerr <<
"error: an unknown exception occurred.\n";
Definitions of the advanced getopt class.
int main(int argc, char *argv[])
std::map< std::string, parameter_value > parameters_t
std::shared_ptr< conf_file > pointer_t
static pointer_t get_conf_file(conf_file_setup const &setup)
Create and read a conf_file.
Class used to parse command line options.
std::string get_string(std::string const &name, int idx=0, bool raw=false) const
Get the content of an option as a string.
bool is_defined(std::string const &name) const
Check whether a parameter is defined.
int extract_value(std::string const &name, std::string const &value, int64_t &id, std::string &result)
std::string f_sub_project
advgetopt::conf_file::pointer_t f_names
atomic_names(int argc, char *argv[])
std::string f_output_path
int validate_name(std::string const &what, std::string &name, bool start_end_underscore=false)
Declaration of the conf_file class used to read a configuration file.
Definitions of the advanced getopt exceptions.
constexpr group_description define_group(ARGS ...args)
constexpr group_description end_groups()
static constexpr flag_t GETOPT_FLAG_GROUP_OPTIONS
constexpr option define_option(ARGS ...args)
constexpr flag_t standalone_command_flags()
constexpr option end_options()
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS
static constexpr flag_t GETOPT_FLAG_GROUP_COMMANDS
constexpr flag_t all_flags()
std::string unquote(std::string const &s, std::string const &pairs)
Remove single (') or double (") quotes from a string.
static constexpr flag_t GETOPT_FLAG_REQUIRED
constexpr char const *const g_configuration_files[]
advgetopt::options_environment const g_options_environment
const advgetopt::option g_options[]
advgetopt::group_description const g_group_descriptions[]
Structure representing an option.
char const * f_environment_variable_name
char const * f_project_name