DeltaQt
Version 0.2.0 End user documentation
A C++/Qt library for parsing DELTA (DEscription Language for TAxonomy) files
|
00001 /* Copyright 2012 Craig Robbins 00002 00003 This file is part of DeltaQt 00004 00005 DeltaQt is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 DeltaQt is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with DeltaQt. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 #ifndef DELIMITEDTEXT_H 00020 #define DELIMITEDTEXT_H 00021 00022 #include <QString> 00023 #include <QList> 00024 #include <QChar> 00025 #include <QString> 00026 #include <QStringList> 00027 00028 class DelimPairBoundary 00029 { 00030 public: 00031 DelimPairBoundary(); 00032 DelimPairBoundary(int level, 00033 int indexStart, 00034 int indexEnd, 00035 int lineNumber = 0); 00036 00037 int level(void) const 00038 { return m_level; } 00039 00040 int indexStart(void) const 00041 { return m_indexStart; } 00042 00043 int indexEnd(void) const 00044 { return m_indexEnd; } 00045 00046 protected: 00047 int m_level; 00048 int m_indexStart; 00049 int m_indexEnd; 00050 int m_indexStartLineNum; 00051 }; 00052 00053 00054 typedef QList<DelimPairBoundary>DelimPairBoundaries; 00055 00056 00057 class DelimitedBuffer : public QString 00058 { 00059 public: 00060 typedef enum { 00061 MAP_NOTINITIALISED, 00062 MAP_OK, 00063 MAP_ERROR_UNBALANCED_DELIMS, 00064 MAP_ERROR_SPLIT_EXCEPT_INVALID_DELIM 00065 } MapStatus; 00066 00067 00068 /* 00069 * CONSTRUCTORS 00070 */ 00071 DelimitedBuffer(); 00072 DelimitedBuffer(QString& other); 00073 DelimitedBuffer(const char* str); 00074 DelimitedBuffer(const QString& other); 00075 00076 DelimitedBuffer( 00077 const char* str, 00078 char startDelim, 00079 char endDelim, 00080 QString legalPreChars = QString(), 00081 QString legalPostChars = QString()); 00082 00083 DelimitedBuffer( 00084 const QString& other, 00085 char startDelim, 00086 char endDelim, 00087 QString legalPreChars = QString(), 00088 QString legalPostChars = QString()); 00089 00090 /* 00091 * NON-MAPPING FUNCTIONS 00092 */ 00093 bool isMapped(void) const; 00094 bool indexIsBetweenDelims(int idx) const; 00095 int numLevels(void) const; 00096 DelimPairBoundaries getChunkBoundaries(void) const; 00097 void clear(); 00098 DelimitedBuffer noWhitespace(void) const; 00099 00100 QString getChunk(DelimPairBoundary boundary) const; 00101 QString getChunk(int startPos, int endPos = -1) const; 00102 00103 QStringList getChunkStrings( 00104 int minLevel = -1, 00105 int maxLevel = -1 00106 ); 00107 00108 QString getStringsAppended( 00109 QChar delimWith, 00110 int minLevel = -1, 00111 int maxLevel = -1, 00112 bool simplify = true 00113 ); 00114 00115 DelimitedBuffer::MapStatus status(void) const; 00116 00117 /* 00118 * MAPPING FUNCTIONS 00119 */ 00120 bool initMap( 00121 QChar startDelim, 00122 QChar endDelim, 00123 QString legalPreChars = QString(), 00124 QString legalPostChars = QString()); 00125 00126 bool mapDelimBoundaries( 00127 QChar startDelim, 00128 QChar endDelim, 00129 QString legalPreChars = QString(), 00130 QString legalPostChars = QString()); 00131 00132 bool mapDelimBoundaries_EqDelims( 00133 QChar delim, 00134 QString legalPreChars = QString(), 00135 QString legalPostChars = QString()); 00136 00137 bool getTokens( 00138 QList<QString>* dest, 00139 char searchChar, 00140 QChar startDelim, 00141 QChar endDelim, 00142 QString legalPreChars = QString(), 00143 QString legalPostChars = QString(), 00144 bool splitOnPreIsSpace = false, 00145 bool splitOnPostIsSpace = false, 00146 bool removeSearchCharFromStart = false 00147 ); 00148 /* 00149 * Operator overloads 00150 */ 00151 QString operator =(const QString& other) { 00152 m_status = MAP_NOTINITIALISED; 00153 m_delimBoundaries.clear(); 00154 return QString::operator =(other); 00155 } 00156 QString operator =(const char* other) { 00157 m_status = MAP_NOTINITIALISED; 00158 m_delimBoundaries.clear(); 00159 return QString::operator =(other); 00160 } 00161 00162 bool preCharOk(QChar c, QString legalPreChars); 00163 bool postCharOk(QChar c, QString legalPostChars); 00164 00165 private: 00166 00167 int findCharPosition(QChar searchChar, 00168 int from = 0, 00169 int* lineNum = NULL) const; 00170 00171 QList<int> findCharPositionsExcept( 00172 char searchChar, 00173 QChar startDelim, 00174 QChar endDelim, 00175 QString legalPreChars = QString(), 00176 QString legalPostChars = QString(), 00177 bool splitOnPreIsSpace = false, 00178 bool splitOnPostIsSpace = false 00179 ); 00180 00181 MapStatus m_status; 00182 DelimPairBoundaries m_delimBoundaries; 00183 00184 }; 00185 00186 #endif // DELIMITEDTEXT_H