comp20003-project02/input.c
2021-09-15 00:06:04 +10:00

92 lines
3 KiB
C

/* input.c
*
* Created by Rory Healy (healyr@student.unimelb.edu.au)
* Created on 8th September 2021
* Last modified 14th September 2021
*
* Contains functions for ensuring correct input arguments are given for
* each stage of the voronoi2 program. Adapted from Grady's base code on Ed.
*
*/
#ifndef INPUT_HEADER
#include "input.h"
#endif
#define STAGE_1_ARG_COUNT 4
#define STAGE_2_ARG_COUNT 5
#define STAGE_3_ARG_COUNT 5
#define STAGE_4_ARG_COUNT 5
enum stages getStage(char *stage) {
if (strcmp(stage, "1") == 0){
return STAGE_1;
} else if (strcmp(stage, "2") == 0){
return STAGE_2;
} else if (strcmp(stage, "3") == 0){
return STAGE_3;
} else if (strcmp(stage, "4") == 0){
return STAGE_4;
} else {
return STAGE_ERROR;
}
}
void printArgError(enum stages stage, char *errorMessage) {
fprintf(stderr, "Error: %s\n\n", errorMessage);
/* Stage 1 - Print bisector equations */
if (stage == STAGE_ERROR || stage == STAGE_1) {
fputs("./voronoi2 1 pointsFile outputFile\n"
"\tTo output the bisectors for the given pointsFile to the \n"
"\tgiven outputFile.\n\n", stderr);
}
/* Stage 2 - Print intersections between bisectors and polygon */
if (stage == STAGE_ERROR || stage == STAGE_2) {
fputs("./voronoi2 2 pointsFile polygonFile outputFile\n"
"\tTo output the intersections the given bisectors make with \n"
"\tthe polygon to the given outputFile.\n\n", stderr);
}
/* Stage 3: Print diameter of each face, constructing using
* incremental method
*/
if (stage == STAGE_ERROR || stage == STAGE_3) {
fputs("./voronoi2 3 dataFile polygonFile outputFile\n"
"\tTo generate the initial polygon in polygonFile, layout the \n"
"\twatchtower points in the dataFile on the polygon, generate \n"
"\the voronoi diagram, adding or modifying the polydon so that \n"
"\ta voronoi diagram is constructed, seperating all points \n"
"\tinto their own cell with all tpoints in the cell being \n"
"\tclosest to the watchtower in the cell. The watchtower data \n"
"\tand its diameter is output to the outputFile.\n\n", stderr);
}
/* Stage 4 - Sort by diameter */
if (stage == STAGE_ERROR || stage == STAGE_4) {
fputs("./voronoi2 4 dataFile polygonFile outputFile\n"
"\tSame as stage 3, but output watchtowers are in order of \n"
"\tdiameter, smallest to largest.\n\n", stderr);
}
exit(EXIT_FAILURE);
}
int getExpectedArgs(enum stages stage) {
switch(stage){
case STAGE_1:
return STAGE_1_ARG_COUNT;
case STAGE_2:
return STAGE_2_ARG_COUNT;
case STAGE_3:
return STAGE_3_ARG_COUNT;
case STAGE_4:
return STAGE_4_ARG_COUNT;
case STAGE_ERROR:
default:
printArgError(stage, "Invalid stage number.");
exit(EXIT_FAILURE);
}
return -1;
}