Move Regex functions into common code and header
[tpg/opendispense2.git] / src / common / doregex.c
diff --git a/src/common/doregex.c b/src/common/doregex.c
new file mode 100644 (file)
index 0000000..8ad9b39
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * OpenDispense 2 
+ * UCC (University [of WA] Computer Club) Electronic Accounting System
+ *
+ * doregex.c - Initialisation Code
+ *
+ * This file is licenced under the 3-clause BSD Licence. See the file
+ * COPYING for full details.
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include "doregex.h"
+
+// === CODE ===
+int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
+{
+        int    ret;
+       
+       ret = regexec(regex, string, nMatches, matches, 0);
+       if( ret == REG_NOMATCH ) {
+               return -1;
+       }
+       if( ret ) {
+               size_t  len = regerror(ret, regex, NULL, 0);
+               char    errorStr[len];
+               regerror(ret, regex, errorStr, len);
+               printf("string = '%s'\n", string);
+               fprintf(stderr, "%s\n%s", errorMessage, errorStr);
+               exit(-1);
+       }
+       
+       return ret;
+}
+
+void CompileRegex(regex_t *regex, const char *pattern, int flags)
+{
+        int    ret = regcomp(regex, pattern, flags);
+       if( ret ) {
+               size_t  len = regerror(ret, regex, NULL, 0);
+               char    errorStr[len];
+               regerror(ret, regex, errorStr, len);
+               fprintf(stderr, "Regex compilation failed - %s\n%s\n", errorStr, pattern);
+               exit(-1);
+       }
+}
+

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