/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Calibration/facilities/facilities-00-00-04/src/Util.cxx

Go to the documentation of this file.
00001 // $Header: /bes/bes/BossCvs/Calibration/facilities/src/Util.cxx,v 1.2 2011/02/23 15:35:54 maqm Exp $
00002 
00003 #include "facilities/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> 
00019 namespace facilities {
00020     
00021   int Util::expandEnvVar(std::string* toExpand, 
00022                          const std::string& openDel,
00023                          const std::string& closeDel) {
00024     unsigned opLen = openDel.size();
00025     unsigned clLen = closeDel.size();
00026     int nSuccess = 0;
00027         
00028     int envStart = toExpand->find(openDel.c_str());
00029     while (envStart != -1) {
00030       int envEnd = toExpand->find(closeDel.c_str());
00031             
00032       // add  characters to account for opening delimiter
00033       int afterBracket = envStart + opLen;
00034             
00035       if (!( (envStart==-1) || (envEnd==-1) ))
00036       {
00037         std::string envVariable = 
00038           toExpand->substr(afterBracket,(envEnd-afterBracket));
00039         const char * path = getenv(envVariable.c_str());
00040         if (path) {
00041           toExpand->replace(envStart,(envEnd+clLen), path);
00042           if (nSuccess > -1) nSuccess++;
00043         }
00044         else {
00045           std::cerr << "Util::expandEnvVar unable to translate " 
00046                     << envVariable << std::endl;
00047           throw Untranslatable(envVariable);
00048         }
00049       }
00050       envStart = toExpand->find(openDel.c_str());
00051     }
00052     return nSuccess;
00053   }
00054     
00055   const char* Util::itoa(int val, std::string &outStr) {
00056     // Purpose and Method:  Provide a standard routine to convert integers
00057     //    into std::string.  The method used depends upon the availability of
00058     //    the stringstream classes.  The stringstream classes are the
00059     //    standard, but some compilers do yet support them.
00060     //    The method used is determined by the DEFECT_NO_STRINGSTREAM
00061     //    macro, defined in the facilities requirements file.
00062 
00063     static char outCharPtr[20];
00064 
00065 #ifdef DEFECT_NO_STRINGSTREAM
00066     // Using static buffer to avoid problems with memory allocation
00067     char a[100]=""; 
00068     std::ostrstream locStream(a,100);
00069 #else
00070     std::ostringstream locStream;
00071     locStream.str("");
00072 #endif
00073     locStream << val;
00074 #ifdef DEFECT_NO_STRINGSTREAM
00075     locStream << std::ends;
00076 #endif
00077     outStr = locStream.str();
00078     strcpy(outCharPtr, outStr.c_str());
00079     return outCharPtr;
00080   }
00081     
00082   int Util::atoi(const std::string& inStr) {
00083     // Purpose and Method:  Provide a standard routine to convert std::strings
00084     //    into integers.  The method used depends upon the availability of
00085     //    the stringstream classes.  The stringstream classes are the
00086     //    standard, but some compilers do yet support them.
00087     //    The method used is determined by the DEFECT_NO_STRINGSTREAM
00088     //    macro, defined in the facilities requirements file.
00089     // Output: returns an integer
00090     //    if string cannot be converted to an integer, returns zero
00091 
00092     int val;
00093 
00094 #ifdef DEFECT_NO_STRINGSTREAM
00095     std::istrstream locStream(inStr.c_str());
00096 #else
00097     std::istringstream locStream(inStr);
00098 #endif
00099     locStream >> val;
00100     if (!locStream) {return 0;}
00101     return val;
00102   }  
00103 
00104   double Util::stringToDouble(const std::string& inStr) {
00105     double val;
00106     char  junk[3];
00107     int nItem = sscanf(inStr.c_str(), "%lg %1s", &val, junk);
00108     if (nItem != 1) {
00109       throw WrongType(inStr, "double");
00110     }
00111     return val;
00112   }
00113 
00114 
00115   int Util::stringToInt(const std::string& inStr) {
00116     int  val;
00117     char junk[3];
00118     int nItem = sscanf(inStr.c_str(), "%d %1s", &val, junk);
00119     if (nItem != 1) {
00120       throw WrongType(inStr, "int");
00121     }
00122     return val;
00123   }
00124 
00125   void Util::stringTokenize(std::string input, const std::string& delimiters,
00126                             std::vector<std::string>& tokens, bool clear) {
00127     if (clear) tokens.clear();
00128 
00129     std::string::size_type j;
00130     while ( (j = input.find_first_of(delimiters)) != std::string::npos ) {
00131       if (j != 0) tokens.push_back(input.substr(0, j));
00132       input = input.substr(j+1);
00133     }
00134     tokens.push_back(input);
00135     if (tokens.back() == "") tokens.pop_back();
00136   }
00137 
00138 
00139   void Util::keyValueTokenize(std::string input, 
00140                               const std::string& delimiters,
00141                               std::map<std::string,std::string>& tokens, 
00142                               const std::string& pairDelimiter,
00143                               bool clear) {
00144     if (clear) tokens.clear();
00145 
00146     std::vector<std::string> strvec;
00147     stringTokenize(input,delimiters,strvec,true);
00148     unsigned advance = pairDelimiter.size();
00149 
00150     std::vector<std::string>::const_iterator input_itr = strvec.begin();
00151     while(input_itr!=strvec.end())
00152       {
00153         std::string current = *input_itr++;
00154         std::string::size_type j = current.find(pairDelimiter);
00155         std::string key   = current.substr(0, j);
00156         std::string value = current.substr(j+advance);
00157         tokens[key] = value;
00158       } 
00159   }
00160   
00161   std::string Util::basename(const std::string& path) {
00162      std::vector<std::string> names;
00163      stringTokenize(path, "\\/", names);
00164      return *(names.end() - 1);
00165   }
00166 
00167   unsigned Util::trimTrailing(std::string* toTrim) {
00168     static const char blank=' ';
00169     static const char LF=0xA;    // new line
00170     static const char FF=0xC;    // form feed
00171     static const char CR=0xD;    // carriage return
00172 
00173     unsigned orig = toTrim->size();
00174     unsigned last = orig - 1;
00175     bool notDone = true;
00176     unsigned nTrimmed = 0;
00177 
00178 
00179     while (notDone) {
00180       char lastChar = (*toTrim)[last];
00181       switch (lastChar) {
00182       case blank:
00183       case LF:
00184       case FF:
00185       case CR:
00186         last--;
00187         nTrimmed++;
00188         break;
00189       default:
00190         notDone=false;
00191         break;
00192       }
00193     }
00194     if (nTrimmed)  toTrim->resize(orig - nTrimmed);
00195 
00196     return nTrimmed;
00197   }
00198 
00199 }
00200 
00201 
00202 
00203 
00204 
00205 

Generated on Tue Nov 29 22:57:56 2016 for BOSS_7.0.2 by  doxygen 1.4.7