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

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::vector< unsigned char > sha1 (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()

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

Definition at line 434 of file Config.cpp.

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

Referenced by createCommandLineOptions().

Here is the caller graph for this function:

◆ createCommandLineOptions() [1/2]

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

Definition at line 519 of file Config.cpp.

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

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]

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

Definition at line 454 of file Config.cpp.

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

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()

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

Definition at line 534 of file Config.cpp.

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

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]

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

Definition at line 564 of file Config.cpp.

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

References createCommandLineOptions(), and createCommandLineTemplate().

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

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

◆ createCommandLineTemplate() [2/2]

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

Definition at line 549 of file Config.cpp.

549 {
550 std::string outString;
551
552 outString = createCommandLineOptions(app, mode);
553 if (!outString.empty()) {
554 outString += " ";
555 }
556
557 outString += createCommandLineSubcommands(app, mode);
558
559 if (!outString.empty()) {
560 out << app->get_name() << " " << outString;
561 }
562 }

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 }

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()

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

Definition at line 95 of file Config.cpp.

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

References logger::Logger::init().

Here is the call graph for this function:

◆ makeSectionFormatter()

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

Definition at line 688 of file Config.cpp.

688 {
689 const std::shared_ptr<CLI::HelpFormatter> sectionFormatter = std::make_shared<CLI::HelpFormatter>();
690
691 sectionFormatter->label("SUBCOMMAND", "SECTION");
692 sectionFormatter->label("SUBCOMMANDS", "SECTIONS");
693 sectionFormatter->label("PERSISTENT", "");
694 sectionFormatter->label("Persistent Options", "Options (persistent)");
695 sectionFormatter->label("Nonpersistent Options", "Options (nonpersistent)");
696 sectionFormatter->label("Usage", "\nUsage");
697 sectionFormatter->label("bool:{true,false}", "{true,false}");
698 sectionFormatter->label(":{standard,required,full,default}", "{standard,required,full,default}");
699 sectionFormatter->label(":{standard,exact,expanded}", "{standard,exact,expanded}");
700 sectionFormatter->column_width(7);
701
702 return sectionFormatter;
703 }

◆ 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(), transform_to_binary(), and utils::SHA1::update().

Referenced by base64::serverWebSocketKey().

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

◆ 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 }

Referenced by sha1().

Here is the caller graph for this function: