1 /*
2   This file is part of CDO. CDO is a collection of Operators to manipulate and analyse Climate model Data.
3 
4   Author: Uwe Schulzweida
5 
6 */
7 #ifndef NAMELIST_H_
8 #define NAMELIST_H_
9 
10 #include <vector>
11 
12 enum class NamelistType
13 {
14   UNDEFINED = 0,
15   OBJECT = 1,
16   KEY = 2,
17   STRING = 3,
18   WORD = 4
19 };
20 
21 enum class NamelistError
22 {
23   UNDEFINED = 0,
24   INVAL = -1,  // Invalid character inside NAMELIST string/word
25   PART = -2,   // The string is not a full NAMELIST packet, more bytes expected
26   INKEY = -3,  // Invalid character inside NAMELIST key
27   INTYP = -4,  // Invalid NAMELIST key type
28   INOBJ = -5,  // Invalid NAMELIST object
29   EMKEY = -6   // Empty key name
30 };
31 
32 // NAMELIST token description.
33 class NamelistToken
34 {
35 public:
36   NamelistType type;  // type (object, key, string word)
37   long start;         // start position in NAMELIST buffer
38   long end;           // end position in NAMELIST buffer
39 
40   void fill(NamelistType type, long start, long end);
41 };
42 
43 class NamelistParser
44 {
45 public:
46   std::vector<NamelistToken> tokens;
47   unsigned long num_tokens = 0;
48   unsigned long toknext = 0;
49   unsigned long pos = 0;
50   unsigned long lineno = 0;
51 
52   NamelistError parse(const char *buf, size_t len);
53   void dump(const char *buf);
54   int verify();
55 
56 private:
57   NamelistError checkKeyname(const char *buf, NamelistToken *t);
58   NamelistError parseString(const char *buf, size_t len, char quote);
59   NamelistError parseWord(const char *buf, size_t len);
60   void newObject();
61   NamelistToken *allocToken();
62 };
63 
64 #endif  // NAMELIST_H_
65