BOSS 7.1.1
BESIII Offline Software System
Loading...
Searching...
No Matches
facilities::Util Class Reference

#include <Util.h>

Static Public Member Functions

static int expandEnvVar (std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
 
static const char * itoa (int val, std::string &outStr)
 
static int atoi (const std::string &InStr)
 converts an std::string to an integer
 
static double stringToDouble (const std::string &InStr)
 
static int stringToInt (const std::string &InStr)
 
static void stringTokenize (std::string input, const std::string &delimiters, std::vector< std::string > &tokens, bool clear=true)
 
static void keyValueTokenize (std::string input, const std::string &delimiters, std::map< std::string, std::string > &tokenMap, const std::string &pairDelimiter=std::string("="), bool clear=true)
 
static std::string basename (const std::string &path)
 
static unsigned trimTrailing (std::string *toTrim)
 
static int expandEnvVar (std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
 
static int catchOptionVal (std::string *toCatch, const int ops=0, const std::string &openDel=std::string("#("), const std::string &closeDel=std::string(")"))
 
static const char * itoa (int val, std::string &outStr)
 
static int atoi (const std::string &InStr)
 converts an std::string to an integer
 
static double stringToDouble (const std::string &InStr)
 
static int stringToInt (const std::string &InStr)
 
static void stringTokenize (std::string input, const std::string &delimiters, std::vector< std::string > &tokens, bool clear=true)
 
static std::string basename (const std::string &path)
 

Detailed Description

Member Function Documentation

◆ atoi() [1/2]

int facilities::Util::atoi ( const std::string & InStr)
static

converts an std::string to an integer

Definition at line 82 of file Calibration/facilities/facilities-00-00-04/src/Util.cxx.

82 {
83 // Purpose and Method: Provide a standard routine to convert std::strings
84 // into integers. The method used depends upon the availability of
85 // the stringstream classes. The stringstream classes are the
86 // standard, but some compilers do yet support them.
87 // The method used is determined by the DEFECT_NO_STRINGSTREAM
88 // macro, defined in the facilities requirements file.
89 // Output: returns an integer
90 // if string cannot be converted to an integer, returns zero
91
92 int val;
93
94#ifdef DEFECT_NO_STRINGSTREAM
95 std::istrstream locStream(inStr.c_str());
96#else
97 std::istringstream locStream(inStr);
98#endif
99 locStream >> val;
100 if (!locStream) {return 0;}
101 return val;
102 }

Referenced by catchOptionVal(), and calibUtil::Metadata::getReadInfo().

◆ atoi() [2/2]

static int facilities::Util::atoi ( const std::string & InStr)
static

converts an std::string to an integer

◆ basename() [1/2]

std::string facilities::Util::basename ( const std::string & path)
static

return the "non-directory" part of a (supposed) file identifier, path. Environment variable translation should be done before calling basename.

See also
{ Util::expandEnvVar }
Parameters
pathstring assumed to be a file identifier.

Definition at line 161 of file Calibration/facilities/facilities-00-00-04/src/Util.cxx.

161 {
162 std::vector<std::string> names;
163 stringTokenize(path, "\\/", names);
164 return *(names.end() - 1);
165 }
static void stringTokenize(std::string input, const std::string &delimiters, std::vector< std::string > &tokens, bool clear=true)

Referenced by main().

◆ basename() [2/2]

static std::string facilities::Util::basename ( const std::string & path)
static

return the "non-directory" part of a (supposed) file identifier, path. Environment variable translation should be done before calling basename.

See also
{ Util::expandEnvVar }
Parameters
pathstring assumed to be a file identifier.

◆ catchOptionVal()

int facilities::Util::catchOptionVal ( std::string * toCatch,
const int ops = 0,
const std::string & openDel = std::string("#("), const std::string& closeDel = std::string(")") )
static

Given input string toCatch catch val from string opt, by default of the form #(opt) and remove "#(opt)" from the original string.

Parameters
toCatchstring for which expansion is to be done
openDelopening delimiter (defaults to "#(")
closeDelclosing delimiter (defaults to ")")
Returns
-1 if attempt at expansion failed, else atoi(opt). TODO: Perhaps add optional arguments to specify alternate delimiters.

Definition at line 59 of file Event/RootCnvSvc/RootCnvSvc-03-01-03/src/Util.cxx.

62 {
63 unsigned opLen = openDel.size();
64 unsigned clLen = closeDel.size();
65
66 int valStart = toCatch->find(openDel.c_str(),pos);
67 while (valStart != -1) {
68 int valEnd = toCatch->find(closeDel.c_str(),valStart);
69
70 // add characters to account for opening delimiter
71 int afterBracket = valStart + opLen;
72
73 if (valEnd!=-1){
74 std::string valStr = toCatch->substr(afterBracket,(valEnd-afterBracket));
75 toCatch->erase(valStart,(valEnd+clLen-valStart));
76 return atoi(valStr);
77 }
78 else{
79 std::cerr << "Util::can't find the close delimiter "
80 << closeDel << std::endl;
81 throw Untranslatable(*toCatch);
82 }
83 }
84 return -1;
85 }
static int atoi(const std::string &InStr)
converts an std::string to an integer

◆ expandEnvVar() [1/2]

static int facilities::Util::expandEnvVar ( std::string * toExpand,
const std::string & openDel = std::string("$("), const std::string &closeDel=std::string(")") )
static

Given input string toExpand expand references to environment variables, by default of the form and put the expanded version back into the original string. Alternate delimiters for the varname may optionally be specified

Parameters
toExpandstring for which expansion is to be done
openDelopening delimiter (defaults to "$(")
closeDelclosing delimiter (defaults to ")")
Returns
-1 if attempt at expansion failed at least once, else number of successful expansions.

TODO: Perhaps add optional arguments to specify alternate delimiters.

Referenced by XmlBaseCnv::createObj(), digiRootReaderAlg::initialize(), digiRootWriterAlg::initialize(), main(), main(), calibUtil::Metadata::Metadata(), RootCalBaseCnv::openRead(), RootCalBaseCnv::openWrite(), and xmlBase::XmlParser::parse().

◆ expandEnvVar() [2/2]

static int facilities::Util::expandEnvVar ( std::string * toExpand,
const std::string & openDel = std::string("$("), const std::string &closeDel=std::string(")") )
static

Given input string toExpand expand references to environment variables, by default of the form and put the expanded version back into the original string. Alternate delimiters for the varname may optionally be specified

Parameters
toExpandstring for which expansion is to be done
openDelopening delimiter (defaults to "$(")
closeDelclosing delimiter (defaults to ")")
Returns
-1 if attempt at expansion failed at least once, else number of successful expansions.

TODO: Perhaps add optional arguments to specify alternate delimiters.

◆ itoa() [1/2]

const char * facilities::Util::itoa ( int val,
std::string & outStr )
static

Given an input integer val to convert and an output string outStr converts val into a std::string. This method duplicates the stdlib.h method itoa, except that it returns std::string rather than char*.

Parameters
val
outStrwill be modified by this method
Returns
const char* based on the contents of outStr.c_str()

Definition at line 55 of file Calibration/facilities/facilities-00-00-04/src/Util.cxx.

55 {
56 // Purpose and Method: Provide a standard routine to convert integers
57 // into std::string. The method used depends upon the availability of
58 // the stringstream classes. The stringstream classes are the
59 // standard, but some compilers do yet support them.
60 // The method used is determined by the DEFECT_NO_STRINGSTREAM
61 // macro, defined in the facilities requirements file.
62
63 static char outCharPtr[20];
64
65#ifdef DEFECT_NO_STRINGSTREAM
66 // Using static buffer to avoid problems with memory allocation
67 char a[100]="";
68 std::ostrstream locStream(a,100);
69#else
70 std::ostringstream locStream;
71 locStream.str("");
72#endif
73 locStream << val;
74#ifdef DEFECT_NO_STRINGSTREAM
75 locStream << std::ends;
76#endif
77 outStr = locStream.str();
78 strcpy(outCharPtr, outStr.c_str());
79 return outCharPtr;
80 }

Referenced by rdbModel::MysqlConnection::dbRequest(), xmlBase::XmlErrorHandler::error(), xmlBase::XmlErrorHandler::fatalError(), calibUtil::Metadata::findBest(), calibUtil::Metadata::findSoonAfter(), calibUtil::Metadata::getReadInfo(), main(), calibUtil::Metadata::registerCalib(), rdbModel::MysqlConnection::select(), and rdbModel::Table::supersedeRow().

◆ itoa() [2/2]

static const char * facilities::Util::itoa ( int val,
std::string & outStr )
static

Given an input integer val to convert and an output string outStr converts val into a std::string. This method duplicates the stdlib.h method itoa, except that it returns std::string rather than char*.

Parameters
val
outStrwill be modified by this method
Returns
const char* based on the contents of outStr.c_str()

◆ keyValueTokenize()

void facilities::Util::keyValueTokenize ( std::string input,
const std::string & delimiters,
std::map< std::string, std::string > & tokenMap,
const std::string & pairDelimiter = std::string("="),
bool clear = true )
static

This routine breaks down a string into key/value token pairs and stores them in the user-supplied map. , based on the characters appearing in the string delimiters and the value of pairDelimiter. In a typical example, input could be "key1=val1,key2=val2,key3=val3". In this case invoke with delimiters=std::string(",") and pairDelimiter=std::string("=") (or omit pairDelimiter since it has the default value)

Parameters
inputstring to be tokenized
delimitersstring containing one or more delimiter characters
tokenMapmap of strings to hold resulting tokens
pairDelimiterstring separating key and value; defaults to "="
clearif true (default) tokens will be cleared at the start of processing

Definition at line 139 of file Calibration/facilities/facilities-00-00-04/src/Util.cxx.

143 {
144 if (clear) tokens.clear();
145
146 std::vector<std::string> strvec;
147 stringTokenize(input,delimiters,strvec,true);
148 unsigned advance = pairDelimiter.size();
149
150 std::vector<std::string>::const_iterator input_itr = strvec.begin();
151 while(input_itr!=strvec.end())
152 {
153 std::string current = *input_itr++;
154 std::string::size_type j = current.find(pairDelimiter);
155 std::string key = current.substr(0, j);
156 std::string value = current.substr(j+advance);
157 tokens[key] = value;
158 }
159 }
*************DOUBLE PRECISION m_pi *DOUBLE PRECISION m_HvecTau2 DOUBLE PRECISION m_HvClone2 DOUBLE PRECISION m_gamma1 DOUBLE PRECISION m_gamma2 DOUBLE PRECISION m_thet1 DOUBLE PRECISION m_thet2 INTEGER m_IFPHOT *COMMON c_Taupair $ !Spin Polarimeter vector first Tau $ !Spin Polarimeter vector second Tau $ !Clone Spin Polarimeter vector first Tau $ !Clone Spin Polarimeter vector second Tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning nd tau $ !Random Euler angle for cloning nd tau $ !Random Euler angle for cloning nd tau $ !phi of HvecTau1 $ !theta of HvecTau1 $ !phi of HvecTau2 $ !theta of HvecTau2 $ !super key
Definition Taupair.h:42

Referenced by main().

◆ stringToDouble() [1/2]

double facilities::Util::stringToDouble ( const std::string & InStr)
static

converts a std::string to a double. If string contents are not of proper form, throws facilities::WrongType

Definition at line 104 of file Calibration/facilities/facilities-00-00-04/src/Util.cxx.

104 {
105 double val;
106 char junk[3];
107 int nItem = sscanf(inStr.c_str(), "%lg %1s", &val, junk);
108 if (nItem != 1) {
109 throw WrongType(inStr, "double");
110 }
111 return val;
112 }

Referenced by xmlBase::IFile::getDouble(), xmlBase::Dom::getDoubleAttribute(), xmlBase::Dom::getDoublesAttribute(), xmlBase::Dom::getFloatsAttribute(), and main().

◆ stringToDouble() [2/2]

static double facilities::Util::stringToDouble ( const std::string & InStr)
static

converts a std::string to a double. If string contents are not of proper form, throws facilities::WrongType

◆ stringToInt() [1/2]

int facilities::Util::stringToInt ( const std::string & InStr)
static

converts a std::string to an int. If string contents are not of proper form, throws facilities::WrongType

Definition at line 115 of file Calibration/facilities/facilities-00-00-04/src/Util.cxx.

115 {
116 int val;
117 char junk[3];
118 int nItem = sscanf(inStr.c_str(), "%d %1s", &val, junk);
119 if (nItem != 1) {
120 throw WrongType(inStr, "int");
121 }
122 return val;
123 }

Referenced by rdbModel::XercesBuilder::buildRdb(), Coverage::checkType(), xmlBase::IFile::getInt(), xmlBase::Dom::getIntAttribute(), xmlBase::Dom::getIntsAttribute(), rdbModel::MysqlResults::getRowCon(), main(), and rdbModel::MysqlConnection::open().

◆ stringToInt() [2/2]

static int facilities::Util::stringToInt ( const std::string & InStr)
static

converts a std::string to an int. If string contents are not of proper form, throws facilities::WrongType

◆ stringTokenize() [1/2]

void facilities::Util::stringTokenize ( std::string input,
const std::string & delimiters,
std::vector< std::string > & tokens,
bool clear = true )
static

This routine breaks down a string into tokens, based on the characters appearing in the string delimiters.

Parameters
inputstring to be tokenized
delimitersstring containing one or more delimiter characters
tokensvector of strings to hold resulting tokens
clearif true (default) tokens will be cleared at the start of processing

Definition at line 125 of file Calibration/facilities/facilities-00-00-04/src/Util.cxx.

126 {
127 if (clear) tokens.clear();
128
129 std::string::size_type j;
130 while ( (j = input.find_first_of(delimiters)) != std::string::npos ) {
131 if (j != 0) tokens.push_back(input.substr(0, j));
132 input = input.substr(j+1);
133 }
134 tokens.push_back(input);
135 if (tokens.back() == "") tokens.pop_back();
136 }

Referenced by basename(), xmlBase::Dom::getDoublesAttribute(), xmlBase::Dom::getFloatsAttribute(), xmlBase::Dom::getIntsAttribute(), keyValueTokenize(), and main().

◆ stringTokenize() [2/2]

static void facilities::Util::stringTokenize ( std::string input,
const std::string & delimiters,
std::vector< std::string > & tokens,
bool clear = true )
static

This routine breaksdown a string into tokens, based on the characters appearing in the string delimiters.

Parameters
inputstring to be tokenized
delimitersstring containing one or more delimiter characters
tokensvector of strings to hold resulting tokens
clearif true (default) tokens will be cleared at the start of processing

◆ trimTrailing()

unsigned facilities::Util::trimTrailing ( std::string * toTrim)
static

Trim trailing white space characters from the supplied string. White space characters for this purpose are blank, carriage return, line feed and form feed. Return # of characters trimmed.

Definition at line 167 of file Calibration/facilities/facilities-00-00-04/src/Util.cxx.

167 {
168 static const char blank=' ';
169 static const char LF=0xA; // new line
170 static const char FF=0xC; // form feed
171 static const char CR=0xD; // carriage return
172
173 unsigned orig = toTrim->size();
174 unsigned last = orig - 1;
175 bool notDone = true;
176 unsigned nTrimmed = 0;
177
178
179 while (notDone) {
180 char lastChar = (*toTrim)[last];
181 switch (lastChar) {
182 case blank:
183 case LF:
184 case FF:
185 case CR:
186 last--;
187 nTrimmed++;
188 break;
189 default:
190 notDone=false;
191 break;
192 }
193 }
194 if (nTrimmed) toTrim->resize(orig - nTrimmed);
195
196 return nTrimmed;
197 }

Referenced by XmlBaseCnv::createObj(), and main().


The documentation for this class was generated from the following files: