changed to linux, added basic reading of datasets
This commit is contained in:
parent
69dd495ac0
commit
c5e2c25789
20 changed files with 84 additions and 18 deletions
0
Makefile
Normal file → Executable file
0
Makefile
Normal file → Executable file
0
README.md
Normal file → Executable file
0
README.md
Normal file → Executable file
0
dataset_1.csv
Normal file → Executable file
0
dataset_1.csv
Normal file → Executable file
0
dataset_10.csv
Normal file → Executable file
0
dataset_10.csv
Normal file → Executable file
0
dataset_100.csv
Normal file → Executable file
0
dataset_100.csv
Normal file → Executable file
0
dataset_2.csv
Normal file → Executable file
0
dataset_2.csv
Normal file → Executable file
0
dataset_full.csv
Normal file → Executable file
0
dataset_full.csv
Normal file → Executable file
0
output.txt
Normal file → Executable file
0
output.txt
Normal file → Executable file
0
poly_1split.txt
Normal file → Executable file
0
poly_1split.txt
Normal file → Executable file
0
poly_2split.txt
Normal file → Executable file
0
poly_2split.txt
Normal file → Executable file
0
poly_3split.txt
Normal file → Executable file
0
poly_3split.txt
Normal file → Executable file
0
poly_4split.txt
Normal file → Executable file
0
poly_4split.txt
Normal file → Executable file
0
poly_5split.txt
Normal file → Executable file
0
poly_5split.txt
Normal file → Executable file
0
polygon_irregular.txt
Normal file → Executable file
0
polygon_irregular.txt
Normal file → Executable file
0
polygon_square.txt
Normal file → Executable file
0
polygon_square.txt
Normal file → Executable file
0
square_1split.txt
Normal file → Executable file
0
square_1split.txt
Normal file → Executable file
0
square_2split.txt
Normal file → Executable file
0
square_2split.txt
Normal file → Executable file
94
voronoi.c
Normal file → Executable file
94
voronoi.c
Normal file → Executable file
|
@ -14,10 +14,17 @@
|
||||||
#define NUM_FILE_ERROR "Error: Incorrect number of inputs (3 required).\n"
|
#define NUM_FILE_ERROR "Error: Incorrect number of inputs (3 required).\n"
|
||||||
#define MALLOC_ERROR "Error: Cannot allocate memory.\n"
|
#define MALLOC_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) {
|
int main(int argc, char **argv) {
|
||||||
|
/* Input and output files */
|
||||||
FILE *dataset = NULL, *polygonData = NULL, *output = NULL;
|
FILE *dataset = NULL, *polygonData = NULL, *output = NULL;
|
||||||
checkInputArgs(argc, argv, &dataset, &polygonData, &output);
|
checkInputArgs(argc, argv, &dataset, &polygonData, &output);
|
||||||
|
|
||||||
|
/* Stores information about the watchtowers given in dataset file */
|
||||||
watchtower_t *watchtowers = (watchtower_t *) malloc(sizeof(watchtower_t));
|
watchtower_t *watchtowers = (watchtower_t *) malloc(sizeof(watchtower_t));
|
||||||
if (watchtowers == NULL) {
|
if (watchtowers == NULL) {
|
||||||
fputs(MALLOC_ERROR, stderr);
|
fputs(MALLOC_ERROR, stderr);
|
||||||
|
@ -26,7 +33,7 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
readWatchtowers(watchtowers, dataset);
|
readWatchtowers(watchtowers, dataset);
|
||||||
|
|
||||||
// Cleaning up data
|
/* Cleaning up data */
|
||||||
free(watchtowers);
|
free(watchtowers);
|
||||||
fclose(dataset);
|
fclose(dataset);
|
||||||
fclose(polygonData);
|
fclose(polygonData);
|
||||||
|
@ -34,9 +41,10 @@ int main(int argc, char **argv) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Checks the validity of the command line input arguments */
|
||||||
void checkInputArgs(int argc, char **argv, FILE **datasetFile, \
|
void checkInputArgs(int argc, char **argv, FILE **datasetFile, \
|
||||||
FILE **polygonFile, FILE **outputFile) {
|
FILE **polygonFile, FILE **outputFile) {
|
||||||
if (argc != 4) {
|
if (argc != ARGC_CORRECT_LEN) {
|
||||||
fputs(NUM_FILE_ERROR, stderr);
|
fputs(NUM_FILE_ERROR, stderr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -60,25 +68,83 @@ FILE **polygonFile, FILE **outputFile) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Reads the CSV file and stores the information in the watchtowers array */
|
||||||
void readWatchtowers(watchtower_t *watchtowers, FILE* datasetFile) {
|
void readWatchtowers(watchtower_t *watchtowers, FILE* datasetFile) {
|
||||||
char *lineBuffer;
|
/* Maximum length of a single CSV line */
|
||||||
int lineCount = 0;
|
size_t lineBufferSize = MAX_CSV_ENTRY_LEN;
|
||||||
size_t lineBufferSize = 512;
|
|
||||||
size_t numberCharsRead;
|
|
||||||
|
|
||||||
lineBuffer = (char *) malloc(lineBufferSize * sizeof(char));
|
/* Stores the current line from the CSV */
|
||||||
|
char *lineBuffer = (char *) malloc(lineBufferSize * sizeof(char));
|
||||||
if (lineBuffer == NULL) {
|
if (lineBuffer == NULL) {
|
||||||
fprintf(stderr, MALLOC_ERROR);
|
fprintf(stderr, MALLOC_ERROR);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
numberCharsRead = getline(&lineBuffer, &lineBufferSize, datasetFile);
|
int numWatchtowers = 1;
|
||||||
while (numberCharsRead > 0) {
|
|
||||||
lineCount += 1;
|
/* Discard the header line, then read the rest of the CSV */
|
||||||
printf("line[%06d]: chars=%06zd, buf size=%06zu, contents: %s",\
|
getline(&lineBuffer, &lineBufferSize, datasetFile);
|
||||||
lineCount, numberCharsRead, lineBufferSize, lineBuffer);
|
while (getline(&lineBuffer, &lineBufferSize, datasetFile) > 0) {
|
||||||
numberCharsRead = getline(&lineBuffer, &lineBufferSize, datasetFile);
|
/* The current tower being filled in with information */
|
||||||
|
watchtower_t *currTower = (watchtower_t *) malloc(sizeof(watchtower_t));
|
||||||
|
|
||||||
|
/* 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:
|
||||||
|
currTower->id = (char *) malloc(sizeof(char) * \
|
||||||
|
tokenLength + 1);
|
||||||
|
strcpy(currTower->id, token);
|
||||||
|
currTower->id[tokenLength] = '\0';
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
currTower->postcode = (char *) malloc(sizeof(char) * \
|
||||||
|
tokenLength + 1);
|
||||||
|
strcpy(currTower->postcode, token);
|
||||||
|
currTower->postcode[tokenLength] = '\0';
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
currTower->manager = (char *) malloc(sizeof(char) * \
|
||||||
|
tokenLength + 1);
|
||||||
|
strcpy(currTower->manager, token);
|
||||||
|
currTower->manager[tokenLength] = '\0';
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
currTower->population = strtol(token, NULL, 10);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
currTower->x = strtod(token, NULL);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
currTower->y = strtod(token, NULL);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
token = strtok(NULL, ",");
|
||||||
|
}
|
||||||
|
// printf("ID: %s\nPostcode: %s\nManager: %s\n", \
|
||||||
|
// currTower->id, currTower->postcode, currTower->manager);
|
||||||
|
// printf("Population: %d\nx: %f\ny: %f\n", \
|
||||||
|
// currTower->population, currTower->x, currTower->y);
|
||||||
|
|
||||||
|
/* Add currTower to the watchtowers array */
|
||||||
|
|
||||||
|
|
||||||
|
/* Clean up currTower before reading next CSV line */
|
||||||
|
free(currTower->id);
|
||||||
|
free(currTower->manager);
|
||||||
|
free(currTower->postcode);
|
||||||
|
free(currTower);
|
||||||
}
|
}
|
||||||
free(lineBuffer);
|
free(lineBuffer);
|
||||||
lineBuffer = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
2
voronoi.h
Normal file → Executable file
2
voronoi.h
Normal file → Executable file
|
@ -1,4 +1,4 @@
|
||||||
typedef struct {
|
typedef struct watchtower {
|
||||||
char *id;
|
char *id;
|
||||||
char *postcode;
|
char *postcode;
|
||||||
char *manager;
|
char *manager;
|
||||||
|
|
BIN
voronoi1
BIN
voronoi1
Binary file not shown.
Loading…
Reference in a new issue