File indexing completed on 2024-06-16 03:42:49

0001 /*
0002     File                 : ReadStatFilterPrivate.h
0003     Project              : LabPlot
0004     Description          : Private implementation class for ReadStatFilter.
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2021 Stefan Gerlach <stefan.gerlach@uni.kn>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 #ifndef READSTATFILTERPRIVATE_H
0011 #define READSTATFILTERPRIVATE_H
0012 
0013 #ifdef HAVE_READSTAT
0014 extern "C" {
0015 #include <readstat.h>
0016 }
0017 #endif
0018 
0019 #include "backend/lib/macros.h"
0020 
0021 class AbstractDataSource;
0022 
0023 class LabelSet {
0024     QVector<QString> m_labels;
0025     QVector<QString> m_valuesString;
0026     QVector<int> m_valuesInt;
0027     QVector<double> m_valuesDouble;
0028 
0029 public:
0030     LabelSet() {
0031     }
0032 
0033     QVector<QString> labels() const {
0034         return m_labels;
0035     }
0036     QString valueString(int i) const {
0037         return m_valuesString.at(i);
0038     }
0039     int valueInt(int i) const {
0040         return m_valuesInt.at(i);
0041     }
0042     double valueDouble(int i) const {
0043         return m_valuesDouble.at(i);
0044     }
0045 
0046     void add(QString value, QString label) {
0047         if (m_valuesInt.size() > 0 || m_valuesDouble.size() > 0) {
0048             DEBUG(Q_FUNC_INFO << ", WARNING: can't add string value to integer/double label set");
0049             return;
0050         }
0051 
0052         m_valuesString.append(value);
0053         m_labels.append(label);
0054     }
0055     void add(int value, QString label) {
0056         if (m_valuesDouble.size() > 0 || m_valuesString.size() > 0) {
0057             DEBUG(Q_FUNC_INFO << ", WARNING: can't add integer value to double/string label set");
0058             return;
0059         }
0060 
0061         m_valuesInt.append(value);
0062         m_labels.append(label);
0063     }
0064     void add(double value, QString label) {
0065         if (m_valuesInt.size() > 0 || m_valuesString.size() > 0) {
0066             DEBUG(Q_FUNC_INFO << ", WARNING: can't add double value to int/string label set");
0067             return;
0068         }
0069 
0070         m_valuesDouble.append(value);
0071         m_labels.append(label);
0072     }
0073 
0074     int size() const {
0075         return m_labels.size();
0076     }
0077 };
0078 
0079 class ReadStatFilterPrivate {
0080 public:
0081     explicit ReadStatFilterPrivate(ReadStatFilter*);
0082 
0083 #ifdef HAVE_READSTAT
0084     // callbacks (get*)
0085     static int getMetaData(readstat_metadata_t*, void*);
0086     static int getVarName(int index, readstat_variable_t*, const char* val_labels, void*);
0087     static int getColumnModes(int row, readstat_variable_t*, readstat_value_t, void*);
0088     static int getValuesPreview(int row, readstat_variable_t*, readstat_value_t, void*);
0089     static int getValues(int row, readstat_variable_t*, readstat_value_t, void*);
0090     static int getNotes(int index, const char* note, void*);
0091     static int getFWeights(readstat_variable_t*, void*);
0092     static int getValueLabels(const char* val_labels, readstat_value_t, const char* label, void*);
0093     readstat_error_t parse(const QString& fileName, bool preview = false, bool prepare = false);
0094 #endif
0095     QVector<QStringList> preview(const QString& fileName, int lines);
0096     void readDataFromFile(const QString& fileName, AbstractDataSource* = nullptr, AbstractFileFilter::ImportMode = AbstractFileFilter::ImportMode::Replace);
0097     void write(const QString& fileName, AbstractDataSource*);
0098 
0099     static QStringList varNames;
0100     static QVector<AbstractColumn::ColumnMode> columnModes;
0101     static QVector<QStringList> dataStrings;
0102 
0103     static int startRow;
0104     static int endRow;
0105     static int startColumn;
0106     static int endColumn;
0107 
0108 private:
0109     static int m_varCount; // nr of cols (vars)
0110     static int m_rowCount; // nr of rows
0111     static QStringList m_lineString;
0112     static std::vector<void*> m_dataContainer;
0113     static QStringList m_notes;
0114     static QVector<QString> m_valueLabels;
0115     static QMap<QString, LabelSet> m_labelSets;
0116 };
0117 
0118 #endif