ret_code amb_init(ambiente_t* ambiente, const char* content, int length) { ret_code ret = ERROR; if (NULL!=content && NULL!=ambiente) { const TRexChar *begin, *end, *error; TRex* x; CHECK(NULL!=(x=trex_compile(_TREXC("QtdMax=(\\d+)&PdvId=(\\d+)" \ "&Recibo=(\\d)&Dormir=(\\d)&Cortesias=(\\d)&Credito=(\\d)&"),&error))); if (trex_search(x,_TREXC(content),&begin,&end)) { int n = trex_getsubexpcount(x); if (n==7) { char *qtdMax, *pdvId, *credito; rx_getnext(x, 1, &qtdMax); rx_getnext(x, 2, &pdvId); rx_getnext(x, 6, &credito); CHECK(NULL!=qtdMax); CHECK(NULL!=pdvId); CHECK(NULL!=credito); ambiente->credito = atoi(credito); ambiente->qtdMax = atoi(qtdMax); ambiente->pdvId = pdvId; free(credito); free(qtdMax); ret = SUCCESS; } else { ret = ERROR; } } /* trex_search */ trex_free(x); } /* NULL!=content */ return ret; }
int main(int argc, char* argv[]) { const TRexChar *begin,*end; TRexChar sTemp[200]; const TRexChar *error = NULL; TRex *x = trex_compile(_TREXC("(x{1,5})xx"),&error); if(x) { trex_sprintf(sTemp,_TREXC("xxxxxxx")); if(trex_search(x,sTemp,&begin,&end)) { int i,n = trex_getsubexpcount(x); TRexMatch match; for(i = 0; i < n; i++) { TRexChar t[200]; trex_getsubexp(x,i,&match); trex_sprintf(t,_TREXC("[%%d]%%.%ds\n"),match.len); trex_printf(t,i,match.begin); } trex_printf(_TREXC("match! %d sub matches\n"),trex_getsubexpcount(x)); } else { trex_printf(_TREXC("no match!\n")); } trex_free(x); } else { trex_printf(_TREXC("compilation error [%s]!\n"),error?error:_TREXC("undefined")); } return 0; }