~learax/csci112-2021-william-culhane

9999e1b7109d014cb362734ba0dfb5cc255398f7 — William Culhane 3 years ago e928d8a
program2: Write CSV parser
1 files changed, 65 insertions(+), 0 deletions(-)

M programs/program2/program2.c
M programs/program2/program2.c => programs/program2/program2.c +65 -0
@@ 1,5 1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_COURSES 30

/*
 * William Culhane


@@ 7,7 10,69 @@
 * Sun May 30 10:27:49 PM MDT 2021
 */

typedef struct Courses {
  char code[10];
  char name[30];
  unsigned int mystery; // TODO What is this? An ID?
  unsigned int seats_total, seats_taken, seats_open;
  char professor[25];
  char weekdays[8];
  char time[10];
} course_t;

// Construct the array of course structs by parsing the csv file
int parse_csv(const char filename[], course_t courses[MAX_COURSES]) {
  FILE *csv = fopen(filename, "r");
  int max_index; // Maximum course index (i.e. # of courses - 1)
  char buf[150];

  {
    int i = 0; // Maximum index tracker
    while (fgets(buf, 150, csv) != NULL) {
      // Basic information
      strcpy(courses[i].code, strtok(buf, ","));
      strcpy(courses[i].name, strtok(NULL, ","));

      // A magical mystery
      courses[i].mystery = atoi(strtok(NULL, ","));

      // Seats information
      courses[i].seats_total = atoi(strtok(NULL, ","));
      courses[i].seats_taken = atoi(strtok(NULL, ","));
      courses[i].seats_open = atoi(strtok(NULL, ","));

      // Misc
      strcpy(courses[i].professor, strtok(NULL, ","));

      // Parse the schedule descriptor
      strcpy(courses[i].weekdays, strtok(NULL, " "));
      strcpy(courses[i].time, strtok(NULL, " "));

      i += 1;
    }
    max_index = i;
  }

  fclose(csv);
  return (max_index);
}

int main(void) {
  unsigned char choice;
  course_t courses[MAX_COURSES];

  unsigned int max_course = parse_csv("classes.csv", courses);

  printf("Choices:\n"
         "a - print all available classes\n"
         "n - print class given course id\n"
         "d - print all classes given day of week combo\n"
         "s - print all classes under certain # seats\n"
         "q - quit\n");

  scanf("%hhu", &choice);

  // TODO

  return (0);
}