025ff404cc3a157e815b18703f1f6103ce3c182b
[AXE.git] / src / regex.cpp
1 /**************************************************************************
2 **  (c) Copyright 2001, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 **      FILE NAME      : regex.cpp
7 **      SYSTEM NAME    : Andromeda X-Windows Encapsulation
8 **      VERSION NUMBER : $Revision: 1.2 $
9 **
10 **  DESCRIPTION      :  regex class implementation.
11 **
12 **  EXPORTED OBJECTS : 
13 **  LOCAL    OBJECTS : 
14 **  MODULES  USED    :
15 ***************************************************************************
16 **  ADMINISTRATIVE INFORMATION *
17 ********************************
18 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 **      CREATION DATE   : Feb 23, 2001
20 **      LAST UPDATE     : Feb 23, 2001
21 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: regex.cpp,v $
26    Revision 1.2  2002-09-28 06:45:51  arjen
27    New feature: subtring selection by regular expression.
28    Bugfix: use the std: namespace for STL classes istream and ostream
29
30    Revision 1.1  2002/07/25 08:01:27  arjen
31    First checkin, AXE release 0.2
32
33 *****************************/
34
35 static const char RCSID[] = "$Id: regex.cpp,v 1.2 2002-09-28 06:45:51 arjen Exp $";
36
37 #include <stdio.h>
38 #include <ctype.h>
39 #include "String.h"
40
41  //  Constructors and destructors for the regex class
42
43 regex::regex(const String &reg)
44 {
45    regcomp (&expression, reg.p->s, REG_EXTENDED);
46 }
47
48 regex::regex(const char *reg)
49 {
50    regcomp (&expression, reg, REG_EXTENDED);
51 }
52
53 regex::~regex()
54 {
55    regfree(&expression);
56 }
57
58
59 bool operator == (const String &s, const regex &r)
60 {
61    return regexec(&r.expression, s.p->s, 0, 0, 0) == 0;
62 }
63 bool operator == (const regex &r, const String &s)
64 {
65    return regexec(&r.expression, s.p->s, 0, 0, 0) == 0;
66 }
67
68 substring String::operator()(const regex &r)
69 {
70    substring sub;
71    regmatch_t  match;
72
73    sub.str = this;
74    sub.start = 0;
75    sub.len   = 0;
76
77    if (regexec(&r.expression, p->s, 1, &match, 0) == 0)
78    {
79       sub.start = match.rm_so;
80       sub.len   = match.rm_eo - match.rm_so;
81    }
82
83    return sub;
84 }
85