#include #include #include #include typedef struct flag_struct { int balise; //keep track of number of opened balises unsigned int quote: 1; //are we in a quote? unsigned int type: 2; //0 balise ?, 1 balise img, 2 balise a unsigned int href: 1; //have we detected href? unsigned int src: 1; //have we detected src? unsigned int print: 1; //should we print it? } flag; void parse() { const char *refs[] = {"img\0","src\0","href" }; int cur; int counter=0,wcounter=0; flag state = { 0 }; while((cur = fgetc(stdin)) != EOF) {//keep on reading stdin while everything's fine. cur = tolower(cur); if(cur == '\"') {//toggle state.quote state.quote = ~state.quote; } else if(!state.quote && cur == '<') {//new marker, increment state.balise state.balise++; if(state.balise == 0) { wcounter = 0; counter = 0; state.print = 0; } } else if(!state.quote && cur == '>') {//closing a marker state.balise--; if(state.balise == 0) { wcounter = 0; counter = 0; state.print = 0; } } else if((state.balise > 0) && isspace(cur) != 0) { if(counter >0) wcounter++; state.print = 0; counter = 0; } else if(!state.quote && (state.balise > 0) && (counter <= 3)) { if (wcounter == 0) {//begining of a marker: we have our marker keyword ('a' or "img") if(cur == refs[0][counter] && (state.type == 1 || state.type == 0)) { state.type = 1; } else if(cur == 'a' && counter == 0) { state.type = 2; } else { state.type = 0; } } else {//determining if we ecounter "href" or "src" if(cur == refs[2][counter] && state.type == 2 && (state.href || counter == 0)) { state.href = 1; } else if(cur == refs[1][counter] && state.type == 1 && (state.src || counter == 0)) { state.src = 1; } else { state.href = 0; state.src = 0; } if((counter == 3 && state.href == 1) || (counter == 2 && state.src)) {//all conditions filled, we can now toggle state.print state.print = 1; printf("\n"); } } counter++; } else if(state.print) { if(counter > strlen(refs[state.type])) fputc(cur,stdout); else counter++; } } printf("\n"); return; } int main() { parse(); return EXIT_SUCCESS; }