BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/RootCnvSvc/src/Util.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Event/RootCnvSvc/src/Util.cxx,v 1.11 2019/03/21 07:43:22 maqm Exp
2// $
3
4#include "RootCnvSvc/Util.h"
5#ifdef DEFECT_NO_STRINGSTREAM
6# include <strstream>
7#else
8# include <sstream>
9#endif
10
11#include <cstdio>
12#include <cstring>
13#include <iostream>
14#include <stdlib.h>
15
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_first_of(openDel.c_str());
29 int envStart = toExpand->find( openDel.c_str() ); // by fucd
30 while ( envStart != -1 )
31 {
32 // int envEnd = toExpand->find_first_of(closeDel.c_str());
33 int envEnd = toExpand->find( closeDel.c_str(), envStart ); // by fucd
34
35 // add characters to account for opening delimiter
36 int afterBracket = envStart + opLen;
37
38 if ( !( ( envStart == -1 ) || ( envEnd == -1 ) ) )
39 {
40 std::string envVariable = toExpand->substr( afterBracket, ( envEnd - afterBracket ) );
41 const char* path = ::getenv( envVariable.c_str() );
42 if ( path )
43 {
44 // toExpand->replace(envStart,(envEnd+clLen), path);
45 toExpand->replace( envStart, ( envEnd + clLen - envStart ), path ); // by fucd
46 if ( nSuccess > -1 ) nSuccess++;
47 }
48 else
49 {
50 std::cerr << "Util::expandEnvVar unable to translate " << envVariable << std::endl;
51 throw Untranslatable( envVariable );
52 }
53 }
54 // envStart = toExpand->find_first_of(openDel.c_str());
55 envStart = toExpand->find( openDel.c_str() ); // by fucd
56 }
57 return nSuccess;
58 }
59
60 int Util::catchOptionVal( std::string* toCatch, const int pos, const std::string& openDel,
61 const std::string& closeDel ) {
62 unsigned opLen = openDel.size();
63 unsigned clLen = closeDel.size();
64
65 int valStart = toCatch->find( openDel.c_str(), pos );
66 while ( valStart != -1 )
67 {
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 {
75 std::string valStr = toCatch->substr( afterBracket, ( valEnd - afterBracket ) );
76 toCatch->erase( valStart, ( valEnd + clLen - valStart ) );
77 return atoi( valStr );
78 }
79 else
80 {
81 std::cerr << "Util::can't find the close delimiter " << closeDel << std::endl;
82 throw Untranslatable( *toCatch );
83 }
84 }
85 return -1;
86 }
87
88 const char* Util::itoa( int val, std::string& outStr ) {
89 // Purpose and Method: Provide a standard routine to convert integers
90 // into std::string. The method used depends upon the availability of
91 // the stringstream classes. The stringstream classes are the
92 // standard, but some compilers do yet support them.
93 // The method used is determined by the DEFECT_NO_STRINGSTREAM
94 // macro, defined in the facilities requirements file.
95
96 static char outCharPtr[20];
97
98#ifdef DEFECT_NO_STRINGSTREAM
99 // Using static buffer to avoid problems with memory allocation
100 char a[100] = "";
101 std::ostrstream locStream( a, 100 );
102#else
103 std::ostringstream locStream;
104 locStream.str( "" );
105#endif
106 locStream << val;
107#ifdef DEFECT_NO_STRINGSTREAM
108 locStream << std::ends;
109#endif
110 outStr = locStream.str();
111 strcpy( outCharPtr, outStr.c_str() );
112 return outCharPtr;
113 }
114
115 int Util::atoi( const std::string& inStr ) {
116 // Purpose and Method: Provide a standard routine to convert std::strings
117 // into integers. The method used depends upon the availability of
118 // the stringstream classes. The stringstream classes are the
119 // standard, but some compilers do yet support them.
120 // The method used is determined by the DEFECT_NO_STRINGSTREAM
121 // macro, defined in the facilities requirements file.
122 // Output: returns an integer
123 // if string cannot be converted to an integer, returns zero
124
125 int val;
126
127#ifdef DEFECT_NO_STRINGSTREAM
128 std::istrstream locStream( inStr.c_str() );
129#else
130 std::istringstream locStream( inStr );
131#endif
132 locStream >> val;
133 if ( !locStream ) { return 0; }
134 return val;
135 }
136
137 double Util::stringToDouble( const std::string& inStr ) {
138 double val;
139 char junk[3];
140 int nItem = sscanf( inStr.c_str(), "%lg %1s", &val, junk );
141 if ( nItem != 1 ) { throw WrongType( inStr, "double" ); }
142 return val;
143 }
144
145 int Util::stringToInt( const std::string& inStr ) {
146 int val;
147 char junk[3];
148 int nItem = sscanf( inStr.c_str(), "%d %1s", &val, junk );
149 if ( nItem != 1 ) { throw WrongType( inStr, "int" ); }
150 return val;
151 }
152
153 void Util::stringTokenize( std::string input, const std::string& delimiters,
154 std::vector<std::string>& tokens, bool clear ) {
155 if ( clear ) tokens.clear();
156
157 std::string::size_type j;
158 while ( ( j = input.find_first_of( delimiters ) ) != std::string::npos )
159 {
160 if ( j != 0 ) tokens.push_back( input.substr( 0, j ) );
161 input = input.substr( j + 1 );
162 }
163 tokens.push_back( input );
164 }
165
166 std::string Util::basename( const std::string& path ) {
167 std::vector<std::string> names;
168 stringTokenize( path, "\\/", names );
169 return *( names.end() - 1 );
170 }
171
172} // namespace facilities
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 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)