comp20003-project01/voronoi.c

84 lines
2.3 KiB
C

/* 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 MALLOC_ERROR "Error: Cannot allocate memory.\n"
int main(int argc, char **argv) {
FILE *dataset = NULL, *polygonData = NULL, *output = NULL;
checkInputArgs(argc, argv, &dataset, &polygonData, &output);
watchtower_t *watchtowers = (watchtower_t *) malloc(sizeof(watchtower_t));
if (watchtowers == NULL) {
fputs(MALLOC_ERROR, stderr);
exit(EXIT_FAILURE);
}
readWatchtowers(watchtowers, dataset);
// Cleaning up data
free(watchtowers);
fclose(dataset);
fclose(polygonData);
fclose(output);
return 0;
}
void checkInputArgs(int argc, char **argv, FILE **datasetFile, \
FILE **polygonFile, FILE **outputFile) {
if (argc != 4) {
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);
}
}
void readWatchtowers(watchtower_t *watchtowers, FILE* datasetFile) {
char *lineBuffer;
int lineCount = 0;
size_t lineBufferSize = 512;
size_t numberCharsRead;
lineBuffer = (char *) malloc(lineBufferSize * sizeof(char));
if (lineBuffer == NULL) {
fprintf(stderr, MALLOC_ERROR);
exit(EXIT_FAILURE);
}
numberCharsRead = getline(&lineBuffer, &lineBufferSize, datasetFile);
while (numberCharsRead > 0) {
lineCount += 1;
printf("line[%06d]: chars=%06zd, buf size=%06zu, contents: %s",\
lineCount, numberCharsRead, lineBufferSize, lineBuffer);
numberCharsRead = getline(&lineBuffer, &lineBufferSize, datasetFile);
}
free(lineBuffer);
lineBuffer = NULL;
}