From: Mark Tearle Date: Sat, 27 Dec 2014 04:36:44 +0000 (+0800) Subject: Move Regex functions into common code and header X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=0cad3846c334d2bc25724bc50b7b69e5e0dab8e4;p=tpg%2Fopendispense2.git Move Regex functions into common code and header --- diff --git a/src/common/config.c b/src/common/config.c index 2328a88..563240a 100644 --- a/src/common/config.c +++ b/src/common/config.c @@ -10,6 +10,7 @@ #include #include #include "config.h" +#include "doregex.h" #include #include #include diff --git a/src/common/config.h b/src/common/config.h index 1e7583e..901cad8 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -9,10 +9,6 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ -#include -extern void CompileRegex(regex_t *Regex, const char *Pattern, int Flags); -extern int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage); - // === HELPER MACROS === #define _EXPSTR(x) #x #define EXPSTR(x) _EXPSTR(x) 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); + } +} + diff --git a/src/common/doregex.h b/src/common/doregex.h new file mode 100644 index 0000000..62c6859 --- /dev/null +++ b/src/common/doregex.h @@ -0,0 +1,16 @@ +/* + * OpenDispense2 + * + * This code is published under the terms of the Acess licence. + * See the file COPYING for details. + * + * doregex.h - Regex Header + */ +#ifndef _DOREGEX_H_ +#define _DOREGEX_H_ + +#include +extern void CompileRegex(regex_t *Regex, const char *Pattern, int Flags); +extern int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage); + +#endif diff --git a/src/server/Makefile b/src/server/Makefile index dcd6f0d..5034bf9 100644 --- a/src/server/Makefile +++ b/src/server/Makefile @@ -3,9 +3,10 @@ INSTALLDIR := /usr/local/opendispense2 -OBJ := main.o server.o logging.o config.o +OBJ := main.o server.o logging.o OBJ += dispense.o itemdb.o OBJ += handler_coke.o handler_snack.o handler_door.o +OBJ += config.o doregex.o BIN := ../../dispsrv OBJ := $(OBJ:%=obj/%) diff --git a/src/server/main.c b/src/server/main.c index c8be473..cbebee2 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -188,38 +188,6 @@ void AddPeriodicFunction(void (*Fcn)(void)) fprintf(stderr, "Error: No space for %p in periodic list\n", Fcn); } -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); - } -} - // Serial helper int InitSerial(const char *File, int BaudRate) {