VPR-7.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
slre.h
Go to the documentation of this file.
1 /* Copyright (c) 2004-2012 Sergey Lyubka <valenok@gmail.com>
2  All rights reserved
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  THE SOFTWARE.
21 
22  This is a regular expression library that implements a subset of Perl RE.
23  Please refer to http://slre.googlecode.com for detailed description.
24 
25  Supported syntax:
26  ^ Match beginning of a buffer
27  $ Match end of a buffer
28  () Grouping and substring capturing
29  [...] Match any character from set
30  [^...] Match any character but ones from set
31  \s Match whitespace
32  \S Match non-whitespace
33  \d Match decimal digit
34  \r Match carriage return
35  \n Match newline
36  + Match one or more times (greedy)
37  +? Match one or more times (non-greedy)
38  * Match zero or more times (greedy)
39  *? Match zero or more times (non-greedy)
40  ? Match zero or once
41  \xDD Match byte with hex value 0xDD
42  \meta Match one of the meta character: ^$().[*+\?
43 
44  Match string buffer "buf" of length "buf_len" against "regexp", which should
45  conform the syntax outlined above. "options" could be either 0 or
46  SLRE_CASE_INSENSITIVE for case-insensitive match. If regular expression
47  "regexp" contains brackets, slre_match() will capture the respective
48  substring into the passed placeholder. Thus, each opening parenthesis
49  should correspond to three arguments passed:
50  placeholder_type, placeholder_size, placeholder_address
51 
52  Usage example: parsing HTTP request line.
53 
54  char method[10], uri[100];
55  int http_version_minor, http_version_major;
56  const char *error;
57  const char *request = " \tGET /index.html HTTP/1.0\r\n\r\n";
58 
59  error = slre_match(0, "^\\s*(GET|POST)\\s+(\\S+)\\s+HTTP/(\\d)\\.(\\d)",
60  request, strlen(request),
61  SLRE_STRING, sizeof(method), method,
62  SLRE_STRING, sizeof(uri), uri,
63  SLRE_INT,sizeof(http_version_major),&http_version_major,
64  SLRE_INT,sizeof(http_version_minor),&http_version_minor);
65 
66  if (error != NULL) {
67  printf("Error parsing HTTP request: %s\n", error);
68  } else {
69  printf("Requested URI: %s\n", uri);
70  }
71 
72 
73  Return:
74  NULL: string matched and all captures successfully made
75  non-NULL: in this case, the return value is an error string */
76 
77 #ifndef SLRE_H
78 #define SLRE_H
79 
82 const char *slre_match(enum slre_option options, const char *regexp,
83  const char *buf, int buf_len, ...);
84 
85 #endif /* SLRE_H */
slre_capture
Definition: slre.h:81
slre_option
Definition: slre.h:80
const char * slre_match(enum slre_option options, const char *regexp, const char *buf, int buf_len,...)
Definition: slre.c:646
Definition: slre.h:81