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

Namespaces

namespace  system

Classes

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

Concepts

concept  InjectableAttribute

Functions

std::vector< unsigned char > sha1 (const std::string &string)
static std::shared_ptr< CLI::App > makeApp ()
static std::string bash_backslash_escape_no_whitespace (std::string_view s)
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 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

◆ bash_backslash_escape_no_whitespace()

std::string utils::bash_backslash_escape_no_whitespace ( std::string_view s)
static

Definition at line 433 of file Config.cpp.

433 {
434 static const std::unordered_set<char> special{
435 '\\', '\'', // quoting/escape
436 '`', '$', // substitution
437 '|', '&', ';', '<', '>', '(', ')', '{', '}', // operators
438 '*', '?', '[', ']', '~', '!', '#', '=' // globbing/history/others
439 };
440
441 std::string out;
442 out.reserve(s.size() * 3);
443
444 for (const char c : s) {
445 if (special.contains(c)) {
446 out.push_back('\\');
447 }
448 out.push_back(c);
449 }
450 return out;
451 }

Referenced by createCommandLineOptions().

Here is the caller graph for this function:

◆ createCommandLineOptions() [1/2]

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

Definition at line 518 of file Config.cpp.

518 {
519 std::stringstream out;
520
521 createCommandLineOptions(out, app, mode);
522
523 std::string optionString = out.str();
524 if (!optionString.empty() && optionString.back() == ' ') {
525 optionString.pop_back();
526 }
527
528 return optionString;
529 }
static void createCommandLineOptions(std::stringstream &out, CLI::App *app, CLI::CallForCommandline::Mode mode)
Definition Config.cpp:453

References createCommandLineOptions().

Referenced by createCommandLineTemplate(), and createCommandLineTemplate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createCommandLineOptions() [2/2]

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

Definition at line 453 of file Config.cpp.

453 {
454 CLI::Option* disabledOpt = app->get_option_no_throw("--disabled");
455 const bool disabled = disabledOpt != nullptr ? disabledOpt->as<bool>() : false;
456 if (!disabled || mode == CLI::CallForCommandline::Mode::DEFAULT) {
457 for (const CLI::Option* option : app->get_options()) {
458 if (option->get_configurable()) {
459 std::string value;
460
461 switch (mode) {
463 if (option->count() > 0) {
464 value = option->as<std::string>();
465 } else if (option->get_required()) {
466 value = "<REQUIRED>";
467 }
468 break;
470 if (option->get_required()) {
471 if (option->count() > 0) {
472 value = option->as<std::string>();
473 } else {
474 value = "<REQUIRED>";
475 }
476 }
477 break;
479 if (option->count() > 0) {
480 value = option->as<std::string>();
481 } else if (!option->get_default_str().empty()) {
482 value = option->get_default_str();
483 } else if (!option->get_required()) {
484 value = "\"\"";
485 } else {
486 value = "<REQUIRED>";
487 }
488 break;
490 if (!option->get_default_str().empty()) {
491 value = option->get_default_str();
492 } else if (!option->get_required()) {
493 value = "\"\"";
494 } else {
495 value = "<REQUIRED>";
496 }
497 break;
498 }
499 }
500
501 if (!value.empty()) {
502 if (value.starts_with("[") && value.ends_with("]")) {
503 value = value.substr(1, value.size() - 2);
504 }
505
506 if (value != "<REQUIRED>" && value != "\"\"") {
508 }
509 out << "--" << option->get_single_name() << ((option->get_items_expected_max() == 0) ? "=" : " ") << value << " ";
510 }
511 }
512 }
513 } else if (disabledOpt->get_default_str() == "false") {
514 out << "--disabled=true ";
515 }
516 }
static std::string bash_backslash_escape_no_whitespace(std::string_view s)
Definition Config.cpp:433

References bash_backslash_escape_no_whitespace(), CLI::CallForCommandline::DEFAULT, CLI::CallForCommandline::FULL, CLI::CallForCommandline::REQUIRED, and CLI::CallForCommandline::STANDARD.

Referenced by createCommandLineOptions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createCommandLineSubcommands()

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

Definition at line 533 of file Config.cpp.

533 {
534 std::stringstream out;
535
536 CLI::Option* disabledOpt = app->get_option_no_throw("--disabled");
537 if (disabledOpt == nullptr || !disabledOpt->as<bool>() || mode == CLI::CallForCommandline::Mode::DEFAULT) {
538 for (CLI::App* subcommand : app->get_subcommands({})) {
539 if (!subcommand->get_name().empty()) {
540 createCommandLineTemplate(out, subcommand, mode);
541 }
542 }
543 }
544
545 return out.str();
546 }
static void createCommandLineTemplate(std::stringstream &out, CLI::App *app, CLI::CallForCommandline::Mode mode)
Definition Config.cpp:548

References createCommandLineTemplate(), and CLI::CallForCommandline::DEFAULT.

Referenced by createCommandLineTemplate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createCommandLineTemplate() [1/2]

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

Definition at line 563 of file Config.cpp.

563 {
564 std::stringstream out;
565
566 createCommandLineTemplate(out, app, mode);
567
568 std::string outString = out.str();
569 while (app->get_parent() != nullptr) {
570 app = app->get_parent();
571 std::string parentOptions = createCommandLineOptions(app, mode);
572 outString =
573 std::string(app->get_name()).append(" ").append(!parentOptions.empty() ? parentOptions.append(" ") : "").append(outString);
574 }
575
576 if (outString.empty()) {
577 outString = Config::getApplicationName();
578 }
579
580 return outString;
581 }
static std::string getApplicationName()
Definition Config.cpp:1000

References createCommandLineOptions(), createCommandLineTemplate(), and utils::Config::getApplicationName().

Referenced by utils::Config::parse2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createCommandLineTemplate() [2/2]

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

Definition at line 548 of file Config.cpp.

548 {
549 std::string outString;
550
551 outString = createCommandLineOptions(app, mode);
552 if (!outString.empty()) {
553 outString += " ";
554 }
555
556 outString += createCommandLineSubcommands(app, mode);
557
558 if (!outString.empty()) {
559 out << app->get_name() << " " << outString;
560 }
561 }
static std::string createCommandLineSubcommands(CLI::App *app, CLI::CallForCommandline::Mode mode)
Definition Config.cpp:533

References createCommandLineOptions(), and createCommandLineSubcommands().

Referenced by createCommandLineSubcommands(), and createCommandLineTemplate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 66 of file hexdump.cpp.

66 {
67 std::stringstream hexStream;
68
69 if (length > 0) {
70 uint8_t buff[17];
71 size_t i = 0;
72
73 hexStream << std::hex;
74
75 int currentPrefixLength = prefixAtFirstLine ? prefixLength : 0;
76
77 // Process every byte in the data.
78 for (i = 0; i < length; i++) {
79 // Multiple of 16 means new line (with line offset).
80
81 if ((i % 16) == 0) {
82 // Just don't print ASCII for the zeroth line.
83 if (i != 0) {
84 hexStream << " " << buff << std::endl;
85 }
86
87 // Output the offset.
88 hexStream << Color::Code::FG_BLUE;
89 hexStream << std::setw(currentPrefixLength) << std::setfill(' ') << ""
90 << ": " << std::setw(8) << std::setfill('0') << static_cast<unsigned int>(i);
91 hexStream << Color::Code::FG_DEFAULT << " ";
92 }
93
94 // Now the hex code for the specific character.
95 hexStream << Color::Code::FG_GREEN;
96 hexStream << " " << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(static_cast<unsigned char>(bytes[i]));
97 hexStream << Color::Code::FG_DEFAULT;
98
99 // And store a printable ASCII character for later.
100 if ((bytes[i] < 0x20) || (bytes[i] > 0x7e)) {
101 buff[i % 16] = '.';
102 } else {
103 buff[i % 16] = static_cast<uint8_t>(bytes[i]);
104 }
105 buff[(i % 16) + 1] = '\0';
106
107 currentPrefixLength = prefixLength;
108 }
109
110 hexStream << std::dec;
111
112 // Pad out last line if not exactly 16 characters.
113 while ((i % 16) != 0) {
114 hexStream << " ";
115 i++;
116 }
117
118 // And print the final ASCII bit.
119 hexStream << " " << buff;
120 }
121
122 return hexStream.str();
123 }
@ FG_DEFAULT
Definition Logger.h:69

References Color::FG_BLUE, Color::FG_DEFAULT, and Color::FG_GREEN.

Referenced by hexDump(), hexDump(), web::websocket::Receiver::readPayload(), and web::websocket::Transmitter::sendFrame().

Here is the caller graph for this function:

◆ hexDump() [2/3]

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

Definition at line 62 of file hexdump.cpp.

62 {
63 return hexDump(string.data(), string.length(), prefixLength, prefixAtFirstLine);
64 }
std::string hexDump(const std::vector< char > &bytes, int prefixLength, bool prefixAtFirstLine)
Definition hexdump.cpp:58

References hexDump().

Here is the call graph for this function:

◆ hexDump() [3/3]

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

Definition at line 58 of file hexdump.cpp.

58 {
59 return hexDump(bytes.data(), bytes.size(), prefixLength, prefixAtFirstLine);
60 }

References hexDump().

Referenced by iot::mqtt::SubProtocol< WSSubProtocolRoleT >::onMessageData(), iot::mqtt::Mqtt::toHexString(), httputils::toString(), and httputils::toString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ makeApp()

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

Definition at line 101 of file Config.cpp.

101 { // NO_LINT
102 const std::shared_ptr<CLI::App> app = std::make_shared<CLI::App>();
103
104 app->configurable(false);
105 app->allow_extras();
106 app->allow_config_extras();
107
108 const std::shared_ptr<CLI::HelpFormatter> helpFormatter = std::make_shared<CLI::HelpFormatter>();
109
110 helpFormatter->label("SUBCOMMAND", "INSTANCE");
111 helpFormatter->label("SUBCOMMANDS", "INSTANCES");
112 helpFormatter->label("PERSISTENT", "");
113 helpFormatter->label("Persistent Options", "Options (persistent)");
114 helpFormatter->label("Nonpersistent Options", "Options (nonpersistent)");
115 helpFormatter->label("Usage", "\nUsage");
116 helpFormatter->label("bool:{true,false}", "{true,false}");
117 helpFormatter->label(":{standard,required,full,default}", "{standard,required,full,default}");
118 helpFormatter->label(":{standard,exact,expanded}", "{standard,exact,expanded}");
119 helpFormatter->column_width(7);
120
121 app->formatter(helpFormatter);
122
123 app->config_formatter(std::make_shared<CLI::ConfigFormatter>());
124 app->get_config_formatter_base()->arrayDelimiter(' ');
125
126 app->option_defaults()->take_last();
127 app->option_defaults()->group(app->get_formatter()->get_label("Nonpersistent Options"));
128
130
131 return app;
132 }
static void init()
Definition Logger.cpp:54

References logger::Logger::init().

Here is the call graph for this function:

◆ makeSectionFormatter()

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

Definition at line 717 of file Config.cpp.

717 {
718 const std::shared_ptr<CLI::HelpFormatter> sectionFormatter = std::make_shared<CLI::HelpFormatter>();
719
720 sectionFormatter->label("SUBCOMMAND", "SECTION");
721 sectionFormatter->label("SUBCOMMANDS", "SECTIONS");
722 sectionFormatter->label("PERSISTENT", "");
723 sectionFormatter->label("Persistent Options", "Options (persistent)");
724 sectionFormatter->label("Nonpersistent Options", "Options (nonpersistent)");
725 sectionFormatter->label("Usage", "\nUsage");
726 sectionFormatter->label("bool:{true,false}", "{true,false}");
727 sectionFormatter->label(":{standard,required,full,default}", "{standard,required,full,default}");
728 sectionFormatter->label(":{standard,exact,expanded}", "{standard,exact,expanded}");
729 sectionFormatter->column_width(7);
730
731 return sectionFormatter;
732 }

◆ operator*()

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

Definition at line 223 of file Timeval.cpp.

223 {
224 utils::Timeval help;
225
226 help.timeVal.tv_sec = static_cast<time_t>(static_cast<double>(timeVal.timeVal.tv_sec) * mul);
227 help.timeVal.tv_usec = static_cast<suseconds_t>(static_cast<double>(timeVal.timeVal.tv_usec) * mul);
228
229 return help.normalize();
230 }
timeval timeVal
Definition Timeval.h:99
const Timeval & normalize()
Definition Timeval.cpp:204

◆ operator<<()

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

Definition at line 218 of file Timeval.cpp.

218 {
219 return ostream << std::string("{") + std::to_string(timeVal.timeVal.tv_sec) + std::string(":") +
220 std::to_string(timeVal.timeVal.tv_usec) + std::string("}");
221 }

◆ 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

References utils::SHA1::final(), and transform_to_binary().

Here is the call graph for this function:

◆ transform_to_binary()

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 }

Referenced by sha1().

Here is the caller graph for this function: