165 lines
5.7 KiB
C
Executable file
165 lines
5.7 KiB
C
Executable file
/* voronoi.c
|
|
*
|
|
* Created by Rory Healy (healyr@student.unimelb.edu.au)
|
|
* 12th August 2021
|
|
*
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "voronoi.h"
|
|
|
|
#define OPEN_FILE_ERROR "Error: Unable to open file %s\n"
|
|
#define NUM_FILE_ERROR "Error: Incorrect number of inputs (3 required).\n"
|
|
#define MEMORY_ALLOCATION_ERROR "Error: Cannot allocate memory.\n"
|
|
|
|
#define ARGC_CORRECT_LEN 4
|
|
#define NUM_CSV_FIELDS 6
|
|
#define MAX_FIELD_LEN 128
|
|
#define MAX_CSV_ENTRY_LEN 512
|
|
|
|
int main(int argc, char **argv) {
|
|
// /* Input and output files */
|
|
FILE *dataset = NULL, *polygonData = NULL, *output = NULL;
|
|
checkInputArgs(argc, argv, &dataset, &polygonData, &output);
|
|
|
|
/* Stores information about the watchtowers given in dataset file */
|
|
watchtower_t **watchtowers = malloc(sizeof(*watchtowers));
|
|
checkNullPointer(watchtowers);
|
|
int numWatchtowers = 0;
|
|
watchtowers = readWatchtowers(watchtowers, dataset, &numWatchtowers);
|
|
|
|
/* Cleaning up data */
|
|
for (int i = 0; i < numWatchtowers; i++) {
|
|
free(watchtowers[i]->id);
|
|
free(watchtowers[i]->manager);
|
|
free(watchtowers[i]->postcode);
|
|
free(watchtowers[i]);
|
|
}
|
|
|
|
free(watchtowers);
|
|
fclose(dataset);
|
|
fclose(polygonData);
|
|
fclose(output);
|
|
return 0;
|
|
}
|
|
|
|
/* Checks the validity of the command line input arguments */
|
|
void checkInputArgs(int argc, char **argv, FILE **datasetFile, \
|
|
FILE **polygonFile, FILE **outputFile) {
|
|
if (argc != ARGC_CORRECT_LEN) {
|
|
fputs(NUM_FILE_ERROR, stderr);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
*datasetFile = fopen(argv[1], "r");
|
|
if (*datasetFile == NULL) {
|
|
fprintf(stderr, OPEN_FILE_ERROR, argv[1]);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
*polygonFile = fopen(argv[2], "r");
|
|
if (*polygonFile == NULL) {
|
|
fprintf(stderr, OPEN_FILE_ERROR, argv[2]);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
*outputFile = fopen(argv[3], "w");
|
|
if (*outputFile == NULL) {
|
|
fprintf(stderr, OPEN_FILE_ERROR, argv[3]);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
/* Reads the CSV file and stores the information in the watchtowers array.
|
|
* Returns the number of watchtowers read.
|
|
*/
|
|
watchtower_t** readWatchtowers(watchtower_t **watchtowers, FILE* datasetFile, \
|
|
int *numWatchtowers) {
|
|
/* Maximum length of a single CSV line */
|
|
size_t lineBufferSize = MAX_CSV_ENTRY_LEN + 1;
|
|
|
|
/* Stores the current line from the CSV */
|
|
char *lineBuffer = (char *) malloc(lineBufferSize * sizeof(char));
|
|
checkNullPointer(lineBuffer);
|
|
|
|
int maxSizeWatchtowers = 1;
|
|
|
|
/* Discard the header line, then read the rest of the CSV */
|
|
getline(&lineBuffer, &lineBufferSize, datasetFile);
|
|
while (getline(&lineBuffer, &lineBufferSize, datasetFile) > 0) {
|
|
/* Check if there enough space in the watchtowers array */
|
|
if (*numWatchtowers == maxSizeWatchtowers) {
|
|
maxSizeWatchtowers *= 2;
|
|
watchtower_t **temp = realloc(watchtowers, \
|
|
maxSizeWatchtowers * sizeof(*watchtowers));
|
|
checkNullPointer(temp);
|
|
watchtowers = temp;
|
|
}
|
|
|
|
/* The current tower being filled in with information */
|
|
watchtowers[*numWatchtowers] = malloc(sizeof(*watchtowers[*numWatchtowers]));
|
|
|
|
/* Stores the current CSV field for the current line */
|
|
char *token = strtok(lineBuffer, ",");
|
|
size_t tokenLength;
|
|
|
|
/* Read the line in to currTower */
|
|
for (int i = 0; i < NUM_CSV_FIELDS; i++) {
|
|
tokenLength = strlen(token);
|
|
switch(i) {
|
|
/* Case 0, 1, and 3 deal with strings in the CSV
|
|
* Case 2 deals with an integer
|
|
* Case 4 and 5 deal with doubles
|
|
* Each case is handled seperately to fill in the
|
|
* watchtower with no space wasted.
|
|
*/
|
|
case 0:
|
|
watchtowers[*numWatchtowers]->id = (char *) malloc( \
|
|
sizeof(char) * tokenLength + 1);
|
|
checkNullPointer(watchtowers[*numWatchtowers]->id);
|
|
strcpy(watchtowers[*numWatchtowers]->id, token);
|
|
watchtowers[*numWatchtowers]->id[tokenLength] = '\0';
|
|
break;
|
|
case 1:
|
|
watchtowers[*numWatchtowers]->postcode = (char *) malloc( \
|
|
sizeof(char) * tokenLength + 1);
|
|
checkNullPointer(watchtowers[*numWatchtowers]->postcode);
|
|
strcpy(watchtowers[*numWatchtowers]->postcode, token);
|
|
watchtowers[*numWatchtowers]->postcode[tokenLength] = '\0';
|
|
break;
|
|
case 3:
|
|
watchtowers[*numWatchtowers]->manager = (char *) malloc( \
|
|
sizeof(char) * tokenLength + 1);
|
|
checkNullPointer(watchtowers[*numWatchtowers]->manager);
|
|
strcpy(watchtowers[*numWatchtowers]->manager, token);
|
|
watchtowers[*numWatchtowers]->manager[tokenLength] = '\0';
|
|
break;
|
|
case 2:
|
|
watchtowers[*numWatchtowers]->population = \
|
|
strtol(token, NULL, 10);
|
|
break;
|
|
case 4:
|
|
watchtowers[*numWatchtowers]->x = strtod(token, NULL);
|
|
break;
|
|
case 5:
|
|
watchtowers[*numWatchtowers]->y = strtod(token, NULL);
|
|
break;
|
|
}
|
|
token = strtok(NULL, ",");
|
|
}
|
|
|
|
*numWatchtowers += 1;
|
|
}
|
|
|
|
free(lineBuffer);
|
|
return watchtowers;
|
|
}
|
|
|
|
void checkNullPointer(void *ptr) {
|
|
if (!ptr) {
|
|
fputs(MEMORY_ALLOCATION_ERROR, stderr);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|