Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-05-18 08:30:10

0001 // Copyright (c) 2005, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 //
0030 // Author: Sanjay Ghemawat
0031 
0032 #ifndef _PCRECPPARG_H
0033 #define _PCRECPPARG_H
0034 
0035 #include <stdlib.h>    // for NULL
0036 #include <string>
0037 
0038 #include <pcre.h>
0039 
0040 namespace pcrecpp {
0041 
0042 class StringPiece;
0043 
0044 // Hex/Octal/Binary?
0045 
0046 // Special class for parsing into objects that define a ParseFrom() method
0047 template <class T>
0048 class _RE_MatchObject {
0049  public:
0050   static inline bool Parse(const char* str, int n, void* dest) {
0051     if (dest == NULL) return true;
0052     T* object = reinterpret_cast<T*>(dest);
0053     return object->ParseFrom(str, n);
0054   }
0055 };
0056 
0057 class PCRECPP_EXP_DEFN Arg {
0058  public:
0059   // Empty constructor so we can declare arrays of Arg
0060   Arg();
0061 
0062   // Constructor specially designed for NULL arguments
0063   Arg(void*);
0064 
0065   typedef bool (*Parser)(const char* str, int n, void* dest);
0066 
0067 // Type-specific parsers
0068 #define PCRE_MAKE_PARSER(type,name)                             \
0069   Arg(type* p) : arg_(p), parser_(name) { }                     \
0070   Arg(type* p, Parser parser) : arg_(p), parser_(parser) { }
0071 
0072 
0073   PCRE_MAKE_PARSER(char,               parse_char);
0074   PCRE_MAKE_PARSER(unsigned char,      parse_uchar);
0075   PCRE_MAKE_PARSER(short,              parse_short);
0076   PCRE_MAKE_PARSER(unsigned short,     parse_ushort);
0077   PCRE_MAKE_PARSER(int,                parse_int);
0078   PCRE_MAKE_PARSER(unsigned int,       parse_uint);
0079   PCRE_MAKE_PARSER(long,               parse_long);
0080   PCRE_MAKE_PARSER(unsigned long,      parse_ulong);
0081 #if 1
0082   PCRE_MAKE_PARSER(long long,          parse_longlong);
0083 #endif
0084 #if 1
0085   PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong);
0086 #endif
0087   PCRE_MAKE_PARSER(float,              parse_float);
0088   PCRE_MAKE_PARSER(double,             parse_double);
0089   PCRE_MAKE_PARSER(std::string,        parse_string);
0090   PCRE_MAKE_PARSER(StringPiece,        parse_stringpiece);
0091 
0092 #undef PCRE_MAKE_PARSER
0093 
0094   // Generic constructor
0095   template <class T> Arg(T*, Parser parser);
0096   // Generic constructor template
0097   template <class T> Arg(T* p)
0098     : arg_(p), parser_(_RE_MatchObject<T>::Parse) {
0099   }
0100 
0101   // Parse the data
0102   bool Parse(const char* str, int n) const;
0103 
0104  private:
0105   void*         arg_;
0106   Parser        parser_;
0107 
0108   static bool parse_null          (const char* str, int n, void* dest);
0109   static bool parse_char          (const char* str, int n, void* dest);
0110   static bool parse_uchar         (const char* str, int n, void* dest);
0111   static bool parse_float         (const char* str, int n, void* dest);
0112   static bool parse_double        (const char* str, int n, void* dest);
0113   static bool parse_string        (const char* str, int n, void* dest);
0114   static bool parse_stringpiece   (const char* str, int n, void* dest);
0115 
0116 #define PCRE_DECLARE_INTEGER_PARSER(name)                                   \
0117  private:                                                                   \
0118   static bool parse_ ## name(const char* str, int n, void* dest);           \
0119   static bool parse_ ## name ## _radix(                                     \
0120     const char* str, int n, void* dest, int radix);                         \
0121  public:                                                                    \
0122   static bool parse_ ## name ## _hex(const char* str, int n, void* dest);   \
0123   static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \
0124   static bool parse_ ## name ## _cradix(const char* str, int n, void* dest)
0125 
0126   PCRE_DECLARE_INTEGER_PARSER(short);
0127   PCRE_DECLARE_INTEGER_PARSER(ushort);
0128   PCRE_DECLARE_INTEGER_PARSER(int);
0129   PCRE_DECLARE_INTEGER_PARSER(uint);
0130   PCRE_DECLARE_INTEGER_PARSER(long);
0131   PCRE_DECLARE_INTEGER_PARSER(ulong);
0132   PCRE_DECLARE_INTEGER_PARSER(longlong);
0133   PCRE_DECLARE_INTEGER_PARSER(ulonglong);
0134 
0135 #undef PCRE_DECLARE_INTEGER_PARSER
0136 };
0137 
0138 inline Arg::Arg() : arg_(NULL), parser_(parse_null) { }
0139 inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { }
0140 
0141 inline bool Arg::Parse(const char* str, int n) const {
0142   return (*parser_)(str, n, arg_);
0143 }
0144 
0145 // This part of the parser, appropriate only for ints, deals with bases
0146 #define MAKE_INTEGER_PARSER(type, name) \
0147   inline Arg Hex(type* ptr) { \
0148     return Arg(ptr, Arg::parse_ ## name ## _hex); } \
0149   inline Arg Octal(type* ptr) { \
0150     return Arg(ptr, Arg::parse_ ## name ## _octal); } \
0151   inline Arg CRadix(type* ptr) { \
0152     return Arg(ptr, Arg::parse_ ## name ## _cradix); }
0153 
0154 MAKE_INTEGER_PARSER(short,              short)     /*                        */
0155 MAKE_INTEGER_PARSER(unsigned short,     ushort)    /*                        */
0156 MAKE_INTEGER_PARSER(int,                int)       /* Don't use semicolons   */
0157 MAKE_INTEGER_PARSER(unsigned int,       uint)      /* after these statement  */
0158 MAKE_INTEGER_PARSER(long,               long)      /* because they can cause */
0159 MAKE_INTEGER_PARSER(unsigned long,      ulong)     /* compiler warnings if   */
0160 #if 1                          /* the checking level is  */
0161 MAKE_INTEGER_PARSER(long long,          longlong)  /* turned up high enough. */
0162 #endif                                             /*                        */
0163 #if 1                         /*                        */
0164 MAKE_INTEGER_PARSER(unsigned long long, ulonglong) /*                        */
0165 #endif
0166 
0167 #undef PCRE_IS_SET
0168 #undef PCRE_SET_OR_CLEAR
0169 #undef MAKE_INTEGER_PARSER
0170 
0171 }   // namespace pcrecpp
0172 
0173 
0174 #endif /* _PCRECPPARG_H */