• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kregexp.cpp
1 /* This file is part of the KDE libraries
2  Copyright (c) 1999 Torben Weis <weis@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include <sys/types.h>
20 
21 #include <string.h>
22 #include <assert.h>
23 #include <stdlib.h>
24 
25 #include "kregexp.h"
26 #include "kregpriv.h"
27 #include "kdebug.h"
28 
29 KRegExpPrivate::KRegExpPrivate()
30 {
31  m_bInit = false;
32 
33  for ( int i = 0; i < 10; i++ )
34  m_strMatches[i] = 0L;
35 }
36 
37 KRegExpPrivate::KRegExpPrivate( const char *_pattern, const char *_mode )
38 {
39  m_bInit = false;
40 
41  for ( int i = 0; i < 10; i++ )
42  m_strMatches[i] = 0L;
43 
44  compile( _pattern, _mode );
45 }
46 
47 KRegExpPrivate::~KRegExpPrivate()
48 {
49  for ( int i = 0; i < 10; i++ )
50  if ( m_strMatches[i] )
51  free( m_strMatches[i] );
52 
53  if ( m_bInit )
54  regfree( &m_pattern );
55 }
56 
57 bool KRegExpPrivate::compile( const char *_pattern, const char *_mode )
58 {
59  if ( m_bInit )
60  regfree( &m_pattern );
61 
62  int res = regcomp( &m_pattern, _pattern, ( strchr( _mode, 'i' ) != 0L ? REG_ICASE : 0 ) | REG_EXTENDED );
63  if ( res == 0 )
64  m_bInit = true;
65 
66  return ( res == 0 );
67 }
68 
69 bool KRegExpPrivate::match( const char *_string )
70 {
71  if ( !m_bInit )
72  {
73  kdDebug(128) << "You must compile a pattern before you can try to match it" << endl;
74  assert( 0 );
75  }
76 
77  for ( int i = 0; i < 10; i++ )
78  {
79  m_matches[i].rm_so = -1;
80  m_matches[i].rm_eo = -1;
81  if ( m_strMatches[i] )
82  {
83  free( m_strMatches[i] );
84  m_strMatches[i] = 0L;
85  }
86  }
87 
88  int res = regexec( &m_pattern, _string, 10, m_matches, 0 );
89  if ( res != 0 )
90  return false;
91 
92  int slen = strlen( _string );
93 
94  for ( int j = 0; j < 10; j++ )
95  {
96  if ( m_matches[j].rm_so >= 0 && m_matches[j].rm_eo >= 0 &&
97  m_matches[j].rm_so <= slen && m_matches[j].rm_eo <= slen &&
98  m_matches[j].rm_so <= m_matches[j].rm_eo )
99  {
100  int len = m_matches[j].rm_eo - m_matches[j].rm_so;
101  m_strMatches[j] = ( char* )malloc( len + 1 );
102  memcpy( m_strMatches[j], _string + m_matches[j].rm_so, len );
103  m_strMatches[j][ len ] = 0;
104  }
105  }
106 
107  return true;
108 }
109 
110 const char* KRegExpPrivate::group( int _grp )
111 {
112  if ( _grp < 0 || _grp >= 10 )
113  {
114  kdDebug(128) << "You may only use a group in the range of 0..9" << endl;
115  assert( 0 );
116  }
117 
118  return m_strMatches[ _grp ];
119 }
120 
121 int KRegExpPrivate::groupStart( int _grp )
122 {
123  if ( _grp < 0 || _grp >= 10 )
124  {
125  kdDebug(128) << "You may only use a group in the range of 0..9" << endl;
126  assert( 0 );
127  }
128 
129  return m_matches[ _grp ].rm_so;
130 }
131 
132 int KRegExpPrivate::groupEnd( int _grp )
133 {
134  if ( _grp < 0 || _grp >= 10 )
135  {
136  kdDebug(128) << "You may only use a group in the range of 0..9" << endl;
137  assert( 0 );
138  }
139 
140  return m_matches[ _grp ].rm_eo;
141 }
142 
143 KRegExp::KRegExp()
144 {
145  m_pPrivate = new KRegExpPrivate;
146 }
147 
148 KRegExp::KRegExp( const char *_pattern, const char *_mode)
149 {
150  m_pPrivate = new KRegExpPrivate( _pattern, _mode );
151 }
152 
153 KRegExp::~KRegExp()
154 {
155  delete m_pPrivate;
156 }
157 
158 bool KRegExp::compile( const char *_pattern, const char *_mode)
159 {
160  return m_pPrivate->compile( _pattern, _mode );
161 }
162 
163 bool KRegExp::match( const char *_string )
164 {
165  return m_pPrivate->match( _string );
166 }
167 
168 const char* KRegExp::group( int _grp )
169 {
170  return m_pPrivate->group( _grp );
171 }
172 
173 int KRegExp::groupStart( int _grp )
174 {
175  return m_pPrivate->groupStart( _grp );
176 }
177 
178 int KRegExp::groupEnd( int _grp )
179 {
180  return m_pPrivate->groupEnd( _grp );
181 }
182 
183 /*
184 int main( int argc, char **argv )
185 {
186  if ( argc != 3 )
187  assert( 0 );
188 
189  KRegExp r( argv[1], "" );
190  cout << "Compiled" << endl;
191 
192  if ( !r.match( argv[2] ) )
193  {
194  cerr << "Does not match" << endl;
195  return 0;
196  }
197 
198  cout << "Match" << endl;
199 
200  for( int i = 0; i < 10; i++ )
201  {
202  const char *c = r.group( i );
203  if ( !c )
204  return 0;
205  cout << "Group #" << i << ":" << c << endl;
206  }
207 
208  return 0;
209 }
210 */
KRegExpPrivate
Used internally by KRegExp.
Definition: kregpriv.h:34
KRegExp::KRegExp
KRegExp()
Creates a KRegExp object without a default pattern.
Definition: kregexp.cpp:143
KRegExp::groupStart
int groupStart(int _grp)
The offset of the given group in the string.
Definition: kregexp.cpp:173
KRegExp::match
bool match(const char *_string)
Match a string to the last supplied regexp.
Definition: kregexp.cpp:163
KRegExp::groupEnd
int groupEnd(int _grp)
The offset of the given group's end in the string.
Definition: kregexp.cpp:178
KRegExp::group
const char * group(int _grp)
Returns a group from the match.
Definition: kregexp.cpp:168
KRegExp::compile
bool compile(const char *_pattern, const char *_mode="")
Prepare a regular expression for subsequent matches.
Definition: kregexp.cpp:158
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.