QGpCoreTools/LineParser.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2009-03-15
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef LINEPARSER_H
00029 #define LINEPARSER_H
00030 
00031 #include "QGpCoreToolsDLLExport.h"
00032 #include "StringSection.h"
00033 
00034 namespace QGpCoreTools {
00035 
00036 class QGPCORETOOLS_EXPORT LineParser
00037 {
00038 public:
00039   LineParser();
00040   LineParser(const QString& str);
00041   LineParser(const StringSection& str);
00042   ~LineParser() {}
00043 
00044   void setString(const QString& str);
00045   void setString(const StringSection& str);
00046 
00047   void setDelimiters(const QString& d) {_delimiters=d;}
00048   const QString& delimiters() const {return _delimiters;}
00049 
00050   void setSkipEmpty(bool s) {_skipEmpty=s;}
00051   bool skipEmpty() const {return _skipEmpty;}
00052 
00053   int count();
00054   inline const StringSection& value(int index);
00055   inline QString toString(int index, bool& ok);
00056   QString toString(int indexBegin, int indexEnd, bool& ok);
00057   inline bool toBool(int index, bool& ok);
00058   inline int toInt(int index, bool& ok, int base=10);
00059   inline uint toUInt(int index, bool& ok, int base=10);
00060   inline qlonglong toLongLong(int index, bool& ok, int base=10);
00061   inline double toDouble(int index, bool& ok);
00062   int startsAt(int index, bool& ok);
00063   int endsAt(int index, bool& ok);
00064 private:
00065   const StringSection& parse(int index);
00066 
00067   QVector<StringSection> _values;
00068   const QChar * _ptr;
00069   StringSection _strSection;
00070   QString _str;
00071   QString _delimiters;
00072   bool _skipEmpty;
00073 };
00074 
00075 inline const StringSection& LineParser::value(int index)
00076 {
00077   if(index<_values.count()) return _values.at(index);
00078   return parse(index);
00079 }
00080 
00081 inline QString LineParser::toString(int index, bool& ok)
00082 {
00083   const StringSection& v=value(index);
00084   if(v.isValid()) {
00085     return v.toString();
00086   } else {
00087     ok=false;
00088     return QString::null;
00089   }
00090 }
00091 
00092 inline bool LineParser::toBool(int index, bool& ok)
00093 {
00094   const StringSection& v=value(index);
00095   if(v.isValid()) {
00096     return v.toBool();
00097   } else {
00098     ok=false;
00099     return true;
00100   }
00101 }
00102 
00103 inline int LineParser::toInt(int index, bool& ok, int base)
00104 {
00105   if(ok) {
00106     return value(index).toInt(&ok, base);
00107   } else {
00108     return 0;
00109   }
00110 }
00111 
00112 inline uint LineParser::toUInt(int index, bool& ok, int base)
00113 {
00114   if(ok) {
00115     return value(index).toUInt(&ok, base);
00116   } else {
00117     return 0;
00118   }
00119 }
00120 
00121 inline qlonglong LineParser::toLongLong(int index, bool& ok, int base)
00122 {
00123   if(ok) {
00124     return value(index).toLongLong(&ok, base);
00125   } else {
00126     return 0;
00127   }
00128 }
00129 
00130 inline double LineParser::toDouble(int index, bool& ok)
00131 {
00132   if(ok) {
00133     return value(index).toDouble(&ok);
00134   } else {
00135     return 0.0;
00136   }
00137 }
00138 
00139 } // namespace QGpCoreTools
00140 
00141 #endif // LINEPARSER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines