Publishing SLRE regex - as a library rather than program as is the case for the existing publish

Dependents:   EMBEDDED_CW2 EMBEDDED_CW2_Final USBSerialIORemotizer

Committer:
andrewbonney
Date:
Fri Apr 08 14:41:11 2011 +0000
Revision:
0:bc6b8c94c371

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewbonney 0:bc6b8c94c371 1 /*
andrewbonney 0:bc6b8c94c371 2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
andrewbonney 0:bc6b8c94c371 3 * All rights reserved
andrewbonney 0:bc6b8c94c371 4 *
andrewbonney 0:bc6b8c94c371 5 * "THE BEER-WARE LICENSE" (Revision 42):
andrewbonney 0:bc6b8c94c371 6 * Sergey Lyubka wrote this file. As long as you retain this notice you
andrewbonney 0:bc6b8c94c371 7 * can do whatever you want with this stuff. If we meet some day, and you think
andrewbonney 0:bc6b8c94c371 8 * this stuff is worth it, you can buy me a beer in return.
andrewbonney 0:bc6b8c94c371 9 */
andrewbonney 0:bc6b8c94c371 10
andrewbonney 0:bc6b8c94c371 11 /*
andrewbonney 0:bc6b8c94c371 12 * This is a regular expression library that implements a subset of Perl RE.
andrewbonney 0:bc6b8c94c371 13 * Please refer to http://slre.sourceforge.net for detailed description.
andrewbonney 0:bc6b8c94c371 14 *
andrewbonney 0:bc6b8c94c371 15 * Usage example (parsing HTTP request):
andrewbonney 0:bc6b8c94c371 16 *
andrewbonney 0:bc6b8c94c371 17 * struct slre slre;
andrewbonney 0:bc6b8c94c371 18 * struct cap captures[4 + 1]; // Number of braket pairs + 1
andrewbonney 0:bc6b8c94c371 19 * ...
andrewbonney 0:bc6b8c94c371 20 *
andrewbonney 0:bc6b8c94c371 21 * slre_compile(&slre,"^(GET|POST) (\S+) HTTP/(\S+?)\r\n");
andrewbonney 0:bc6b8c94c371 22 *
andrewbonney 0:bc6b8c94c371 23 * if (slre_match(&slre, buf, len, captures)) {
andrewbonney 0:bc6b8c94c371 24 * printf("Request line length: %d\n", captures[0].len);
andrewbonney 0:bc6b8c94c371 25 * printf("Method: %.*s\n", captures[1].len, captures[1].ptr);
andrewbonney 0:bc6b8c94c371 26 * printf("URI: %.*s\n", captures[2].len, captures[2].ptr);
andrewbonney 0:bc6b8c94c371 27 * }
andrewbonney 0:bc6b8c94c371 28 *
andrewbonney 0:bc6b8c94c371 29 * Supported syntax:
andrewbonney 0:bc6b8c94c371 30 * ^ Match beginning of a buffer
andrewbonney 0:bc6b8c94c371 31 * $ Match end of a buffer
andrewbonney 0:bc6b8c94c371 32 * () Grouping and substring capturing
andrewbonney 0:bc6b8c94c371 33 * [...] Match any character from set
andrewbonney 0:bc6b8c94c371 34 * [^...] Match any character but ones from set
andrewbonney 0:bc6b8c94c371 35 * \s Match whitespace
andrewbonney 0:bc6b8c94c371 36 * \S Match non-whitespace
andrewbonney 0:bc6b8c94c371 37 * \d Match decimal digit
andrewbonney 0:bc6b8c94c371 38 * \r Match carriage return
andrewbonney 0:bc6b8c94c371 39 * \n Match newline
andrewbonney 0:bc6b8c94c371 40 * + Match one or more times (greedy)
andrewbonney 0:bc6b8c94c371 41 * +? Match one or more times (non-greedy)
andrewbonney 0:bc6b8c94c371 42 * * Match zero or more times (greedy)
andrewbonney 0:bc6b8c94c371 43 * *? Match zero or more times (non-greedy)
andrewbonney 0:bc6b8c94c371 44 * ? Match zero or once
andrewbonney 0:bc6b8c94c371 45 * \xDD Match byte with hex value 0xDD
andrewbonney 0:bc6b8c94c371 46 * \meta Match one of the meta character: ^$().[*+?\
andrewbonney 0:bc6b8c94c371 47 */
andrewbonney 0:bc6b8c94c371 48
andrewbonney 0:bc6b8c94c371 49 #ifndef SLRE_HEADER_DEFINED
andrewbonney 0:bc6b8c94c371 50 #define SLRE_HEADER_DEFINED
andrewbonney 0:bc6b8c94c371 51
andrewbonney 0:bc6b8c94c371 52 /*
andrewbonney 0:bc6b8c94c371 53 * Compiled regular expression
andrewbonney 0:bc6b8c94c371 54 */
andrewbonney 0:bc6b8c94c371 55 struct slre {
andrewbonney 0:bc6b8c94c371 56 unsigned char code[256];
andrewbonney 0:bc6b8c94c371 57 unsigned char data[256];
andrewbonney 0:bc6b8c94c371 58 int code_size;
andrewbonney 0:bc6b8c94c371 59 int data_size;
andrewbonney 0:bc6b8c94c371 60 int num_caps; /* Number of bracket pairs */
andrewbonney 0:bc6b8c94c371 61 int anchored; /* Must match from string start */
andrewbonney 0:bc6b8c94c371 62 const char *err_str; /* Error string */
andrewbonney 0:bc6b8c94c371 63 };
andrewbonney 0:bc6b8c94c371 64
andrewbonney 0:bc6b8c94c371 65 /*
andrewbonney 0:bc6b8c94c371 66 * Captured substring
andrewbonney 0:bc6b8c94c371 67 */
andrewbonney 0:bc6b8c94c371 68 struct cap {
andrewbonney 0:bc6b8c94c371 69 const char *ptr; /* Pointer to the substring */
andrewbonney 0:bc6b8c94c371 70 int len; /* Substring length */
andrewbonney 0:bc6b8c94c371 71 };
andrewbonney 0:bc6b8c94c371 72
andrewbonney 0:bc6b8c94c371 73 /*
andrewbonney 0:bc6b8c94c371 74 * Compile regular expression. If success, 1 is returned.
andrewbonney 0:bc6b8c94c371 75 * If error, 0 is returned and slre.err_str points to the error message.
andrewbonney 0:bc6b8c94c371 76 */
andrewbonney 0:bc6b8c94c371 77 int slre_compile(struct slre *, const char *re);
andrewbonney 0:bc6b8c94c371 78
andrewbonney 0:bc6b8c94c371 79 /*
andrewbonney 0:bc6b8c94c371 80 * Return 1 if match, 0 if no match.
andrewbonney 0:bc6b8c94c371 81 * If `captured_substrings' array is not NULL, then it is filled with the
andrewbonney 0:bc6b8c94c371 82 * values of captured substrings. captured_substrings[0] element is always
andrewbonney 0:bc6b8c94c371 83 * a full matched substring. The round bracket captures start from
andrewbonney 0:bc6b8c94c371 84 * captured_substrings[1].
andrewbonney 0:bc6b8c94c371 85 * It is assumed that the size of captured_substrings array is enough to
andrewbonney 0:bc6b8c94c371 86 * hold all captures. The caller function must make sure it is! So, the
andrewbonney 0:bc6b8c94c371 87 * array_size = number_of_round_bracket_pairs + 1
andrewbonney 0:bc6b8c94c371 88 */
andrewbonney 0:bc6b8c94c371 89 int slre_match(const struct slre *, const char *buf, int buf_len,
andrewbonney 0:bc6b8c94c371 90 struct cap *captured_substrings);
andrewbonney 0:bc6b8c94c371 91
andrewbonney 0:bc6b8c94c371 92 #endif /* SLRE_HEADER_DEFINED */