/* Brian O'Connor * * input.c: file to encapsulate file input routines. */ #include #include #include "input.h" #include "pursuer.h" #include "cell.h" static FILE *OpenInputFile(char *filename); static Obstacle *ReadOneObstacle(FILE *file); static void ReadObstacles(FILE *file, WorldInfo *wInfo); void ReadInputFile( char *filename, WorldInfo *wInfo ) { int ch, i; FILE *file; if( (file = OpenInputFile(filename)) == NULL) return; wInfo->obs = new (Obstacle *)[MAX_OBS]; assert(wInfo->obs != NULL); wInfo->border = NULL; wInfo->robot = NULL; wInfo->nObs = 0; ch ='0'; while( ch != EOF ) { if( (ch = getc(file)) == 'O' ) { while( (ch = getc(file)) != '\n'); ReadObstacles(file, wInfo); } else if( ch == 'B' ) { while( (ch = getc(file)) != '\n'); wInfo->border = ReadOneObstacle(file); } else if( ch == 'R' ) { /* read in a robot as if it was an obstacle */ while( (ch = getc(file)) != '\n'); wInfo->robot = ReadOneObstacle(file); } } } /* for now, this function is making the simplifying assumption that * all obstacles occur sequentially and at the start of the file. */ /* assumed format: #vertexes * x1 y1 * x2 y2 etc.... */ static Obstacle *ReadOneObstacle(FILE *file) { int i; char buffer[160]; float x, y; Obstacle *ob; ob = new Obstacle; /* how many points do we have?*/ fgets(buffer, 80, file); sscanf(buffer, "%d", &(ob->nPts)); ob->pts = new Point[ob->nPts]; assert(ob->pts != NULL); for( i = 0; i < ob->nPts; i++ ) { fgets(buffer, 80, file); sscanf(buffer, "%f %f", &x, &y); ob->pts[i].x = x; ob->pts[i].y = y; ob->pts[i].w = 1; /* printf("point: %f, %f\n", ob->pts[i].x, ob->pts[i].y); */ } return(ob); } static FILE *OpenInputFile(char *filename) { FILE *newfile; newfile = fopen(filename, "r"); if( newfile == NULL ) { printf("Input file %s does not exist\n", filename); exit(0); } return( newfile ); } static void ReadObstacles(FILE *file, WorldInfo *wInfo) { int i = 0; char ch; while( (ch = getc(file)) != EOF && ch != '\n') { ungetc(ch, file); wInfo->obs[i++] = ReadOneObstacle(file); assert( i < MAX_OBS); } wInfo->nObs = i; }