From 0cad3846c334d2bc25724bc50b7b69e5e0dab8e4 Mon Sep 17 00:00:00 2001
From: Mark Tearle <mark@tearle.com>
Date: Sat, 27 Dec 2014 12:36:44 +0800
Subject: [PATCH] Move Regex functions into common code and header

---
 src/common/config.c  |  1 +
 src/common/config.h  |  4 ----
 src/common/doregex.c | 46 ++++++++++++++++++++++++++++++++++++++++++++
 src/common/doregex.h | 16 +++++++++++++++
 src/server/Makefile  |  3 ++-
 src/server/main.c    | 32 ------------------------------
 6 files changed, 65 insertions(+), 37 deletions(-)
 create mode 100644 src/common/doregex.c
 create mode 100644 src/common/doregex.h

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 <stdio.h>
 #include <stdlib.h>
 #include "config.h"
+#include "doregex.h"
 #include <regex.h>
 #include <string.h>
 #include <ctype.h>
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 <regex.h>
-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 <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);
+	}
+}
+
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 <regex.h>
+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)
 {
-- 
GitLab