From 3a4838bed13b767132cbdf06364b2658da6cc356 Mon Sep 17 00:00:00 2001 From: chatty Date: Tue, 15 Dec 1992 10:55:33 +0000 Subject: Initial revision --- utils/RegExp.cc | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 utils/RegExp.cc (limited to 'utils/RegExp.cc') 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 +#include +#include + +/*? +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 , 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 +#include +#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 */ -- cgit v1.1