From 80e980358f1e98efa87b4611a8590ffad9eb7423 Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Tue, 1 Oct 2024 21:37:24 -0700 Subject: [PATCH] create basic students to json file function & clean course_selection_data.csv --- main.c | 48 +++++++++++++++++++++++++-- sample_data/course_selection_data.csv | 8 ++--- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 649a906..f1788e9 100644 --- a/main.c +++ b/main.c @@ -38,7 +38,7 @@ typedef struct { int count_lines(char data_dir[]) { FILE *stream = fopen(data_dir, "r"); if (!stream) { - fputs("Failed to open CSV file", stderr); + fputs("Failed to open CSV file\n", stderr); return -1; } char buff[MAX_CHAR]; @@ -54,7 +54,7 @@ CSV_LINE *csvReader(char data_dir[], size_t size) { FILE *stream = fopen(data_dir, "r"); if (!stream) { - fputs("Failed to open CSV file", stderr); + fputs("Failed to open CSV file\n", stderr); return NULL; } @@ -250,6 +250,48 @@ COURSE *getCourses(CSV_LINE *lines, size_t lines_len, UNIQUE_COURSES unique_cour return courses; } +/*** DEFINE JSON FUNCTION HANDLERS ***/ + +int writeStudentsToJson(STUDENT *students, size_t numberOfStudents, char output_dir[]) { + FILE *stream = fopen(output_dir, "w"); + if (!stream) { + fputs("Failed to open students output file", stderr); + return -1; + } + + /* + TODO: expand this function to handle schedule and remainingAlts when data is provided + */ + + fprintf(stream, "[\n"); + + for (size_t i = 0; i < numberOfStudents; i++) { + fprintf(stream, " {\n"); + fprintf(stream, " \"pupilNum\": %d,\n", students[i].pupilNum); + fprintf(stream, " \"requests\": [\n"); + + for (size_t j = 0; j < students[i].requestsLen; j++) { + fprintf(stream, " {\n"); + fprintf(stream, " \"crsNo\": \"%s\",\n", students[i].requests[j].crsNo); + fprintf(stream, " \"description\": \"%s\",\n", students[i].requests[j].description); + fprintf(stream, " \"alternate\": %s\n", students[i].requests[j].alternate ? "true" : "false"); + fprintf(stream, " }%s\n", j == students[i].requestsLen - 1 ? "" : ","); + } + fprintf(stream, " ],\n"); + + fprintf(stream, " \"schedule\": [],\n"); + fprintf(stream, " \"expectedClasses\": %d,\n", students[i].expectedClasses); + fprintf(stream, " \"classes\": %d,\n", students[i].classes); + fprintf(stream, " \"remainingAlts\": [],\n"); + fprintf(stream, " \"grade\": %d\n", students[i].grade); + fprintf(stream, " }%s\n", i == numberOfStudents - 1 ? "" : ","); + } + + fprintf(stream, "]"); + fclose(stream); + return 0; +} + int main(int argc, char **argv) { /* @@ -287,7 +329,7 @@ int main(int argc, char **argv) { - start algorithm */ - // if (writeStudentsToJson(students, students_info.numberOfStudents, "output/students.json") == -1) return -1; + if (writeStudentsToJson(students, students_info.numberOfStudents, "output/students.json") == -1) return -1; // if (writeCoursesToJson(courses, courses_info.numberOfCourses) == -1) return -1; // Algorithm here diff --git a/sample_data/course_selection_data.csv b/sample_data/course_selection_data.csv index 21bc22b..9fb23ea 100644 --- a/sample_data/course_selection_data.csv +++ b/sample_data/course_selection_data.csv @@ -1308,8 +1308,8 @@ 1203410,MTEW-10--S,WOODWORK 10,FALSE 1203410,XAT--12A-S,FLEX SENIOR SEMESTER 1,FALSE 1203410,XAT--12B-S,FLEX SENIOR SEMESTER 2,FALSE -1203410,XSIEP2C--S,"LSB SR ACADEMIC SUPPORT 1, SEMESTERED",FALSE -1203410,XSIEP2D--S,"LSB SR ACADEMIC SUPPORT 2, SEMESTERED",FALSE +1203410,XSIEP2C--S,LSB SR ACADEMIC SUPPORT 1 SEMESTERED,FALSE +1203410,XSIEP2D--S,LSB SR ACADEMIC SUPPORT 2 SEMESTERED,FALSE 1203410,MVAM-10--S,MEDIA ARTS 10,TRUE 1219043,MCH--11--S,CHEMISTRY 11,FALSE 1219043,MEN--11--S-COMP,ENGLISH 11 COMPOSITION,FALSE @@ -7283,8 +7283,8 @@ 5665836,MTEW-10--S,WOODWORK 10,FALSE 5665836,XAT--12A-S,FLEX SENIOR SEMESTER 1,FALSE 5665836,XAT--12B-S,FLEX SENIOR SEMESTER 2,FALSE -5665836,XSIEP2C--S,"LSB SR ACADEMIC SUPPORT 1, SEMESTERED",FALSE -5665836,XSIEP2D--S,"LSB SR ACADEMIC SUPPORT 2, SEMESTERED",FALSE +5665836,XSIEP2C--S,LSB SR ACADEMIC SUPPORT 1, SEMESTERED,FALSE +5665836,XSIEP2D--S,LSB SR ACADEMIC SUPPORT 2, SEMESTERED,FALSE 5665836,MFDN-10--S,FOODS AND NUTRITION 10,TRUE 5666390,MFR--08--Y,FRENCH 08,FALSE 5666390,MMA--08--Y,MATHEMATICS 08,FALSE