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 if (dest == NULL) return true;
00052 T* object = reinterpret_cast<T*>(dest);
00053 return object->ParseFrom(str, n);
00054 }
00055 };
00056
00057 class PCRECPP_EXP_DEFN Arg {
00058 public:
00059
00060 Arg();
00061
00062
00063 Arg(void*);
00064
00065 typedef bool (*Parser)(const char* str, int n, void* dest);
00066
00067
00068 #define PCRE_MAKE_PARSER(type,name) \
00069 Arg(type* p) : arg_(p), parser_(name) { } \
00070 Arg(type* p, Parser parser) : arg_(p), parser_(parser) { }
00071
00072
00073 PCRE_MAKE_PARSER(char, parse_char);
00074 PCRE_MAKE_PARSER(unsigned char, parse_uchar);
00075 PCRE_MAKE_PARSER(short, parse_short);
00076 PCRE_MAKE_PARSER(unsigned short, parse_ushort);
00077 PCRE_MAKE_PARSER(int, parse_int);
00078 PCRE_MAKE_PARSER(unsigned int, parse_uint);
00079 PCRE_MAKE_PARSER(long, parse_long);
00080 PCRE_MAKE_PARSER(unsigned long, parse_ulong);
00081 #if 1
00082 PCRE_MAKE_PARSER(long long, parse_longlong);
00083 #endif
00084 #if 1
00085 PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong);
00086 #endif
00087 PCRE_MAKE_PARSER(float, parse_float);
00088 PCRE_MAKE_PARSER(double, parse_double);
00089 PCRE_MAKE_PARSER(std::string, parse_string);
00090 PCRE_MAKE_PARSER(StringPiece, parse_stringpiece);
00091
00092 #undef PCRE_MAKE_PARSER
00093
00094
00095 template <class T> Arg(T*, Parser parser);
00096
00097 template <class T> Arg(T* p)
00098 : arg_(p), parser_(_RE_MatchObject<T>::Parse) {
00099 }
00100
00101
00102 bool Parse(const char* str, int n) const;
00103
00104 private:
00105 void* arg_;
00106 Parser parser_;
00107
00108 static bool parse_null (const char* str, int n, void* dest);
00109 static bool parse_char (const char* str, int n, void* dest);
00110 static bool parse_uchar (const char* str, int n, void* dest);
00111 static bool parse_float (const char* str, int n, void* dest);
00112 static bool parse_double (const char* str, int n, void* dest);
00113 static bool parse_string (const char* str, int n, void* dest);
00114 static bool parse_stringpiece (const char* str, int n, void* dest);
00115
00116 #define PCRE_DECLARE_INTEGER_PARSER(name) \
00117 private: \
00118 static bool parse_ ## name(const char* str, int n, void* dest); \
00119 static bool parse_ ## name ## _radix( \
00120 const char* str, int n, void* dest, int radix); \
00121 public: \
00122 static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \
00123 static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \
00124 static bool parse_ ## name ## _cradix(const char* str, int n, void* dest)
00125
00126 PCRE_DECLARE_INTEGER_PARSER(short);
00127 PCRE_DECLARE_INTEGER_PARSER(ushort);
00128 PCRE_DECLARE_INTEGER_PARSER(int);
00129 PCRE_DECLARE_INTEGER_PARSER(uint);
00130 PCRE_DECLARE_INTEGER_PARSER(long);
00131 PCRE_DECLARE_INTEGER_PARSER(ulong);
00132 PCRE_DECLARE_INTEGER_PARSER(longlong);
00133 PCRE_DECLARE_INTEGER_PARSER(ulonglong);
00134
00135 #undef PCRE_DECLARE_INTEGER_PARSER
00136 };
00137
00138 inline Arg::Arg() : arg_(NULL), parser_(parse_null) { }
00139 inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { }
00140
00141 inline bool Arg::Parse(const char* str, int n) const {
00142 return (*parser_)(str, n, arg_);
00143 }
00144
00145
00146 #define MAKE_INTEGER_PARSER(type, name) \
00147 inline Arg Hex(type* ptr) { \
00148 return Arg(ptr, Arg::parse_ ## name ## _hex); } \
00149 inline Arg Octal(type* ptr) { \
00150 return Arg(ptr, Arg::parse_ ## name ## _octal); } \
00151 inline Arg CRadix(type* ptr) { \
00152 return Arg(ptr, Arg::parse_ ## name ## _cradix); }
00153
00154 MAKE_INTEGER_PARSER(short, short)
00155 MAKE_INTEGER_PARSER(unsigned short, ushort)
00156 MAKE_INTEGER_PARSER(int, int)
00157 MAKE_INTEGER_PARSER(unsigned int, uint)
00158 MAKE_INTEGER_PARSER(long, long)
00159 MAKE_INTEGER_PARSER(unsigned long, ulong)
00160 #if 1
00161 MAKE_INTEGER_PARSER(long long, longlong)
00162 #endif
00163 #if 1
00164 MAKE_INTEGER_PARSER(unsigned long long, ulonglong)
00165 #endif
00166
00167 #undef PCRE_IS_SET
00168 #undef PCRE_SET_OR_CLEAR
00169 #undef MAKE_INTEGER_PARSER
00170
00171 }
00172
00173
00174 #endif