/* common.c * * Created by Rory Healy (healyr@student.unimelb.edu.au) * Created on 25th August 2021 * Last modified 25th August 2021 * * Contains functions for general use throughout other files. * */ #ifndef COMMON_HEADER #include "common.h" #endif #define ARGC_CORRECT_LEN 4 #define MEMORY_ALLOCATION_ERROR "Error: Cannot allocate memory.\n" #define OPEN_FILE_ERROR "Error: Unable to open file %s\n" #define NUM_FILE_ERROR "Error: Incorrect number of inputs (3 required).\n" /* Checks if a given pointer is null. Used for malloc() and realloc(). */ void checkNullPointer(void *ptr) { if (!ptr) { fputs(MEMORY_ALLOCATION_ERROR, stderr); exit(EXIT_FAILURE); } } /* 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); } }