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

/*
 *  q5.c
 *
 *	convert input file and output as:    class, last name, first name 
 *
 */ 

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

main()
{

	char    line[LINESIZE];                          /* an array of characters */
	char    record[MAXFIELDS][MAXFLDLEN];            /* the record */
	char    leftSide[LINESIZE], rightSide[LINESIZE]; /* strings for holding left and right */
	char 	LN[LINESIZE],FN[LINESIZE],CL[LINESIZE];  /* strings for holding parameters */
			
	int  	i, number_of_fields;

        /* loop through each line of data */ 
        while ( fgets( line, LINESIZE, stdin) != NULL )
        {		
		/* split line into record */
		number_of_fields = split_line( line, record );

		/* clear record strings */
		LN[0] = '\0';
		FN[0] = '\0';
		CL[0] = '\0';

		
		/* loop through each record */
		for ( i = 0 ; i < number_of_fields ; i++ ) {
			get_left(record[i], leftSide);      /* put left portion into str leftSide   */
			get_right(record[i], rightSide);    /* put right portion into str rightSide */
			
			/* assign one of the three strings */
			if (strcmp(leftSide, "LN") == 0)
				 strcpy(LN, rightSide);
			else if (strcmp(leftSide, "FN") == 0)
				 strcpy(FN, rightSide);
			else if (strcmp(leftSide, "CL") == 0)
				 strcpy(CL, rightSide);
		}

		/* print student record */
		printf("'%s %s, %s \n", CL, LN, FN);

        }
}


int
find_equals( char theField[])
/*
 *
 * find position of the '='
 *
 */
{
	int i;

	/* loop til '=' */
	for ( i = 0 ; theField[i] != '=' ; i++ ) {
	}
	
	return i;
}

get_left( char theField[], char leftSide[])
/*
 *
 * break field into leftside
 *
 */

{
	int  equalPos;

	/* copy left part into string and append null */
	equalPos = find_equals(theField);
	strncpy(leftSide, theField, equalPos);
 	leftSide[equalPos] = '\0';
	
}


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

{
	int  i, j, equalPos;
	
	i = find_equals(theField) + 1;
	
	/* save the right side (sans \n) */
	j = 0;
	for ( ; theField[i] != '\0' ; i++ ) {
		if (theField[i] != '\n')
			rightSide[j] = theField[i];
		j++;
	}
	rightSide[j] = '\0';
}

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;
}


