| 1 |
// Copyright (c) 2005, Google Inc.
|
| 2 |
// All rights reserved.
|
| 3 |
//
|
| 4 |
// Redistribution and use in source and binary forms, with or without
|
| 5 |
// modification, are permitted provided that the following conditions are
|
| 6 |
// met:
|
| 7 |
//
|
| 8 |
// * Redistributions of source code must retain the above copyright
|
| 9 |
// notice, this list of conditions and the following disclaimer.
|
| 10 |
// * Redistributions in binary form must reproduce the above
|
| 11 |
// copyright notice, this list of conditions and the following disclaimer
|
| 12 |
// in the documentation and/or other materials provided with the
|
| 13 |
// distribution.
|
| 14 |
// * Neither the name of Google Inc. nor the names of its
|
| 15 |
// contributors may be used to endorse or promote products derived from
|
| 16 |
// this software without specific prior written permission.
|
| 17 |
//
|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 29 |
//
|
| 30 |
// Author: Sanjay Ghemawat
|
| 31 |
|
| 32 |
#ifndef _PCRECPPARG_H
|
| 33 |
#define _PCRECPPARG_H
|
| 34 |
|
| 35 |
#include <stdlib.h> // for NULL
|
| 36 |
#include <string>
|
| 37 |
|
| 38 |
#include <pcre.h>
|
| 39 |
|
| 40 |
namespace pcrecpp {
|
| 41 |
|
| 42 |
class StringPiece;
|
| 43 |
|
| 44 |
// Hex/Octal/Binary?
|
| 45 |
|
| 46 |
// Special class for parsing into objects that define a ParseFrom() method
|
| 47 |
template <class T>
|
| 48 |
class _RE_MatchObject {
|
| 49 |
public:
|
| 50 |
static inline bool Parse(const char* str, int n, void* dest) {
|
| 51 |
T* object = reinterpret_cast<T*>(dest);
|
| 52 |
return object->ParseFrom(str, n);
|
| 53 |
}
|
| 54 |
};
|
| 55 |
|
| 56 |
class PCRECPP_EXP_DEFN Arg {
|
| 57 |
public:
|
| 58 |
// Empty constructor so we can declare arrays of Arg
|
| 59 |
Arg();
|
| 60 |
|
| 61 |
// Constructor specially designed for NULL arguments
|
| 62 |
Arg(void*);
|
| 63 |
|
| 64 |
typedef bool (*Parser)(const char* str, int n, void* dest);
|
| 65 |
|
| 66 |
// Type-specific parsers
|
| 67 |
#define PCRE_MAKE_PARSER(type,name) \
|
| 68 |
Arg(type* p) : arg_(p), parser_(name) { } \
|
| 69 |
Arg(type* p, Parser parser) : arg_(p), parser_(parser) { }
|
| 70 |
|
| 71 |
|
| 72 |
PCRE_MAKE_PARSER(char, parse_char);
|
| 73 |
PCRE_MAKE_PARSER(unsigned char, parse_uchar);
|
| 74 |
PCRE_MAKE_PARSER(short, parse_short);
|
| 75 |
PCRE_MAKE_PARSER(unsigned short, parse_ushort);
|
| 76 |
PCRE_MAKE_PARSER(int, parse_int);
|
| 77 |
PCRE_MAKE_PARSER(unsigned int, parse_uint);
|
| 78 |
PCRE_MAKE_PARSER(long, parse_long);
|
| 79 |
PCRE_MAKE_PARSER(unsigned long, parse_ulong);
|
| 80 |
#if @pcre_have_long_long@
|
| 81 |
PCRE_MAKE_PARSER(long long, parse_longlong);
|
| 82 |
#endif
|
| 83 |
#if @pcre_have_ulong_long@
|
| 84 |
PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong);
|
| 85 |
#endif
|
| 86 |
PCRE_MAKE_PARSER(float, parse_float);
|
| 87 |
PCRE_MAKE_PARSER(double, parse_double);
|
| 88 |
PCRE_MAKE_PARSER(std::string, parse_string);
|
| 89 |
PCRE_MAKE_PARSER(StringPiece, parse_stringpiece);
|
| 90 |
|
| 91 |
#undef PCRE_MAKE_PARSER
|
| 92 |
|
| 93 |
// Generic constructor
|
| 94 |
template <class T> Arg(T*, Parser parser);
|
| 95 |
// Generic constructor template
|
| 96 |
template <class T> Arg(T* p)
|
| 97 |
: arg_(p), parser_(_RE_MatchObject<T>::Parse) {
|
| 98 |
}
|
| 99 |
|
| 100 |
// Parse the data
|
| 101 |
bool Parse(const char* str, int n) const;
|
| 102 |
|
| 103 |
private:
|
| 104 |
void* arg_;
|
| 105 |
Parser parser_;
|
| 106 |
|
| 107 |
static bool parse_null (const char* str, int n, void* dest);
|
| 108 |
static bool parse_char (const char* str, int n, void* dest);
|
| 109 |
static bool parse_uchar (const char* str, int n, void* dest);
|
| 110 |
static bool parse_float (const char* str, int n, void* dest);
|
| 111 |
static bool parse_double (const char* str, int n, void* dest);
|
| 112 |
static bool parse_string (const char* str, int n, void* dest);
|
| 113 |
static bool parse_stringpiece (const char* str, int n, void* dest);
|
| 114 |
|
| 115 |
#define PCRE_DECLARE_INTEGER_PARSER(name) \
|
| 116 |
private: \
|
| 117 |
static bool parse_ ## name(const char* str, int n, void* dest); \
|
| 118 |
static bool parse_ ## name ## _radix( \
|
| 119 |
const char* str, int n, void* dest, int radix); \
|
| 120 |
public: \
|
| 121 |
static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \
|
| 122 |
static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \
|
| 123 |
static bool parse_ ## name ## _cradix(const char* str, int n, void* dest)
|
| 124 |
|
| 125 |
PCRE_DECLARE_INTEGER_PARSER(short);
|
| 126 |
PCRE_DECLARE_INTEGER_PARSER(ushort);
|
| 127 |
PCRE_DECLARE_INTEGER_PARSER(int);
|
| 128 |
PCRE_DECLARE_INTEGER_PARSER(uint);
|
| 129 |
PCRE_DECLARE_INTEGER_PARSER(long);
|
| 130 |
PCRE_DECLARE_INTEGER_PARSER(ulong);
|
| 131 |
PCRE_DECLARE_INTEGER_PARSER(longlong);
|
| 132 |
PCRE_DECLARE_INTEGER_PARSER(ulonglong);
|
| 133 |
|
| 134 |
#undef PCRE_DECLARE_INTEGER_PARSER
|
| 135 |
};
|
| 136 |
|
| 137 |
inline Arg::Arg() : arg_(NULL), parser_(parse_null) { }
|
| 138 |
inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { }
|
| 139 |
|
| 140 |
inline bool Arg::Parse(const char* str, int n) const {
|
| 141 |
return (*parser_)(str, n, arg_);
|
| 142 |
}
|
| 143 |
|
| 144 |
// This part of the parser, appropriate only for ints, deals with bases
|
| 145 |
#define MAKE_INTEGER_PARSER(type, name) \
|
| 146 |
inline Arg Hex(type* ptr) { \
|
| 147 |
return Arg(ptr, Arg::parse_ ## name ## _hex); } \
|
| 148 |
inline Arg Octal(type* ptr) { \
|
| 149 |
return Arg(ptr, Arg::parse_ ## name ## _octal); } \
|
| 150 |
inline Arg CRadix(type* ptr) { \
|
| 151 |
return Arg(ptr, Arg::parse_ ## name ## _cradix); }
|
| 152 |
|
| 153 |
MAKE_INTEGER_PARSER(short, short) /* */
|
| 154 |
MAKE_INTEGER_PARSER(unsigned short, ushort) /* */
|
| 155 |
MAKE_INTEGER_PARSER(int, int) /* Don't use semicolons */
|
| 156 |
MAKE_INTEGER_PARSER(unsigned int, uint) /* after these statement */
|
| 157 |
MAKE_INTEGER_PARSER(long, long) /* because they can cause */
|
| 158 |
MAKE_INTEGER_PARSER(unsigned long, ulong) /* compiler warnings if */
|
| 159 |
#if @pcre_have_long_long@ /* the checking level is */
|
| 160 |
MAKE_INTEGER_PARSER(long long, longlong) /* turned up high enough. */
|
| 161 |
#endif /* */
|
| 162 |
#if @pcre_have_ulong_long@ /* */
|
| 163 |
MAKE_INTEGER_PARSER(unsigned long long, ulonglong) /* */
|
| 164 |
#endif
|
| 165 |
|
| 166 |
#undef PCRE_IS_SET
|
| 167 |
#undef PCRE_SET_OR_CLEAR
|
| 168 |
#undef MAKE_INTEGER_PARSER
|
| 169 |
|
| 170 |
} // namespace pcrecpp
|
| 171 |
|
| 172 |
|
| 173 |
#endif /* _PCRECPPARG_H */
|