File indexing completed on 2024-12-22 04:52:50

0001 /*
0002    MorkParser.h - Mozilla Mork Format Parser/Reader
0003    SPDX-FileCopyrightText: 2007 <ScalingWeb.com>
0004    SPDX-FileContributor: Yuriy Soroka <ysoroka@scalingweb.com>
0005    SPDX-FileContributor: Anton Fedoruk <afedoruk@scalingweb.com>
0006 
0007    SPDX-License-Identifier: BSD-3-Clause
0008 */
0009 
0010 #pragma once
0011 
0012 #include <QByteArray>
0013 #include <QMap>
0014 #include <iostream>
0015 class QString;
0016 // Types
0017 
0018 using MorkDict = QMap<int, QString>;
0019 using MorkCells = QMap<int, int>; // ColumnId : ValueId
0020 using MorkRowMap = QMap<int, MorkCells>; // Row id
0021 using RowScopeMap = QMap<int, MorkRowMap>; // Row scope
0022 using MorkTableMap = QMap<int, RowScopeMap>; // Table id
0023 using TableScopeMap = QMap<int, MorkTableMap>; // Table Scope
0024 
0025 // Mork header of supported format version
0026 const char MorkMagicHeader[] = "// <!-- <mdb:mork:z v=\"1.4\"/> -->";
0027 
0028 const char MorkDictColumnMeta[] = "<(a=c)>";
0029 
0030 // Error codes
0031 enum MorkErrors { NoError = 0, FailedToOpen, UnsupportedVersion, DefectedFormat };
0032 
0033 // Mork term types
0034 enum MorkTerm { NoneTerm = 0, DictTerm, GroupTerm, TableTerm, RowTerm, CellTerm, CommentTerm, LiteralTerm };
0035 
0036 /// Class MorkParser
0037 
0038 class MorkParser
0039 {
0040 public:
0041     explicit MorkParser(int defaultScope = 0x80);
0042 
0043     ///
0044     /// Open and parse mork file
0045 
0046     bool open(const QString &path);
0047 
0048     ///
0049     /// Return error status
0050 
0051     MorkErrors error() const;
0052 
0053     ///
0054     /// Returns all tables of specified scope
0055 
0056     MorkTableMap *getTables(int tableScope);
0057 
0058     ///
0059     /// Rerturns all rows under specified scope
0060 
0061     MorkRowMap *getRows(int rowScope, RowScopeMap *table);
0062 
0063     ///
0064     /// Return value of specified value oid
0065 
0066     QString getValue(int oid);
0067 
0068     ///
0069     /// Return value of specified column oid
0070 
0071     QString getColumn(int oid);
0072 
0073 protected: // Members
0074     void initVars();
0075 
0076     bool isWhiteSpace(char c) const;
0077     char nextChar();
0078 
0079     void parseScopeId(const QString &TextId, int &Id, int &Scope) const;
0080     void setCurrentRow(int TableScope, int TableId, int RowScope, int RowId);
0081 
0082     // Parse methods
0083     bool parse();
0084     bool parseDict();
0085     bool parseComment();
0086     bool parseCell();
0087     bool parseTable();
0088     bool parseMeta(char c);
0089     bool parseRow(int TableId, int TableScope);
0090     bool parseGroup();
0091 
0092 protected: // Data
0093     // Columns in mork means value names
0094     MorkDict mColumns;
0095     MorkDict mValues;
0096 
0097     // All mork file data
0098     TableScopeMap mMork;
0099     MorkCells *mCurrentCells = nullptr;
0100 
0101     // Error status of last operation
0102     MorkErrors mError;
0103 
0104     // All Mork data
0105     QByteArray mMorkData;
0106 
0107     int mMorkPos;
0108     int mNextAddValueId;
0109     int mDefaultScope;
0110 
0111     // Indicates intity is being parsed
0112     enum { NPColumns, NPValues, NPRows } nowParsing_;
0113 };