2ec3132458368e0181967b860efab776a4061beb
[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.3 $
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     : Mar 31, 2010
21 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: regex.cpp,v $
26    Revision 1.3  2007/05/04 13:56:05  arjen
27    Added a copy contructor to the regex class. This prevents multiple frees in the destructor.
28
29    Revision 1.2  2002/09/28 06:45:51  arjen
30    New feature: subtring selection by regular expression.
31    Bugfix: use the std: namespace for STL classes istream and ostream
32
33    Revision 1.1  2002/07/25 08:01:27  arjen
34    First checkin, AXE release 0.2
35
36 *****************************/
37
38 static const char RCSID[] = "$Id: regex.cpp,v 1.3 2007/05/04 13:56:05 arjen Exp $";
39
40 #include <stdio.h>
41 #include <ctype.h>
42 #include "String.h"
43
44  //  Constructors and destructors for the regex class
45
46 regex::regex(const String &reg)
47 {
48    original = reg;
49    int error = regcomp (&expression, reg.p->s, REG_EXTENDED);
50 }
51
52 regex::regex(const char *reg)
53 {
54    original = reg;
55    int error = regcomp (&expression, reg, REG_EXTENDED);
56 }
57
58 regex::regex(const regex & reg)
59 {
60    original = reg.original;
61    int error = regcomp (&expression, reg.original, REG_EXTENDED);
62 }
63
64 regex::~regex()
65 {
66    regfree(&expression);
67 }
68
69 regex& regex::operator=(const regex& x)
70 {
71    original = x.original;
72    int error = regcomp (&expression, x.original, REG_EXTENDED);
73
74    return *this;
75 }
76
77 bool operator == (const String &s, const regex &r)
78 {
79    return regexec(&r.expression, s.p->s, 0, 0, 0) == 0;
80 }
81 bool operator == (const regex &r, const String &s)
82 {
83    return regexec(&r.expression, s.p->s, 0, 0, 0) == 0;
84 }
85
86 substring String::operator()(const regex &r)
87 {
88    substring sub;
89    regmatch_t  match;
90
91    sub.str = this;
92    sub.start = 0;
93    sub.len   = 0;
94
95    if (regexec(&r.expression, p->s, 1, &match, 0) == 0)
96    {
97       sub.start = match.rm_so;
98       sub.len   = match.rm_eo - match.rm_so;
99    }
100
101    return sub;
102 }
103