YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef UTILS_H
7#define UTILS_H
8
9#include <string>
10
11//adds the symbol "*" at the beginning of each line, as requested by doxygen format
12inline std::string doxygenize_string (std::string s)
13{
14 for (size_t i = 0; i < s.length(); ++i) {
15 if (s[i] == '\n') {
16 s.insert(i + 1, 1, '*');
17 ++i;
18 }
19 }
20 return s;
21}
22
23//generate a string containing the current date and time
24inline std::string current_time()
25{
26 auto now = std::chrono::system_clock::now();
27 std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
28 std::string currentTimeString = std::ctime(&currentTime);
29 return std::string ("// Generated on: ") + currentTimeString + std::string("\n");
30}
31
32//return true if the string does not contain any valid character
33inline bool containsOnlySymbols(const std::string& str) {
34 for (char ch : str) {
35 if (std::isalnum(static_cast<unsigned char>(ch))) {
36 return false; // If any alphanumeric character is found, return false
37 }
38 }
39 return true; // If all characters are symbols, return true
40}
41
42//remove all traling/leading spaces from a string
43inline std::string trimSpaces(const std::string& str) {
44 size_t firstNonSpace = str.find_first_not_of(" \t");
45 size_t lastNonSpace = str.find_last_not_of(" \t");
46
47 if (firstNonSpace != std::string::npos && lastNonSpace != std::string::npos) {
48 return str.substr(firstNonSpace, lastNonSpace - firstNonSpace + 1);
49 }
50 else {
51 return ""; // If string contains only spaces or is empty, return an empty string
52 }
53}
54
55//add the escape character in front of each special character of a string
56inline std::string escapeQuotes(const std::string& str)
57{
58 std::string result;
59 for (char c : str) {
60 if (c == '"') {
61 result.push_back('\\');
62 }
63 result.push_back(c);
64 }
65 return result;
66}
67
68#endif
bool containsOnlySymbols(const std::string &str)
Definition utils.h:33
std::string trimSpaces(const std::string &str)
Definition utils.h:43
std::string doxygenize_string(std::string s)
Definition utils.h:12
std::string current_time()
Definition utils.h:24
std::string escapeQuotes(const std::string &str)
Definition utils.h:56