Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / JSONReader / plugin / JSONParserModule / vtkJSONParser.h
1 // Copyright (C) 2015-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author: Roman NIKOLAEV (roman.nikolaev@opencascade.com)
20
21 #ifndef __vtkJSONParser_h_
22 #define __vtkJSONParser_h_
23
24 #include <exception>
25 #include <stdio.h>
26 #include <string>
27 #include <vector>
28 #include <vtkObject.h>
29
30 class vtkTable;
31 class vtkJSONNode;
32 class vtkJSONMetaNode;
33 class vtkJSONInfoNode;
34
35 //---------------------------------------------------
36 class VTK_EXPORT vtkJSONException : public std::exception
37 {
38 public:
39   vtkJSONException(const char* reason);
40   ~vtkJSONException() noexcept;
41   const char* what() const noexcept;
42
43 protected:
44   std::string Reason;
45 };
46
47 class VTK_EXPORT vtkJSONParser : public vtkObject
48 {
49 public:
50   static vtkJSONParser* New();
51
52   // Description:
53   // Specifies the name of the file
54   vtkGetStringMacro(FileName);
55   vtkSetStringMacro(FileName);
56
57   virtual int Parse(vtkTable* theTable);
58
59 protected:
60   // Struct to store cursor information
61   //----------------------------------
62   struct Info
63   {
64   public:
65     char type;
66     long ln;
67     long cn;
68     long pos;
69
70     Info()
71     {
72       type = 0;
73       ln = -1;
74       cn = -1;
75       pos = -1;
76     }
77   };
78
79   vtkJSONParser();
80   ~vtkJSONParser();
81
82   // name of the file to read from
83   //----------------------------------
84   char* FileName;
85
86   // current line and column
87   //----------------------------------
88   int LineNumber;
89   int ColumnNumber;
90
91   // markup information
92   //----------------------------------
93   std::vector<Info> CInfoVector;
94
95   // file
96   //----------------------------------
97   FILE* File;
98
99   // Nodes
100   //----------------------------------
101   std::vector<vtkJSONNode*> Nodes;
102   vtkJSONNode* CurrentNode;
103   vtkJSONMetaNode* MetaNode;
104
105   // Nodes
106   //----------------------------------
107   std::vector<char> ExpectedCharacters;
108
109   // Flags
110   //----------------------------------
111   bool InsideQuotes;
112   bool ParseList;
113   bool ParseObjectList;
114   bool ShortNamesFilled;
115
116   // Last parced string
117   //----------------------------------
118   char* LastString;
119   std::vector<const char*> Strings;
120   std::vector<const char*> CurrentList;
121   std::vector<const char*> ShortNames;
122
123   // Last parced values
124   //----------------------------------
125   double LastValue;
126
127 private:
128   vtkJSONParser(const vtkJSONParser&) = delete;
129   void operator=(const vtkJSONParser&) = delete;
130
131   vtkJSONMetaNode* GetMetaNode();
132   vtkJSONInfoNode* GetInfoNode();
133
134   void processOCB();
135   void processCCB();
136
137   void processOSB();
138   void processCSB();
139
140   void processCOMMA();
141
142   void processCOLON();
143
144   void processENDL();
145
146   void processQTS();
147
148   void processCharacter(const char ch);
149
150   void processMetaNode();
151   void processInfoNode();
152
153   void readDoubleValue();
154
155   char* getString(long b, long e);
156
157   void allowsDigits();
158
159   bool isDigitsAllowed();
160
161   void checkShortName(const char* unit);
162
163   void finalize(vtkTable* t);
164
165   void clean();
166
167   void throwSimpleException(const char* message);
168
169   void throwException(const char* message);
170
171   void throwException(const char* message, int ln, int cn);
172
173   void throwException(const char* message, int ln);
174 };
175 #endif //__vtkJSONParser_h_