SNode.C
Loading...
Searching...
No Matches
utils Namespace Reference

Namespaces

namespace  system
 

Classes

class  AttributeProxy
 
class  Config
 
class  Daemon
 
class  DaemonError
 
class  DaemonFailure
 
class  DaemonSignaled
 
struct  fixed_string
 
class  MultibleAttributeInjector
 
class  PreserveErrno
 
class  Random
 
class  SHA1
 
class  SingleAttributeInjector
 
class  Timeval
 
class  Uuid
 

Concepts

concept  InjectableAttribute
 

Functions

std::vector< unsigned char > sha1 (const std::string &string)
 
static const std::shared_ptr< CLI::App > makeApp ()
 
static void createCommandLineOptions (std::stringstream &out, CLI::App *app, CLI::CallForCommandline::Mode mode)
 
static std::string createCommandLineOptions (CLI::App *app, CLI::CallForCommandline::Mode mode)
 
static void createCommandLineTemplate (std::stringstream &out, CLI::App *app, CLI::CallForCommandline::Mode mode)
 
static std::string createCommandLineSubcommands (CLI::App *app, CLI::CallForCommandline::Mode mode)
 
static std::string createCommandLineTemplate (CLI::App *app, CLI::CallForCommandline::Mode mode)
 
static const std::shared_ptr< CLI::HelpFormattermakeSectionFormatter ()
 
std::string hexDump (const std::vector< char > &bytes, int prefixLength, bool prefixAtFirstLine)
 
std::string hexDump (const std::string &string, int prefixLength, bool prefixAtFirstLine)
 
std::string hexDump (const char *bytes, uint64_t length, int prefixLength, bool prefixAtFirstLine)
 
static std::vector< unsigned char > transform_to_binary (const std::string &string)
 
std::ostream & operator<< (std::ostream &ostream, const utils::Timeval &timeVal)
 
Timeval operator* (double mul, const utils::Timeval &timeVal)
 
template<unsigned N>
 fixed_string (char const (&)[N]) -> fixed_string< N - 1 >
 

Function Documentation

◆ createCommandLineOptions() [1/2]

static std::string utils::createCommandLineOptions ( CLI::App * app,
CLI::CallForCommandline::Mode mode )
static

Definition at line 467 of file Config.cpp.

467 {
468 std::stringstream out;
469
470 createCommandLineOptions(out, app, mode);
471
472 std::string optionString = out.str();
473 if (optionString.back() == ' ') {
474 optionString.pop_back();
475 }
476
477 return optionString;
478 }
static void createCommandLineOptions(std::stringstream &out, CLI::App *app, CLI::CallForCommandline::Mode mode)
Definition Config.cpp:409

◆ createCommandLineOptions() [2/2]

static void utils::createCommandLineOptions ( std::stringstream & out,
CLI::App * app,
CLI::CallForCommandline::Mode mode )
static

Definition at line 409 of file Config.cpp.

409 {
410 CLI::Option* disabledOpt = app->get_option_no_throw("--disabled");
411 const bool disabled = disabledOpt != nullptr ? disabledOpt->as<bool>() : false;
412 if (!disabled || mode == CLI::CallForCommandline::Mode::DEFAULT) {
413 for (const CLI::Option* option : app->get_options()) {
414 if (option->get_configurable()) {
415 std::string value;
416
417 switch (mode) {
419 if (option->count() > 0) {
420 value = option->as<std::string>();
421 } else if (option->get_required()) {
422 value = "<REQUIRED>";
423 }
424 break;
426 if (option->get_required()) {
427 if (option->count() > 0) {
428 value = option->as<std::string>();
429 } else {
430 value = "<REQUIRED>";
431 }
432 }
433 break;
435 if (option->count() > 0) {
436 value = option->as<std::string>();
437 } else if (!option->get_default_str().empty()) {
438 value = option->get_default_str();
439 } else if (!option->get_required()) {
440 value = "\"\"";
441 } else {
442 value = "<REQUIRED>";
443 }
444 break;
446 if (!option->get_default_str().empty()) {
447 value = option->get_default_str();
448 } else if (!option->get_required()) {
449 value = "\"\"";
450 } else {
451 value = "<REQUIRED>";
452 }
453 break;
454 }
455 }
456
457 if (!value.empty()) {
458 out << "--" << option->get_single_name() << ((option->get_items_expected_max() == 0) ? "=" : " ") << value << " ";
459 }
460 }
461 }
462 } else if (disabledOpt->get_default_str() == "false") {
463 out << "--disabled=true ";
464 }
465 }

References CLI::CallForCommandline::DEFAULT.

◆ createCommandLineSubcommands()

static std::string utils::createCommandLineSubcommands ( CLI::App * app,
CLI::CallForCommandline::Mode mode )
static

Definition at line 482 of file Config.cpp.

482 {
483 std::stringstream out;
484
485 CLI::Option* disabledOpt = app->get_option_no_throw("--disabled");
486 if (disabledOpt == nullptr || !disabledOpt->as<bool>() || mode == CLI::CallForCommandline::Mode::DEFAULT) {
487 for (CLI::App* subcommand : app->get_subcommands({})) {
488 if (!subcommand->get_name().empty()) {
489 createCommandLineTemplate(out, subcommand, mode);
490 }
491 }
492 }
493
494 return out.str();
495 }

References CLI::CallForCommandline::DEFAULT.

◆ createCommandLineTemplate() [1/2]

static std::string utils::createCommandLineTemplate ( CLI::App * app,
CLI::CallForCommandline::Mode mode )
static

Definition at line 512 of file Config.cpp.

512 {
513 std::stringstream out;
514
515 createCommandLineTemplate(out, app, mode);
516
517 std::string outString = out.str();
518 while (app->get_parent() != nullptr) {
519 app = app->get_parent();
520 std::string parentOptions = createCommandLineOptions(app, mode);
521 outString =
522 std::string(app->get_name()).append(" ").append(!parentOptions.empty() ? parentOptions.append(" ") : "").append(outString);
523 }
524
525 if (outString.empty()) {
526 outString = Config::getApplicationName();
527 }
528
529 return outString;
530 }
static void createCommandLineTemplate(std::stringstream &out, CLI::App *app, CLI::CallForCommandline::Mode mode)
Definition Config.cpp:497

◆ createCommandLineTemplate() [2/2]

static void utils::createCommandLineTemplate ( std::stringstream & out,
CLI::App * app,
CLI::CallForCommandline::Mode mode )
static

Definition at line 497 of file Config.cpp.

497 {
498 std::string outString;
499
500 outString = createCommandLineOptions(app, mode);
501 if (!outString.empty()) {
502 outString += " ";
503 }
504
505 outString += createCommandLineSubcommands(app, mode);
506
507 if (!outString.empty()) {
508 out << app->get_name() << " " << outString;
509 }
510 }

◆ fixed_string()

template<unsigned N>
utils::fixed_string ( char const(&)[N]) -> fixed_string< N - 1 >

◆ hexDump() [1/3]

std::string utils::hexDump ( const char * bytes,
uint64_t length,
int prefixLength = 0,
bool prefixAtFirstLine = false )

Definition at line 44 of file hexdump.cpp.

44 {
45 std::stringstream hexStream;
46
47 if (length > 0) {
48 uint8_t buff[17];
49 size_t i = 0;
50
51 hexStream << std::hex;
52
53 int currentPrefixLength = prefixAtFirstLine ? prefixLength : 0;
54
55 // Process every byte in the data.
56 for (i = 0; i < length; i++) {
57 // Multiple of 16 means new line (with line offset).
58
59 if ((i % 16) == 0) {
60 // Just don't print ASCII for the zeroth line.
61 if (i != 0) {
62 hexStream << " " << buff << std::endl;
63 }
64
65 // Output the offset.
66 hexStream << Color::Code::FG_BLUE;
67 hexStream << std::setw(currentPrefixLength) << std::setfill(' ') << ""
68 << ": " << std::setw(8) << std::setfill('0') << static_cast<unsigned int>(i);
69 hexStream << Color::Code::FG_DEFAULT << " ";
70 }
71
72 // Now the hex code for the specific character.
73 hexStream << Color::Code::FG_GREEN;
74 hexStream << " " << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(static_cast<unsigned char>(bytes[i]));
75 hexStream << Color::Code::FG_DEFAULT;
76
77 // And store a printable ASCII character for later.
78 if ((bytes[i] < 0x20) || (bytes[i] > 0x7e)) {
79 buff[i % 16] = '.';
80 } else {
81 buff[i % 16] = static_cast<uint8_t>(bytes[i]);
82 }
83 buff[(i % 16) + 1] = '\0';
84
85 currentPrefixLength = prefixLength;
86 }
87
88 hexStream << std::dec;
89
90 // Pad out last line if not exactly 16 characters.
91 while ((i % 16) != 0) {
92 hexStream << " ";
93 i++;
94 }
95
96 // And print the final ASCII bit.
97 hexStream << " " << buff;
98 }
99
100 return hexStream.str();
101 }

◆ hexDump() [2/3]

std::string utils::hexDump ( const std::string & string,
int prefixLength = 0,
bool prefixAtFirstLine = false )

Definition at line 40 of file hexdump.cpp.

40 {
41 return hexDump(string.data(), string.length(), prefixLength, prefixAtFirstLine);
42 }
std::string hexDump(const std::vector< char > &bytes, int prefixLength, bool prefixAtFirstLine)
Definition hexdump.cpp:36

◆ hexDump() [3/3]

std::string utils::hexDump ( const std::vector< char > & bytes,
int prefixLength = 0,
bool prefixAtFirstLine = false )

Definition at line 36 of file hexdump.cpp.

36 {
37 return hexDump(bytes.data(), bytes.size(), prefixLength, prefixAtFirstLine);
38 }

◆ makeApp()

static const std::shared_ptr< CLI::App > utils::makeApp ( )
static

Definition at line 71 of file Config.cpp.

71 { // NO_LINT
72 const std::shared_ptr<CLI::App> app = std::make_shared<CLI::App>();
73
74 app->configurable(false);
75 app->allow_extras();
76 app->allow_config_extras();
77
78 const std::shared_ptr<CLI::HelpFormatter> helpFormatter = std::make_shared<CLI::HelpFormatter>();
79
80 helpFormatter->label("SUBCOMMAND", "INSTANCE");
81 helpFormatter->label("SUBCOMMANDS", "INSTANCES");
82 helpFormatter->label("PERSISTENT", "");
83 helpFormatter->label("Persistent Options", "Options (persistent)");
84 helpFormatter->label("Nonpersistent Options", "Options (nonpersistent)");
85 helpFormatter->label("Usage", "\nUsage");
86 helpFormatter->label("bool:{true,false}", "{true,false}");
87 helpFormatter->label(":{standard,required,full,default}", "{standard,required,full,default}");
88 helpFormatter->label(":{standard,expanded}", "{standard,expanded}");
89 helpFormatter->column_width(7);
90
91 app->formatter(helpFormatter);
92
93 app->config_formatter(std::make_shared<CLI::ConfigFormatter>());
94 app->get_config_formatter_base()->arrayDelimiter(' ');
95
96 app->option_defaults()->take_last();
97 app->option_defaults()->group(app->get_formatter()->get_label("Nonpersistent Options"));
98
100
101 return app;
102 }
static void init()
Definition Logger.cpp:32

References logger::Logger::init().

Here is the call graph for this function:

◆ makeSectionFormatter()

static const std::shared_ptr< CLI::HelpFormatter > utils::makeSectionFormatter ( )
static

Definition at line 633 of file Config.cpp.

633 {
634 const std::shared_ptr<CLI::HelpFormatter> sectionFormatter = std::make_shared<CLI::HelpFormatter>();
635
636 sectionFormatter->label("SUBCOMMAND", "SECTION");
637 sectionFormatter->label("SUBCOMMANDS", "SECTIONS");
638 sectionFormatter->label("PERSISTENT", "");
639 sectionFormatter->label("Persistent Options", "Options (persistent)");
640 sectionFormatter->label("Nonpersistent Options", "Options (nonpersistent)");
641 sectionFormatter->label("Usage", "\nUsage");
642 sectionFormatter->label("bool:{true,false}", "{true,false}");
643 sectionFormatter->label(":{standard,required,full,default}", "{standard,required,full,default}");
644 sectionFormatter->label(":{standard,expanded}", "{standard,expanded}");
645 sectionFormatter->column_width(7);
646
647 return sectionFormatter;
648 }

◆ operator*()

Timeval utils::operator* ( double mul,
const utils::Timeval & timeVal )

Definition at line 201 of file Timeval.cpp.

201 {
202 utils::Timeval help;
203
204 help.timeVal.tv_sec = static_cast<time_t>(static_cast<double>(timeVal.timeVal.tv_sec) * mul);
205 help.timeVal.tv_usec = static_cast<suseconds_t>(static_cast<double>(timeVal.timeVal.tv_usec) * mul);
206
207 return help.normalize();
208 }
timeval timeVal
Definition Timeval.h:77
const Timeval & normalize()
Definition Timeval.cpp:182

◆ operator<<()

std::ostream & utils::operator<< ( std::ostream & ostream,
const utils::Timeval & timeVal )

Definition at line 196 of file Timeval.cpp.

196 {
197 return ostream << std::string("{") + std::to_string(timeVal.timeVal.tv_sec) + std::string(":") +
198 std::to_string(timeVal.timeVal.tv_usec) + std::string("}");
199 }

◆ sha1()

std::vector< unsigned char > utils::sha1 ( const std::string & string)

Definition at line 267 of file sha1.cpp.

267 {
268 SHA1 checksum;
269 checksum.update(string);
270 return transform_to_binary(checksum.final());
271 }
std::string final()
Definition sha1.cpp:79
void update(const std::string &s)
Definition sha1.cpp:57
static std::vector< unsigned char > transform_to_binary(const std::string &string)
Definition sha1.cpp:251

◆ transform_to_binary()

static std::vector< unsigned char > utils::transform_to_binary ( const std::string & string)
static

Definition at line 251 of file sha1.cpp.

251 {
252 std::vector<unsigned char> buf;
253
254 char hex_byte[3];
255 hex_byte[2] = 0;
256
257 for (std::size_t i = 0, j = 0; i < string.size(); i += 2, j += 1) {
258 hex_byte[0] = string.at(i);
259 hex_byte[1] = string.at(i + 1);
260 char* end_ptr = nullptr;
261 buf.push_back(static_cast<unsigned char>(strtoul(hex_byte, &end_ptr, 16)));
262 }
263
264 return buf;
265 }