#include	<stdio.h>
#include	<ctype.h>
#include    <string.h>

/*
 *	tt2ht1.c
 *
 *	convert input file into html data table
 *
 */ 

#define	LINESIZE	     		512
#define MAXFIELDS               20
#define MAXFLDLEN               512
#define MAXDELIM                10


main()
{

	char    line[LINESIZE];									// line array of characters
	char    record[MAXFIELDS][MAXFLDLEN];					// the records	
	int 	itemDelimeters[MAXDELIM] = {' ', '\t', '\n'}; 	// item delimeters
	int  	i                        = 0,					// counter
	        numberOfColumns          = 0,					// column count
			numberOfRows             = 0;					// row count

	// output header
	printf("<html>\n");	
	printf("\n");
	printf("<head>\n");
	printf("	<title> table </title>\n");
	printf("</head>\n");
	printf("\n");
	printf("<body bgcolor = white>\n");
	printf("<center> table </center>\n");
	printf("\n");
	printf("	<table border = 2 cellpadding = 10 cellspacing = 1>\n");
	printf("\n");	
	
	
    // loop through each line of data 
    while ( fgets( line, LINESIZE, stdin) != NULL ) {
		
		numberOfRows++;
		
		// output fields
		printf("		<!-- Row %d -->\n", numberOfRows);
		printf("		<tr>\n");
		
		// parse line into records
		numberOfColumns = parse_line(itemDelimeters, line, record );

		// loop through each record
		for ( i = 0 ; i < numberOfColumns ; i++ ) {

			if (i == 0) {
				printf("			<td bgcolor = ltgreen>%s</td>\n", record[i]);
			}
			else if (i == 1){
				printf("			<td bgcolor = yellow>%s</td>\n", record[i]);
			}
			else {
				printf("			<td bgcolor = yellow align = center>>%s</td>\n", record[i]);
			}
		}
		printf("		</tr>\n");
		printf("\n");
	}

	// output footer
	printf("	</table>\n");
	printf("\n");
	printf("</body></html>\n");
	
}



int parse_line(int itemDel[], char mString[], char fields[MAXFIELDS][MAXFLDLEN]) 
//
// parse mString into an array of separate fields
// accepts an array of item delimiters to determine breaks
//
//  method: loop through mString looking for delimiters
//  		while coping to target array.
//
	
{
		int     i           = 0,		// general purpose counter
				endFlag     = 0,        // flag for indicating record is finished
				readAt      = -1,       // reading position in source array
                currentFld  = 0,        // item in fields[] array
				destination = 0;        // destination in current field				
		
        do {
                // loop through mString
			
				readAt++;
				
				// loop through each possible delimiter
				for (i = 0; itemDel[i] != NULL && ! endFlag ; i++) {
					
					// check for delimeter or the end of string
					if ( mString[readAt] == itemDel[i] || mString[readAt]  == NULL )
						endFlag = 1;			// flag record as ended
				}

				// 

                if ( ! endFlag ) {
						//
                 		// copy the char
					
                        fields[currentFld][destination] = mString[readAt];
                        destination++;
                }
                else {
						//
						// end record, terminate this field
					
						fields[currentFld][destination] = '\0';    

						// check if field is empty before incrementing
						if ( fields[currentFld][0] != '\0')
                        	currentFld++;

						// reset for new field
						destination = 0;
						endFlag = 0;
                }    
				
				// error check: array limits
				if ( readAt      == LINESIZE  ||
				     currentFld  == MAXFIELDS ||
					 destination == MAXFLDLEN )
					break;
					
        } while ( mString[readAt] != NULL );
		
		// return number of fields found in line
        return currentFld;
}
