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     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   // Empty constructor so we can declare arrays of Arg
00060   Arg();
00061 
00062   // Constructor specially designed for NULL arguments
00063   Arg(void*);
00064 
00065   typedef bool (*Parser)(const char* str, int n, void* dest);
00066 
00067 // Type-specific parsers
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   // Generic constructor
00095   template <class T> Arg(T*, Parser parser);
00096   // Generic constructor template
00097   template <class T> Arg(T* p)
00098     : arg_(p), parser_(_RE_MatchObject<T>::Parse) {
00099   }
00100 
00101   // Parse the data
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 // This part of the parser, appropriate only for ints, deals with bases
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)       /* Don't use semicolons   */
00157 MAKE_INTEGER_PARSER(unsigned int,       uint)      /* after these statement  */
00158 MAKE_INTEGER_PARSER(long,               long)      /* because they can cause */
00159 MAKE_INTEGER_PARSER(unsigned long,      ulong)     /* compiler warnings if   */
00160 #if 1                          /* the checking level is  */
00161 MAKE_INTEGER_PARSER(long long,          longlong)  /* turned up high enough. */
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 }   // namespace pcrecpp
00172 
00173 
00174 #endif /* _PCRECPPARG_H */

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