ATLAS Offline Software
Loading...
Searching...
No Matches
IOVDbStringFunctions.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
5#include <regex>
6#include <iostream>
7#include <algorithm>
8
9namespace IOVDbNamespace{
10 std::string
11 spaceStrip( const std::string & input){
12 // return the input string stripped of leading/trailing spaces
13 std::string::size_type idx1=input.find_first_not_of(" \n\r\t");
14 std::string::size_type idx2=input.find_last_not_of(" \n\r\t");
15 if (idx1==std::string::npos || idx2==std::string::npos) {
16 return "";
17 } else {
18 return std::string(input.substr(idx1,1+idx2-idx1));
19 }
20 }
21
22 int
23 makeChannel(const std::string& strval, const int defchan) {
24 // construct a cool channelId from the string value (numeric)
25 // if empty, use the default value
26 if (!strval.empty()) return std::stoi(strval);
27 return defchan;
28 }
29
30 unsigned long long
31 iovFromTimeString(const std::string & iovString){
32 if (iovString.empty()) return 0LL;
33 unsigned long long time=std::stoi(iovString);
34 return time*1000000000LL;
35 }
36
37 unsigned long long
38 iovFromRunString(const std::string & runString){
39 if (runString.empty()) return 0LL;
40 unsigned long long run=std::stoi(runString);
41 return run<<32LL;
42 }
43
44 unsigned long long
45 iovFromLumiBlockString(const std::string & lbString){
46 if (lbString.empty()) return 0LL;
47 unsigned long long lb=std::stoll(lbString.c_str());
48 return lb;
49 }
50
51 std::string
52 sanitiseJsonString(const std::string & dataString){
53 const std::string regex="\n";
54 const std::regex lf(regex);
55 return std::regex_replace(dataString,lf,"\\n");
56 }
57
58 int
59 parseClid(const std::string & addrHeaderStr){
60 //string of form
61 //<addrHeader><address_header service_type="256" clid="12345" /></addrHeader>
62 std::string regex=R"delim(clid\s*=\s*"([0-9]*)")delim";
63 std::regex re(regex);
64 std::smatch clidMatch;
65 bool match=std::regex_search(addrHeaderStr, clidMatch,re);
66 return (match) ? std::stoi(clidMatch[1]) : -1;
67 }
68
69 std::string
70 parseTypename(const std::string & description){
71 std::string regex=R"delim(<typeName>\s*([^\s]+)\s*</typeName>)delim";
72 std::regex re(regex);
73 std::smatch typeMatch;
74 bool match=std::regex_search(description, typeMatch,re);
75 return (match) ? std::string(typeMatch[1]) : std::string("");
76 }
77
78 std::string
79 deleteRegex(const std::string & original, const std::string & regex){
80 const std::regex delre(regex);
81 return std::regex_replace(original,delre,"");
82 }
83
84 std::string
85 quote(const std::string & sentence){
86 std::string out;
87 out.reserve(sentence.size() + 2);
88 out += '\"';
89 out += sentence;
90 out += '\"';
91 return out;
92 }
93
94 std::string
95 unescapeQuotes(const std::string & original){
96 const std::string regex=R"delim(\\")delim";
97 std::regex re(regex);
98 return std::regex_replace(original, re,"\"");
99 }
100
101 std::string
102 unescapeBackslash(const std::string & original){
103 const std::string regex=R"delim([\\]+)delim";
104 const std::string replace=R"delim(\)delim";
105 const std::regex re(regex);
106 return std::regex_replace(original, re,replace);
107 }
108
109 std::string
110 sanitiseFilename(const std::string & fname){
111 std::string newName{fname};
112 auto oldEnd = newName.end();
113 auto newEnd = std::remove(newName.begin(), oldEnd, '/');
114 //keep this line for reference
115 //std::replace(newName.begin(), newName.end(), '/', '^');
116 newName.erase(newEnd, newName.end());
117 return newName;
118 }
119
120 std::string
121 sanitiseCrestTag(const std::string & fname){
122 return sanitiseFilename(fname);
123 }
124
125 std::string
126 replaceNULL(const std::string & possibleNULL){
127 const std::string &original{possibleNULL};
128 const std::string regex=R"delim( NULL)delim";
129 const std::regex nullre(regex);
130 return std::regex_replace(original,nullre," null");
131 }
132
133 std::string
134 sanitiseXml(const std::string & pseudoXmlString){
135 std::string result;
136 unsigned int strSize(pseudoXmlString.size());
137 unsigned int bufsize(strSize*1.1);
138 result.reserve(bufsize);
139 for(size_t pos = 0; pos != strSize; ++pos) {
140 switch(pseudoXmlString[pos]) {
141 case '\"': result.append("\\\""); break;
142
143 default: result.append(&pseudoXmlString[pos], 1); break;
144 }
145 }
146 return result;
147 }
148
149 bool
150 looksLikeMagicTag(const std::string & candidateTag){
151 return (candidateTag.compare(0,7, "TagInfo")==0 and
152 candidateTag.find('/')!=std::string::npos);
153 }
154
155 bool
156 tagIsMagic(const std::string & candidateTag){
157 const std::string regex=R"delim(TagInfo(Major|Minor)/.*)delim";
158 const std::regex magicx(regex);
159 return std::regex_match(candidateTag, magicx);
160 }
161
162 std::vector<std::string>
163 parseMagicTag(const std::string & v){
164 std::vector<std::string> result;
165 std::string regex7=R"delim(TagInfo(Major|Minor)/([^/]*)/?([^/]*)?)delim";
166 std::regex matchmagic(regex7);
167 std::smatch x;
168 bool foundmagic=std::regex_match(v,x,matchmagic);
169 if (foundmagic){
170 for (const auto & i:x)
171 if (i!="") result.push_back(i);
172 }
173 return result;
174 }
175
176 std::vector<std::string>
177 parseLinkNames(const std::string &linktext){
178 std::vector<std::string> v{};
179 if (linktext.empty()) return v;
180 //regex:
181 //(anything except colon, multiple times) then _possibly_ (two colons and string of anything except colons)
182 // anything except colon) then (colon or end-of-line)
183 const std::string linkRegexStr{"([^:]*(::[^:]*)?)(:|$)"};
184 std::regex linkMatchSpec(linkRegexStr);
185 //give a token iterator using the regex and returning the first substring (i.e. the
186 //bit before a single colon or line end, which would be for example "ALink" or "MyContext::AnotherLink" )
187 std::sregex_token_iterator pos(linktext.cbegin(), linktext.cend(),linkMatchSpec,{1});
188 std::sregex_token_iterator e;
189 for (;pos!=e;++pos) {
190 // the resulting text _should not_ contain spaces, but strip it to be sure.
191 if (not pos->str().empty()) v.push_back(spaceStrip(pos->str()));
192 }
193 return v;
194 }
195
196 std::pair<std::string, std::string>
197 tag2PrefixTarget(const std::vector<std::string> & tagParseResults){
198 std::pair<std::string, std::string> pair;
199 std::string &prefix{pair.first};
200 std::string &target{pair.second};
201 if (tagParseResults.size() == 4){ //4 is the size of result set if there is a prefix
202 prefix = tagParseResults[2]; //index of first path
203 target = tagParseResults[3]; //index of second path
204 } else {
205 target = tagParseResults[2];
206 }
207 return pair;
208 }
209
210 bool
211 replaceServiceType71(std::string & addrHeader){
212 const std::size_t svcType = addrHeader.find("service_type=\"71\"");
213 if (svcType != std::string::npos) {
214 addrHeader.replace(svcType, 17, "service_type=\"256\"");
215 return true;
216 }
217 return false;
218 }
219
220
221}
const boost::regex re(r_e)
#define x
STL class.
int lb
Definition globals.cxx:23
std::string description
glabal timer - how long have I taken so far?
Definition hcg.cxx:91
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:310
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:357
unsigned long long iovFromRunString(const std::string &runString)
Take a string run number and convert it to an ULL representing run<<32.
std::string deleteRegex(const std::string &original, const std::string &regex)
Delete characters of a matching regex from the input string.
std::string sanitiseFilename(const std::string &fname)
Replace the '/' of a file path with '^'.
std::string replaceNULL(const std::string &possibleNULL)
replace an uppercase NULL (such as returned by oracle) in string with a lowercase null (such as used ...
std::string quote(const std::string &sentence)
Enclose a string in ".
std::string unescapeBackslash(const std::string &original)
std::string unescapeQuotes(const std::string &original)
std::string sanitiseCrestTag(const std::string &fname)
return valid CREST tag name from folder name
unsigned long long iovFromLumiBlockString(const std::string &lbString)
String representation of lumiblock just converted to LL (as in original code) and returned as a ULL.
std::pair< std::string, std::string > tag2PrefixTarget(const std::vector< std::string > &tagParseResults)
Takes a vector<string> containing {"<fulltag>", "Major|Minor", "<prefix>", "<tag>"}...
std::string spaceStrip(const std::string &input)
Trim leading and trailing spaces,return a new trimmed string.
std::string parseTypename(const std::string &description)
Extract the typename from a folder description.
bool replaceServiceType71(std::string &addrHeader)
unsigned long long iovFromTimeString(const std::string &iovString)
Take a string integer giving a time in seconds and convert it to a ULL in nanoseconds.
bool tagIsMagic(const std::string &candidateTag)
Resolve magic tag.
int parseClid(const std::string &addrHeaderStr)
Extract the Class ID (an integer) from a string of form <addrHeader><address_header service_type="256...
std::string sanitiseXml(const std::string &pseudoXmlString)
for use when converting cool folder description JSON
std::vector< std::string > parseMagicTag(const std::string &v)
Takes a tag of form TagInfo{Major|Minor}/<tag> or TagInfo{Major|Minor}/<prefix>/<tag> and resolve it ...
cool::ChannelId makeChannel(const std::string &strval, const cool::ChannelId defchan)
Create a ChannelId from a string; if string is empty, return the default channel number given.
bool looksLikeMagicTag(const std::string &candidateTag)
Looks like it should be magic.
std::vector< std::string > parseLinkNames(const std::string &linktext)
Parse string of format "A:X::B:C" to "A" , "X::B", "C".
std::string sanitiseJsonString(const std::string &dataString)
Sanitise json string, escaping raw carriage returns.
Definition run.py:1
DataModel_detail::iterator< DVL > remove(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, const T &value)
Specialization of remove for DataVector/List.