fix csvReader defining CSV_LINE values

This commit is contained in:
SowinskiBraeden committed 2024-09-29 18:39:53 -07:00
1 parent f9aea0df76
commit 7980d56a5c
1 file changed
+49 -31
+49 -31
View File
@@ -9,7 +9,7 @@
*/ */
#define MAX_COURSE_DES_LEN 50 #define MAX_COURSE_DES_LEN 50
#define MAX_COURSE_NO_LEN 20 #define MAX_COURSE_NO_LEN 20
#define MAX_PUPIL_NUM_LEN 7 #define MAX_PUPIL_NUM_LEN 8
#define TOTAL_BLOCKS 10 // the number of blocks between 2 semesters i.e 8 = 4 blocks per semester, 10 = 5 blocks per semester #define TOTAL_BLOCKS 10 // the number of blocks between 2 semesters i.e 8 = 4 blocks per semester, 10 = 5 blocks per semester
#define MAX_REQUEST_ALTS 6 #define MAX_REQUEST_ALTS 6
@@ -28,19 +28,10 @@ typedef struct {
bool alternate; bool alternate;
} CSV_LINE; } CSV_LINE;
const char* getField(char *line, int num) { size_t count_lines(char data_dir[]) {
const char* tok;
for (tok = strtok(line, ","); tok && *tok; tok = strtok(NULL, ",\n")) {
if (!--num)
return tok;
}
return NULL;
}
int count_lines(char data_dir[]) {
FILE *stream = fopen(data_dir, "r"); FILE *stream = fopen(data_dir, "r");
char buff[MAX_CHAR]; char buff[MAX_CHAR];
int counter = 0; size_t counter = 0;
for (;;) { for (;;) {
size_t res = fread(buff, 1, MAX_CHAR, stream); size_t res = fread(buff, 1, MAX_CHAR, stream);
if (ferror(stream)) if (ferror(stream))
@@ -55,36 +46,53 @@ int count_lines(char data_dir[]) {
break; break;
} }
fclose(stream);
return counter; return counter;
} }
CSV_LINE *csvReader(char data_dir[]) { CSV_LINE *csvReader(char data_dir[], size_t size) {
int num_lines = count_lines(data_dir) + 1;
if (num_lines == -1) {
fputs("Failed to read number of lines in the given CSV file!\n", stderr);
return NULL;
}
FILE *stream = fopen(data_dir, "r"); FILE *stream = fopen(data_dir, "r");
if (!stream) {
CSV_LINE *lines = malloc(num_lines * sizeof(CSV_LINE)); fputs("Failed to open CSV file", stderr);
if (lines == NULL) {
fputs("Failed to allocate memory!\n", stderr);
return NULL; return NULL;
} }
int i = 0; CSV_LINE *lines = malloc(size * sizeof(CSV_LINE));
char buff[MAX_CHAR]; char buff[MAX_CHAR];
while (fgets(buff, sizeof(buff), stream) != NULL) { int i = 0;
// Read each line in CSV file
while (fgets(buff, sizeof(buff), stream) && i < size) {
CSV_LINE csv_line; CSV_LINE csv_line;
strcpy(csv_line.pupilNum, getField(strdup(buff), 1));
strcpy(csv_line.crsNo, getField(strdup(buff), 2));
strcpy(csv_line.description, getField(strdup(buff), 3));
csv_line.alternate = strcmp(getField(strdup(buff), 4), "TRUE") ? true : false;
lines[i] = csv_line; lines[i] = csv_line;
// Tokenize the line to extract data
char *token = strtok(buff, ",");
if (token) {
strncpy(lines[i].pupilNum, token, MAX_PUPIL_NUM_LEN);
// lines[i].pupilNum[strcspn(lines[i].pupilNum, "\n")] = 0; // Remove newline
}
token = strtok(NULL, ",");
if (token) strncpy(lines[i].crsNo, token, MAX_COURSE_NO_LEN);
token = strtok(NULL, ",");
if (token) strncpy(lines[i].description, token, MAX_COURSE_DES_LEN);
token = strtok(NULL, ",");
if (token) {
char alternate[5];
strncpy(alternate, token, 5);
alternate[strcspn(alternate, "\n")] = 0; // Remove newline
lines[i].alternate = strcmp(alternate, "TRUE") == 0 ? true : false;
}
i++; i++;
} }
fclose(stream);
return lines; return lines;
} }
@@ -117,8 +125,11 @@ typedef struct {
/*** DEFINE GET FUNCTIONS FOR STUDENTS & COURSES FROM CSV ***/ /*** DEFINE GET FUNCTIONS FOR STUDENTS & COURSES FROM CSV ***/
STUDENT *getStudents(CSV_LINE *lines, int total_blocks) { STUDENT *getStudents(CSV_LINE *lines, size_t lines_len, int total_blocks) {
return NULL; return NULL;
// for (size_t i = 0; i < lines_len; i++) {
// }
} }
COURSE *getCourses(CSV_LINE *lines) { COURSE *getCourses(CSV_LINE *lines) {
@@ -133,10 +144,17 @@ int main(int argc, char **argv) {
and an array of course structs and an array of course structs
*/ */
char data_dir[] = "sample_data/course_selection_data.csv"; char data_dir[] = "sample_data/course_selection_data.csv";
CSV_LINE *lines = csvReader(data_dir);
size_t num_lines = count_lines(data_dir);
if (num_lines == -1) {
fputs("Failed to read number of lines in the given CSV file!\n", stderr);
return -1;
}
CSV_LINE *lines = csvReader(data_dir, num_lines);
if (lines == NULL) return -1; if (lines == NULL) return -1;
STUDENT *students = getStudents(lines, TOTAL_BLOCKS); STUDENT *students = getStudents(lines, num_lines, TOTAL_BLOCKS);
if (students == NULL) return -1; if (students == NULL) return -1;
free(lines); free(lines);