QGpCoreTools/StringSection.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 : 2004-10-21
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (ULg, Liège, Belgium)
00025 **    Marc Wathelet (LGIT, Grenoble, France)
00026 **
00027 ***************************************************************************/
00028 
00029 #ifndef STRINGSECTION_H
00030 #define STRINGSECTION_H
00031 
00032 #include "Trace.h"
00033 #include "Complex.h"
00034 #include "QGpCoreToolsDLLExport.h"
00035 
00036 namespace QGpCoreTools {
00037 
00038 class QGPCORETOOLS_EXPORT StringSection
00039 {
00040 public:
00041   inline StringSection();
00042   inline StringSection(const QString& s);
00043   inline StringSection(const QChar * s);
00044   inline StringSection(const QChar * s, int len);
00045   inline StringSection(const QChar * s1, const QChar * s2);
00046   inline StringSection(const StringSection& o);
00047   ~StringSection() {}
00048   inline StringSection& operator=(const StringSection& o);
00049 
00050   inline bool operator==(const QChar * o) const;
00051   inline bool operator!=(const QChar * o) const;
00052   bool operator<(const QChar * o) const {return compare(o) < 0;}
00053   bool operator<=(const QChar * o) const {return compare(o) <= 0;}
00054   bool operator>(const QChar * o) const {return compare(o) > 0;}
00055   bool operator>=(const QChar * o) const {return compare(o) >= 0;}
00056   inline int compare(const QChar * o) const;
00057 
00058   inline bool operator==(const StringSection& o) const;
00059   inline bool operator!=(const StringSection& o) const;
00060   bool operator<(const StringSection& o) const {return compare(o) < 0;}
00061   bool operator<=(const StringSection& o) const {return compare(o) <= 0;}
00062   bool operator>(const StringSection& o) const {return compare(o) > 0;}
00063   bool operator>=(const StringSection& o) const {return compare(o) >= 0;}
00064   int compare(const StringSection &o) const;
00065 
00066   inline bool operator==(const QString& o) const;
00067   inline bool operator!=(const QString& o) const;
00068   bool operator<(const QString& o) const {return compare(o) < 0;}
00069   bool operator<=(const QString& o) const {return compare(o) <= 0;}
00070   bool operator>(const QString& o) const {return compare(o) > 0;}
00071   bool operator>=(const QString& o) const {return compare(o) >= 0;}
00072   inline int compare(const QString& o) const;
00073 
00074   QChar operator[] (int i) const {return _str[ i ];}
00075   void operator++() {_str++;if(_size > 0) _size--;}
00076   void operator+=(int l) {_str += l;if((int) _size >= l) _size -= l; else _size=0;}
00077   void operator--() {_str--;_size++;}
00078 
00079   bool isValid() const {return _str;}
00080   bool isEmpty() const {return _size==0;}
00081   const QChar * data() const {return _str;}
00082   const QChar * begin() const {return _str;}
00083   const QChar * end() const {return _str+_size;}
00084   int size() const {return _size;}
00085 
00086   void shrink(int nChar) {if(nChar>=_size) _size=0; else _size-=nChar;}
00087   StringSection& trimmed();
00088   void setInvalid() {_str=0; _size=0;}
00089   void set(const QString& s) {_str=s.data(); _size=s.size();}
00090   void set(const QChar * s, int size) {_str=s; _size=size;}
00091   void setEnd(const QChar * e) {_size=e-_str; if(_size< 0) _size=0;}
00092   void setSize(int size) {_size=size;}
00093 
00094   bool toBool(bool * ok=0) const;
00095   int toInt(bool * ok=0, int base=10) const;
00096   uint toUInt(bool * ok=0, int base=10) const;
00097   qlonglong toLongLong(bool * ok=0, int base=10) const;
00098   double toDouble(bool * ok=0) const;
00099   inline Complex toComplex() const;
00100   inline Complex toComplex(bool * ok) const;
00101 
00102   QString toString() const {return isValid() ? QString(_str, _size) : QString::null;}
00103   void appendTo(QString& str) const {str.insert(str.size(), _str, _size);}
00104 
00105   // Force conversion to a type given by a fake argument (used by XMLMap)
00106   bool to(bool, bool * ok=0) const {return toBool(ok);}
00107   QString to(QString) const {return toString();}
00108   int to(int, bool * ok=0) const {return toInt(ok);}
00109   uint to(uint, bool * ok=0) const {return toUInt(ok);}
00110   qlonglong to(qlonglong, bool * ok=0) const {return toLongLong(ok);}
00111   double to(double, bool * ok=0) const {return toDouble(ok);}
00112   Complex to(const Complex&, bool * ok=0) const {return toComplex(ok);}
00113 
00114   inline StringSection left(int n) const;
00115   inline StringSection right(int n) const;
00116   inline StringSection mid(int position, int n) const;
00117 
00118   const QChar * find(QChar c) const;
00119   int find(const StringSection target, bool reverse) const;
00120   bool contains(const StringSection target) const {return find(target, false)>-1;}
00121   bool contains(QChar target) const {return find(StringSection(&target, 1), false)>-1;}
00122   inline bool beginWith(const QString& o) const;
00123   inline bool endWith(const QString& o) const;
00124   bool beginWith(const StringSection& o) const;
00125   bool endWith(const StringSection& o) const;
00126 
00127   StringSection readLine(const QChar *& ptr) const;
00128   bool nextLine(const QChar *& ptr, const QString& sep=defaultSeparators) const;
00129   StringSection nextField(const QChar *& ptr, const QString& sep=defaultSeparators, bool skipEmpty=true) const;
00130   bool nextNumber(int& n, const QChar *& ptr, const QString& sep=defaultSeparators, bool skipEmpty=true) const;
00131   bool nextNumber(double& n, const QChar *& ptr, const QString& sep=defaultSeparators, bool skipEmpty=true) const;
00132   bool nextNumber(Complex& n, const QChar *& ptr, const QString& sep=defaultSeparators, bool skipEmpty=true) const;
00133 
00134   static int size(const QChar * str);
00135   static int compare(const QChar * str1, const QChar * str2, int n);
00136   static const QString defaultSeparators;
00137   static const StringSection null;
00138 private:
00139   const QChar * _str;
00140   int _size;
00141 };
00142 
00143 QGPCORETOOLS_EXPORT uint qHash(StringSection key);
00144 
00145 inline StringSection::StringSection()
00146 {
00147   _str=0;
00148   _size=0;
00149 }
00150 
00151 inline StringSection::StringSection(const QString& s)
00152 {
00153   _str=s.data();
00154   _size=s.size();
00155 }
00156 
00157 inline StringSection::StringSection(const QChar * s)
00158 {
00159   _str=s;
00160   _size=size(s);
00161 }
00162 
00163 inline StringSection::StringSection(const QChar * s1, const QChar * s2)
00164 {
00165   _str=s1;
00166   setEnd(s2);
00167 }
00168 
00169 inline StringSection::StringSection(const QChar * s, int len)
00170 {
00171   _str=s;
00172   _size=len;
00173 }
00174 
00175 inline StringSection::StringSection(const StringSection& o)
00176 {
00177   *this=o;
00178 }
00179 
00180 inline StringSection& StringSection::operator=(const StringSection& o)
00181 {
00182   _str=o._str;
00183   _size=o._size;
00184   return *this;
00185 }
00186 
00187 inline int StringSection::compare(const QChar * o) const
00188 {
00189   StringSection os(o);
00190   return compare(os);
00191 }
00192 
00193 inline int StringSection::compare(const QString& o) const
00194 {
00195   StringSection os(o);
00196   return compare(os);
00197 }
00198 
00199 inline bool StringSection::operator==(const QChar * o) const
00200 {
00201   if(_size!=size(o)) return false;
00202   return compare(o, _str, _size)==0;
00203 }
00204 
00205 inline bool StringSection::operator==(const StringSection& o) const
00206 {
00207   if(_size!=o._size) return false;
00208   return compare(o._str, _str, _size)==0;
00209 }
00210 
00211 inline bool StringSection::operator==(const QString& o) const
00212 {
00213   if(_size!=o.size()) return false;
00214   return compare(o.data(), _str, _size)==0;
00215 }
00216 
00217 inline bool StringSection::operator!=(const QChar * o) const
00218 {
00219   if(_size!=size(o)) return true;
00220   return compare(o, _str, _size)!=0;
00221 }
00222 
00223 inline bool StringSection::operator!=(const StringSection& o) const
00224 {
00225   if(_size!=o._size) return true;
00226   return compare(o._str, _str, _size)!=0;
00227 }
00228 
00229 inline bool StringSection::operator!=(const QString& o) const
00230 {
00231   if(_size!=o.size()) return true;
00232   return compare(o.data(), _str, _size)!=0;
00233 }
00234 
00235 inline bool StringSection::beginWith(const StringSection& o) const
00236 {
00237   if(_size < o._size) return false;
00238   return compare(o._str, _str, o.size())==0;
00239 }
00240 
00241 inline bool StringSection::endWith(const StringSection& o) const
00242 {
00243   if(_size < o._size) return false;
00244   return compare(o._str, _str+_size-o._size, o._size)==0;
00245 }
00246 
00247 inline bool StringSection::beginWith(const QString& o) const
00248 {
00249   if(_size < o.size()) return false;
00250   return compare(o.data(), _str, o.size())==0;
00251 }
00252 
00253 inline bool StringSection::endWith(const QString& o) const
00254 {
00255   if(_size < o.size()) return false;
00256   return compare(o.data(), _str+_size-o.size(), o.size())==0;
00257 }
00258 
00259 inline StringSection StringSection::left(int n) const
00260 {
00261   if(n>_size) n=_size;
00262   return StringSection(_str, n);
00263 }
00264 
00265 inline StringSection StringSection::right(int n) const
00266 {
00267   if(n>_size) n=_size;
00268   return StringSection(_str+_size-n, n);
00269 }
00270 
00271 inline StringSection StringSection::mid(int position, int n) const
00272 {
00273   if(position>=_size) return StringSection::null;
00274   int ns=_size-position;
00275   if(n<ns) ns=n;
00276   return StringSection(_str+position, ns);
00277 }
00278 
00279 inline Complex StringSection::toComplex() const
00280 {
00281   Complex c;
00282   c.fromString(*this);
00283   return c;
00284 }
00285 
00286 inline Complex StringSection::toComplex(bool * ok) const
00287 {
00288   Complex c;
00289   bool tok=c.fromString(*this);
00290   if(ok) *ok=tok;
00291   return c;
00292 }
00293 
00294 } // namespace QGpCoreTools
00295 
00296 #endif // STRINGSECTION_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines