Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

StringParser.h

Go to the documentation of this file.
00001 #ifndef Impala_Util_StringParser_h
00002 #define Impala_Util_StringParser_h
00003 
00004 #include <string>
00005 #include <iostream>
00006 #include "Basis/String.h"
00007 #include "Core/Geometry/Rectangle.h"
00008 
00009 namespace Impala
00010 {
00011 namespace Util
00012 {
00013 
00014 
00015 class StringParser
00016 {
00017 public:
00018 
00019     StringParser(CString text, String::size_type startPoint = 0)
00020         : mText(text), mSize(text.size())
00021     {
00022         mP = Min<String::size_type>(startPoint, mSize);
00023     }
00024 
00025     int
00026     GetInt(char delim = ' ', bool delimRequired = false, bool eatDelim = false)
00027     {
00028         SkipWhitespace(delim);
00029         String::size_type end = mText.find(delim, mP);
00030         if ((end == String::npos) && delimRequired)
00031             std::cout << "StringParser::getInt: delim not found" << std::endl;
00032         String iStr = mText.substr(mP, end - mP);
00033         //std::cout << "iStr = " << iStr << std::endl;
00034         if (eatDelim)
00035         {
00036             mP = (end == String::npos) ? mSize : end+1;
00037         }
00038         else
00039         {
00040             mP = (end == String::npos) ? mSize : end;
00041         }
00042         return atoi(iStr);
00043     }
00044 
00045     double
00046     GetDouble(char delim = ' ', bool delimRequired = false,
00047               bool eatDelim = false)
00048     {
00049         SkipWhitespace(delim);
00050         String::size_type end = mText.find(delim, mP);
00051         if ((end == String::npos) && delimRequired)
00052             std::cout << "StringParser::getDouble: delim not found" <<std::endl;
00053         String fStr = mText.substr(mP, end - mP);
00054         //std::cout << "fStr = " << fStr << std::endl;
00055         if (eatDelim)
00056         {
00057             mP = (end == String::npos) ? mSize : end+1;
00058         }
00059         else
00060         {
00061             mP = (end == String::npos) ? mSize : end;
00062         }
00063         return atof(fStr);
00064     }
00065 
00066     String
00067     GetString(char delim = '"', bool delimRequired = true)
00068     {
00069         SkipWhitespace();
00070         if (mText[mP] == delim)
00071             mP++;
00072         String::size_type end = mText.find(delim, mP);
00073         if ((end == String::npos) && delimRequired)
00074             std::cout << "StringParser::getString: delim not found" <<std::endl;
00075         String str = mText.substr(mP, end - mP);
00076         //std::cout << "str = " << str << std::endl;
00077         mP = (end == String::npos) ? mSize : end+1; // skip delim
00078         return str;
00079     }
00080 
00081     String
00082     GetString2(bool delimRequired = true)
00083     {
00084         SkipWhitespace();
00085         if (Peek() == '"')
00086             return GetString('"', delimRequired);
00087         return GetString(' ', delimRequired);
00088     }
00089 
00090     Core::Geometry::Rectangle
00091     GetRectangle(char delim = '"', bool delimRequired = true)
00092     {
00093         SkipWhitespace(delim);
00094         int coord[4];
00095         char delims[4] = {' ', ' ', ' ', delim};
00096         for (int i=0 ; i<4 ; i++)
00097         {
00098             String::size_type end = mText.find(delims[i], mP);
00099             if ((end == String::npos) && delimRequired)
00100                 std::cout << "StringParser::getRectangle: delim not found"
00101                           << std::endl;
00102             String str = mText.substr(mP, end - mP);
00103             //std::cout << "str = " << str << std::endl;
00104             coord[i] = atoi(str);
00105             mP = (end == String::npos) ? end : end+1;
00106         }
00107         return Core::Geometry::Rectangle(coord[0],coord[1],coord[2],coord[3]);
00108     }
00109 
00110     bool
00111     TheEnd() const
00112     {
00113         return ((mP == String::npos) || (mP >= mSize));
00114     }
00115 
00116     void
00117     Eat(char toChar, int nrToFind = 1)
00118     {
00119         while ((mP != String::npos) && (--nrToFind >= 0))
00120         {
00121             mP = mText.find(toChar, mP);
00122             if (mP != String::npos)
00123                 mP++;
00124         }
00125     }
00126 
00127     void
00128     Eat(CString str, int nrToFind = 1)
00129     {
00130         while ((mP != String::npos) && (--nrToFind >= 0))
00131         {
00132             mP = mText.find(str, mP);
00133             if (mP != String::npos)
00134                 mP += str.size();
00135         }
00136     }
00137 
00138     void
00139     Advance(char toChar, int nrToFind = 1)
00140     {
00141         while ((mP != String::npos) && (--nrToFind >= 0))
00142         {
00143             mP = mText.find(toChar, mP);
00144             if ((mP != String::npos) && (nrToFind >= 1))
00145                 mP++; // skip it if looking for more
00146         }
00147     }
00148 
00149     void
00150     Advance(CString toStr, int nrToFind = 1)
00151     {
00152         while ((mP != String::npos) && (--nrToFind >= 0))
00153         {
00154             mP = mText.find(toStr, mP);
00155             if ((mP != String::npos) && (nrToFind >= 1))
00156                 mP += toStr.size(); // skip it if looking for more
00157         }
00158     }
00159 
00160     void
00161     AdvanceP(int nrPos = 1)
00162     {
00163         if (mP == String::npos)
00164             return;
00165         mP += nrPos;
00166         if (mP >= mSize)
00167             mP = String::npos;
00168     }
00169 
00170     bool
00171     At(const String& s)
00172     {
00173         return mText.compare(mP, s.size(), s) == 0;
00174     }
00175 
00176     char
00177     Peek()
00178     {
00179         return mText[mP];
00180     }
00181 
00182     bool
00183     Contains(String subStr)
00184     {
00185         return (mText.find(subStr, mP) != String::npos);
00186     }
00187 
00188     // skips spaces and delim
00189     void
00190     SkipWhitespace(char delim = ' ')
00191     {
00192         while (mText[mP] == ' ')
00193             mP++;
00194         if (mText[mP] == delim)
00195             mP++;
00196     }
00197 
00198     String::size_type
00199     Position() const
00200     {
00201         return mP;
00202     }
00203 
00204 private:
00205 
00206     const String            mText;
00207     const String::size_type mSize;
00208     String::size_type mP;
00209 
00210 };
00211 
00212 } // namespace Util
00213 } // namespace Impala
00214 
00215 #endif

Generated on Fri Mar 19 09:31:48 2010 for ImpalaSrc by  doxygen 1.5.1