/*-------------------------------------------------------------------*/ /* Auteur: Renan Lavarec (nickname: Ti-R) */ /* This is free to use and modify provided my name is included. */ /*-------------------------------------------------------------------*/ #include #include #include #define UINT unsigned int #define bool int #define false 0 #define true 1 #define BUFFER_SIZE 512 char g_dif_a=('A'-'a'); /* clock_t begin_quantify; void StartQuantify() { begin_quantify=clock(); } void StopQuantifyWindows(char *_string) { printf("\n\n%s: %f ms\n",_string,((clock()-begin_quantify)/(float)CLOCKS_PER_SEC)*1000); } */ bool tr_strincmp_first(char *_s1,char *_s2,int _size) { while(_size--) { if(*_s1!=*_s2 && *_s1!=((*_s2)-g_dif_a)) return false; ++_s1; ++_s2; } return true; } char buffer[BUFFER_SIZE]; char *pt_buffer; char final_link[1024]; char *pt_current_link; int max_keyword_size=4; int len; int nb_link=0; bool TryToLoad(char **_s) { // Copy the last part of buffer to the begining to detect through the test the correct string memcpy(buffer,buffer+BUFFER_SIZE-max_keyword_size-1,max_keyword_size); if((len=fread(buffer+max_keyword_size, 1, BUFFER_SIZE-1-max_keyword_size, stdin)) != 0) { buffer[len+max_keyword_size]='\0'; pt_buffer = &buffer[max_keyword_size]; *_s = pt_buffer; return true; } return false; } inline void ParseLink(char **_s, char* _low_case_name, int _size_name) { ++(*_s); while(1) { // Try to find '=' while(1) { if(!(**_s) && !TryToLoad(_s)) return; if((**_s)=='>') return; if(**_s=='=') break; ++(*_s); } // Compare if it is href/img and so on in from of the '=' if(tr_strincmp_first(_low_case_name, (*_s)-_size_name,_size_name)) { ++(*_s); if(!(**_s) && !TryToLoad(_s)) return; if((**_s)=='"') { // Get the link ++(*_s); pt_current_link=final_link; while(1) { if(!(**_s) && !TryToLoad(_s)) return; if((**_s)=='>') return; if(**_s=='"') { (*pt_current_link)='\0'; ++(*_s); break; } (*pt_current_link++)=**_s; ++(*_s); } // Display the link fprintf(stdout,"%s\n",final_link); ++nb_link; // After finding the link, jump to the end > while(1) { if(!(**_s) && !TryToLoad(_s)) return; if((**_s)=='>') return; ++(*_s); } } } ++(*_s); } } void GetLink(char **_s) { ParseLink(_s, "href", 4); } void GetImg(char **_s) { ParseLink(_s, "src", 3); } void Parse() { void (*pt_functions[256])(char **); memset(pt_functions,0,sizeof(void (*)(char **))*256); pt_functions['a']=GetLink; pt_functions['A']=GetLink; pt_functions['i']=GetImg; pt_functions['I']=GetImg; pt_buffer = &buffer[max_keyword_size]; char **_s = &pt_buffer; buffer[max_keyword_size]='\0'; while(1) { if(!(**_s) && !TryToLoad(_s)) break; if((**_s)=='<') { ++(*_s); if(!(**_s) && !TryToLoad(_s)) break; if(pt_functions[**_s]) pt_functions[**_s](_s); } else ++(*_s); } // printf("\nnb liens: %i",nb_link); } int main(int argc, char *argv[]) { // StartQuantify(); Parse(); // StopQuantifyWindows("time: "); return EXIT_SUCCESS; }