/* * CENA C++ Utilities * * by Stephane Chatty * * Copyright 1991-1996 * 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) == 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 */