void doChecks() { char *checkBuffer,*checkresult,*sIn; long i,n,total,invalid=0; typedef struct { char *in,*s,*r,*out; } sCheck; sCheck checks[]= { {"{{div.cOptions}}go {{tag.tag56}} {{get.link.parent.html}}/ edit {{tag.tag57}} / edit {{tag.tag58}} / rename {{tag.tag59}} / move {{tag.tag60}} / add {{tag.tag61}}{{get.delete.html}} / {{get.link.login.html}}{{enddiv}}", ".*(\\{\\{\\w+\\.\\w+(\\.\\w+)?\\}\\}).*","\\1","{{get.delete.html}}"}, {"abracadabra", "a", "b", "bbrbcbdbbrb"}, {"a1a2a3a4a5a", "\\d", "_", "a_a_a_a_a_a"}, {"z1b4a5a", "(\\w)", "[\\1]", "[z][1][b][4][a][5][a]"}, {"farooeboar", "(.)..(..).(.).", "\\1\\2\\3", "foobar"}, {"file.c", "(([^\\.]+)\\.(.+))", "[\\1] name=\\2 ext=\\3", "[file.c] name=file ext=c"}, {"helloworld", "([e-o])", "_\\1_", "_h__e__l__l__o_w_o_r_l_d"}, {"I' a {{get.tag}}",".*(\\{\\{\\w+\\.\\w+(\\.\\w+)?\\}\\}).*","found tag \"\\1\"","found tag \"{{get.tag}}\""}, {"get.param","(\\w+?)\\..+","method is \"\\1\"","method is \"get\""}, {"get.param","[^\\.]+\\.([^\\.]+).*","tagname is \"\\1\"","tagname is \"param\""} }; total=sizeof(checks)/sizeof(checks[0])-1; if((checkBuffer=(char*)malloc(CHECKS_CHUNCK_COUNT*CHECKS_CHUNCK_SIZE))==NULL) { perror("malloc"); } else { memset(checkBuffer,0,CHECKS_CHUNCK_COUNT*CHECKS_CHUNCK_SIZE); } fprintf(stdout," [+] Performing several replacements to check consistence "); for(n=0; n<100; n++) { if(!(n%25)) { printf("."); fflush(stdout); } for(i=0; i<total; i++) { checkresult=treplace(checks[i].in,checks[i].s,checks[i].r); sIn=strdup(checks[i].in); if(strlen(sIn)>20) memcpy(sIn+15," ...\0",5); if(strcmp(checkresult,checks[i].out)) { fprintf(stderr,"\r[%d/%d] %-20s s: %-30s r: %-20s => %-25s ",i+1,total,sIn,checks[i].s,checks[i].r,checkresult); fprintf(stdout," ERR :(\n"); exit(0); } else { if(!n) { fprintf(stderr,"\r[%d/%d] %-20s s: %-30s r: %-20s => %-25s ",i+1,total,sIn,checks[i].s,checks[i].r,checkresult); fprintf(stdout," OK :)\n"); } } } } for(n=1; n<=CHECKS_CHUNCK_COUNT; n+=1+CHECKS_CHUNCK_COUNT/10) { fprintf(stdout,"\r[ + ] Checking stability for different input sizes consistence %d bytes, memory allocated: %d bytes",n*CHECKS_CHUNCK_COUNT*CHECKS_CHUNCK_SIZE,memAllocated()); fflush(stdout); memset(checkBuffer,'.',n*CHECKS_CHUNCK_SIZE-1); checkBuffer[n*CHECKS_CHUNCK_SIZE]=0; mreplace(checkBuffer,"\\.","_"); treplace(checkBuffer,"_","."); } fprintf(stdout,"\n"); fprintf(stdout,"[ m ] Memory allocated final: %d bytes\n",memAllocated()); }
static int parse(RParse *p, const char *data, char *str) { const struct mreplace_t *sdata = (struct mreplace_t*)data; char *buf = treplace (sdata->data, sdata->search, sdata->replace); memcpy (str, buf, R_PARSE_STRLEN); free (buf); return true; }
/* builds a binary for command line "search & replace" tests */ int main(char argc,char **argv) { if(argc==4 && strlen(argv[2])) { #if MDEBUG2 printf("Input string: %s, length %d, search %s, replace %s\n",argv[1],strlen(argv[1]),argv[2],argv[3]); #endif fprintf(stdout,"%s\n",treplace(argv[1],argv[2],argv[3])); } else { fprintf(stdout, "Perl alike \"search & replace\" v1.01 by Mandingo, Copyleft, 2009\n"); doChecks(); fprintf(stdout, "Usage: %s \"<text>\" \"<search>\" \"<replace>\"\n",argv[0]); } return 1; }