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

/*
 *  q4.c
 *
 *	convert input file and output as printed XML
 *
 */ 

#define	LINESIZE		512
#define MAXFIELDS               10
#define MAXFLDLEN               30

main()
{

	char    line[LINESIZE];                 /* an array of characters */
	char    record[MAXFIELDS][MAXFLDLEN];		
	int	studentNum       = 0;
	int  	number_of_fields = 0;
	int	i, j;
	
	/* print header */
	printf("<?xml version = \"1.0\"?> \n");
	printf("<list> \n");

        /* loop through each line of data */ 
        while ( fgets( line, LINESIZE, stdin) != NULL )
        {
		/* print student header */
		studentNum++;
		printf("  <student id= \"%d\" > \n", studentNum);

		/* split line into record */
		number_of_fields = split_line( line, record );

		/* loop through each record */
		for ( i = 0 ; i < number_of_fields ; i++ ) {
			show_field(record[i]);
		}
		
		/* print student footer */
		printf("  </student> \n", studentNum);
        }

	printf("</list> \n");

}


show_field( char theField[])
/*
 *
 * break field into left and right, then printf
 *
 */

{
	int  i;
	char c, leftSide[LINESIZE];
	int equalpos;

	/* find position of '=' */
	for ( i = 0 ; theField[i] != '=' ; i++ ) {
	}
	equalpos = i;

	/* copy left part into string and append null */
	strncpy(leftSide, theField, equalpos);	
	leftSide[equalpos] = '\0';
	
	/* print the left side */
	printf("    <%s>", leftSide);

	i++;    /* skip the '='  */

	/* print the right side (sans \n) */
	for ( ; theField[i] != '\0' ; i++ ) {
		if (theField[i] != '\n')
			printf("%c", theField[i]);
	}
	
	/* print the left side again */
	printf("</%s>\n", leftSide);
}


int
split_line( char orig[], char fields[MAXFIELDS][MAXFLDLEN] )
/*
 * purpose: parse a line of colon-separated intms into an array of strings
 *    args: a line of colon-separted items and an array of char arrays
 *  method: loop through orig copying and copy to target array.  Change
 *          to next row at each colon
 * returns: number of fields found in line
 *    note: no error checking.  See what you can do to break it
 */
{
        int     src_pos,                /* position in source array     */
                cur_fld,                /* item in fields[] array       */
                dest_pos;               /* position in dest array       */

        src_pos = cur_fld = dest_pos = 0;

        while( 1 )
        {
                /* 
                 * if end of record, then terminate current string
                 * and advance to next row in fields[] table
                 */

                if ( orig[src_pos] == ':' || orig[src_pos] == '\0' )
                                         
                {
                        fields[cur_fld][dest_pos] = '\0';
                        cur_fld++;
                        dest_pos = 0;
                        if ( orig[src_pos] == '\0' )
                                break;
                }
                else

                /*
                 * not end of record, so copy the char from the source
                 * to the current char position in the current string
                 */
                {
                        fields[cur_fld][dest_pos] = orig[src_pos];
                        dest_pos++;
                }
                src_pos++;
        }
        return cur_fld;
}



outputXML()
/*
 * purpose: output XML version from the input 
 */
{
	int	i;					/* local variable */

	
	printf("    <LN> abrams</LN> \n");
	printf("    <FN> christopher</FN><CL>93</CL> \n");

}

