jpayne@69
|
1 // Copyright (c) 2005, Google Inc.
|
jpayne@69
|
2 // All rights reserved.
|
jpayne@69
|
3 //
|
jpayne@69
|
4 // Redistribution and use in source and binary forms, with or without
|
jpayne@69
|
5 // modification, are permitted provided that the following conditions are
|
jpayne@69
|
6 // met:
|
jpayne@69
|
7 //
|
jpayne@69
|
8 // * Redistributions of source code must retain the above copyright
|
jpayne@69
|
9 // notice, this list of conditions and the following disclaimer.
|
jpayne@69
|
10 // * Redistributions in binary form must reproduce the above
|
jpayne@69
|
11 // copyright notice, this list of conditions and the following disclaimer
|
jpayne@69
|
12 // in the documentation and/or other materials provided with the
|
jpayne@69
|
13 // distribution.
|
jpayne@69
|
14 // * Neither the name of Google Inc. nor the names of its
|
jpayne@69
|
15 // contributors may be used to endorse or promote products derived from
|
jpayne@69
|
16 // this software without specific prior written permission.
|
jpayne@69
|
17 //
|
jpayne@69
|
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
jpayne@69
|
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
jpayne@69
|
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
jpayne@69
|
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
jpayne@69
|
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
jpayne@69
|
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
jpayne@69
|
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
jpayne@69
|
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
jpayne@69
|
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
jpayne@69
|
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
jpayne@69
|
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
jpayne@69
|
29 //
|
jpayne@69
|
30 // Author: Sanjay Ghemawat
|
jpayne@69
|
31
|
jpayne@69
|
32 #ifndef _PCRECPPARG_H
|
jpayne@69
|
33 #define _PCRECPPARG_H
|
jpayne@69
|
34
|
jpayne@69
|
35 #include <stdlib.h> // for NULL
|
jpayne@69
|
36 #include <string>
|
jpayne@69
|
37
|
jpayne@69
|
38 #include <pcre.h>
|
jpayne@69
|
39
|
jpayne@69
|
40 namespace pcrecpp {
|
jpayne@69
|
41
|
jpayne@69
|
42 class StringPiece;
|
jpayne@69
|
43
|
jpayne@69
|
44 // Hex/Octal/Binary?
|
jpayne@69
|
45
|
jpayne@69
|
46 // Special class for parsing into objects that define a ParseFrom() method
|
jpayne@69
|
47 template <class T>
|
jpayne@69
|
48 class _RE_MatchObject {
|
jpayne@69
|
49 public:
|
jpayne@69
|
50 static inline bool Parse(const char* str, int n, void* dest) {
|
jpayne@69
|
51 if (dest == NULL) return true;
|
jpayne@69
|
52 T* object = reinterpret_cast<T*>(dest);
|
jpayne@69
|
53 return object->ParseFrom(str, n);
|
jpayne@69
|
54 }
|
jpayne@69
|
55 };
|
jpayne@69
|
56
|
jpayne@69
|
57 class PCRECPP_EXP_DEFN Arg {
|
jpayne@69
|
58 public:
|
jpayne@69
|
59 // Empty constructor so we can declare arrays of Arg
|
jpayne@69
|
60 Arg();
|
jpayne@69
|
61
|
jpayne@69
|
62 // Constructor specially designed for NULL arguments
|
jpayne@69
|
63 Arg(void*);
|
jpayne@69
|
64
|
jpayne@69
|
65 typedef bool (*Parser)(const char* str, int n, void* dest);
|
jpayne@69
|
66
|
jpayne@69
|
67 // Type-specific parsers
|
jpayne@69
|
68 #define PCRE_MAKE_PARSER(type,name) \
|
jpayne@69
|
69 Arg(type* p) : arg_(p), parser_(name) { } \
|
jpayne@69
|
70 Arg(type* p, Parser parser) : arg_(p), parser_(parser) { }
|
jpayne@69
|
71
|
jpayne@69
|
72
|
jpayne@69
|
73 PCRE_MAKE_PARSER(char, parse_char);
|
jpayne@69
|
74 PCRE_MAKE_PARSER(unsigned char, parse_uchar);
|
jpayne@69
|
75 PCRE_MAKE_PARSER(short, parse_short);
|
jpayne@69
|
76 PCRE_MAKE_PARSER(unsigned short, parse_ushort);
|
jpayne@69
|
77 PCRE_MAKE_PARSER(int, parse_int);
|
jpayne@69
|
78 PCRE_MAKE_PARSER(unsigned int, parse_uint);
|
jpayne@69
|
79 PCRE_MAKE_PARSER(long, parse_long);
|
jpayne@69
|
80 PCRE_MAKE_PARSER(unsigned long, parse_ulong);
|
jpayne@69
|
81 #if 1
|
jpayne@69
|
82 PCRE_MAKE_PARSER(long long, parse_longlong);
|
jpayne@69
|
83 #endif
|
jpayne@69
|
84 #if 1
|
jpayne@69
|
85 PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong);
|
jpayne@69
|
86 #endif
|
jpayne@69
|
87 PCRE_MAKE_PARSER(float, parse_float);
|
jpayne@69
|
88 PCRE_MAKE_PARSER(double, parse_double);
|
jpayne@69
|
89 PCRE_MAKE_PARSER(std::string, parse_string);
|
jpayne@69
|
90 PCRE_MAKE_PARSER(StringPiece, parse_stringpiece);
|
jpayne@69
|
91
|
jpayne@69
|
92 #undef PCRE_MAKE_PARSER
|
jpayne@69
|
93
|
jpayne@69
|
94 // Generic constructor
|
jpayne@69
|
95 template <class T> Arg(T*, Parser parser);
|
jpayne@69
|
96 // Generic constructor template
|
jpayne@69
|
97 template <class T> Arg(T* p)
|
jpayne@69
|
98 : arg_(p), parser_(_RE_MatchObject<T>::Parse) {
|
jpayne@69
|
99 }
|
jpayne@69
|
100
|
jpayne@69
|
101 // Parse the data
|
jpayne@69
|
102 bool Parse(const char* str, int n) const;
|
jpayne@69
|
103
|
jpayne@69
|
104 private:
|
jpayne@69
|
105 void* arg_;
|
jpayne@69
|
106 Parser parser_;
|
jpayne@69
|
107
|
jpayne@69
|
108 static bool parse_null (const char* str, int n, void* dest);
|
jpayne@69
|
109 static bool parse_char (const char* str, int n, void* dest);
|
jpayne@69
|
110 static bool parse_uchar (const char* str, int n, void* dest);
|
jpayne@69
|
111 static bool parse_float (const char* str, int n, void* dest);
|
jpayne@69
|
112 static bool parse_double (const char* str, int n, void* dest);
|
jpayne@69
|
113 static bool parse_string (const char* str, int n, void* dest);
|
jpayne@69
|
114 static bool parse_stringpiece (const char* str, int n, void* dest);
|
jpayne@69
|
115
|
jpayne@69
|
116 #define PCRE_DECLARE_INTEGER_PARSER(name) \
|
jpayne@69
|
117 private: \
|
jpayne@69
|
118 static bool parse_ ## name(const char* str, int n, void* dest); \
|
jpayne@69
|
119 static bool parse_ ## name ## _radix( \
|
jpayne@69
|
120 const char* str, int n, void* dest, int radix); \
|
jpayne@69
|
121 public: \
|
jpayne@69
|
122 static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \
|
jpayne@69
|
123 static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \
|
jpayne@69
|
124 static bool parse_ ## name ## _cradix(const char* str, int n, void* dest)
|
jpayne@69
|
125
|
jpayne@69
|
126 PCRE_DECLARE_INTEGER_PARSER(short);
|
jpayne@69
|
127 PCRE_DECLARE_INTEGER_PARSER(ushort);
|
jpayne@69
|
128 PCRE_DECLARE_INTEGER_PARSER(int);
|
jpayne@69
|
129 PCRE_DECLARE_INTEGER_PARSER(uint);
|
jpayne@69
|
130 PCRE_DECLARE_INTEGER_PARSER(long);
|
jpayne@69
|
131 PCRE_DECLARE_INTEGER_PARSER(ulong);
|
jpayne@69
|
132 PCRE_DECLARE_INTEGER_PARSER(longlong);
|
jpayne@69
|
133 PCRE_DECLARE_INTEGER_PARSER(ulonglong);
|
jpayne@69
|
134
|
jpayne@69
|
135 #undef PCRE_DECLARE_INTEGER_PARSER
|
jpayne@69
|
136 };
|
jpayne@69
|
137
|
jpayne@69
|
138 inline Arg::Arg() : arg_(NULL), parser_(parse_null) { }
|
jpayne@69
|
139 inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { }
|
jpayne@69
|
140
|
jpayne@69
|
141 inline bool Arg::Parse(const char* str, int n) const {
|
jpayne@69
|
142 return (*parser_)(str, n, arg_);
|
jpayne@69
|
143 }
|
jpayne@69
|
144
|
jpayne@69
|
145 // This part of the parser, appropriate only for ints, deals with bases
|
jpayne@69
|
146 #define MAKE_INTEGER_PARSER(type, name) \
|
jpayne@69
|
147 inline Arg Hex(type* ptr) { \
|
jpayne@69
|
148 return Arg(ptr, Arg::parse_ ## name ## _hex); } \
|
jpayne@69
|
149 inline Arg Octal(type* ptr) { \
|
jpayne@69
|
150 return Arg(ptr, Arg::parse_ ## name ## _octal); } \
|
jpayne@69
|
151 inline Arg CRadix(type* ptr) { \
|
jpayne@69
|
152 return Arg(ptr, Arg::parse_ ## name ## _cradix); }
|
jpayne@69
|
153
|
jpayne@69
|
154 MAKE_INTEGER_PARSER(short, short) /* */
|
jpayne@69
|
155 MAKE_INTEGER_PARSER(unsigned short, ushort) /* */
|
jpayne@69
|
156 MAKE_INTEGER_PARSER(int, int) /* Don't use semicolons */
|
jpayne@69
|
157 MAKE_INTEGER_PARSER(unsigned int, uint) /* after these statement */
|
jpayne@69
|
158 MAKE_INTEGER_PARSER(long, long) /* because they can cause */
|
jpayne@69
|
159 MAKE_INTEGER_PARSER(unsigned long, ulong) /* compiler warnings if */
|
jpayne@69
|
160 #if 1 /* the checking level is */
|
jpayne@69
|
161 MAKE_INTEGER_PARSER(long long, longlong) /* turned up high enough. */
|
jpayne@69
|
162 #endif /* */
|
jpayne@69
|
163 #if 1 /* */
|
jpayne@69
|
164 MAKE_INTEGER_PARSER(unsigned long long, ulonglong) /* */
|
jpayne@69
|
165 #endif
|
jpayne@69
|
166
|
jpayne@69
|
167 #undef PCRE_IS_SET
|
jpayne@69
|
168 #undef PCRE_SET_OR_CLEAR
|
jpayne@69
|
169 #undef MAKE_INTEGER_PARSER
|
jpayne@69
|
170
|
jpayne@69
|
171 } // namespace pcrecpp
|
jpayne@69
|
172
|
jpayne@69
|
173
|
jpayne@69
|
174 #endif /* _PCRECPPARG_H */
|