pcrecpparg.h

Go to the documentation of this file.
00001 // Copyright (c) 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: Sanjay Ghemawat
00031 
00032 #ifndef _PCRECPPARG_H
00033 #define _PCRECPPARG_H
00034 
00035 #include <stdlib.h>    // for NULL
00036 #include <string>
00037 
00038 #include <pcre.h>
00039 
00040 namespace pcrecpp {
00041 
00042 class StringPiece;
00043 
00044 // Hex/Octal/Binary?
00045 
00046 // Special class for parsing into objects that define a ParseFrom() method
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   // Empty constructor so we can declare arrays of Arg
00059   Arg();
00060 
00061   // Constructor specially designed for NULL arguments
00062   Arg(void*);
00063 
00064   typedef bool (*Parser)(const char* str, int n, void* dest);
00065 
00066 // Type-specific parsers
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   // Generic constructor
00094   template <class T> Arg(T*, Parser parser);
00095   // Generic constructor template
00096   template <class T> Arg(T* p)
00097     : arg_(p), parser_(_RE_MatchObject<T>::Parse) {
00098   }
00099 
00100   // Parse the data
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 // This part of the parser, appropriate only for ints, deals with bases
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)       /* Don't use semicolons   */
00156 MAKE_INTEGER_PARSER(unsigned int,       uint)      /* after these statement  */
00157 MAKE_INTEGER_PARSER(long,               long)      /* because they can cause */
00158 MAKE_INTEGER_PARSER(unsigned long,      ulong)     /* compiler warnings if   */
00159 #if 1                          /* the checking level is  */
00160 MAKE_INTEGER_PARSER(long long,          longlong)  /* turned up high enough. */
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 }   // namespace pcrecpp
00171 
00172 
00173 #endif /* _PCRECPPARG_H */

Generated on Tue Jul 5 14:11:58 2011 for ROOT_528-00b_version by  doxygen 1.5.1