Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

textparser.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2001 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qt Palmtop Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 #include "textparser.h"
00021 
00022 TextParser::TextParser(const QString &in, const QString &lineBreak)
00023 {
00024   data = in;
00025   lineSep = lineBreak;
00026   
00027   init();
00028   createSeparators();
00029   split();
00030 }
00031 
00032 TextParser::TextParser(const QString &in, const QString &lineBreak, const QString &sep)
00033 {
00034   data = in;
00035   lineSep = lineBreak;
00036   
00037   init();
00038   separators = sep;
00039   split();
00040 }
00041 
00042 void TextParser::init()
00043 {
00044   lineCount = 0;
00045   linePos = 0;
00046   totalElmCount = 0;
00047   separatorPos = -1;        //not initialized
00048   wordPos = -1;                   //not initialized
00049   sepAtLine = 0;
00050   sepAtPosElm = -1;       //such that nextSep equals 0
00051   wordAtLine = 0;
00052   wordAtPosElm = -1;        //such that nextWord equals 0
00053   atLine = 0;
00054   atPosElm = 0;
00055 }
00056 
00057 void TextParser::createSeparators()
00058 {
00059   separators = " @#,.:;<>*/(){}|'?-+=_";
00060 }
00061 
00062 /*  Returns pos of given search criteria, -1 if not found */
00063 int TextParser::find(const QString &target, QChar sep, int pos, bool upperCase)
00064 {
00065   
00066   t_splitElm parsstr;
00067   QString pString, pTarget;
00068   pTarget = target;
00069   int atLine = 0, atPosElm = 0;
00070   
00071   getLineReference(pos,&atLine,&atPosElm);
00072   
00073   for (int x = pos; x < totalElmCount; x++) 
00074   {
00075     parsstr=splitDone[atLine].elm[atPosElm++];
00076   
00077     if (upperCase)
00078     {
00079       pString=parsstr.str.upper();
00080       pTarget=pTarget.upper();
00081     } 
00082     else
00083     {
00084       pString=parsstr.str;
00085     }       
00086     if ((pString == pTarget) && (parsstr.separator == sep))
00087     {
00088       return x;
00089     } 
00090     if (atPosElm >= splitDone[atLine].elmCount) 
00091     {  //new Line
00092       atLine++;
00093       atPosElm = 0;
00094     }
00095   }
00096   return -1;
00097 }
00098 
00099 int TextParser::elmCount()
00100 {
00101   return totalElmCount;
00102 }
00103 
00104 QChar TextParser::separatorAt(int pos)
00105 {
00106   if (getLineReference(pos, &sepAtLine, &sepAtPosElm) == -1)
00107     return QChar::null;
00108   
00109   separatorPos = pos;
00110   return splitDone[sepAtLine].elm[sepAtPosElm].separator;
00111 }
00112 
00113 QChar TextParser::nextSeparator()
00114 {
00115   sepAtPosElm++;
00116   if (splitDone[sepAtLine].elmCount <= sepAtPosElm) {
00117     sepAtLine++;
00118     sepAtPosElm = 0;
00119   }
00120   
00121   separatorPos++;
00122   return splitDone[sepAtLine].elm[sepAtPosElm].separator;
00123 }
00124 
00125 bool TextParser::hasNextSeparator()
00126 {
00127   return ((separatorPos+1) < totalElmCount);
00128 }
00129 
00130 QString TextParser::wordAt(int pos)
00131 {
00132   if (getLineReference(pos, &wordAtLine, &wordAtPosElm) == -1)
00133     return NULL;
00134   
00135   wordPos = pos;
00136   return splitDone[wordAtLine].elm[wordAtPosElm].str;
00137 }
00138 
00139 QString TextParser::nextWord()
00140 {
00141   wordAtPosElm++;
00142   if (splitDone[wordAtLine].elmCount <= wordAtPosElm) {
00143     wordAtLine++;
00144     wordAtPosElm = 0;
00145   }
00146     
00147   wordPos++;
00148   return splitDone[wordAtLine].elm[wordAtPosElm].str;
00149 }
00150 
00151 bool TextParser::hasNextWord()
00152 {
00153   return ((wordPos + 1) < totalElmCount);
00154 }
00155 
00156 QString TextParser::getString(int *pos, QChar stop, bool lineEnd = false)
00157 {
00158   QString returnStr = wordAt(*pos);
00159   QChar chr = separatorAt(*pos);
00160   QString s;
00161   
00162   if (returnStr == "")
00163     return "";
00164   if (chr == stop)
00165     return returnStr;
00166   
00167   if (!lineEnd) {
00168     while ((chr != stop) && hasNextWord()) {
00169       returnStr.append(chr);
00170       returnStr += nextWord();
00171       chr = nextSeparator();
00172     }
00173   } else {                //copy from pos to end of line
00174     getLineReference(*pos, &atLine, &atPosElm);
00175     returnStr = "";
00176     while (atPosElm < splitDone[atLine].elmCount) {
00177       if (splitDone[atLine].elm[atPosElm].str != "") {
00178         returnStr += splitDone[atLine].elm[atPosElm].str;
00179       }
00180       chr = splitDone[atLine].elm[atPosElm].separator;
00181       if (!chr.isNull() && (int) chr != 0) {
00182         returnStr.append(splitDone[atLine].elm[atPosElm].separator);
00183       }
00184       atPosElm++;
00185     }
00186   }
00187   
00188   *pos = wordPos;
00189   return returnStr;
00190 }
00191 
00192 QString TextParser::getNextLine()
00193 {
00194   atLine++;
00195   atPosElm = 0;
00196   if (atLine < lineCount)
00197     return splitDone[atLine].str;
00198   return "";
00199 }
00200 
00201 bool TextParser::hasNextLine()
00202 {
00203   if (atLine+1 < lineCount)
00204     return TRUE;;
00205   return FALSE;
00206 }
00207 
00208 int TextParser::endLinePos(int pos)
00209 {
00210   if ( (getLineReference(pos, &atLine, &atPosElm)) == -1)
00211     return -1;
00212   
00213   return (pos + (splitDone[atLine].elmCount - atPosElm) + 1);
00214 }
00215 
00216 int TextParser::getLineReference(int pos, int *line, int *inLinePos)
00217 {
00218   int currentPos = 0;
00219   
00220   for (int x = 0; x < lineCount; x++) {
00221     if ( currentPos + splitDone[x].elmCount > pos) {
00222       *line = x;
00223       *inLinePos = pos - currentPos;
00224       return 0;           //pos found okay
00225     }
00226     currentPos += splitDone[x].elmCount;
00227   }
00228   return -1;                //no reference found
00229 }
00230 
00231 void TextParser::split()
00232 {
00233   t_splitLine newLine;
00234 
00235   while ((uint) linePos < data.length()) {
00236     newLine = nextLine();
00237     splitDone[lineCount] = splitLine(newLine);
00238     totalElmCount += splitDone[lineCount].elmCount;
00239     lineCount++;
00240   }
00241 }
00242 
00243 t_splitLine TextParser::splitLine(t_splitLine line)
00244 {
00245   uint pos = 0;
00246   uint elmCount = 0;
00247   t_splitLine tempLine = line;
00248   
00249   tempLine.str = line.str.simplifyWhiteSpace();
00250   tempLine.elm[0].str = "";
00251   while ( pos < line.str.length() ) {
00252     if ( isSeparator(tempLine.str[pos]) ) {
00253       tempLine.elm[elmCount].separator = tempLine.str[pos];
00254       elmCount++;
00255       pos++;
00256       while (tempLine.str[pos] == ' ')
00257         pos++;
00258       if (pos > line.str.length())
00259         elmCount--;
00260       tempLine.elm[elmCount].str = "";
00261     } else {
00262       if (!tempLine.str[pos].isNull())
00263         tempLine.elm[elmCount].str += tempLine.str[pos];
00264       pos++;
00265     }
00266   }
00267   
00268   tempLine.elmCount = elmCount + 1;
00269   return tempLine;
00270 }
00271 
00272 bool TextParser::isSeparator(QChar chr)
00273 {
00274   for (uint x = 0; x < separators.length(); x++) {
00275     if (chr == separators[x])
00276       return true;
00277   }
00278   return false;
00279 }
00280 
00281 t_splitLine TextParser::nextLine()
00282 {
00283   int newLinePos;
00284   t_splitLine lineType;
00285   
00286   newLinePos = data.find(lineSep, linePos);
00287   
00288   lineType.lineType = NewLine;
00289   lineType.str = "";
00290   
00291   if (newLinePos == -1) {
00292     newLinePos = data.length();
00293     lineType.lineType = LastLine;
00294   }
00295   
00296   for (int x = linePos; x < newLinePos; x++)
00297     lineType.str += data[x];
00298     
00299   linePos = newLinePos;
00300   if ((uint) linePos < data.length())   //if not EOF, add length of lineSep
00301     linePos += lineSep.length();
00302   
00303   return lineType;
00304 }

Generated on Sat Nov 5 16:18:09 2005 for OPIE by  doxygen 1.4.2