BOSS 7.1.2
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/facilities-00-00-04/src/Util.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/facilities/src/Util.cxx,v 1.2 2011/02/23 15:35:54 maqm Exp $
2
3#include "facilities/Util.h"
4
5#ifdef DEFECT_NO_STRINGSTREAM
6#include <strstream>
7#else
8#include <sstream>
9#endif
10
11#include <iostream>
12#include <cstdio>
13#include <stdlib.h>
14#include <cstring>
15/** @file Util.cxx
16@author J. Bogart
17*/
18
19namespace facilities {
20
21 int Util::expandEnvVar(std::string* toExpand,
22 const std::string& openDel,
23 const std::string& closeDel) {
24 unsigned opLen = openDel.size();
25 unsigned clLen = closeDel.size();
26 int nSuccess = 0;
27
28 int envStart = toExpand->find(openDel.c_str());
29 while (envStart != -1) {
30 int envEnd = toExpand->find(closeDel.c_str());
31
32 // add characters to account for opening delimiter
33 int afterBracket = envStart + opLen;
34
35 if (!( (envStart==-1) || (envEnd==-1) ))
36 {
37 std::string envVariable =
38 toExpand->substr(afterBracket,(envEnd-afterBracket));
39 const char * path = getenv(envVariable.c_str());
40 if (path) {
41 toExpand->replace(envStart,(envEnd+clLen), path);
42 if (nSuccess > -1) nSuccess++;
43 }
44 else {
45 std::cerr << "Util::expandEnvVar unable to translate "
46 << envVariable << std::endl;
47 throw Untranslatable(envVariable);
48 }
49 }
50 envStart = toExpand->find(openDel.c_str());
51 }
52 return nSuccess;
53 }
54
55 const char* Util::itoa(int val, std::string &outStr) {
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 }
81
82 int Util::atoi(const std::string& inStr) {
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 }
103
104 double Util::stringToDouble(const std::string& inStr) {
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 }
113
114
115 int Util::stringToInt(const std::string& inStr) {
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 }
124
125 void Util::stringTokenize(std::string input, const std::string& delimiters,
126 std::vector<std::string>& tokens, bool clear) {
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 }
137
138
139 void Util::keyValueTokenize(std::string input,
140 const std::string& delimiters,
141 std::map<std::string,std::string>& tokens,
142 const std::string& pairDelimiter,
143 bool clear) {
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 }
160
161 std::string Util::basename(const std::string& path) {
162 std::vector<std::string> names;
163 stringTokenize(path, "\\/", names);
164 return *(names.end() - 1);
165 }
166
167 unsigned Util::trimTrailing(std::string* toTrim) {
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 }
198
199}
200
201
202
203
204
205
*************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
static double stringToDouble(const std::string &InStr)
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
static void stringTokenize(std::string input, const std::string &delimiters, std::vector< std::string > &tokens, bool clear=true)
static int atoi(const std::string &InStr)
converts an std::string to an integer
static std::string basename(const std::string &path)
static int stringToInt(const std::string &InStr)
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 const char * itoa(int val, std::string &outStr)
static unsigned trimTrailing(std::string *toTrim)
Exception class used when converting from string to numeric type.