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