00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
#include <sys/types.h>
00020
00021
#include <string.h>
00022
#include <assert.h>
00023
#include <stdlib.h>
00024
00025
#include "kregexp.h"
00026
#include "kregpriv.h"
00027
#include "kdebug.h"
00028
00029 KRegExpPrivate::KRegExpPrivate()
00030 {
00031 m_bInit =
false;
00032
00033
for (
int i = 0; i < 10; i++ )
00034 m_strMatches[i] = 0L;
00035 }
00036
00037 KRegExpPrivate::KRegExpPrivate(
const char *_pattern,
const char *_mode )
00038 {
00039 m_bInit =
false;
00040
00041
for (
int i = 0; i < 10; i++ )
00042 m_strMatches[i] = 0L;
00043
00044 compile( _pattern, _mode );
00045 }
00046
00047 KRegExpPrivate::~KRegExpPrivate()
00048 {
00049
for (
int i = 0; i < 10; i++ )
00050
if ( m_strMatches[i] )
00051 free( m_strMatches[i] );
00052
00053
if ( m_bInit )
00054 regfree( &m_pattern );
00055 }
00056
00057
bool KRegExpPrivate::compile(
const char *_pattern,
const char *_mode )
00058 {
00059
if ( m_bInit )
00060 regfree( &m_pattern );
00061
00062
int res = regcomp( &m_pattern, _pattern, ( strchr( _mode,
'i' ) != 0L ? REG_ICASE : 0 ) | REG_EXTENDED );
00063
if ( res == 0 )
00064 m_bInit =
true;
00065
00066
return ( res == 0 );
00067 }
00068
00069
bool KRegExpPrivate::match(
const char *_string )
00070 {
00071
if ( !m_bInit )
00072 {
00073 kdDebug(128) <<
"You must compile a pattern before you can try to match it" <<
endl;
00074 assert( 0 );
00075 }
00076
00077
for (
int i = 0; i < 10; i++ )
00078 {
00079 m_matches[i].rm_so = -1;
00080 m_matches[i].rm_eo = -1;
00081
if ( m_strMatches[i] )
00082 {
00083 free( m_strMatches[i] );
00084 m_strMatches[i] = 0L;
00085 }
00086 }
00087
00088
int res = regexec( &m_pattern, _string, 10, m_matches, 0 );
00089
if ( res != 0 )
00090
return false;
00091
00092
int slen = strlen( _string );
00093
00094
for (
int j = 0; j < 10; j++ )
00095 {
00096
if ( m_matches[j].rm_so >= 0 && m_matches[j].rm_eo >= 0 &&
00097 m_matches[j].rm_so <= slen && m_matches[j].rm_eo <= slen &&
00098 m_matches[j].rm_so <= m_matches[j].rm_eo )
00099 {
00100
int len = m_matches[j].rm_eo - m_matches[j].rm_so;
00101 m_strMatches[j] = (
char* )malloc( len + 1 );
00102 memcpy( m_strMatches[j], _string + m_matches[j].rm_so, len );
00103 m_strMatches[j][ len ] = 0;
00104 }
00105 }
00106
00107
return true;
00108 }
00109
00110
const char* KRegExpPrivate::group(
int _grp )
00111 {
00112
if ( _grp < 0 || _grp >= 10 )
00113 {
00114 kdDebug(128) <<
"You may only use a group in the range of 0..9" <<
endl;
00115 assert( 0 );
00116 }
00117
00118
return m_strMatches[ _grp ];
00119 }
00120
00121
int KRegExpPrivate::groupStart(
int _grp )
00122 {
00123
if ( _grp < 0 || _grp >= 10 )
00124 {
00125 kdDebug(128) <<
"You may only use a group in the range of 0..9" <<
endl;
00126 assert( 0 );
00127 }
00128
00129
return m_matches[ _grp ].rm_so;
00130 }
00131
00132
int KRegExpPrivate::groupEnd(
int _grp )
00133 {
00134
if ( _grp < 0 || _grp >= 10 )
00135 {
00136 kdDebug(128) <<
"You may only use a group in the range of 0..9" <<
endl;
00137 assert( 0 );
00138 }
00139
00140
return m_matches[ _grp ].rm_eo;
00141 }
00142
00143 KRegExp::KRegExp()
00144 {
00145 m_pPrivate =
new KRegExpPrivate;
00146 }
00147
00148 KRegExp::KRegExp(
const char *_pattern,
const char *_mode)
00149 {
00150 m_pPrivate =
new KRegExpPrivate( _pattern, _mode );
00151 }
00152
00153 KRegExp::~KRegExp()
00154 {
00155
delete m_pPrivate;
00156 }
00157
00158 bool KRegExp::compile(
const char *_pattern,
const char *_mode)
00159 {
00160
return m_pPrivate->
compile( _pattern, _mode );
00161 }
00162
00163 bool KRegExp::match(
const char *_string )
00164 {
00165
return m_pPrivate->
match( _string );
00166 }
00167
00168 const char*
KRegExp::group(
int _grp )
00169 {
00170
return m_pPrivate->
group( _grp );
00171 }
00172
00173 int KRegExp::groupStart(
int _grp )
00174 {
00175
return m_pPrivate->
groupStart( _grp );
00176 }
00177
00178 int KRegExp::groupEnd(
int _grp )
00179 {
00180
return m_pPrivate->
groupEnd( _grp );
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210