/* Auteur: Ti-R-3 */
/* Plateforme: Win32 */
/* 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>
#include <fcntl.h>
 
typedef unsigned int UINT;
typedef unsigned char UCHAR;
typedef int bool;
#define false 0
#define true 1

#define MAX_LINK_SIZE 1024

char *buffer;
char *pt_buffer;
char final_link[MAX_LINK_SIZE];
char *pt_current_link;
int max_keyword_size=4;
int len;

int nb_link=0;

inline void ParseLink(char **_s, uint32_t* _low_case_name, uint32_t* _up_case_name)
{
    static uint32_t tmp_i;
    static uint32_t count_max_link_size;
	++(*_s);
    while(**_s)
    {
		// Try to find '='
	    while(**_s)
		{
			if((**_s)=='>')
				return;
			if(**_s=='=')
				break;
	        ++(*_s);
		}

        // Compare if it is href/img and so on in from of the '='
   		if(*_low_case_name == (tmp_i = *(uint32_t*)((*_s)-4)) || *_up_case_name == tmp_i)
		{
		    ++(*_s);
				
			if((**_s)=='"')
			{
               // Get the link
		 	   ++(*_s);
				pt_current_link=final_link;
				count_max_link_size = MAX_LINK_SIZE;
			    while(--count_max_link_size)

				{
					if(!(**_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);
				
				// After finding the link, jump to the end >
			    while((**_s))
				{
					if((**_s)=='>')
						return;
			    	++(*_s);
				}
			}
		}
	    ++(*_s);
	}
}

void GetLink(char **_s)
{
     ParseLink(_s, (uint32_t*)"href", (uint32_t*)"HREF");
}
 
void GetImg(char **_s)
{
     ParseLink(_s, (uint32_t*)" src", (uint32_t*)" SRC");
}

void Parse()
{
	// Get the size of file
	fseek(stdin, 0, SEEK_END);
	int buffer_size = ftell(stdin);
	rewind(stdin);
	
	// Allocate memory
	buffer = malloc(buffer_size+1);
	buffer[buffer_size]='\0';
	
	// Copy all file in the memory
	fread(buffer, 1, buffer_size, stdin);

	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;

    final_link[MAX_LINK_SIZE-1]='\0';
	pt_buffer = buffer;
	char **_s = &pt_buffer;
	
    while((**_s))
	{
		if((**_s)=='<')
		{	
		    ++(*_s);
			if(pt_functions[(UCHAR)**_s])
				pt_functions[(UCHAR)**_s](_s);
		}
		else
		    ++(*_s);
	}
	free(buffer);
}
 
int main(int argc, char *argv[])
{
	Parse();
	return EXIT_SUCCESS;
}