00001
00002
00003 #include "RootCnvSvc/Util.h"
00004
00005 #ifdef DEFECT_NO_STRINGSTREAM
00006 #include <strstream>
00007 #else
00008 #include <sstream>
00009 #endif
00010
00011 #include <iostream>
00012 #include <cstdio>
00013 #include <stdlib.h>
00014 #include <cstring>
00015
00020 namespace facilities {
00021
00022 int Util::expandEnvVar(std::string* toExpand,
00023 const std::string& openDel,
00024 const std::string& closeDel) {
00025 unsigned opLen = openDel.size();
00026 unsigned clLen = closeDel.size();
00027 int nSuccess = 0;
00028
00029
00030 int envStart = toExpand->find(openDel.c_str());
00031 while (envStart != -1) {
00032
00033 int envEnd = toExpand->find(closeDel.c_str(),envStart);
00034
00035
00036 int afterBracket = envStart + opLen;
00037
00038 if (!( (envStart==-1) || (envEnd==-1) ))
00039 {
00040 std::string envVariable =
00041 toExpand->substr(afterBracket,(envEnd-afterBracket));
00042 const char * path = ::getenv(envVariable.c_str());
00043 if (path) {
00044
00045 toExpand->replace(envStart,(envEnd+clLen-envStart), path);
00046 if (nSuccess > -1) nSuccess++;
00047 }
00048 else {
00049 std::cerr << "Util::expandEnvVar unable to translate "
00050 << envVariable << std::endl;
00051 throw Untranslatable(envVariable);
00052 }
00053 }
00054
00055 envStart = toExpand->find(openDel.c_str());
00056 }
00057 return nSuccess;
00058 }
00059
00060 int Util::catchOptionVal(std::string* toCatch,
00061 const int pos,
00062 const std::string& openDel,
00063 const std::string& closeDel) {
00064 unsigned opLen = openDel.size();
00065 unsigned clLen = closeDel.size();
00066
00067 int valStart = toCatch->find(openDel.c_str(),pos);
00068 while (valStart != -1) {
00069 int valEnd = toCatch->find(closeDel.c_str(),valStart);
00070
00071
00072 int afterBracket = valStart + opLen;
00073
00074 if (valEnd!=-1){
00075 std::string valStr = toCatch->substr(afterBracket,(valEnd-afterBracket));
00076 toCatch->erase(valStart,(valEnd+clLen-valStart));
00077 return atoi(valStr);
00078 }
00079 else{
00080 std::cerr << "Util::can't find the close delimiter "
00081 << closeDel << std::endl;
00082 throw Untranslatable(*toCatch);
00083 }
00084 }
00085 return -1;
00086 }
00087
00088 const char* Util::itoa(int val, std::string &outStr) {
00089
00090
00091
00092
00093
00094
00095
00096 static char outCharPtr[20];
00097
00098 #ifdef DEFECT_NO_STRINGSTREAM
00099
00100 char a[100]="";
00101 std::ostrstream locStream(a,100);
00102 #else
00103 std::ostringstream locStream;
00104 locStream.str("");
00105 #endif
00106 locStream << val;
00107 #ifdef DEFECT_NO_STRINGSTREAM
00108 locStream << std::ends;
00109 #endif
00110 outStr = locStream.str();
00111 strcpy(outCharPtr, outStr.c_str());
00112 return outCharPtr;
00113 }
00114
00115 int Util::atoi(const std::string& inStr) {
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 int val;
00126
00127 #ifdef DEFECT_NO_STRINGSTREAM
00128 std::istrstream locStream(inStr.c_str());
00129 #else
00130 std::istringstream locStream(inStr);
00131 #endif
00132 locStream >> val;
00133 if (!locStream) {return 0;}
00134 return val;
00135 }
00136
00137 double Util::stringToDouble(const std::string& inStr) {
00138 double val;
00139 char junk[3];
00140 int nItem = sscanf(inStr.c_str(), "%lg %1s", &val, junk);
00141 if (nItem != 1) {
00142 throw WrongType(inStr, "double");
00143 }
00144 return val;
00145 }
00146
00147
00148 int Util::stringToInt(const std::string& inStr) {
00149 int val;
00150 char junk[3];
00151 int nItem = sscanf(inStr.c_str(), "%d %1s", &val, junk);
00152 if (nItem != 1) {
00153 throw WrongType(inStr, "int");
00154 }
00155 return val;
00156 }
00157
00158 void Util::stringTokenize(std::string input, const std::string& delimiters,
00159 std::vector<std::string>& tokens, bool clear) {
00160 if (clear) tokens.clear();
00161
00162 std::string::size_type j;
00163 while ( (j = input.find_first_of(delimiters)) != std::string::npos ) {
00164 if (j != 0) tokens.push_back(input.substr(0, j));
00165 input = input.substr(j+1);
00166 }
00167 tokens.push_back(input);
00168 }
00169
00170 std::string Util::basename(const std::string& path) {
00171 std::vector<std::string> names;
00172 stringTokenize(path, "\\/", names);
00173 return *(names.end() - 1);
00174 }
00175
00176 }
00177
00178
00179
00180
00181
00182