1 /*****************************************************************************/ 2 /* */ 3 /* STRPARSE.H */ 4 /* */ 5 /* (C) 1993-96 Ullrich von Bassewitz */ 6 /* Wacholderweg 14 */ 7 /* D-70597 Stuttgart */ 8 /* EMail: uz@ibb.schwaben.com */ 9 /* */ 10 /*****************************************************************************/ 11 12 13 14 // $Id$ 15 // 16 // $Log$ 17 // 18 // 19 20 21 22 #ifndef _STRPARSE_H 23 #define _STRPARSE_H 24 25 26 27 #include "chartype.h" 28 #include "str.h" 29 #include "charset.h" 30 #include "datetime.h" 31 32 33 34 /*****************************************************************************/ 35 /* Constants for parsing results */ 36 /*****************************************************************************/ 37 38 39 40 const unsigned prOk = 0x0000; // Conversion done 41 const unsigned prTooLarge = 0x0001; // Value too large for data type 42 const unsigned prTooSmall = 0x0002; // Value to small for data type 43 const unsigned prSyntax = 0x0003; // Syntax error 44 const unsigned prMaxError = 0x0003; 45 46 47 48 /*****************************************************************************/ 49 /* class StringParser */ 50 /*****************************************************************************/ 51 52 53 54 class StringParser: public Object { 55 56 public: 57 enum { 58 SkipWS = 0x0001, // Skip white space before parsing 59 UseAll = 0x0002, // Error if no EOS is reached 60 PascalHex = 0x0004, // Allow pascal style hex values 61 CHex = 0x0008, // Allow C style hex values 62 COct = 0x0010, // Allow C style octal digits 63 AllowDP = 0x0020 // Allow real decimal point ('.') 64 }; 65 66 67 protected: 68 int P; // Position in String 69 const String& S; 70 u16 Flags; // Parsing flags 71 CharSet WhiteSpace; // White space characters 72 73 private: 74 int AllUsed (); 75 // Return true if UseAll is not set or if EOS is reached 76 77 static int Num (char C); 78 // Calculate the numeric value of the digit C (assumes ASCII) 79 80 public: 81 StringParser (const String& X, u16 ParseFlags = StringParser::UseAll); 82 83 void Reset (); 84 // Reset position to 0 85 86 int GetPos () const; 87 // Get current position in string 88 89 void SetPos (int NewPos); 90 // Set a new position for parsing 91 92 void SkipWhite (); 93 // Skip white space 94 95 void Skip (const String& Chars); 96 // Skip any chars contained in Chars 97 98 void Skip (const CharSet& Chars); 99 // Skip any chars contained in Chars 100 101 int EOS () const; 102 // Return 1 if the end of the string is reached 103 104 const String& GetMsg (unsigned Res); 105 // Return an apropriate error message for the result Res (!= 0) 106 107 void SetFlags (u16 F); 108 void ResetFlags (u16 F); 109 u16 GetFlags (); 110 // Handling the flags 111 112 // Extracting data 113 unsigned GetI32 (i32& Val, unsigned Base = 10); 114 unsigned GetU32 (u32& Val, unsigned Base = 10); 115 unsigned GetFloat (double& Val); 116 unsigned GetTime (Time& Val); 117 unsigned GetDate (Time& Val); 118 119 unsigned GetToken (String& Tok, const CharSet& Chars = ::WhiteSpace); 120 // Return token from the string that is separated by characters contained 121 // in Chars 122 123 unsigned GetString (String& S); 124 // Return a string enclosed in double quotes 125 126 }; 127 128 129 Reset()130inline void StringParser::Reset () 131 // Reset position to 0 132 { 133 P = 0; 134 } 135 136 137 GetPos()138inline int StringParser::GetPos () const 139 // Get current position in string 140 { 141 return P; 142 } 143 144 145 EOS()146inline int StringParser::EOS () const 147 // Return 1 if the end of the string is reached 148 { 149 return S [P] == '\0'; 150 } 151 152 153 SkipWhite()154inline void StringParser::SkipWhite () 155 // Skip white space 156 { 157 Skip (WhiteSpace); 158 } 159 160 161 SetFlags(u16 F)162inline void StringParser::SetFlags (u16 F) 163 { 164 Flags |= F; 165 } 166 167 168 ResetFlags(u16 F)169inline void StringParser::ResetFlags (u16 F) 170 { 171 Flags &= ~F; 172 } 173 174 175 GetFlags()176inline u16 StringParser::GetFlags () 177 { 178 return Flags; 179 } 180 181 182 183 // End of STRPARSE.H 184 185 #endif 186