// split `source' with `regexp_str' regexp VArray str_split( const char* regexp_str, const char* source, int maxcount ) { VArray arr; VRegexp re; int z = re.comp( regexp_str ); ASSERT( z ); if ( ! z ) return arr; const char* ps = source; while( ps && ps[0] && re.m( ps ) ) { if ( maxcount != -1 ) { maxcount--; if ( maxcount == 0 ) break; } VString s; s.setn( ps, re.sub_sp( 0 ) ); arr.push( s ); ps += re.sub_ep( 0 ); } if ( ps && ps[0] ) arr.push( ps ); return arr; }
int mem_string_search( const char *p, const char* d, const char* opt ) { int ps = strlen(p); ASSERT( ps < MAX_PATTERN ); int nocase = str_find( opt, 'i' ) > -1; long pos = -1; if( str_find( opt, 'r' ) > -1 ) { VRegexp re; if ( ! re.comp( p ) ) return -1; if ( ! re.m( d ) ) return -1; pos = re.sub_sp( 0 ); } else if( str_find( opt, 'h' ) > -1 ) { char new_p[MAX_PATTERN+1]; int pl = hex_string_to_pattern( p, new_p ); if (pl > 0) if ( nocase ) pos = mem_quick_search_nc( new_p, pl, d, strlen(d) ); else pos = mem_quick_search( new_p, pl, d, strlen(d) ); } else { if ( nocase ) pos = mem_quick_search_nc( p, ps, d, strlen(d) ); else pos = mem_quick_search( p, ps, d, strlen(d) ); } return pos; };
int str_rfind_regexp( const char* target, const char* pattern ) { VRegexp re; if ( ! re.comp( pattern ) ) return -1; int z = str_len( target ); while(4) { z--; if ( re.m( target + z ) ) return z + re.sub_sp( 0 ); if ( z == 0 ) break; } return -1; }
int str_find_regexp( const char* target, const char* pattern, int startpos ) { VRegexp re; if ( ! re.comp( pattern ) ) return -1; if ( startpos < 0 ) return -1; int z = 0; while( startpos-- ) { if ( target[z] == 0 ) return -1; z++; } if ( re.m( target + z ) ) return z + re.sub_sp( 0 ); else return -1; }
long file_grep( const char *re_string, FILE* f, int nocase, off_t spos ) { if ( strlen(re_string) >= (size_t)file_grep_max_line ) return -2; // just in case, and for now... char newpat[MAX_PATTERN+1]; strcpy( newpat, re_string ); if ( nocase ) str_up( newpat ); VRegexp re; if ( ! re.comp( newpat ) ) return -2; char *line = (char*)malloc( file_grep_max_line+1 ); off_t opos = ftello( f ); ASSERT( spos >= -1 ); if (spos != -1) fseeko( f, spos, SEEK_SET ); off_t cpos = ftello( f ); file_grep_lines_read = 0; int found = 0; while( fgets( line, file_grep_max_line, f ) ) { if ( nocase ) str_up( line ); if ( re.m( line ) ) { found = 1; break; } cpos = ftello( f ); file_grep_lines_read++; if (feof(f)) break; } fseeko( f, opos, SEEK_SET ); if (found) cpos += ( re.sub_sp( 0 ) ); free(line); file_grep_max_line = MAX_GREP_LINE; return found ? cpos : -1; }