pcrecpparg.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #ifndef _PCRECPPARG_H
  32. #define _PCRECPPARG_H
  33. #include <stdlib.h> // for NULL
  34. #include <string>
  35. #include <pcre.h>
  36. namespace pcrecpp {
  37. class StringPiece;
  38. // Hex/Octal/Binary?
  39. // Special class for parsing into objects that define a ParseFrom() method
  40. template <class T>
  41. class _RE_MatchObject {
  42. public:
  43. static inline bool Parse(const char* str, int n, void* dest) {
  44. if (dest == NULL) return true;
  45. T* object = reinterpret_cast<T*>(dest);
  46. return object->ParseFrom(str, n);
  47. }
  48. };
  49. class PCRECPP_EXP_DEFN Arg {
  50. public:
  51. // Empty constructor so we can declare arrays of Arg
  52. Arg();
  53. // Constructor specially designed for NULL arguments
  54. Arg(void*);
  55. typedef bool (*Parser)(const char* str, int n, void* dest);
  56. // Type-specific parsers
  57. #define PCRE_MAKE_PARSER(type,name) \
  58. Arg(type* p) : arg_(p), parser_(name) { } \
  59. Arg(type* p, Parser parser) : arg_(p), parser_(parser) { }
  60. PCRE_MAKE_PARSER(char, parse_char);
  61. PCRE_MAKE_PARSER(unsigned char, parse_uchar);
  62. PCRE_MAKE_PARSER(short, parse_short);
  63. PCRE_MAKE_PARSER(unsigned short, parse_ushort);
  64. PCRE_MAKE_PARSER(int, parse_int);
  65. PCRE_MAKE_PARSER(unsigned int, parse_uint);
  66. PCRE_MAKE_PARSER(long, parse_long);
  67. PCRE_MAKE_PARSER(unsigned long, parse_ulong);
  68. #if 1
  69. PCRE_MAKE_PARSER(long long, parse_longlong);
  70. #endif
  71. #if 1
  72. PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong);
  73. #endif
  74. PCRE_MAKE_PARSER(float, parse_float);
  75. PCRE_MAKE_PARSER(double, parse_double);
  76. PCRE_MAKE_PARSER(std::string, parse_string);
  77. PCRE_MAKE_PARSER(StringPiece, parse_stringpiece);
  78. #undef PCRE_MAKE_PARSER
  79. // Generic constructor
  80. template <class T> Arg(T*, Parser parser);
  81. // Generic constructor template
  82. template <class T> Arg(T* p)
  83. : arg_(p), parser_(_RE_MatchObject<T>::Parse) {
  84. }
  85. // Parse the data
  86. bool Parse(const char* str, int n) const;
  87. private:
  88. void* arg_;
  89. Parser parser_;
  90. static bool parse_null (const char* str, int n, void* dest);
  91. static bool parse_char (const char* str, int n, void* dest);
  92. static bool parse_uchar (const char* str, int n, void* dest);
  93. static bool parse_float (const char* str, int n, void* dest);
  94. static bool parse_double (const char* str, int n, void* dest);
  95. static bool parse_string (const char* str, int n, void* dest);
  96. static bool parse_stringpiece (const char* str, int n, void* dest);
  97. #define PCRE_DECLARE_INTEGER_PARSER(name) \
  98. private: \
  99. static bool parse_ ## name(const char* str, int n, void* dest); \
  100. static bool parse_ ## name ## _radix( \
  101. const char* str, int n, void* dest, int radix); \
  102. public: \
  103. static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \
  104. static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \
  105. static bool parse_ ## name ## _cradix(const char* str, int n, void* dest)
  106. PCRE_DECLARE_INTEGER_PARSER(short);
  107. PCRE_DECLARE_INTEGER_PARSER(ushort);
  108. PCRE_DECLARE_INTEGER_PARSER(int);
  109. PCRE_DECLARE_INTEGER_PARSER(uint);
  110. PCRE_DECLARE_INTEGER_PARSER(long);
  111. PCRE_DECLARE_INTEGER_PARSER(ulong);
  112. PCRE_DECLARE_INTEGER_PARSER(longlong);
  113. PCRE_DECLARE_INTEGER_PARSER(ulonglong);
  114. #undef PCRE_DECLARE_INTEGER_PARSER
  115. };
  116. inline Arg::Arg() : arg_(NULL), parser_(parse_null) { }
  117. inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { }
  118. inline bool Arg::Parse(const char* str, int n) const {
  119. return (*parser_)(str, n, arg_);
  120. }
  121. // This part of the parser, appropriate only for ints, deals with bases
  122. #define MAKE_INTEGER_PARSER(type, name) \
  123. inline Arg Hex(type* ptr) { \
  124. return Arg(ptr, Arg::parse_ ## name ## _hex); } \
  125. inline Arg Octal(type* ptr) { \
  126. return Arg(ptr, Arg::parse_ ## name ## _octal); } \
  127. inline Arg CRadix(type* ptr) { \
  128. return Arg(ptr, Arg::parse_ ## name ## _cradix); }
  129. MAKE_INTEGER_PARSER(short, short) /* */
  130. MAKE_INTEGER_PARSER(unsigned short, ushort) /* */
  131. MAKE_INTEGER_PARSER(int, int) /* Don't use semicolons */
  132. MAKE_INTEGER_PARSER(unsigned int, uint) /* after these statement */
  133. MAKE_INTEGER_PARSER(long, long) /* because they can cause */
  134. MAKE_INTEGER_PARSER(unsigned long, ulong) /* compiler warnings if */
  135. #if 1 /* the checking level is */
  136. MAKE_INTEGER_PARSER(long long, longlong) /* turned up high enough. */
  137. #endif /* */
  138. #if 1 /* */
  139. MAKE_INTEGER_PARSER(unsigned long long, ulonglong) /* */
  140. #endif
  141. #undef PCRE_IS_SET
  142. #undef PCRE_SET_OR_CLEAR
  143. #undef MAKE_INTEGER_PARSER
  144. } // namespace pcrecpp
  145. #endif /* _PCRECPPARG_H */