ATLAS Offline Software
SbStringHelpers.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // this :
6 
7 #include <Inventor/C/errors/debugerror.h>
8 #include <Inventor/SbPList.h>//hack to work around numitems not initialised in coin
9 #include "SbStringHelpers.h"
10 
11 #include <Inventor/SbString.h>
12 
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #define SbMinimum(a,b) ((a)<(b)?(a):(b))
17 
18 static SbString* SbStringCreateV(int,const char*,va_list);
19 //static SbString* SbStringCreateF(int,const char*,...);
20 
24 char* SbSTRDUP(
25  const char* aString
26 )
29 {
30  char* p = (char*)::malloc((unsigned)::strlen(aString)+1);
31  if(p==NULL) return 0;
32  //cppcheck-suppress returnDanglingLifetime
33  return ::strcpy(p,aString);
34 }
36 void SbSTRDEL(
37  char*& aString
38 )
41 {
42  ::free(aString);
43  aString = NULL;
44 }
47  const SbString& aString
48 ,const char* aLimiter
49 )
52 {
53  SbPList list(16);
54 
55  if(aString.getLength()<=0) return list;
56 
57  SbString string = aString;
58 
59  char* token = (char*)string.getString();
60 
61  while(1) {
62  char* pos = ::strstr (token,aLimiter);
63  if(pos!=NULL) {
64  *pos = '\0';
65  if(*token!='\0') {
66  list.append(token);
67  }
68  token = pos + ::strlen(aLimiter);
69  } else {
70  //last word
71  if(*token!='\0') {
72  list.append(token);
73  }
74  break;
75  }
76  }
77 
78  int wordn = list.getLength();
79  char* word;
80  for(int count=0;count<wordn;count++) {
81  word = (char*)list[count];
82  list[count] = new SbString(word);
83  }
84 
85  return list;
86 }
123  SbPList& aList
124 )
127 {
128  int linen = aList.getLength();
129  for(int count=0;count<linen;count++) delete ((SbString*)aList[count]);
130  //aList.Clear();
131 }
134  const SbString& aString
135 ,SbBool& aValue
136 )
139 {
140  if( (aString=="true") || (aString=="TRUE") ||
141  (aString=="yes") || (aString=="YES") ) {
142  aValue = TRUE;
143  return TRUE;
144  } else if( (aString=="false") || (aString=="FALSE") ||
145  (aString=="no") || (aString=="NO") ) {
146  aValue = FALSE;
147  return TRUE;
148  } else {
149  aValue = FALSE;
150  return FALSE;
151  }
152 }
155  const SbString& aString
156 ,int& aValue
157 )
160 {
161  const char* string = aString.getString();
162  char* s;
163  long value = ::strtol(string,&s,10);
164  if(s==string) {
165  aValue = 0L;
166  return FALSE;
167  }
168  aValue = (int)value;
169  return TRUE;
170 }
173  const SbString& aString
174 ,float& aValue
175 )
178 {
179  const char* string = aString.getString();
180  char* s;
181  double value = ::strtod(string,&s);
182  if(s==string) {
183  aValue = 0;
184  return FALSE;
185  }
186  aValue = (float)value;
187  return TRUE;
188 }
191  const SbString& aString
192 ,unsigned long& aValue
193 )
196 {
197  aValue = 0L;
198  if(::sscanf(aString.getString(),"%lx",&aValue)!=1) {
199  if(::sscanf(aString.getString(),"%lu",&aValue)!=1) {
200  aValue = 0L;
201  return FALSE;
202  }
203  }
204  return TRUE;
205 }
208  SbString& aString
209 ,int aNumber
210 ,const char* aFormat
211 ,...
212 )
215 {
216  va_list args;
217  va_start(args,aFormat);
218  SbString* sbString = SbStringCreateV(aNumber,aFormat,args);
219  va_end(args);
220  if(sbString==NULL) {
221  aString = "";
222  return FALSE;
223  }
224  aString = *sbString;
225  delete sbString;
226  return TRUE;
227 }
229 SbString SbStringPrintF (
230  int aNumber
231 ,const char* aFormat
232 ,...
233 )
236 {
237  va_list args;
238  va_start(args,aFormat);
239  SbString* sbString = SbStringCreateV(aNumber,aFormat,args);
240  va_end(args);
241  if(sbString==NULL) return "";
242  SbString s(*sbString);
243  delete sbString;
244  return s;
245 }
248  const SbString& aString
249 ,const SbString& aSearch
250 )
253 {
254  int lsearch = aSearch.getLength();
255  if(lsearch<=0) return SbStringNotFound;
256  int number = aString.getLength();
257  for(int index=0;index<=(number-lsearch);index++) {
258  if(aString.getSubString(index,index+lsearch-1)==aSearch) return index;
259  }
260  return SbStringNotFound;
261 }
264  const SbString& aString
265 ,const SbString& aSearch
266 )
269 {
270  int lsearch = aSearch.getLength();
271  if(lsearch<=0) return SbStringNotFound;
272  int number = aString.getLength();
273  for(int index=number-lsearch;index>=0;index--) {
274  if(aString.getSubString(index,index+lsearch-1)==aSearch) return index;
275  }
276  return SbStringNotFound;
277 }
279 SbString SbStringSuffix (
280  const SbString& aString
281 )
284 {
285  int index = SbStringReverseFind(aString,".");
286  if(index==SbStringNotFound) return SbString("");
287  return aString.getSubString(index+1);
288 }
293  int aLength
294 )
297 {
298  if(aLength<=0) aLength = 0;
299  char* string = (char*)::malloc((aLength+1)*sizeof(char));
300  if(string==NULL) return NULL;
301  string[aLength] = '\0';
302  for(int count=0;count<aLength;count++) string[count] = ' ';
303  SbString* sbString = new SbString(string);
304  free(string);
305  return sbString;
306 }
307 /*
309 SbString* SbStringCreateF (
310  int aNumber
311 ,const char* aFormat
312 ,...
313 )
316 {
317  va_list args;
318  va_start(args,aFormat);
319  SbString* sbString = SbStringCreateV(aNumber,aFormat,args);
320  va_end(args);
321  return sbString;
322 }
323 */
325 SbString* SbStringCreateV (
326  int aNumber
327 ,const char* aFormat
328 ,va_list aArgs
329 )
332 {
333  if(aNumber<0) return NULL;
334  SbString* sbString = SbStringCreateEmpty(aNumber);
335  if(sbString==NULL) return NULL;
336  const char* string = sbString->getString();
337  ::vsprintf((char*)string,aFormat,aArgs);
338  if(string[aNumber]!='\0') {
339  ::printf("SbStringCreateV : buffer overflow \n");
340  delete sbString;
341  return NULL;
342  }
343  return sbString;
344 }
347  const SbString& aString
348 )
351 {
352  if(aString.getLength()<4) return FALSE;
353  if(aString.getSubString(0,3)!="TTF/") return FALSE;
354  return TRUE;
355 }
356 #include <string>
359  SbString& aString
360 )
362 // From Lib::smanip::getenv.
364 {
365  std::string a_String(aString.getString());
366  std::string::size_type dollar;
367  while((dollar=a_String.find('$'))!=std::string::npos){
368  std::string::size_type slash = a_String.find('/',dollar+1);
369  std::string::size_type back_slash = a_String.find('\\',dollar+1);
370  std::string::size_type pos = std::string::npos;
371  if(slash!=std::string::npos) {
372  if(back_slash!=std::string::npos) {
373  pos = SbMinimum(slash,back_slash);
374  } else {
375  pos = slash;
376  }
377  } else {
378  if(back_slash!=std::string::npos) {
379  pos = back_slash;
380  } else {
381  pos = std::string::npos;
382  }
383  }
384  std::string env;
385  if(pos==std::string::npos) {
386  env = a_String.substr(dollar+1,a_String.length()-(dollar+1));
387  } else {
388  // abc$xxx/ef
389  // 0 3 7 9
390  env = a_String.substr(dollar+1,pos-(dollar+1));
391  }
392  char* val = ::getenv(env.c_str());
393  if(val) {
394  std::string value = a_String.substr(0,dollar);
395  value += val;
396  if(pos!=std::string::npos)
397  value += a_String.substr(pos,a_String.length()-pos);
398  a_String = value;
399  }
400  if(!val) {
401  aString = SbString(a_String.c_str());
402  return FALSE; // Stop looping if env variable not found.
403  }
404  }
405  aString = SbString(a_String.c_str());
406  return TRUE;
407 }
410  const SbString& aString
411 )
414 {
415  const char* env = ::getenv(aString.getString());
416  return (env?TRUE:FALSE);
417 }
420  const SbString& aString
421 ,SbString& aValue
422 )
424 // From bool Lib::System::getenv(
426 {
427  const char* env = ::getenv(aString.getString());
428  if(env) {
429  aValue = SbString(env);
430  return TRUE;
431  } else {
432  aValue = "";
433  return FALSE;
434  }
435 }
438  SbString& aString
439 ,const SbString& aOld
440 ,const SbString& aNew
441 )
443 // Lib::smanip::replace.
444 // return true : some replacement done.
445 // return false : nothing replaced.
447 {
448  std::string a_Old(aOld.getString());
449  std::string a_New(aNew.getString());
450  if(a_Old=="") return FALSE;
451  std::string snew;
452  std::string::size_type lold = a_Old.length();
453  bool status = FALSE;
454  std::string stmp(aString.getString());
455  while(true) {
456  std::string::size_type pos = stmp.find(a_Old);
457  if(pos==std::string::npos){
458  snew += stmp;
459  break;
460  } else {
461  snew += stmp.substr(0,pos);
462  snew += a_New;
463  stmp = stmp.substr(pos+lold,stmp.length()-(pos+lold));
464  status = TRUE;
465  }
466  }
467  aString = SbString(snew.c_str());
468  return status;
469 }
SbSTRDEL
void SbSTRDEL(char *&aString)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:36
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
SbStringNotFound
#define SbStringNotFound
Definition: SbStringHelpers.h:37
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
index
Definition: index.py:1
xAOD::char
char
Definition: TrigDecision_v1.cxx:38
SbStringSuffix
SbString SbStringSuffix(const SbString &aString)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:279
SbStringConvertToBool
SbBool SbStringConvertToBool(const SbString &aString, SbBool &aValue)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:133
athena.value
value
Definition: athena.py:124
SbString_isenv
SbBool SbString_isenv(const SbString &aString)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:409
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
SbSTRDUP
char * SbSTRDUP(const char *aString)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:24
SbStringGetWords
SbPList SbStringGetWords(const SbString &aString, const char *aLimiter)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:46
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
SbStringFind
int SbStringFind(const SbString &aString, const SbString &aSearch)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:247
SbStringReplace
SbBool SbStringReplace(SbString &aString, const SbString &aOld, const SbString &aNew)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:437
python.Constants.TRUE
bool TRUE
for job options legacy (TODO: get rid of these!) ----------------------—
Definition: Control/AthenaCommon/python/Constants.py:22
TrigInDetValidation_Base.malloc
malloc
Definition: TrigInDetValidation_Base.py:132
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
SbStringIsTTF
SbBool SbStringIsTTF(const SbString &aString)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:346
SbStringConvertToFloat
SbBool SbStringConvertToFloat(const SbString &aString, float &aValue)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:172
SbStringCreateEmpty
SbString * SbStringCreateEmpty(int aLength)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:292
SbStringConvertToInt
SbBool SbStringConvertToInt(const SbString &aString, int &aValue)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:154
SbStringDelete
void SbStringDelete(SbPList &aList)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:122
SbStringConvertToUnsignedLong
SbBool SbStringConvertToUnsignedLong(const SbString &aString, unsigned long &aValue)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:190
python.selection.number
number
Definition: selection.py:20
SbStringHelpers.h
SbStringPrintF
SbBool SbStringPrintF(SbString &aString, int aNumber, const char *aFormat,...)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:207
python.Constants.FALSE
bool FALSE
Definition: Control/AthenaCommon/python/Constants.py:23
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
DeMoScan.index
string index
Definition: DeMoScan.py:364
SCT_ConditionsAlgorithms::CoveritySafe::getenv
std::string getenv(const std::string &variableName)
get an environment variable
Definition: SCT_ConditionsUtilities.cxx:17
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
merge.status
status
Definition: merge.py:17
SbStringReverseFind
int SbStringReverseFind(const SbString &aString, const SbString &aSearch)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:263
SbMinimum
#define SbMinimum(a, b)
Definition: SbStringHelpers.cxx:16
SbStringGetenv
SbBool SbStringGetenv(SbString &aString)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:358
python.DataFormatRates.env
env
Definition: DataFormatRates.py:32
readCCLHist.float
float
Definition: readCCLHist.py:83
python.CaloScaleNoiseConfig.args
args
Definition: CaloScaleNoiseConfig.py:80
SbString_getenv
SbBool SbString_getenv(const SbString &aString, SbString &aValue)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
Definition: SbStringHelpers.cxx:419