/** * Determine the portion of the original file name to preserve. * * If an error occurs in this function it will be appended to the log and * error mail messages, and the process status will be set appropriately. * * @param ds_id datastream ID * @param file_name name of the raw data file * * @retval 1 if successful * @retval 0 if a pattern matching error occurred */ int dsproc_set_preserve_dots_from_name(int ds_id, const char *file_name) { DataStream *ds = _DSProc->datastreams[ds_id]; char pattern[512]; regex_t preg; regmatch_t pmatch[1]; const char *strp; int status; int ndots; sprintf(pattern, "^%s\\.[[:digit:]]{8}\\.[[:digit:]]{6}\\.[[:alpha:]]+\\.", ds->name); if (!re_compile(&preg, pattern, REG_EXTENDED)) { DSPROC_ERROR( NULL, "Could not compile regular expression: '%s'", pattern); return(0); } status = re_execute(&preg, file_name, 1, pmatch, 0); if (status < 0) { DSPROC_ERROR( NULL, "Could not execute regular expression: '%s'", pattern); re_free(&preg); return(0); } strp = file_name; if (status == 1) { strp += pmatch[0].rm_eo; } ndots = 1; while ((strp = strchr(strp, '.'))) { ++ndots; ++strp; } re_free(&preg); DEBUG_LV1( DSPROC_LIB_NAME, "%s: Setting rename preserve dots value to: %d\n", ds->name, ndots); ds->preserve_dots = ndots; return(1); }
// Reads input char sequence line by line and test it against the // regular expression. If line is matched with regexp it is printed // to stdout. // Input files usage: sgrep REGEXP FILE1 FILE2 // stdin usage: echo foo | sgrep REGEXP // If at lease one matching found exit code is 0, otherwise it is 1. int main(int argc, char *argv[]) { if (argc < 2) { return print_usage(); } re *regexp; int matched; regexp = re_compile(argv[1]); if (argc > 2) { // Use input files. for (int i = 2; i < argc; i++) { FILE *f = fopen(argv[i], "r"); if (f == NULL) { die("Failed to open file %s", argv[i]); } matched |= match_file(f, regexp); fclose(f); } } else { // Use stdin as input. matched = match_file(stdin, regexp); } re_free(regexp); return !matched; }
void dict_recurse_free(t_dict_recurse *x) { TRACE("dict_recurse_free"); if (x->path) { sysmem_freeptr(x->path); } regexpr_free(x->search_key_expr); regexpr_free(x->search_val_expr); re_free(&x->re2); }
/* ** Compile a textual regular expression in zIn[] into a compiled regular ** expression suitable for us by re_match() and return a pointer to the ** compiled regular expression in *ppRe. Return NULL on success or an ** error message if something goes wrong. */ const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){ ReCompiled *pRe; const char *zErr; int i, j; *ppRe = 0; pRe = sqlite3_malloc( sizeof(*pRe) ); if( pRe==0 ){ return "out of memory"; } memset(pRe, 0, sizeof(*pRe)); pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char; if( re_resize(pRe, 30) ){ re_free(pRe); return "out of memory"; } if( zIn[0]=='^' ){ zIn++; }else{ re_append(pRe, RE_OP_ANYSTAR, 0); } pRe->sIn.z = (unsigned char*)zIn; pRe->sIn.i = 0; pRe->sIn.mx = (int)strlen(zIn); zErr = re_subcompile_re(pRe); if( zErr ){ re_free(pRe); return zErr; } if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){ re_append(pRe, RE_OP_MATCH, RE_EOF); re_append(pRe, RE_OP_ACCEPT, 0); *ppRe = pRe; }else if( pRe->sIn.i>=pRe->sIn.mx ){ re_append(pRe, RE_OP_ACCEPT, 0); *ppRe = pRe; }else{ re_free(pRe); return "unrecognized character"; } /* The following is a performance optimization. If the regex begins with ** ".*" (if the input regex lacks an initial "^") and afterwards there are ** one or more matching characters, enter those matching characters into ** zInit[]. The re_match() routine can then search ahead in the input ** string looking for the initial match without having to run the whole ** regex engine over the string. Do not worry able trying to match ** unicode characters beyond plane 0 - those are very rare and this is ** just an optimization. */ if( pRe->aOp[0]==RE_OP_ANYSTAR ){ for(j=0, i=1; j<sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){ unsigned x = pRe->aArg[i]; if( x<=127 ){ pRe->zInit[j++] = (unsigned char)x; }else if( x<=0xfff ){ pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6)); pRe->zInit[j++] = 0x80 | (x&0x3f); }else if( x<=0xffff ){ pRe->zInit[j++] = (unsigned char)(0xd0 | (x>>12)); pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f); pRe->zInit[j++] = 0x80 | (x&0x3f); }else{