summaryrefslogtreecommitdiff
path: root/utils/RegExp.cc
blob: 994b8d712177e98461c817ecdf51b593f77f01e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 *	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 IvlRegExp
The class \typ{IvlRegExp} 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{IvlRegExp}, then compiling it, and finally match strings against it.
?*/

#ifdef DOC
/*?
Initialize a \typ{IvlRegExp} with expression \var{expr}. The string \var{epxr}
is {\em not} copied.
?*/
IvlRegExp :: IvlRegExp (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
IvlRegExp :: 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
IvlRegExp :: Match (const char* s)
{
	if (Compiled)
		return regexec ((struct regex_t*) Compiled, s, 0, 0, 0) == 0 ? true : false;
	else
		return false;
}

/*?nodoc?*/
IvlRegExp :: ~IvlRegExp ()
{
	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 ... */

IvlRegExp* IvlRegExp::Compiled = 0;

/*?nodoc?*/
bool
IvlRegExp :: Compile ()
{
	if (re_comp (String) != 0) {
		Compiled = 0;
		return false;
	} else {
		Compiled = this;
		return true;
	}
}

/*?nodoc?*/
bool
IvlRegExp :: 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
IvlRegExp :: 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
IvlRegExp :: Match (const char* s)
{
	if (Compiled)
		return regex (Compiled, s) ? true : false;
	else
		return false;
}

/*?nodoc?*/
IvlRegExp :: ~IvlRegExp ()
{
	if (Compiled)
		free (Compiled);
}

#endif	/* !REGCOMP && !RE_COMP */