Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

TranslatorsUtils.cpp

00001                               /* TranslatorsUtils.cpp */
00002 
00003 /*
00004  *  Implementation of TranslatorsUtils.h
00005  */
00006 #include "TranslatorsUtils.h"
00007 
00008 using namespace LcgInfo;
00009 using namespace std;
00010 
00011 /********************  OTHER METHODS (General Usage)  **********************/
00012 
00013 void LcgInfo::tokenizeStr(string const & pStr, vector<string>& pTokens, string const & pDelimiters)
00014 {
00015     pTokens.clear(); // Just in case
00016      
00017     // Skip delimiters at beginning.
00018     string::size_type lastPos = pStr.find_first_not_of(pDelimiters, 0);
00019     // Find first "non-delimiter".
00020     string::size_type pos     = pStr.find_first_of(pDelimiters, lastPos);
00021 
00022     while (string::npos != pos || string::npos != lastPos)
00023     {
00024         // Found a token, add it to the vector.
00025         pTokens.push_back(pStr.substr(lastPos, pos - lastPos));
00026         // Skip delimiters.  Note the "not_of"
00027         lastPos = pStr.find_first_not_of(pDelimiters, pos);
00028         // Find next "non-delimiter"
00029         pos = pStr.find_first_of(pDelimiters, lastPos);
00030     }
00031 }
00032 
00033 void LcgInfo::tokenizeStrWithIndices (string const & pStr, vector<string>& pTokens,
00034                              vector<short int>& pIndices, string const & pDelimiters)
00035 {
00036    pTokens.clear();
00037    pIndices.clear();
00038    
00039     // Skip delimiters at beginning.
00040     string::size_type lastPos = pStr.find_first_not_of(pDelimiters, 0);
00041     // Find first "non-delimiter".
00042     string::size_type pos     = pStr.find_first_of(pDelimiters, lastPos);
00043 
00044     while (string::npos != pos || string::npos != lastPos)
00045     {
00046         pTokens.push_back(pStr.substr(lastPos, pos - lastPos)); // Add found token to vector 
00047         pIndices.push_back(lastPos);  // Also add its index to the indices vector
00048         lastPos = pStr.find_first_not_of(pDelimiters, pos); // Skip delimiters. Note the "not_of"
00049         pos = pStr.find_first_of(pDelimiters, lastPos);  // Find next "non-delimiter"
00050     }
00051 }
00052 
00053 void LcgInfo::delLeadingChars(string& pStr, string const & pChars){
00054    string::size_type pos=pStr.find_first_not_of(pChars,0);
00055    pStr.erase(0,pos);
00056 }
00057 
00058 void LcgInfo::delEndingChars(string& pStr, string const & pChars){
00059    string::size_type end=pStr.length()-1;
00060    string::size_type pos=pStr.find_last_not_of(pChars,end);
00061    pStr.erase(pos+1,end-pos);
00062 }
00063 
00064 void LcgInfo::delSurroundingChars(string& pStr, string const & pChars){
00065    LcgInfo::delLeadingChars(pStr, pChars);
00066    LcgInfo::delEndingChars(pStr, pChars);
00067 }
00068 
00069 bool LcgInfo::nocaseCompare(string const & pStr1, string const & pStr2){
00070    string::const_iterator it1=pStr1.begin();
00071    string::const_iterator it2=pStr2.begin();
00072 
00073    //stop when either string's end has been reached
00074    while ( (it1!=pStr1.end()) && (it2!=pStr2.end()) ){ 
00075       if(toupper(*it1) != toupper(*it2))  //letters differ?
00076          return false;  //then strings are different
00077        ++it1;  //proceed to the next character in each string
00078        ++it2;
00079    }
00080    string::size_type size1=pStr1.size(), size2=pStr2.size();// cache lengths
00081    if (size1==size2)  return true;  //if sizes are equal
00082    return false; //false otherwise
00083 }
00084 
00085 bool LcgInfo::isNumber(string const & pStr){
00086    float aux;
00087    istringstream is(pStr, istringstream::in);
00088    is >> aux;
00089    if(is.fail())   return false;
00090    else    return true;
00091 }
00092 
00093 bool LcgInfo::isLiteral(string const & pStr){
00094    string aux;
00095    aux=pStr;
00096    delSurroundingChars(aux); //eliminate blanks
00097    //first and last chars are "'"
00098    return ((aux.at(0)=='\'') && (aux.at(aux.size()-1)=='\''));
00099 }
00100 
00101 string LcgInfo::int2str(int const & pValue){
00102    ostringstream os(ostringstream::out);
00103    os << (pValue);
00104    return os.str();
00105 }
00106 
00107 int LcgInfo::str2int(string const & pStr){
00108    int aux;
00109    istringstream is(pStr, istringstream::in);
00110    is >> aux;
00111    return aux;
00112 }
00113 
00114 void LcgInfo::escapeLiterals(std::string & pStr, std::string const & pChars,
00115                     char const & pEscape, char const & pDelimiter){
00116    string::size_type posIni, posFin, posCh, whichCh;
00117    string aux=pStr;
00118    string toInsert;
00119    posIni = aux.find(pDelimiter, 0);   
00120    while (posIni != string::npos){
00121       posFin = aux.find(pDelimiter, posIni+1);
00122       if(posFin == string::npos){
00123          string msg="Parsing error when escaping literals. Unmatched \"\'\".";
00124          msg+="\nString: " + pStr;
00125          throw QueryTranslationException(msg,__FILE__,__LINE__);
00126       }
00127       posCh=aux.find_first_of(pChars, posIni+1);
00128       while (posCh < posFin){
00129          whichCh=pChars.find(aux.at(posCh));
00130          aux.erase(posCh,1);
00131          toInsert=string(1,pEscape)+"n"+LcgInfo::int2str(whichCh)+string(1,pEscape);
00132          aux.insert(posCh, toInsert);
00133          posFin+=toInsert.length()-1;
00134          posCh=aux.find_first_of(pChars, posCh+toInsert.length());
00135       }
00136    posIni = aux.find(pDelimiter, posFin+1); 
00137    }
00138    pStr=aux;
00139 }
00140 
00141 void LcgInfo::unescapeLiterals(std::string & pStr, std::string const & pChars,
00142                       char const & pEscape, char const & pDelimiter){
00143    string::size_type posIni, posFin, posCh, posCh2, whichCh;
00144    string aux=pStr;
00145    posIni = aux.find(pDelimiter, 0);   
00146    while (posIni != string::npos){
00147       posFin = aux.find(pDelimiter, posIni+1);
00148       if(posFin == string::npos){
00149          string msg="Parsing error when unescaping literals. Unmatched \"\'\".";
00150          msg+="\nString: " + pStr;
00151          throw QueryTranslationException(msg,__FILE__,__LINE__);
00152       }
00153       posCh=aux.find(pEscape, posIni+1);
00154       while (posCh < posFin){
00155          if(aux.at(posCh+1)=='n'){
00156             posCh2=aux.find(pEscape, posCh+1);
00157             whichCh=LcgInfo::str2int(aux.substr(posCh+2, posCh2-posCh-2));   
00158             if(whichCh<pChars.size()){
00159                aux.erase(posCh,posCh2-posCh+1);
00160                aux.insert(posCh, 1, pChars.at(whichCh));
00161                posFin-=posCh2-posCh;
00162             }
00163          }
00164          posCh=aux.find(pEscape, posCh+1);
00165       }
00166    posIni = aux.find(pDelimiter, posFin+1); 
00167    }
00168    pStr=aux;
00169 }
00170 
00171 string LcgInfo::extractTable(string const & pStr){
00172    string::size_type pos;
00173    pos=pStr.find_last_of('.',pStr.size()-1);
00174    return pStr.substr(0,pos);
00175 }
00176 
00177 string LcgInfo::extractRow(string const & pStr){
00178    string::size_type pos;
00179    pos=pStr.find_last_of('.',pStr.size()-1)+1;
00180    if(pos==string::npos) pos=0;
00181    return pStr.substr(pos);
00182 }
00183 
00184 

Generated on Tue Oct 5 14:42:45 2004 for LCG Information System Interface by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002