QGpCoreTools/ColumnTextParser.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 : 2008-07-11
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef COLUMNTEXTPARSER_H
00029 #define COLUMNTEXTPARSER_H
00030 
00031 #include "Thread.h"
00032 #include "XMLClass.h"
00033 #include "ColumnTextDescription.h"
00034 #include "QGpCoreToolsDLLExport.h"
00035 
00036 namespace QGpCoreTools {
00037 
00038 class QGPCORETOOLS_EXPORT ColumnTextParser : public Thread, public XMLClass
00039 {
00040   Q_OBJECT
00041 public:
00042   ColumnTextParser(QObject * parent=0);
00043   ColumnTextParser(const ColumnTextParser& o);
00044   ~ColumnTextParser();
00045 
00046   virtual const QString& xml_tagName() const {return xmlColumnTextParserTag;}
00047   static const QString xmlColumnTextParserTag;
00048 
00049   // Initialization
00050   void setText(QTextStream * s);
00051   void setStandardTypes(const QStringList& standardTypes);
00052   const QStringList& standardTypes() const {return _standardTypes;}
00053 
00054   // Line properties
00055   int startLine() const {return _startLine;}
00056   void setStartLine(int s) {_startLine=s<0 ? 0 : s;  }
00057 
00058   int endLine() const {return _endLine;}
00059   void setEndLine(int e) {_endLine=e<0 ? 0 : e;}
00060 
00061   QString acceptPattern() const {return _acceptPattern;}
00062   void setAcceptPattern(QString a) {_acceptPattern=a;}
00063 
00064   QString rejectPattern() const {return _rejectPattern;}
00065   void setRejectPattern(QString r) {_rejectPattern=r;}
00066 
00067   bool hasComments() const {return _hasComments;}
00068   void setHasComments(bool c) {_hasComments=c;}
00069 
00070   // Column properties
00071   void setFixedWidth(bool f) {_fixedWidth=f;}
00072   bool isFixedWidth() const {return _fixedWidth;}
00073 
00074   QString delimiters() const {return _delimiters;}
00075   void setDelimiters(QString d) {_delimiters=d;}
00076 
00077   int maximumColumnCount() const {return _maximumColumnCount;}
00078   void setMaximumColumnCount(int m) {_maximumColumnCount=m;}
00079 
00080   bool acceptNullColumns() const {return _acceptNullColumns;}
00081   void setAcceptNullColumns(bool a) {_acceptNullColumns=a;}
00082 
00083   QString columnWidths() const {return columnWidths(_columnWidths);}
00084   void setColumnWidths(QString w) {_columnWidths=columnWidths(w);}
00085 
00086   // Section properties
00087   bool isSectionLinePattern() const {return _isSectionLinePattern;}
00088   void setSectionLinePattern(bool b) {_isSectionLinePattern=b;}
00089 
00090   QString sectionLinePattern() const {return _sectionLinePattern;}
00091   void setSectionLinePattern(QString s) {_sectionLinePattern=s;}
00092 
00093   bool isSectionLineCount() const {return _isSectionLineCount;}
00094   void setSectionLineCount(bool b) {_isSectionLineCount=b;}
00095 
00096   int sectionLineCount() const {return _sectionLineCount;}
00097   void setSectionLineCount(int s) {_sectionLineCount=s;}
00098 
00099   bool isSectionRowCount() const {return _isSectionRowCount;}
00100   void setSectionRowCount(bool b) {_isSectionRowCount=b;}
00101 
00102   int sectionRowCount() const {return _sectionRowCount;}
00103   void setSectionRowCount(int s) {_sectionRowCount=s;}
00104 
00105   bool isSectionCellPattern() const {return _isSectionCellPattern;}
00106   void setSectionCellPattern(bool b) {_isSectionCellPattern=b;}
00107 
00108   int sectionCellColumn() const {return _sectionCellColumn;}
00109   void setSectionCellColumn(int s) {_sectionCellColumn=s;}
00110 
00111   QString sectionCellPattern() const {return _sectionCellPattern;}
00112   void setSectionCellPattern(QString s) {_sectionCellPattern=s;}
00113 
00114   // Column type and operations
00115   int type(int column) const {return columnDescription(column).type();}
00116   void setType(int column, int type) {return columnDescription(column).setType(type);}
00117   QString typeName(int column) const {return _standardTypes[columnDescription(column).type()];}
00118   void setTypeName(int column, QString type);
00119 
00120   double factor(int column) const {return columnDescription(column).factor();}
00121   void setFactor(int column, double f) {columnDescription(column).setFactor(f);}
00122 
00123   QString replaceRx(int column) const {return columnDescription(column).replaceRx();}
00124   void setReplaceRx(int column, QString rx) {columnDescription(column).setReplaceRx(rx);}
00125 
00126   QString replaceAfter(int column) const {return columnDescription(column).replaceAfter();}
00127   void setReplaceAfter(int column, QString after) {columnDescription(column).setReplaceAfter(after);}
00128 
00129   // Start/stop parser engine
00130   void startUpdates();
00131   void stopUpdates();
00132 
00133   // Final queries
00134   const QString& text(int row, int column) const;
00135   int columnCount() const;
00136   int rowCount() const;
00137   int lineNumber(int row) const;
00138   int sectionBeginRow(int index) const;
00139   int sectionEndRow(int index) const;
00140   int sectionIndexOf(int row) const;
00141   const QString& sectionComments(int index) const;
00142 protected:
00143   virtual void xml_writeProperties(XML_WRITEPROPERTIES_ARGS) const;
00144   virtual void xml_writeChildren(XML_WRITECHILDREN_ARGS) const;
00145   virtual bool xml_setProperty(XML_SETPROPERTY_ARGS);
00146   virtual XMLMember xml_member(XML_MEMBER_ARGS);
00147   virtual void xml_polish(XML_POLISH_ARGS);
00148 signals:
00149   void dataAvailable();
00150 private:
00151   virtual void run();
00152   void parseRows();
00153   void addSection(int row);
00154   void parseColumns(const QString& rowStr, QRegExp * sectionCellRegExp);
00155   QString cellContent(int column, QString s) const;
00156   static QVector<int> columnWidths(const QString& ws);
00157   static QString columnWidths(const QVector<int>& wi);
00158 
00159   ColumnTextDescription& columnDescription(int column);
00160   const ColumnTextDescription& columnDescription(int column) const;
00161 
00162   QTextStream * _text;
00163   QStringList _standardTypes;
00164 
00165   QVector<ColumnTextDescription> _columnDescriptions;
00166 
00167   int _startLine, _endLine;
00168   QString _acceptPattern, _rejectPattern;
00169   QString _sectionLinePattern, _sectionCellPattern;
00170   int _sectionLineCount, _sectionRowCount, _sectionCellColumn;
00171   unsigned int _fixedWidth:1;
00172   unsigned int _acceptNullColumns:1;
00173   unsigned int _isSectionLinePattern:1;
00174   unsigned int _isSectionLineCount:1;
00175   unsigned int _isSectionRowCount:1;
00176   unsigned int _isSectionCellPattern:1;
00177   unsigned int _hasComments:1;
00178   QString _delimiters;
00179   int _maximumColumnCount;
00180   QVector<int> _columnWidths;
00181 
00182   QAtomicInt _terminate;
00183   mutable QMutex _mutex;
00184   int _columnCount, _lineNumber;
00185   QList<QStringList> _cells;
00186   QMap<int,int> _lines2rows;
00187   QList<int> _sectionMarkers;
00188   QList<QString> _sectionComments;
00189   int _lastSectionMarkerLine, _lastSectionMarkerRow;
00190 
00191   static const QString _nullCell;
00192 };
00193 
00194 } // namespace QGpCoreTools
00195 
00196 #endif // COLUMNTEXTPARSER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines