BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/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
2// Exp $
3
4#include "facilities/Util.h"
5
6#ifdef DEFECT_NO_STRINGSTREAM
7# include <strstream>
8#else
9# include <sstream>
10#endif
11
12#include <cstdio>
13#include <cstring>
14#include <iostream>
15#include <stdlib.h>
16/** @file Util.cxx
17@author J. Bogart
18*/
19
20namespace facilities {
21
22 int Util::expandEnvVar( std::string* toExpand, 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 {
31 int envEnd = toExpand->find( closeDel.c_str() );
32
33 // add characters to account for opening delimiter
34 int afterBracket = envStart + opLen;
35
36 if ( !( ( envStart == -1 ) || ( envEnd == -1 ) ) )
37 {
38 std::string envVariable = toExpand->substr( afterBracket, ( envEnd - afterBracket ) );
39 const char* path = getenv( envVariable.c_str() );
40 if ( path )
41 {
42 toExpand->replace( envStart, ( envEnd + clLen ), path );
43 if ( nSuccess > -1 ) nSuccess++;
44 }
45 else
46 {
47 std::cerr << "Util::expandEnvVar unable to translate " << envVariable << std::endl;
48 throw Untranslatable( envVariable );
49 }
50 }
51 envStart = toExpand->find( openDel.c_str() );
52 }
53 return nSuccess;
54 }
55
56 const char* Util::itoa( int val, std::string& outStr ) {
57 // Purpose and Method: Provide a standard routine to convert integers
58 // into std::string. The method used depends upon the availability of
59 // the stringstream classes. The stringstream classes are the
60 // standard, but some compilers do yet support them.
61 // The method used is determined by the DEFECT_NO_STRINGSTREAM
62 // macro, defined in the facilities requirements file.
63
64 static char outCharPtr[20];
65
66#ifdef DEFECT_NO_STRINGSTREAM
67 // Using static buffer to avoid problems with memory allocation
68 char a[100] = "";
69 std::ostrstream locStream( a, 100 );
70#else
71 std::ostringstream locStream;
72 locStream.str( "" );
73#endif
74 locStream << val;
75#ifdef DEFECT_NO_STRINGSTREAM
76 locStream << std::ends;
77#endif
78 outStr = locStream.str();
79 strcpy( outCharPtr, outStr.c_str() );
80 return outCharPtr;
81 }
82
83 int Util::atoi( const std::string& inStr ) {
84 // Purpose and Method: Provide a standard routine to convert std::strings
85 // into integers. The method used depends upon the availability of
86 // the stringstream classes. The stringstream classes are the
87 // standard, but some compilers do yet support them.
88 // The method used is determined by the DEFECT_NO_STRINGSTREAM
89 // macro, defined in the facilities requirements file.
90 // Output: returns an integer
91 // if string cannot be converted to an integer, returns zero
92
93 int val;
94
95#ifdef DEFECT_NO_STRINGSTREAM
96 std::istrstream locStream( inStr.c_str() );
97#else
98 std::istringstream locStream( inStr );
99#endif
100 locStream >> val;
101 if ( !locStream ) { return 0; }
102 return val;
103 }
104
105 double Util::stringToDouble( const std::string& inStr ) {
106 double val;
107 char junk[3];
108 int nItem = sscanf( inStr.c_str(), "%lg %1s", &val, junk );
109 if ( nItem != 1 ) { throw WrongType( inStr, "double" ); }
110 return val;
111 }
112
113 int Util::stringToInt( const std::string& inStr ) {
114 int val;
115 char junk[3];
116 int nItem = sscanf( inStr.c_str(), "%d %1s", &val, junk );
117 if ( nItem != 1 ) { throw WrongType( inStr, "int" ); }
118 return val;
119 }
120
121 void Util::stringTokenize( std::string input, const std::string& delimiters,
122 std::vector<std::string>& tokens, bool clear ) {
123 if ( clear ) tokens.clear();
124
125 std::string::size_type j;
126 while ( ( j = input.find_first_of( delimiters ) ) != std::string::npos )
127 {
128 if ( j != 0 ) tokens.push_back( input.substr( 0, j ) );
129 input = input.substr( j + 1 );
130 }
131 tokens.push_back( input );
132 if ( tokens.back() == "" ) tokens.pop_back();
133 }
134
135 void Util::keyValueTokenize( std::string input, const std::string& delimiters,
136 std::map<std::string, std::string>& tokens,
137 const std::string& pairDelimiter, bool clear ) {
138 if ( clear ) tokens.clear();
139
140 std::vector<std::string> strvec;
141 stringTokenize( input, delimiters, strvec, true );
142 unsigned advance = pairDelimiter.size();
143
144 std::vector<std::string>::const_iterator input_itr = strvec.begin();
145 while ( input_itr != strvec.end() )
146 {
147 std::string current = *input_itr++;
148 std::string::size_type j = current.find( pairDelimiter );
149 std::string key = current.substr( 0, j );
150 std::string value = current.substr( j + advance );
151 tokens[key] = value;
152 }
153 }
154
155 std::string Util::basename( const std::string& path ) {
156 std::vector<std::string> names;
157 stringTokenize( path, "\\/", names );
158 return *( names.end() - 1 );
159 }
160
161 unsigned Util::trimTrailing( std::string* toTrim ) {
162 static const char blank = ' ';
163 static const char LF = 0xA; // new line
164 static const char FF = 0xC; // form feed
165 static const char CR = 0xD; // carriage return
166
167 unsigned orig = toTrim->size();
168 unsigned last = orig - 1;
169 bool notDone = true;
170 unsigned nTrimmed = 0;
171
172 while ( notDone )
173 {
174 char lastChar = ( *toTrim )[last];
175 switch ( lastChar )
176 {
177 case blank:
178 case LF:
179 case FF:
180 case CR:
181 last--;
182 nTrimmed++;
183 break;
184 default: notDone = false; break;
185 }
186 }
187 if ( nTrimmed ) toTrim->resize( orig - nTrimmed );
188
189 return nTrimmed;
190 }
191
192} // namespace facilities
*************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.