summaryrefslogtreecommitdiff
path: root/utils/RegExp.cc
diff options
context:
space:
mode:
Diffstat (limited to 'utils/RegExp.cc')
-rw-r--r--utils/RegExp.cc163
1 files changed, 163 insertions, 0 deletions
diff --git a/utils/RegExp.cc b/utils/RegExp.cc
new file mode 100644
index 0000000..4fdc030
--- /dev/null
+++ b/utils/RegExp.cc
@@ -0,0 +1,163 @@
+/*
+ * CENA C++ Utilities
+ *
+ * by Stephane Chatty
+ *
+ * Copyright 1991, 1992
+ * Laboratoire de Recherche en Informatique (LRI)
+ * Centre d'Etudes de la Navigation Aerienne (CENA)
+ *
+ * Regular expressions
+ *
+ * $Id$
+ * $CurLog$
+ */
+
+#include "RegExp.h"
+
+/*?class CcuRegExp
+The class \typ{CcuRegExp} was designed to encapsulate regular expression management,
+implemented by \fun{re_comp} and \fun{re_exec}, or \fun{regcmp} and \fun{regex},
+depending on the operating system. The standard usage consists in initializing
+a \typ{CcuRegExp}, then compiling it, and finally match strings against it.
+?*/
+
+#ifdef DOC
+/*?
+Initialize a \typ{CcuRegExp} with expression \var{expr}. The string \var{epxr}
+is {\em not} copied.
+?*/
+CcuRegExp :: CcuRegExp (const char* expr)
+{
+}
+#endif
+
+
+#ifdef REGCOMP
+#include <regex.h>
+#include <memory.h>
+#include <malloc.h>
+
+/*?
+Compile a regular expression before using it. This function returns FALSE upon failure,
+TRUE upon success.
+?*/
+bool
+CcuRegExp :: Compile ()
+{
+ struct regex_t tmp;
+ if (regcomp (&tmp, String, REG_NOSUB) == 0) {
+ Compiled = malloc (sizeof (struct regex_t));
+ memcpy (&Compiled, &tmp, sizeof (struct regex_t));
+ return TRUE;
+ } else
+ return FALSE;
+}
+
+/*?
+Match a string against a regular expression. This function returns FALSE upon failure,
+TRUE upon success.
+?*/
+bool
+CcuRegExp :: Match (const char* s)
+{
+ if (Compiled)
+ return regexec ((struct regex_t*) Compiled, s, 0, 0, 0) ? TRUE : FALSE;
+ else
+ return FALSE;
+}
+
+/*?nodoc?*/
+CcuRegExp :: ~CcuRegExp ()
+{
+ if (Compiled) {
+ regfree ((struct regex_t*) Compiled);
+ free (Compiled);
+ }
+}
+
+#endif
+
+
+#ifdef RE_COMP
+
+extern "C" {
+ char* re_comp (const char*);
+ int re_exec (const char*);
+}
+/* should be #include <regcmp.h>, but not available everywhere ... */
+
+
+CcuRegExp* CcuRegExp::Compiled = 0;
+
+/*?nodoc?*/
+bool
+CcuRegExp :: Compile ()
+{
+ if (re_comp (String) != 0) {
+ Compiled = 0;
+ return FALSE;
+ } else {
+ Compiled = this;
+ return TRUE;
+ }
+}
+
+/*?nodoc?*/
+bool
+CcuRegExp :: Match (const char* s)
+{
+ if (Compiled != this)
+ if (!Compile ())
+ return FALSE;
+ if (re_exec (s) == 1)
+ return TRUE;
+ else
+ return FALSE;
+}
+
+#endif /* RE_COMP */
+
+#if !defined(REGCOMP) && !defined(RE_COMP)
+
+#include <stdlib.h>
+#include <malloc.h>
+#ifdef __GNUG__
+extern "C" {
+ char* regcmp (const char * ...);
+ char* regex (const char *, const char * ...);
+}
+#endif
+
+/*?
+Compile a regular expression before using it. This function returns FALSE upon failure,
+TRUE upon success.
+?*/
+bool
+CcuRegExp :: Compile ()
+{
+ Compiled = regcmp (String, 0);
+ return Compiled ? TRUE : FALSE;
+}
+
+/*?
+Match a string against a regular expression. This function returns FALSE upon failure,
+TRUE upon success.
+?*/
+bool
+CcuRegExp :: Match (const char* s)
+{
+ if (Compiled)
+ return regex (Compiled, s) ? TRUE : FALSE;
+ else
+ return FALSE;
+}
+
+/*?nodoc?*/
+CcuRegExp :: ~CcuRegExp ()
+{
+ if (Compiled)
+ free (Compiled);
+}
+
+#endif /* !REGCOMP && !RE_COMP */