/*
	table2tab.c
	Convert HTML tables to tab-seperated columns
*/

#include <stdio.h>

#define MAX_TAG  1000
#define MAX_CELL 1000

char *append_char(char *s, char c, int size);
int stristart(char *haystack, char *needle);

int main() {
	int	in_tag = 0;
	int	in_table = 0, in_tr = 0, in_td = 0,
		table = 0, row = 0, column = 0;
	char	c;
	char	tag[MAX_TAG+1] = "";
	char	cell[MAX_CELL+1] = "";
	
	while( (c=getchar()) != EOF ) {
		switch(c) {
			case '<':
				if(!in_tag) {
					in_tag = 1;
				}
				break;
			case '>':
				if(in_tag) {
					if(stristart(tag,"table")) {
						in_table = 1;
						++table;
						row = 0;
					} else
					
					if(stristart(tag,"/table")) {
						in_table = 0;
					} else

					if(stristart(tag,"tr")) {
						in_tr = 1;
						++row;
						column = 0;
					} else
					
					if(stristart(tag,"/tr")) {
						in_tr = 0;
						putchar('\n'); /* newline */
					} else

					if(stristart(tag,"td")) {
						in_td = 1;
						++column;
					} else
					
					if(stristart(tag,"/td")) {
			/*			printf("[%d: %d,%d,\"%s\"]\n",
							table,row,column,cell);
			*/
						if(column>1) putchar('\t'); /* tab */
						printf("%s",cell);
						in_td = 0;
						*cell = '\0';
					}
					
					in_tag = 0;
					*tag = '\0';
				}
				break;
			default:
				if(in_tag)
					append_char(tag,c,MAX_TAG);

				if(in_td && !in_tag)
					append_char(cell,c,MAX_CELL);
		}
	}
}

char *append_char(char *s, char c, int size) {
        char *p;

        for(p=s;*p;p++);
        if( (p-s) < size ) {
                p[0] = c;
                p[1] = '\0';
        }

        return s;       
}

int stristart(char *haystack, char *needle) {
        for(;*haystack && *needle;haystack++,needle++)
                if(toupper(*haystack) != toupper(*needle))
                        return 0;
        return 1;
}

