Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_RegExp.cpp

00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  *
00016  * (c)Copyright 2006 Hewlett-Packard Development Company, LP.
00017  *
00018  */
00019 
00020 #include "C_RegExp.hpp"
00021 #include "string_t.hpp"
00022 #include "Utils.hpp"
00023 
00024 #define MAX_MATCH 10
00025 
00026 
00027 C_RegExp::C_RegExp(char *P_RegularExpression, 
00028                    int  *P_error_code,                        
00029                    int   P_nb_match,
00030                    int   P_sub_match,
00031                    int   P_line) {
00032   m_regularExpression = P_RegularExpression ;
00033   m_nb_match = P_nb_match ;
00034   m_sub_match = P_sub_match ;
00035   m_line = P_line ;
00036   (*P_error_code) = regcomp(&m_internalRegExp, P_RegularExpression, REG_EXTENDED | REG_NEWLINE);
00037 }
00038 
00039 char* C_RegExp::setSubString(char* P_source, int P_start, int P_stop)
00040 {
00041   char   *L_target = NULL ;
00042 
00043   if(P_source != NULL) {
00044 
00045     //        std::cerr << "P_source " << P_source 
00046     //      << " P_start " << P_start
00047     //      << " P_stop " << P_stop << std::endl;
00048 
00049     if(P_stop > P_start) {
00050       ALLOC_TABLE(L_target, char*, sizeof(char), (P_stop-P_start+1));
00051       memcpy(L_target, &(P_source[P_start]), (P_stop-P_start));
00052       L_target[(P_stop-P_start)] = '\0';
00053     }
00054   }
00055 
00056   return (L_target);
00057 }
00058 
00059 char* C_RegExp::execute(char *P_buffer) {
00060 
00061   regmatch_t L_pmatch[MAX_MATCH]      ;
00062   int        L_error                  ;
00063   int        L_i                      ;
00064   char*      L_result      = NULL     ;
00065   char*      L_buffer      = P_buffer ;
00066   char*      L_line        = NULL     ;
00067   char*      L_ptr         = NULL     ;
00068   
00069   memset((void*)L_pmatch, 0, sizeof(regmatch_t)*MAX_MATCH);
00070 
00071   if (m_line > 0) {
00072     // explicit line of the buffer = m_line
00073     int   L_current_line = -1 ;
00074 
00075     L_ptr = L_buffer ;
00076     L_line = L_ptr;
00077 
00078     while (L_current_line != m_line) {
00079       L_ptr = strstr(L_ptr, "\n");
00080       if (L_ptr) { 
00081         L_current_line++; 
00082         if (L_current_line != m_line) {
00083           L_ptr++;
00084           if (*L_ptr) { L_line = L_ptr ;}
00085         } else {
00086           *L_ptr = 0 ;
00087           L_buffer = L_line ;
00088         }
00089       } else {
00090         L_ptr = NULL ;
00091         L_buffer = NULL ;
00092         break ;
00093       }
00094     }
00095   }
00096   
00097 
00098   if (L_buffer) {
00099     // L_error = regexec(&m_internalRegExp, L_buffer, MAX_MATCH, L_pmatch, REG_EXTENDED | REG_NEWLINE);
00100     L_error = regexec(&m_internalRegExp, L_buffer, MAX_MATCH, L_pmatch, REG_EXTENDED);
00101     if (L_error == 0) {
00102       for(L_i=0; L_i < MAX_MATCH; L_i++) {
00103         if(L_pmatch[L_i].rm_eo == -1) break ;
00104         if (L_i == m_sub_match) {
00105           L_result = setSubString(L_buffer, 
00106                                   L_pmatch[L_i].rm_so, L_pmatch[L_i].rm_eo);
00107         }
00108       }
00109       if (L_i != m_nb_match) {
00110         FREE_TABLE(L_result);
00111       }
00112     }
00113   }
00114 
00115   if (L_ptr) { *L_ptr = '\n'; }
00116 
00117   return(L_result);
00118 }
00119 
00120 
00121 int C_RegExp::execute(char* P_buffer, int *P_start, int *P_end) {
00122 
00123   int        L_ret         = -1       ;
00124   regmatch_t L_pmatch[MAX_MATCH]      ;
00125   int        L_error                  ;
00126   int        L_i                      ;
00127   char*      L_buffer      = P_buffer ;
00128   char*      L_line        = NULL     ;
00129   char*      L_ptr         = NULL     ;
00130   
00131   memset((void*)L_pmatch, 0, sizeof(regmatch_t)*MAX_MATCH);
00132 
00133   if (m_line > 0) {
00134     // explicit line of the buffer = m_line
00135     int   L_current_line = -1 ;
00136 
00137     L_ptr = L_buffer ;
00138     L_line = L_ptr;
00139 
00140     while (L_current_line != m_line) {
00141       L_ptr = strstr(L_ptr, "\n");
00142       if (L_ptr) { 
00143         L_current_line++; 
00144         if (L_current_line != m_line) {
00145           L_ptr++;
00146           if (*L_ptr) { L_line = L_ptr ;}
00147         } else {
00148           *L_ptr = 0 ;
00149           L_buffer = L_line ;
00150         }
00151       } else {
00152         L_ptr = NULL ;
00153         L_buffer = NULL ;
00154         break ;
00155       }
00156     }
00157   }
00158   
00159 
00160   if (L_buffer) {
00161     // L_error = regexec(&m_internalRegExp, L_buffer, MAX_MATCH, L_pmatch, REG_EXTENDED | REG_NEWLINE);
00162     L_error = regexec(&m_internalRegExp, L_buffer, MAX_MATCH, L_pmatch, REG_EXTENDED);
00163     if (L_error == 0) {
00164       for(L_i=0; L_i < MAX_MATCH; L_i++) {
00165         if(L_pmatch[L_i].rm_eo == -1) break ;
00166         if (L_i == m_sub_match) {
00167           L_ret = 0 ;
00168           *P_start = L_pmatch[L_i].rm_so ;
00169           *P_end = L_pmatch[L_i].rm_eo ;
00170         }
00171       }
00172     }
00173   }
00174 
00175   if (L_ptr) { *L_ptr = '\n'; }
00176 
00177   return(L_ret);
00178 }
00179 
00180 char* C_RegExp::execute(char* P_buffer, int *P_size) {
00181   regmatch_t L_pmatch[MAX_MATCH]      ;
00182   int        L_error                  ;
00183   int        L_i                      ;
00184   char*      L_result      = NULL     ;
00185   char*      L_buffer      = P_buffer ;
00186   char*      L_line        = NULL     ;
00187   char*      L_ptr         = NULL     ;
00188   
00189   memset((void*)L_pmatch, 0, sizeof(regmatch_t)*MAX_MATCH);
00190 
00191   if (m_line > 0) {
00192     // explicit line of the buffer = m_line
00193     int   L_current_line = -1 ;
00194 
00195     L_ptr = L_buffer ;
00196     L_line = L_ptr;
00197 
00198     while (L_current_line != m_line) {
00199       L_ptr = strstr(L_ptr, "\n");
00200       if (L_ptr) { 
00201         L_current_line++; 
00202         if (L_current_line != m_line) {
00203           L_ptr++;
00204           if (*L_ptr) { L_line = L_ptr ;}
00205         } else {
00206           *L_ptr = 0 ;
00207           L_buffer = L_line ;
00208         }
00209       } else {
00210         L_ptr = NULL ;
00211         L_buffer = NULL ;
00212         break ;
00213       }
00214     }
00215   }
00216   
00217   if (L_buffer) {
00218     // L_error = regexec(&m_internalRegExp, L_buffer, MAX_MATCH, L_pmatch, REG_EXTENDED | REG_NEWLINE);
00219     L_error = regexec(&m_internalRegExp, L_buffer, MAX_MATCH, L_pmatch, REG_EXTENDED);
00220     if (L_error == 0) {
00221       for(L_i=0; L_i < MAX_MATCH; L_i++) {
00222         if(L_pmatch[L_i].rm_eo == -1) break ;
00223         if (L_i == m_sub_match) {
00224           *P_size = L_pmatch[L_i].rm_eo - L_pmatch[L_i].rm_so ;
00225           L_result = setSubString(L_buffer, 
00226                                   L_pmatch[L_i].rm_so, L_pmatch[L_i].rm_eo);
00227         }
00228       }
00229       if (L_i != m_nb_match) {
00230         FREE_TABLE(L_result);
00231       }
00232     }
00233   }
00234   
00235   if (L_ptr) { *L_ptr = '\n'; }
00236 
00237   return(L_result);
00238 
00239 }
00240 
00241 
00242 
00243 C_RegExp::~C_RegExp() {
00244   m_regularExpression = NULL ;
00245   regfree(&m_internalRegExp); 
00246 }
00247 
00248 
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 

Generated on Wed Mar 7 14:44:57 2007 for Seagull by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002