%{ #include #include //project media directory char *statePath; //counter int measures = 0; //X field char *abcID = ""; char *checkID; //updatedContent char upContent[3000]; %} %option yylineno %option outfile = "abcfilter.c" %option noyywrap %x UPDATEMODE %% "X:".* {abcID = strdup(yytext+2); fprintf(stdout,"X: %s\n",abcID);} "||" {measures++;} "|" {measures++;} .|\n {;} <> { BEGIN(UPDATEMODE); yyin = NULL; yyin = fopen( statePath, "r" ); fprintf(stdout,"Total measures: %d\n",measures); } [0-9]\[[0-9]+\] { checkID = strdup(yytext+2); checkID[strlen(checkID)-1]='\0'; if(strcmp(abcID,checkID)==0){ char newState; if(measures==0) newState='0'; else newState='1'; yytext[0]=newState; strcat(upContent,yytext); } else strcat(upContent,yytext); } .|\n { strcat(upContent,yytext); } <> { //write upContent FILE *file; file = fopen( statePath, "w"); fprintf(file,"%s",upContent); /*writes*/ fclose(file); /*done!*/ return 0; } %% void yyerror(char *s){ fprintf(stderr,"ERRO (linha %d):antes de '%s'\n-%s",yylineno, yytext,s); } int main( int argc, char **argv ) { ++argv, --argc; upContent[0]='\0'; if ( argc > 1 ){ yyin = fopen( argv[0], "r" ); statePath = argv[1]; } else{ yyin = stdin; } yylex(); return 0; }