00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _PCRECPPARG_H
00033 #define _PCRECPPARG_H
00034
00035 #include <stdlib.h>
00036 #include <string>
00037
00038 #include <pcre.h>
00039
00040 namespace pcrecpp {
00041
00042 class StringPiece;
00043
00044
00045
00046
00047 template <class T>
00048 class _RE_MatchObject {
00049 public:
00050 static inline bool Parse(const char* str, int n, void* dest) {
00051 T* object = reinterpret_cast<T*>(dest);
00052 return object->ParseFrom(str, n);
00053 }
00054 };
00055
00056 class PCRECPP_EXP_DEFN Arg {
00057 public:
00058
00059 Arg();
00060
00061
00062 Arg(void*);
00063
00064 typedef bool (*Parser)(const char* str, int n, void* dest);
00065
00066
00067 #define PCRE_MAKE_PARSER(type,name) \
00068 Arg(type* p) : arg_(p), parser_(name) { } \
00069 Arg(type* p, Parser parser) : arg_(p), parser_(parser) { }
00070
00071
00072 PCRE_MAKE_PARSER(char, parse_char);
00073 PCRE_MAKE_PARSER(unsigned char, parse_uchar);
00074 PCRE_MAKE_PARSER(short, parse_short);
00075 PCRE_MAKE_PARSER(unsigned short, parse_ushort);
00076 PCRE_MAKE_PARSER(int, parse_int);
00077 PCRE_MAKE_PARSER(unsigned int, parse_uint);
00078 PCRE_MAKE_PARSER(long, parse_long);
00079 PCRE_MAKE_PARSER(unsigned long, parse_ulong);
00080 #if 1
00081 PCRE_MAKE_PARSER(long long, parse_longlong);
00082 #endif
00083 #if 1
00084 PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong);
00085 #endif
00086 PCRE_MAKE_PARSER(float, parse_float);
00087 PCRE_MAKE_PARSER(double, parse_double);
00088 PCRE_MAKE_PARSER(std::string, parse_string);
00089 PCRE_MAKE_PARSER(StringPiece, parse_stringpiece);
00090
00091 #undef PCRE_MAKE_PARSER
00092
00093
00094 template <class T> Arg(T*, Parser parser);
00095
00096 template <class T> Arg(T* p)
00097 : arg_(p), parser_(_RE_MatchObject<T>::Parse) {
00098 }
00099
00100
00101 bool Parse(const char* str, int n) const;
00102
00103 private:
00104 void* arg_;
00105 Parser parser_;
00106
00107 static bool parse_null (const char* str, int n, void* dest);
00108 static bool parse_char (const char* str, int n, void* dest);
00109 static bool parse_uchar (const char* str, int n, void* dest);
00110 static bool parse_float (const char* str, int n, void* dest);
00111 static bool parse_double (const char* str, int n, void* dest);
00112 static bool parse_string (const char* str, int n, void* dest);
00113 static bool parse_stringpiece (const char* str, int n, void* dest);
00114
00115 #define PCRE_DECLARE_INTEGER_PARSER(name) \
00116 private: \
00117 static bool parse_ ## name(const char* str, int n, void* dest); \
00118 static bool parse_ ## name ## _radix( \
00119 const char* str, int n, void* dest, int radix); \
00120 public: \
00121 static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \
00122 static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \
00123 static bool parse_ ## name ## _cradix(const char* str, int n, void* dest)
00124
00125 PCRE_DECLARE_INTEGER_PARSER(short);
00126 PCRE_DECLARE_INTEGER_PARSER(ushort);
00127 PCRE_DECLARE_INTEGER_PARSER(int);
00128 PCRE_DECLARE_INTEGER_PARSER(uint);
00129 PCRE_DECLARE_INTEGER_PARSER(long);
00130 PCRE_DECLARE_INTEGER_PARSER(ulong);
00131 PCRE_DECLARE_INTEGER_PARSER(longlong);
00132 PCRE_DECLARE_INTEGER_PARSER(ulonglong);
00133
00134 #undef PCRE_DECLARE_INTEGER_PARSER
00135 };
00136
00137 inline Arg::Arg() : arg_(NULL), parser_(parse_null) { }
00138 inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { }
00139
00140 inline bool Arg::Parse(const char* str, int n) const {
00141 return (*parser_)(str, n, arg_);
00142 }
00143
00144
00145 #define MAKE_INTEGER_PARSER(type, name) \
00146 inline Arg Hex(type* ptr) { \
00147 return Arg(ptr, Arg::parse_ ## name ## _hex); } \
00148 inline Arg Octal(type* ptr) { \
00149 return Arg(ptr, Arg::parse_ ## name ## _octal); } \
00150 inline Arg CRadix(type* ptr) { \
00151 return Arg(ptr, Arg::parse_ ## name ## _cradix); }
00152
00153 MAKE_INTEGER_PARSER(short, short)
00154 MAKE_INTEGER_PARSER(unsigned short, ushort)
00155 MAKE_INTEGER_PARSER(int, int)
00156 MAKE_INTEGER_PARSER(unsigned int, uint)
00157 MAKE_INTEGER_PARSER(long, long)
00158 MAKE_INTEGER_PARSER(unsigned long, ulong)
00159 #if 1
00160 MAKE_INTEGER_PARSER(long long, longlong)
00161 #endif
00162 #if 1
00163 MAKE_INTEGER_PARSER(unsigned long long, ulonglong)
00164 #endif
00165
00166 #undef PCRE_IS_SET
00167 #undef PCRE_SET_OR_CLEAR
00168 #undef MAKE_INTEGER_PARSER
00169
00170 }
00171
00172
00173 #endif