/* Auteur: Ti-R-5 */
/* Plateforme: Win32/Linux */
/* Compilateur/version:gcc(mingw-special)/? */
/*-------------------------------------------------------------------*/
/* Auteur: Renan Lavarec (nickname: Ti-R)                            */
/* This is free to use and modify provided my name is included.      */
/*-------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
typedef unsigned int UINT;
typedef unsigned char UCHAR;
typedef int bool;
#define false 0
#define true 1

#define BUFFER_SIZE 8192

/*
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)
{
    static char g_dif_a=('A'-'a');
    
	while(_size--)
	{
		if(*_s1!=*_s2 && *_s1!=((*_s2)-g_dif_a))
			return false;
		++_s1;
		++_s2;
	}
	return true;
}

char buffer[BUFFER_SIZE];
char *pt_buffer;
char *pt_current_link;
int max_keyword_size=4;

int nb_link=0;

bool TryToLoad(char **_s)
{
	static UINT len;
	// 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)
{
    static tmp_i;
    static count_max_link_size;
	++(*_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
				pt_current_link=++(*_s);
				count_max_link_size = BUFFER_SIZE;
			    while(--count_max_link_size)
				{
					if(!(**_s))
					{
						max_keyword_size = (*_s)-pt_current_link;
						pt_current_link=buffer;
						if(!TryToLoad(_s))
							return;
						max_keyword_size=4;
					}
					if((**_s)=='>')
						return;
					if(*((*_s)++)=='"')
					{
						(*(*_s-1))='\0';
						break;
					}
				}
				
				// Display the link
				fprintf(stdout,"%s\n",pt_current_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[(UCHAR)**_s])
				pt_functions[(UCHAR)**_s](_s);
		}
		else
		    ++(*_s);
	}

//	printf("\nnb liens: %i",nb_link);
}
 
int main(int argc, char *argv[])
{
//	StartQuantify();
	Parse();
//	StopQuantifyWindows("time: ");
	return EXIT_SUCCESS;
}