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 493 of file Config.cpp.

493 {
494 std::stringstream out;
495
496 createCommandLineOptions(out, app, mode);
497
498 std::string optionString = out.str();
499 if (optionString.back() == ' ') {
500 optionString.pop_back();
501 }
502
503 return optionString;
504 }
static void createCommandLineOptions(std::stringstream &out, CLI::App *app, CLI::CallForCommandline::Mode mode)
Definition Config.cpp:431

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 431 of file Config.cpp.

431 {
432 CLI::Option* disabledOpt = app->get_option_no_throw("--disabled");
433 const bool disabled = disabledOpt != nullptr ? disabledOpt->as<bool>() : false;
434 if (!disabled || mode == CLI::CallForCommandline::Mode::DEFAULT) {
435 for (const CLI::Option* option : app->get_options()) {
436 if (option->get_configurable()) {
437 std::string value;
438
439 switch (mode) {
441 if (option->count() > 0) {
442 value = option->as<std::string>();
443 } else if (option->get_required()) {
444 value = "<REQUIRED>";
445 }
446 break;
448 if (option->get_required()) {
449 if (option->count() > 0) {
450 value = option->as<std::string>();
451 } else {
452 value = "<REQUIRED>";
453 }
454 }
455 break;
457 if (option->count() > 0) {
458 value = option->as<std::string>();
459 } else if (!option->get_default_str().empty()) {
460 value = option->get_default_str();
461 } else if (!option->get_required()) {
462 value = "\"\"";
463 } else {
464 value = "<REQUIRED>";
465 }
466 break;
468 if (!option->get_default_str().empty()) {
469 value = option->get_default_str();
470 } else if (!option->get_required()) {
471 value = "\"\"";
472 } else {
473 value = "<REQUIRED>";
474 }
475 break;
476 }
477 }
478
479 if (!value.empty()) {
480 if (value.starts_with("[") && value.ends_with("]")) {
481 value = value.substr(1, value.size() - 2);
482 }
483
484 out << "--" << option->get_single_name() << ((option->get_items_expected_max() == 0) ? "=" : " ") << value << " ";
485 }
486 }
487 }
488 } else if (disabledOpt->get_default_str() == "false") {
489 out << "--disabled=true ";
490 }
491 }

References CLI::CallForCommandline::DEFAULT, CLI::CallForCommandline::FULL, CLI::CallForCommandline::REQUIRED, and CLI::CallForCommandline::STANDARD.

Referenced by createCommandLineOptions().

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 508 of file Config.cpp.

508 {
509 std::stringstream out;
510
511 CLI::Option* disabledOpt = app->get_option_no_throw("--disabled");
512 if (disabledOpt == nullptr || !disabledOpt->as<bool>() || mode == CLI::CallForCommandline::Mode::DEFAULT) {
513 for (CLI::App* subcommand : app->get_subcommands({})) {
514 if (!subcommand->get_name().empty()) {
515 createCommandLineTemplate(out, subcommand, mode);
516 }
517 }
518 }
519
520 return out.str();
521 }

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 538 of file Config.cpp.

538 {
539 std::stringstream out;
540
541 createCommandLineTemplate(out, app, mode);
542
543 std::string outString = out.str();
544 while (app->get_parent() != nullptr) {
545 app = app->get_parent();
546 std::string parentOptions = createCommandLineOptions(app, mode);
547 outString =
548 std::string(app->get_name()).append(" ").append(!parentOptions.empty() ? parentOptions.append(" ") : "").append(outString);
549 }
550
551 if (outString.empty()) {
552 outString = Config::getApplicationName();
553 }
554
555 return outString;
556 }
static void createCommandLineTemplate(std::stringstream &out, CLI::App *app, CLI::CallForCommandline::Mode mode)
Definition Config.cpp:523

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 523 of file Config.cpp.

523 {
524 std::string outString;
525
526 outString = createCommandLineOptions(app, mode);
527 if (!outString.empty()) {
528 outString += " ";
529 }
530
531 outString += createCommandLineSubcommands(app, mode);
532
533 if (!outString.empty()) {
534 out << app->get_name() << " " << outString;
535 }
536 }

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 }

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 const std::shared_ptr< CLI::App > utils::makeApp ( )
static

Definition at line 93 of file Config.cpp.

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

◆ makeSectionFormatter()

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

Definition at line 659 of file Config.cpp.

659 {
660 const std::shared_ptr<CLI::HelpFormatter> sectionFormatter = std::make_shared<CLI::HelpFormatter>();
661
662 sectionFormatter->label("SUBCOMMAND", "SECTION");
663 sectionFormatter->label("SUBCOMMANDS", "SECTIONS");
664 sectionFormatter->label("PERSISTENT", "");
665 sectionFormatter->label("Persistent Options", "Options (persistent)");
666 sectionFormatter->label("Nonpersistent Options", "Options (nonpersistent)");
667 sectionFormatter->label("Usage", "\nUsage");
668 sectionFormatter->label("bool:{true,false}", "{true,false}");
669 sectionFormatter->label(":{standard,required,full,default}", "{standard,required,full,default}");
670 sectionFormatter->label(":{standard,expanded}", "{standard,expanded}");
671 sectionFormatter->column_width(7);
672
673 return sectionFormatter;
674 }

◆ 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 transform_to_binary().

Here is the call 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: