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

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


// my Library

void initArray(char mArray[], int arraySize)
{
    //
    // ensure all values in array are set to '\0'

    int i;
    
    for (i = 0;  i <= arraySize-1 ; i++) {
        mArray[i] = '\0';
    }
}

void trimWhiteSpace(char source[])
{
    //
    // remove all leading and trailing whitespace (including '\n')
    
    int  i;
    char target[BUFLEN];
    
    // strip trailing whitespace
    for (i = strlen(source)-1; isspace((int)source[i]) && i >= 0 ; i-- ) {
        source[i] = '\0';
    }

    // count leading whitespace
    for (i = 0; isspace((int)source[i]) ; i++) {
    }

    // copy into target
    strcpy(target, &source[i]);

    // copy back to source
    strcpy(source, target);
}


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


void printCharArray(char mArray[], int arraySize) {
    //
    // print contents of array
    //
    // if arraySize = -1 then array terminated
    // with '\0' (string)
    
    int i;

    printf("[");

    // if is string. (p.s. could use strlen!)
    if (arraySize == -1) {
        for (arraySize = 0; mArray[arraySize] != '\0'; arraySize++) {
        }
    }
        
    for (i = 0; i <= arraySize ; i++) {
        
        if (mArray[i] == '\0')
            printf(" 0 ");
        else
            printf("%c", mArray[i]);
        
        if (i < arraySize)
            printf(",");
    }

    printf("]");

}

