File indexing completed on 2025-01-19 04:46:30

0001 /*
0002   SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QObject>
0010 
0011 #include <memory>
0012 class QTextCodec;
0013 
0014 class QIODevice;
0015 
0016 /**
0017  * @short An interface to build data structures from a CSV file.
0018  *
0019  * This class provides an abstract interface that can be used
0020  * to build up data structures from a comma separated value file
0021  * that is parsed with QCsvReader.
0022  *
0023  * @author Tobias Koenig <tokoe@kde.org>
0024  */
0025 class QCsvBuilderInterface
0026 {
0027 public:
0028     /**
0029      * This method is called on the destruction of the interface.
0030      */
0031     virtual ~QCsvBuilderInterface();
0032 
0033     /**
0034      * This method is called on start of the parsing.
0035      */
0036     virtual void begin() = 0;
0037 
0038     /**
0039      * This method is called whenever a new line starts.
0040      */
0041     virtual void beginLine() = 0;
0042 
0043     /**
0044      * This method is called for every parsed field.
0045      *
0046      * @param data The data of the field.
0047      * @param row The row of the field.
0048      * @param column The column of the field.
0049      */
0050     virtual void field(const QString &data, uint row, uint column) = 0;
0051 
0052     /**
0053      * This method is called whenever a line ends.
0054      */
0055     virtual void endLine() = 0;
0056 
0057     /**
0058      * This method is called at the end of parsing.
0059      */
0060     virtual void end() = 0;
0061 
0062     /**
0063      * This method is called whenever an error occurs during parsing.
0064      *
0065      * @param errorMsg The error message.
0066      */
0067     virtual void error(const QString &errorMsg) = 0;
0068 };
0069 
0070 class QCsvReaderPrivate;
0071 
0072 /**
0073  * @short A parser for comma separated value data.
0074  *
0075  * QCsvReader is a class that reads a comma separated value list (csv)
0076  * from a device and parses it into its fields. The parsed data are
0077  * passed to a QCsvBuilderInterface instance, which can build up
0078  * arbitrary data structures from it.
0079  *
0080  * @author Tobias Koenig <tokoe@kde.org>
0081  */
0082 class QCsvReader : public QObject
0083 {
0084     Q_OBJECT
0085 
0086     Q_PROPERTY(QChar textQuote READ textQuote WRITE setTextQuote)
0087     Q_PROPERTY(QChar delimiter READ delimiter WRITE setDelimiter)
0088     Q_PROPERTY(uint startRow READ startRow WRITE setStartRow)
0089 
0090 public:
0091     /**
0092      * Creates a new csv reader.
0093      *
0094      * @param builder The builder to use.
0095      */
0096     explicit QCsvReader(QCsvBuilderInterface *builder);
0097 
0098     /**
0099      * Destroys the csv reader.
0100      */
0101     ~QCsvReader() override;
0102 
0103     /**
0104      * Parses the csv data from @p device.
0105      *
0106      * @return true on success, false otherwise.
0107      */
0108     bool read(QIODevice *device);
0109 
0110     /**
0111      * Sets the character that is used for quoting. The default is '"'.
0112      */
0113     void setTextQuote(QChar textQuote);
0114 
0115     /**
0116      * Returns the character that is used for quoting.
0117      */
0118     QChar textQuote() const;
0119 
0120     /**
0121      * Sets the character that is used as delimiter for fields.
0122      * The default is ' '.
0123      */
0124     void setDelimiter(QChar delimiter);
0125 
0126     /**
0127      * Returns the delimiter that is used as delimiter for fields.
0128      */
0129     QChar delimiter() const;
0130 
0131     /**
0132      * Sets the row from where the parsing shall be started.
0133      *
0134      * Some csv files have some kind of header in the first line with
0135      * the column titles. To retrieve only the real data, set the start row
0136      * to '1' in this case.
0137      *
0138      * The default start row is 0.
0139      */
0140     void setStartRow(uint startRow);
0141 
0142     /**
0143      * Returns the start row.
0144      */
0145     uint startRow() const;
0146 
0147     /**
0148      * Sets the text codec that shall be used for parsing the csv list.
0149      *
0150      * The default is the system locale.
0151      */
0152     void setTextCodec(QTextCodec *textCodec);
0153 
0154     /**
0155      * Returns the text codec that is used for parsing the csv list.
0156      */
0157     QTextCodec *textCodec() const;
0158 
0159     /**
0160      * Terminates the parsing of the csv data.
0161      */
0162     void terminate();
0163 
0164 private:
0165     std::unique_ptr<QCsvReaderPrivate> const d;
0166 };
0167 
0168 class QCsvStandardBuilderPrivate;
0169 
0170 /**
0171  * @short A convenience class that implements QCsvBuilderInterface.
0172  *
0173  * QCsvStandardBuilder is a convenience class which stores
0174  * the parsed data from a csv list.
0175  *
0176  * @author Tobias Koenig <tokoe@kde.org>
0177  */
0178 class QCsvStandardBuilder : public QCsvBuilderInterface
0179 {
0180 public:
0181     /**
0182      * Creates a new csv standard builder.
0183      */
0184     QCsvStandardBuilder();
0185 
0186     /**
0187      * Destroys the csv standard builder.
0188      */
0189     ~QCsvStandardBuilder() override;
0190 
0191     /**
0192      * Returns the error message of the last error.
0193      */
0194     QString lastErrorString() const;
0195 
0196     /**
0197      * Returns the number of rows.
0198      */
0199     uint rowCount() const;
0200 
0201     /**
0202      * Returns the number of columns.
0203      */
0204     uint columnCount() const;
0205 
0206     /**
0207      * Returns the data of the field at the given
0208      * @p row and @p column.
0209      */
0210     QString data(uint row, uint column) const;
0211 
0212     /**
0213      * @internal
0214      */
0215     void begin() override;
0216     void beginLine() override;
0217     void field(const QString &data, uint row, uint column) override;
0218     void endLine() override;
0219     void end() override;
0220     void error(const QString &errorMsg) override;
0221 
0222 private:
0223     std::unique_ptr<QCsvStandardBuilderPrivate> const d;
0224 
0225     Q_DISABLE_COPY(QCsvStandardBuilder)
0226 };