BOSS 7.0.2
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/RootCnvSvc/RootCnvSvc-02-01-12/src/Util.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Event/RootCnvSvc/src/Util.cxx,v 1.10 2011/02/17 01:20:06 maqm Exp $
2
3#include "RootCnvSvc/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
16/** @file Util.cxx
17@author J. Bogart
18*/
19
20namespace facilities {
21
22 int Util::expandEnvVar(std::string* toExpand,
23 const std::string& openDel,
24 const std::string& closeDel) {
25 unsigned opLen = openDel.size();
26 unsigned clLen = closeDel.size();
27 int nSuccess = 0;
28
29 //int envStart = toExpand->find_first_of(openDel.c_str());
30 int envStart = toExpand->find(openDel.c_str()); //by fucd
31 while (envStart != -1) {
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 =
41 toExpand->substr(afterBracket,(envEnd-afterBracket));
42 const char * path = ::getenv(envVariable.c_str());
43 if (path) {
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 std::cerr << "Util::expandEnvVar unable to translate "
50 << 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,
61 const int pos,
62 const std::string& openDel,
63 const std::string& closeDel) {
64 unsigned opLen = openDel.size();
65 unsigned clLen = closeDel.size();
66
67 int valStart = toCatch->find(openDel.c_str(),pos);
68 while (valStart != -1) {
69 int valEnd = toCatch->find(closeDel.c_str(),valStart);
70
71 // add characters to account for opening delimiter
72 int afterBracket = valStart + opLen;
73
74 if (valEnd!=-1){
75 std::string valStr = toCatch->substr(afterBracket,(valEnd-afterBracket));
76 toCatch->erase(valStart,(valEnd+clLen-valStart));
77 return atoi(valStr);
78 }
79 else{
80 std::cerr << "Util::can't find the close delimiter "
81 << 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) {
142 throw WrongType(inStr, "double");
143 }
144 return val;
145 }
146
147
148 int Util::stringToInt(const std::string& inStr) {
149 int val;
150 char junk[3];
151 int nItem = sscanf(inStr.c_str(), "%d %1s", &val, junk);
152 if (nItem != 1) {
153 throw WrongType(inStr, "int");
154 }
155 return val;
156 }
157
158 void Util::stringTokenize(std::string input, const std::string& delimiters,
159 std::vector<std::string>& tokens, bool clear) {
160 if (clear) tokens.clear();
161
162 std::string::size_type j;
163 while ( (j = input.find_first_of(delimiters)) != std::string::npos ) {
164 if (j != 0) tokens.push_back(input.substr(0, j));
165 input = input.substr(j+1);
166 }
167 tokens.push_back(input);
168 }
169
170 std::string Util::basename(const std::string& path) {
171 std::vector<std::string> names;
172 stringTokenize(path, "\\/", names);
173 return *(names.end() - 1);
174 }
175
176}
177
178
179
180
181
182
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)