kregexp.cpp Source File (kdecore Library)

| # kdecore Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kregexp.cpp

    00001 /* This file is part of the KDE libraries
    00002    Copyright (c) 1999 Torben Weis <weis@kde.org>
    00003
    00004    This library is free software; you can redistribute it and/or
    00005    modify it under the terms of the GNU Library General Public
    00006    License version 2 as published by the Free Software Foundation.
    00007
    00008    This library is distributed in the hope that it will be useful,
    00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00011    Library General Public License for more details.
    00012
    00013    You should have received a copy of the GNU Library General Public License
    00014    along with this library; see the file COPYING.LIB.  If not, write to
    00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00016    Boston, MA 02111-1307, USA.
    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 int main( int argc, char **argv )
    00185 {
    00186   if ( argc != 3 )
    00187     assert( 0 );
    00188
    00189   KRegExp r( argv[1], "" );
    00190   cout << "Compiled" << endl;
    00191
    00192   if ( !r.match( argv[2] ) )
    00193   {
    00194     cerr << "Does not match" << endl;
    00195     return 0;
    00196   }
    00197
    00198   cout << "Match" << endl;
    00199
    00200   for( int i = 0; i < 10; i++ )
    00201   {
    00202     const char *c = r.group( i );
    00203     if ( !c )
    00204       return 0;
    00205     cout << "Group #" << i << ":" << c << endl;
    00206   }
    00207
    00208   return 0;
    00209 }
    00210 */

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:49 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001