Public feed #721296

Something

public text v1 · immutable
Raw Edit
·published 2007-10-01 03:47 UTC
rendered paste body
/* Copyright 2007 - Paul Greidanus <paul.greidanus@ualberta.ca>
 *
 * Permission is granted solely for grading purposes by University of Alberta
 * staff or students only.  Permission is not granted for modification, copying,
 * or distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

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

/* Define constants for the max sizes of things */
#define MAX_FIG 16 /* Max number of figures allowed */
#define MAX_FIG_SEG 16 /* Max number of segments per figure */
#define MAX_FIG_NAME 128 /* Max size of the figure names */
#define MAX_LINE_LENGTH 256 /* Maximum size of input lines */
#define PI 3.141592653589793 /* Pi.. just a number */

/* If -DDEBUG=1 set at compile time, set debug to 1, else set it to 0
 * May not be ideal, but it is sufficient for the time being
 * maybe add command line -debug parser?
 */
#if defined (DEBUG)
#define DEBUG 1
#else
#define DEBUG 0
#endif

/* Define the structure of the figure data */
struct figStruct
                {
                char* name[MAX_FIG_NAME];
                int segments[MAX_FIG_SEG][2];
                }; 

/* Function to parse input file into internal data structure 
 * takes filename as input, and outputs int (for now)
 */

int parseToken ( char* tokenline, double* cur_y, double* cur_x, double* direction, int* cur_fig, struct figStruct *figure) {
        char* operator = strtok(tokenline," ");
        int index=0;

        if ( ! strncmp(operator,"Figure", MAX_LINE_LENGTH) ) {
                for ( index=0; index < MAX_FIG; index++ ) {
                        if ( ! *figure[index].name ) {
                                strncpy(*figure[index].name,strtok(NULL, " "), MAX_LINE_LENGTH);
                                *cur_fig = index;
                                printf(" --- %i %i %s\n", index, *cur_fig, *figure[index].name );
                                return 0;
                                }
                        }
                return -1;
                }

        if ( ! strncmp(operator,"start", MAX_LINE_LENGTH) ) {
                /* We have a start of a figure, set cur_x and cur_y */
                 /* Check that we have an active figure */
                printf("In operator loop\n");
                return 0;
                }
        return 1;
        }

int parseInputFile ( char * filename, struct figStruct * figure ) {

        char buf[MAX_LINE_LENGTH]; /* buffer to read file into */
        FILE *fh; /* File handle for read */
        int curfig = 0; /* Current figure that we are looking at set to 0 */
        double cur_x, cur_y, direction;
        int index = 0;

        if ((fh = fopen(filename, "r")) == NULL) {
        fprintf(stderr, "Cannot open %s\n", filename);
                }

        /* Walk through the file one line at a time and populate the struct */
        while( fgets(buf, MAX_LINE_LENGTH ,fh) ) {
                parseToken (buf, &cur_x, &cur_y, &direction, &curfig, figure);
                /* printf("My curent figure is %i\n", curfig); */
        }
        fclose(fh);

                printf(" -- %s\n", *figure[0].name);
        for ( index=0; index < MAX_FIG; index++ ) {
                printf("Fig %i name :  %s\n", index, *figure[index].name);
                }

        return 0;
}

/* Main function */

int main(int argc, char** argv)
{
        struct figStruct figure[MAX_FIG];
        int index = 0;

        /* If we have no CLI options, we die */
        if ( argc <= 1 ) {
                printf("ERROR: No Command line arguments supplied, exiting\n");
                return 1;
        }

        /* Let's grab the filename off of the command line */
        if ( DEBUG ) {
                printf("DEBUG: Opening %s\n", argv[1]);
        }
        /* Init the struct */
        for ( index=0; index < MAX_FIG; index++ ) {
                *figure[index].name = 0;
                }

        /* Read the file into the data structure */
        parseInputFile(argv[1], figure);

        /* Return 0 to the OS telling it that things are done and successful */
        return (0);
}