X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fcommon%2Fdoregex.c;fp=src%2Fcommon%2Fdoregex.c;h=8ad9b3918ed0e6d4c106304ee382ee5d10083132;hb=0cad3846c334d2bc25724bc50b7b69e5e0dab8e4;hp=0000000000000000000000000000000000000000;hpb=6d657891a5410b7d93cc90f376a3ef27b72b20f6;p=tpg%2Fopendispense2.git diff --git a/src/common/doregex.c b/src/common/doregex.c new file mode 100644 index 0000000..8ad9b39 --- /dev/null +++ b/src/common/doregex.c @@ -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 +#include +#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); + } +} +