slre.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) 2004-2012 Sergey Lyubka <valenok@gmail.com>
  2. // All rights reserved
  3. //
  4. // Enhanced and modified by beoran@gmail.com, 2013.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #ifndef SLRE_HEADER_DEFINED
  24. #define SLRE_HEADER_DEFINED
  25. // This is a regular expression library that implements a subset of Perl RE.
  26. // Please refer to http://slre.googlecode.com for detailed description.
  27. //
  28. // Supported syntax:
  29. // ^ Match beginning of a buffer
  30. // $ Match end of a buffer
  31. // () Grouping and substring capturing
  32. // [...] Match any character from set
  33. // [^...] Match any character but ones from set
  34. // \s Match whitespace
  35. // \S Match non-whitespace
  36. // \d Match decimal digit
  37. // \D Match anything but a decimal digit
  38. // \a Match alphabetical character
  39. // \A Match anything but an alphabetical character
  40. // \w Match alphanumerical character
  41. // \W Match anything but an alphanumerical character
  42. // \b Match a blank character, i.e. space or tab
  43. // \B Match anything but a blank character
  44. // \x Match a hexadecimal digit
  45. // \X Match anything but a hexadecimal digit
  46. // \t Match a tab
  47. // \r Match carriage return
  48. // \n Match newline
  49. // + Match one or more times (greedy)
  50. // +? Match one or more times (non-greedy)
  51. // * Match zero or more times (greedy)
  52. // *? Match zero or more times (non-greedy)
  53. // ? Match zero or once
  54. // \xDD Match byte with hex value 0xDD
  55. // \meta Match one of the meta character: ^$().[*+\?
  56. // Match string buffer "buf" of length "buf_len" against "regexp", which should
  57. // conform the syntax outlined above. "options" could be either 0 or
  58. // SLRE_CASE_INSENSITIVE for case-insensitive match. If regular expression
  59. // "regexp" contains brackets, slre_match() will capture the respective
  60. // substring into the passed placeholder. Thus, each opening parenthesis
  61. // should correspond to three arguments passed:
  62. // placeholder_type, placeholder_size, placeholder_address
  63. //
  64. // Usage example: parsing HTTP request line.
  65. //
  66. // char method[10], uri[100];
  67. // int http_version_minor, http_version_major;
  68. // const char *error;
  69. // const char *request = " \tGET /index.html HTTP/1.0\r\n\r\n";
  70. // error = slre_match(0, "^\\s*(GET|POST)\\s+(\\S+)\\s+HTTP/(\\d)\\.(\\d)",
  71. // request, strlen(request),
  72. // SLRE_STRING, sizeof(method), method,
  73. // SLRE_STRING, sizeof(uri), uri,
  74. // SLRE_INT,sizeof(http_version_major),&http_version_major,
  75. // SLRE_INT,sizeof(http_version_minor),&http_version_minor);
  76. //
  77. // if (error != NULL) {
  78. // printf("Error parsing HTTP request: %s\n", error);
  79. // } else {
  80. // printf("Requested URI: %s\n", uri);
  81. // }
  82. //
  83. // If the option SLRE_NO_CAPTURE is passed, captures are not stored.
  84. //
  85. // If SLRE_CALLBACK is passed as the first variable arument, then the 2 next
  86. // arguments must be a function pointer of the type * slre_callback, and a void *
  87. // that points to extra data or to NULL. The callback will be called once
  88. // for every match. It should return 0 to signal success or any of slre_result
  89. // to signal failure. The nonzero result of the callback will be returned from
  90. // slre_match().
  91. //
  92. // If SLRE_CAPTURED is passed, addresses to slre_captured structs must be passed
  93. // for storage of the results.
  94. //
  95. // If SLRE_IGNORE is passed, then all further captures are ignored.
  96. //
  97. // Return:
  98. // NULL: string matched and all captures successfully made
  99. // non-NULL: in this case, the return value is an error string
  100. /* Match options. */
  101. enum slre_option {
  102. SLRE_CASE_INSENSITIVE = 1, SLRE_NO_CAPTURE = 2
  103. };
  104. /* Possible capture types. */
  105. enum slre_capture {
  106. SLRE_STRING, SLRE_INT, SLRE_FLOAT, SLRE_CALLBACK, SLRE_CAPTURED, SLRE_IGNORE
  107. };
  108. /* Captured substring */
  109. struct slre_captured {
  110. const char *ptr; // Pointer to the substring
  111. int len; // Substring length
  112. };
  113. /* Possible results of slre_match. */
  114. enum slre_result {
  115. SLRE_OK = 0,
  116. SLRE_ERROR_NO_MATCH = 1,
  117. SLRE_ERROR_JUMP_OFFSET = 2,
  118. SLRE_ERROR_CODE_TOO_LONG = 3,
  119. SLRE_ERROR_DATA_TOO_LONG = 4,
  120. SLRE_ERROR_NO_PAREN = 5,
  121. SLRE_ERROR_BAD_PAREN = 6,
  122. SLRE_ERROR_NO_BRACKET = 7,
  123. SLRE_ERROR_TOO_MANY_PAREN = 8,
  124. SLRE_ERROR_INT_FAILED = 9,
  125. SLRE_ERROR_INT_SIZE = 10,
  126. SLRE_ERROR_FLOAT_SIZE = 11,
  127. SLRE_ERROR_FLOAT_FAILED = 12,
  128. SLRE_ERROR_STRING_SIZE = 13,
  129. SLRE_ERROR_UNKNOWN_TYPE = 14,
  130. SLRE_ERROR_TEXT_TOO_LONG = 15,
  131. SLRE_ERROR_NULL_CAPTURED = 16,
  132. SLRE_ERROR_LAST = 225,
  133. };
  134. /* Maximum amount of captures. */
  135. #ifndef SLRE_CAPURES_MAX
  136. #define SLRE_CAPURES_MAX 64
  137. #endif
  138. /* Callback type to use for SLRE_CALLBACK. */
  139. typedef int slre_callback(int index, const char * capture, int size, void * data);
  140. /* Matching function. */
  141. int slre_match(enum slre_option options, const char *regexp,
  142. const char *buf, int buf_len, ...);
  143. /* Converts error code to string or NULL if not known. */
  144. const char * slre_error(int code);
  145. #endif /* SLRE_HEADER_DEFINED */