ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
Utilities.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#include "Utilities.h"
4
5#include "EntityOwner.h"
6
7#include <boost/algorithm/algorithm.hpp>
8#include <boost/format.hpp>
9
10#include <pthread.h>
11
12#include <cassert>
13#include <iostream>
14
15namespace ChimeraTK::Utilities {
16
17 /********************************************************************************************************************/
18
19 std::string getUnqualifiedName(const std::string& qualifiedName) {
20 auto found = qualifiedName.find_last_of('/');
21 if(found == std::string::npos) {
22 return qualifiedName;
23 }
24 return qualifiedName.substr(found + 1);
25 }
26
27 /********************************************************************************************************************/
28
29 std::string getPathName(const std::string& qualifiedName) {
30 auto found = qualifiedName.find_last_of('/');
31 if(found == std::string::npos) {
32 return ".";
33 }
34 return qualifiedName.substr(0, found);
35 }
36
37 /********************************************************************************************************************/
38
39 namespace {
40 constexpr std::string_view legalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
41 } // namespace
42
43 /********************************************************************************************************************/
44
45 std::string escapeName(const std::string& name, bool allowDotsAndSlashes) {
46 std::string myLegalChars(legalChars);
47
48 // remove underscore from legal chars, as we use it as an escape character
49 assert(myLegalChars[myLegalChars.size() - 1] == '_');
50 myLegalChars.resize(myLegalChars.size() - 1);
51
52 if(allowDotsAndSlashes) {
53 myLegalChars += "./";
54 }
55
56 std::string name_stripped;
57 size_t i;
58 size_t iLast = 0;
59 while((i = name.find_first_not_of(myLegalChars, iLast)) != std::string::npos) {
60 name_stripped += name.substr(iLast, i - iLast);
61 name_stripped += (boost::format("_%03d") % int(name[i])).str();
62 iLast = i + 1;
63 }
64 name_stripped += name.substr(iLast);
65
66 return name_stripped;
67 }
68
69 /********************************************************************************************************************/
70
71 std::string unescapeName(const std::string& name_stripped) {
72 std::string name;
73 size_t i;
74 size_t iLast = 0;
75 while((i = name_stripped.find('_', iLast)) != std::string::npos) {
76 name += name_stripped.substr(iLast, i - iLast);
77 char code = char(std::stoi(name_stripped.substr(i + 1, 3)));
78 name += code;
79 iLast = i + 4;
80 }
81 name += name_stripped.substr(iLast);
82
83 return name;
84 }
85
86 /********************************************************************************************************************/
87
88 bool checkName(const std::string& name, bool allowDotsAndSlashes) {
89 if(name == EntityOwner::namePrefixConstant.substr(1)) { // namePrefixConstant starts with /
90 // EntityOwner::namePrefixConstant violates the allowed characters, so we need to make an exception here.
91 return true;
92 }
93
94 std::string myLegalChars(legalChars);
95
96 if(allowDotsAndSlashes) {
97 myLegalChars += "./";
98 }
99
100 return (name.find_first_not_of(myLegalChars) == std::string::npos);
101 }
102
103 /********************************************************************************************************************/
104
105 void setThreadName(const std::string& name) {
106#if defined(__linux__)
107 pthread_setname_np(pthread_self(), name.substr(0, std::min<std::string::size_type>(name.length(), 15)).c_str());
108#elif defined(__APPLE__)
109 pthread_setname_np(name.substr(0, std::min<std::string::size_type>(name.length(), 15)).c_str());
110#endif
111 }
112
113 /********************************************************************************************************************/
114
115 // removes trailing slashes, used for variable names
116 // raises for multiple slashes
117 std::string raiseIftrailingSlash(const std::string& name, bool isModule) {
118 if(isModule && name == "/") {
119 return name;
120 }
121 if(boost::ends_with(name, "/")) {
122 throw ChimeraTK::logic_error(name + ": " + (isModule ? "module" : "variable") + " names cannot end with /");
123 }
124 if(name.find("//") != std::string::npos) {
125 throw ChimeraTK::logic_error(name + " variable names cannot contain consecutive slashes");
126 }
127 return name;
128 }
129
130 /********************************************************************************************************************/
131
133 try {
134 std::ifstream status_file("/proc/self/status");
135 std::string line;
136 while(std::getline(status_file, line)) {
137 if(line.substr(0, 10) == "TracerPid:") {
138 int tracer_pid = std::stoi(line.substr(10));
139 return tracer_pid != 0;
140 }
141 }
142 return false;
143 }
144 catch(...) {
145 // assume no debugger if anything goes wrong (e.g. on different platforms)
146 return false;
147 }
148 }
149
150 /********************************************************************************************************************/
151
152} // namespace ChimeraTK::Utilities
static constexpr std::string_view namePrefixConstant
Prefix for constants created by constant().
std::string raiseIftrailingSlash(const std::string &name, bool isModule)
Raises logic error if name ends in a slash, or if it contains consecutive slashes.
Definition Utilities.cc:117
bool checkName(const std::string &name, bool allowDotsAndSlashes)
Check given name for characters which are not allowed in variable or module names.
Definition Utilities.cc:88
std::string escapeName(const std::string &name, bool allowDotsAndSlashes)
Convert all characters which are not allowed in variable or module names into underscores followed by...
Definition Utilities.cc:45
std::string getPathName(const std::string &qualifiedName)
Return all but the last components of the given qualified name.
Definition Utilities.cc:29
void setThreadName(const std::string &name)
Set name of the current thread.
Definition Utilities.cc:105
std::string getUnqualifiedName(const std::string &qualifiedName)
Return the last component of the given qualified path name.
Definition Utilities.cc:19
bool isBeingDebugged()
Checks whether the current process is being debugged.
Definition Utilities.cc:132
std::string unescapeName(const std::string &name_stripped)
Undo the escaping done by escapeName().
Definition Utilities.cc:71