/* Auteur: Tchetch-3 */ /* Plateforme: Linux/Win32 */ /* Compilateur/version: gcc/4.0.3 */ /* Derniere modif: 10 aout 20:30 */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* Si l'on veut compter avec clock() le temps d'execution */ #ifdef COUNT_TIME #include <time.h> #endif /* Fonction getc_unlocked et putc_unlocked sont POSIX.1 * Ne fonctionne pas forcement sur Win32, mais simplement la suppression des * redefinitions permet une compilation sur Win32 */ #ifndef WIN32 #undef getc #define getc getc_unlocked #undef putc #define putc putc_unlocked #endif #undef strcmp #define strcmp StrCmp #define IN_TAG 1 #define SPACE 2 #define IN_A 4 #define IN_IMG 8 #define BUFFER_SIZE 1024 char buffer[BUFFER_SIZE]; int a_img[BUFFER_SIZE]; #ifdef COUNT_LINK int nb_link=0; #endif inline int StrCmp(char *a, char *b) { while(*a!='\0'&&*b!='\0') { if(*a!=*b) { return 1; } ++a; ++b; } if(*a!=*b) { return 1; } return 0; } inline char parse_stdin() { int c=0; static int state=0; int i=0, j=0; int *pa_img=&a_img[0]; char *pbuffer=&buffer[0]; buffer[0]='\0'; a_img[0]=-1; while(i<BUFFER_SIZE) { c=getc(stdin); if(c==EOF) { break; } if(c=='<' && !(state & IN_TAG)) { state+=IN_TAG; *pa_img=i; ++pa_img; } if(state & IN_TAG) { if(c>64&&c<91) { c+=32; } if(c=='\n' || c=='\r') { if(*(pbuffer-1)!='=') { c=' '; } else { continue; } } if(c==' ') { if(!(state & SPACE)) { c='\0'; } else { continue; } } *pbuffer=(char)c; if(state & SPACE) { *pa_img=i; ++pa_img; state-=SPACE; } if(c=='\0') { state+=SPACE; } ++i; ++pbuffer; } if(c=='>' && (state & IN_TAG)) { state-=IN_TAG; break; } } *pbuffer='\0'; *pa_img=-1; return c; } inline void get_a_img() { int i=0, j=0; int x=0; int static state=0; char href[]="href"; char src[]="src"; int found=0; char tmp; char *pbuffer=&buffer[0]; int *pa_img=&a_img[0]; while((x=*pa_img)!=-1) { pbuffer=&buffer[x]; if(*pbuffer=='<') { state=0; } if(strcmp(pbuffer, "<a")==0) { state+=IN_A; ++pa_img; continue; } else if(strcmp(pbuffer, "<img")==0) { state+=IN_IMG; ++pa_img; continue; } if(state & IN_A) { if(*pbuffer=='h') { j=1; found=1; while((href[j]!='\0')) { if(*(pbuffer+j)!=href[j]) { found=0; break; } ++j; } if(found && (*(pbuffer+j)<97 || *(pbuffer+j)>122)) { ++j; found=0; while((tmp=*(pbuffer+j))!='\0') { if((tmp==' ' || tmp=='>' || tmp=='\'' || tmp=='"') && found) { break; } if((tmp>96&&tmp <123|| tmp==47|| tmp==46) && !found) { found=1; #ifdef COUNT_LINK ++nb_link; #endif } if(found) { putc(tmp, stdout); } ++j; } putc('\n', stdout); } } else { ++pa_img; continue; } } if(state & IN_IMG) { if(*(pbuffer)=='s') { j=1; found=1; while((src[j]!='\0')) { if(*(pbuffer+j)!=src[j]) { found=0; break; } ++j; } if(found && (*(pbuffer+j)<97 || *(buffer+j)>122)) { ++j; found=0; while((tmp=*(pbuffer+j))!='\0') { if((tmp==' ' || tmp=='>' || tmp=='\'' || tmp=='"') && found) { break; } if((tmp>96&&tmp<123 || tmp==47|| tmp==46) && !found) { found=1; #ifdef COUNT_LINK ++nb_link; #endif } if(found) { putc(tmp, stdout); } ++j; } putc('\n', stdout); } } else { ++pa_img; continue; } } ++pa_img; } } int main(int argc, char **argv) { char c='a'; #ifdef COUNT_TIME clock_t start, end; start=clock(); #endif while(1) { c=parse_stdin(); get_a_img(); if(c==EOF) { break; } } #ifdef COUNT_TIME end=clock(); fprintf(stderr, "Temps d'execution %.2f\n", (double)(end-start) / (double)CLOCKS_PER_SEC); #endif #ifdef COUNT_LINK fprintf(stderr, "Nombre de liens %d\n", nb_link); #endif return 0; }