| 1 |
nigel |
87 |
// 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 |
|
|
namespace pcrecpp { |
| 39 |
|
|
|
| 40 |
|
|
class StringPiece; |
| 41 |
|
|
|
| 42 |
|
|
// Hex/Octal/Binary? |
| 43 |
|
|
|
| 44 |
|
|
// Special class for parsing into objects that define a ParseFrom() method |
| 45 |
|
|
template <class T> |
| 46 |
|
|
class _RE_MatchObject { |
| 47 |
|
|
public: |
| 48 |
|
|
static inline bool Parse(const char* str, int n, void* dest) { |
| 49 |
|
|
T* object = reinterpret_cast<T*>(dest); |
| 50 |
|
|
return object->ParseFrom(str, n); |
| 51 |
|
|
} |
| 52 |
|
|
}; |
| 53 |
|
|
|
| 54 |
|
|
class Arg { |
| 55 |
|
|
public: |
| 56 |
|
|
// Empty constructor so we can declare arrays of Arg |
| 57 |
|
|
Arg(); |
| 58 |
|
|
|
| 59 |
|
|
// Constructor specially designed for NULL arguments |
| 60 |
|
|
Arg(void*); |
| 61 |
|
|
|
| 62 |
|
|
typedef bool (*Parser)(const char* str, int n, void* dest); |
| 63 |
|
|
|
| 64 |
|
|
// Type-specific parsers |
| 65 |
|
|
#define PCRE_MAKE_PARSER(type,name) \ |
| 66 |
|
|
Arg(type* p) : arg_(p), parser_(name) { } \ |
| 67 |
|
|
Arg(type* p, Parser parser) : arg_(p), parser_(parser) { } |
| 68 |
|
|
|
| 69 |
|
|
|
| 70 |
|
|
PCRE_MAKE_PARSER(char, parse_char); |
| 71 |
|
|
PCRE_MAKE_PARSER(unsigned char, parse_uchar); |
| 72 |
|
|
PCRE_MAKE_PARSER(short, parse_short); |
| 73 |
|
|
PCRE_MAKE_PARSER(unsigned short, parse_ushort); |
| 74 |
|
|
PCRE_MAKE_PARSER(int, parse_int); |
| 75 |
|
|
PCRE_MAKE_PARSER(unsigned int, parse_uint); |
| 76 |
|
|
PCRE_MAKE_PARSER(long, parse_long); |
| 77 |
|
|
PCRE_MAKE_PARSER(unsigned long, parse_ulong); |
| 78 |
|
|
#if @pcre_have_long_long@ |
| 79 |
|
|
PCRE_MAKE_PARSER(long long, parse_longlong); |
| 80 |
|
|
#endif |
| 81 |
|
|
#if @pcre_have_ulong_long@ |
| 82 |
|
|
PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong); |
| 83 |
|
|
#endif |
| 84 |
|
|
PCRE_MAKE_PARSER(float, parse_float); |
| 85 |
|
|
PCRE_MAKE_PARSER(double, parse_double); |
| 86 |
|
|
PCRE_MAKE_PARSER(std::string, parse_string); |
| 87 |
|
|
PCRE_MAKE_PARSER(StringPiece, parse_stringpiece); |
| 88 |
|
|
|
| 89 |
|
|
#undef PCRE_MAKE_PARSER |
| 90 |
|
|
|
| 91 |
|
|
// Generic constructor |
| 92 |
|
|
template <class T> Arg(T*, Parser parser); |
| 93 |
|
|
// Generic constructor template |
| 94 |
|
|
template <class T> Arg(T* p) |
| 95 |
|
|
: arg_(p), parser_(_RE_MatchObject<T>::Parse) { |
| 96 |
|
|
} |
| 97 |
|
|
|
| 98 |
|
|
// Parse the data |
| 99 |
|
|
bool Parse(const char* str, int n) const; |
| 100 |
|
|
|
| 101 |
|
|
private: |
| 102 |
|
|
void* arg_; |
| 103 |
|
|
Parser parser_; |
| 104 |
|
|
|
| 105 |
|
|
static bool parse_null (const char* str, int n, void* dest); |
| 106 |
|
|
static bool parse_char (const char* str, int n, void* dest); |
| 107 |
|
|
static bool parse_uchar (const char* str, int n, void* dest); |
| 108 |
|
|
static bool parse_float (const char* str, int n, void* dest); |
| 109 |
|
|
static bool parse_double (const char* str, int n, void* dest); |
| 110 |
|
|
static bool parse_string (const char* str, int n, void* dest); |
| 111 |
|
|
static bool parse_stringpiece (const char* str, int n, void* dest); |
| 112 |
|
|
|
| 113 |
|
|
#define PCRE_DECLARE_INTEGER_PARSER(name) \ |
| 114 |
|
|
private: \ |
| 115 |
|
|
static bool parse_ ## name(const char* str, int n, void* dest); \ |
| 116 |
|
|
static bool parse_ ## name ## _radix( \ |
| 117 |
|
|
const char* str, int n, void* dest, int radix); \ |
| 118 |
|
|
public: \ |
| 119 |
|
|
static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \ |
| 120 |
|
|
static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \ |
| 121 |
|
|
static bool parse_ ## name ## _cradix(const char* str, int n, void* dest) |
| 122 |
|
|
|
| 123 |
|
|
PCRE_DECLARE_INTEGER_PARSER(short); |
| 124 |
|
|
PCRE_DECLARE_INTEGER_PARSER(ushort); |
| 125 |
|
|
PCRE_DECLARE_INTEGER_PARSER(int); |
| 126 |
|
|
PCRE_DECLARE_INTEGER_PARSER(uint); |
| 127 |
|
|
PCRE_DECLARE_INTEGER_PARSER(long); |
| 128 |
|
|
PCRE_DECLARE_INTEGER_PARSER(ulong); |
| 129 |
|
|
PCRE_DECLARE_INTEGER_PARSER(longlong); |
| 130 |
|
|
PCRE_DECLARE_INTEGER_PARSER(ulonglong); |
| 131 |
|
|
|
| 132 |
|
|
#undef PCRE_DECLARE_INTEGER_PARSER |
| 133 |
|
|
}; |
| 134 |
|
|
|
| 135 |
|
|
inline Arg::Arg() : arg_(NULL), parser_(parse_null) { } |
| 136 |
|
|
inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { } |
| 137 |
|
|
|
| 138 |
|
|
inline bool Arg::Parse(const char* str, int n) const { |
| 139 |
|
|
return (*parser_)(str, n, arg_); |
| 140 |
|
|
} |
| 141 |
|
|
|
| 142 |
|
|
// This part of the parser, appropriate only for ints, deals with bases |
| 143 |
|
|
#define MAKE_INTEGER_PARSER(type, name) \ |
| 144 |
|
|
inline Arg Hex(type* ptr) { \ |
| 145 |
|
|
return Arg(ptr, Arg::parse_ ## name ## _hex); } \ |
| 146 |
|
|
inline Arg Octal(type* ptr) { \ |
| 147 |
|
|
return Arg(ptr, Arg::parse_ ## name ## _octal); } \ |
| 148 |
|
|
inline Arg CRadix(type* ptr) { \ |
| 149 |
|
|
return Arg(ptr, Arg::parse_ ## name ## _cradix); } |
| 150 |
|
|
|
| 151 |
|
|
MAKE_INTEGER_PARSER(short, short); |
| 152 |
|
|
MAKE_INTEGER_PARSER(unsigned short, ushort); |
| 153 |
|
|
MAKE_INTEGER_PARSER(int, int); |
| 154 |
|
|
MAKE_INTEGER_PARSER(unsigned int, uint); |
| 155 |
|
|
MAKE_INTEGER_PARSER(long, long); |
| 156 |
|
|
MAKE_INTEGER_PARSER(unsigned long, ulong); |
| 157 |
|
|
#if @pcre_have_long_long@ |
| 158 |
|
|
MAKE_INTEGER_PARSER(long long, longlong); |
| 159 |
|
|
#endif |
| 160 |
|
|
#if @pcre_have_ulong_long@ |
| 161 |
|
|
MAKE_INTEGER_PARSER(unsigned long long, ulonglong); |
| 162 |
|
|
#endif |
| 163 |
|
|
|
| 164 |
|
|
#undef PCRE_IS_SET |
| 165 |
|
|
#undef PCRE_SET_OR_CLEAR |
| 166 |
|
|
#undef MAKE_INTEGER_PARSER |
| 167 |
|
|
|
| 168 |
|
|
} // namespace pcrecpp |
| 169 |
|
|
|
| 170 |
|
|
|
| 171 |
|
|
#endif /* _PCRECPPARG_H */ |