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