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