jpayne@69: // Copyright (c) 2005, Google Inc. jpayne@69: // All rights reserved. jpayne@69: // jpayne@69: // Redistribution and use in source and binary forms, with or without jpayne@69: // modification, are permitted provided that the following conditions are jpayne@69: // met: jpayne@69: // jpayne@69: // * Redistributions of source code must retain the above copyright jpayne@69: // notice, this list of conditions and the following disclaimer. jpayne@69: // * Redistributions in binary form must reproduce the above jpayne@69: // copyright notice, this list of conditions and the following disclaimer jpayne@69: // in the documentation and/or other materials provided with the jpayne@69: // distribution. jpayne@69: // * Neither the name of Google Inc. nor the names of its jpayne@69: // contributors may be used to endorse or promote products derived from jpayne@69: // this software without specific prior written permission. jpayne@69: // jpayne@69: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS jpayne@69: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT jpayne@69: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR jpayne@69: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT jpayne@69: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, jpayne@69: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT jpayne@69: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, jpayne@69: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY jpayne@69: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT jpayne@69: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE jpayne@69: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jpayne@69: // jpayne@69: // Author: Sanjay Ghemawat jpayne@69: jpayne@69: #ifndef _PCRECPPARG_H jpayne@69: #define _PCRECPPARG_H jpayne@69: jpayne@69: #include // for NULL jpayne@69: #include jpayne@69: jpayne@69: #include jpayne@69: jpayne@69: namespace pcrecpp { jpayne@69: jpayne@69: class StringPiece; jpayne@69: jpayne@69: // Hex/Octal/Binary? jpayne@69: jpayne@69: // Special class for parsing into objects that define a ParseFrom() method jpayne@69: template jpayne@69: class _RE_MatchObject { jpayne@69: public: jpayne@69: static inline bool Parse(const char* str, int n, void* dest) { jpayne@69: if (dest == NULL) return true; jpayne@69: T* object = reinterpret_cast(dest); jpayne@69: return object->ParseFrom(str, n); jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: class PCRECPP_EXP_DEFN Arg { jpayne@69: public: jpayne@69: // Empty constructor so we can declare arrays of Arg jpayne@69: Arg(); jpayne@69: jpayne@69: // Constructor specially designed for NULL arguments jpayne@69: Arg(void*); jpayne@69: jpayne@69: typedef bool (*Parser)(const char* str, int n, void* dest); jpayne@69: jpayne@69: // Type-specific parsers jpayne@69: #define PCRE_MAKE_PARSER(type,name) \ jpayne@69: Arg(type* p) : arg_(p), parser_(name) { } \ jpayne@69: Arg(type* p, Parser parser) : arg_(p), parser_(parser) { } jpayne@69: jpayne@69: jpayne@69: PCRE_MAKE_PARSER(char, parse_char); jpayne@69: PCRE_MAKE_PARSER(unsigned char, parse_uchar); jpayne@69: PCRE_MAKE_PARSER(short, parse_short); jpayne@69: PCRE_MAKE_PARSER(unsigned short, parse_ushort); jpayne@69: PCRE_MAKE_PARSER(int, parse_int); jpayne@69: PCRE_MAKE_PARSER(unsigned int, parse_uint); jpayne@69: PCRE_MAKE_PARSER(long, parse_long); jpayne@69: PCRE_MAKE_PARSER(unsigned long, parse_ulong); jpayne@69: #if 1 jpayne@69: PCRE_MAKE_PARSER(long long, parse_longlong); jpayne@69: #endif jpayne@69: #if 1 jpayne@69: PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong); jpayne@69: #endif jpayne@69: PCRE_MAKE_PARSER(float, parse_float); jpayne@69: PCRE_MAKE_PARSER(double, parse_double); jpayne@69: PCRE_MAKE_PARSER(std::string, parse_string); jpayne@69: PCRE_MAKE_PARSER(StringPiece, parse_stringpiece); jpayne@69: jpayne@69: #undef PCRE_MAKE_PARSER jpayne@69: jpayne@69: // Generic constructor jpayne@69: template Arg(T*, Parser parser); jpayne@69: // Generic constructor template jpayne@69: template Arg(T* p) jpayne@69: : arg_(p), parser_(_RE_MatchObject::Parse) { jpayne@69: } jpayne@69: jpayne@69: // Parse the data jpayne@69: bool Parse(const char* str, int n) const; jpayne@69: jpayne@69: private: jpayne@69: void* arg_; jpayne@69: Parser parser_; jpayne@69: jpayne@69: static bool parse_null (const char* str, int n, void* dest); jpayne@69: static bool parse_char (const char* str, int n, void* dest); jpayne@69: static bool parse_uchar (const char* str, int n, void* dest); jpayne@69: static bool parse_float (const char* str, int n, void* dest); jpayne@69: static bool parse_double (const char* str, int n, void* dest); jpayne@69: static bool parse_string (const char* str, int n, void* dest); jpayne@69: static bool parse_stringpiece (const char* str, int n, void* dest); jpayne@69: jpayne@69: #define PCRE_DECLARE_INTEGER_PARSER(name) \ jpayne@69: private: \ jpayne@69: static bool parse_ ## name(const char* str, int n, void* dest); \ jpayne@69: static bool parse_ ## name ## _radix( \ jpayne@69: const char* str, int n, void* dest, int radix); \ jpayne@69: public: \ jpayne@69: static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \ jpayne@69: static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \ jpayne@69: static bool parse_ ## name ## _cradix(const char* str, int n, void* dest) jpayne@69: jpayne@69: PCRE_DECLARE_INTEGER_PARSER(short); jpayne@69: PCRE_DECLARE_INTEGER_PARSER(ushort); jpayne@69: PCRE_DECLARE_INTEGER_PARSER(int); jpayne@69: PCRE_DECLARE_INTEGER_PARSER(uint); jpayne@69: PCRE_DECLARE_INTEGER_PARSER(long); jpayne@69: PCRE_DECLARE_INTEGER_PARSER(ulong); jpayne@69: PCRE_DECLARE_INTEGER_PARSER(longlong); jpayne@69: PCRE_DECLARE_INTEGER_PARSER(ulonglong); jpayne@69: jpayne@69: #undef PCRE_DECLARE_INTEGER_PARSER jpayne@69: }; jpayne@69: jpayne@69: inline Arg::Arg() : arg_(NULL), parser_(parse_null) { } jpayne@69: inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { } jpayne@69: jpayne@69: inline bool Arg::Parse(const char* str, int n) const { jpayne@69: return (*parser_)(str, n, arg_); jpayne@69: } jpayne@69: jpayne@69: // This part of the parser, appropriate only for ints, deals with bases jpayne@69: #define MAKE_INTEGER_PARSER(type, name) \ jpayne@69: inline Arg Hex(type* ptr) { \ jpayne@69: return Arg(ptr, Arg::parse_ ## name ## _hex); } \ jpayne@69: inline Arg Octal(type* ptr) { \ jpayne@69: return Arg(ptr, Arg::parse_ ## name ## _octal); } \ jpayne@69: inline Arg CRadix(type* ptr) { \ jpayne@69: return Arg(ptr, Arg::parse_ ## name ## _cradix); } jpayne@69: jpayne@69: MAKE_INTEGER_PARSER(short, short) /* */ jpayne@69: MAKE_INTEGER_PARSER(unsigned short, ushort) /* */ jpayne@69: MAKE_INTEGER_PARSER(int, int) /* Don't use semicolons */ jpayne@69: MAKE_INTEGER_PARSER(unsigned int, uint) /* after these statement */ jpayne@69: MAKE_INTEGER_PARSER(long, long) /* because they can cause */ jpayne@69: MAKE_INTEGER_PARSER(unsigned long, ulong) /* compiler warnings if */ jpayne@69: #if 1 /* the checking level is */ jpayne@69: MAKE_INTEGER_PARSER(long long, longlong) /* turned up high enough. */ jpayne@69: #endif /* */ jpayne@69: #if 1 /* */ jpayne@69: MAKE_INTEGER_PARSER(unsigned long long, ulonglong) /* */ jpayne@69: #endif jpayne@69: jpayne@69: #undef PCRE_IS_SET jpayne@69: #undef PCRE_SET_OR_CLEAR jpayne@69: #undef MAKE_INTEGER_PARSER jpayne@69: jpayne@69: } // namespace pcrecpp jpayne@69: jpayne@69: jpayne@69: #endif /* _PCRECPPARG_H */