colloscope/structure.c

98 lines
2.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <unistd.h>
#include <time.h>
typedef enum topic {MATH, PHYSICS, ENGLISH, FRENCH, INFO} topic;
// colles subjects
typedef struct date {int hour; int day; int month; int year;} date;
// nothing to say here
typedef struct creneau {date date; int group; char* name; topic mat;} creneau;
// créneau de colle
// /!\ creneau has to be sorted by ascending dates
typedef struct colleur {char* name; topic mat; date* disp; int n_disp;} colleur;
// available creneaux for the colleurs
static int width = 1200;
static int height = 900;
int date_dist(date d1, date d2) {
// returns distance between d1 and d2 in days
// if d2 is sooner than d1, it will return -1
if(d2.month == d1.month && d2.year == d1.year) {
if(d2.day < d1.day) {
return -1;
}
return d2.day - d1.day;
} else {
int is_31 = 0;
if(d1.month == 1 || d1.month == 3 || d1.month == 5 || d1.month == 7 || d1.month == 8 || d1.month == 10 || d1.month == 12) {
is_31 = 1;
} else {
is_31 = (d1.month == 2)*(-2) + (d1.month == 2 && d1.year%4 == 0 && d1.year%100 != 0);
}
if(d2.month - d1.month < 0 && d2.year <= d1.year) {
return -1;
} else if(d2.month - d1.month == 1 && d2.year == d1.year) {
return d2.day + (30 + is_31 - d1.day);
} else {
date copy;
copy.day = d1.day;
copy.month = d1.month;
copy.year = d1.year;
copy.month = (copy.month + 1);
if(copy.month == 13) {
copy.month = 1;
copy.year += 1;
}
return (30 + is_31 + date_dist(copy, d2));
}
}
}
creneau* import_creneaux(char* filename, int size) {
// import creneau data from a file
// see file.txt for an example
FILE* ptr = fopen(filename, "r");
char c = fgetc(ptr);
creneau* edt = malloc(sizeof(creneau)*size);
int i = 0;
int to_fill = 0;
int buffer = 0;
while(c != EOF && c != '$') {
if((int)(c) >= 48 && (int)(c) <= 57) {
buffer *= 10;
buffer += (int)(c) - 48;
} else {
if(to_fill == 0) {
edt[i].date.hour = buffer;
} else if(to_fill == 1) {
edt[i].date.day = buffer;
} else if(to_fill == 2) {
edt[i].date.month = buffer;
} else {
edt[i].date.year = buffer;
}
to_fill++;
buffer = 0;
if(c == '\n') {
i++;
to_fill = 0;
}
}
c = fgetc(ptr);
}
fclose(ptr);
return edt;
}