Move Regex functions into common code and header
[tpg/opendispense2.git] / src / common / doregex.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * doregex.c - Initialisation Code
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file
8  * COPYING for full details.
9  */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include "doregex.h"
13
14 // === CODE ===
15 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
16 {
17          int    ret;
18         
19         ret = regexec(regex, string, nMatches, matches, 0);
20         if( ret == REG_NOMATCH ) {
21                 return -1;
22         }
23         if( ret ) {
24                 size_t  len = regerror(ret, regex, NULL, 0);
25                 char    errorStr[len];
26                 regerror(ret, regex, errorStr, len);
27                 printf("string = '%s'\n", string);
28                 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
29                 exit(-1);
30         }
31         
32         return ret;
33 }
34
35 void CompileRegex(regex_t *regex, const char *pattern, int flags)
36 {
37          int    ret = regcomp(regex, pattern, flags);
38         if( ret ) {
39                 size_t  len = regerror(ret, regex, NULL, 0);
40                 char    errorStr[len];
41                 regerror(ret, regex, errorStr, len);
42                 fprintf(stderr, "Regex compilation failed - %s\n%s\n", errorStr, pattern);
43                 exit(-1);
44         }
45 }
46

UCC git Repository :: git.ucc.asn.au