conf-database/fileManager.c
2023-11-30 15:59:10 +01:00

119 lines
No EOL
3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "conf.h"
void readConfs(ptConf confChain) {
FILE *file = fopen("./data/confs", "r");
char line[100];
char *token;
if (file == NULL) {
printf("ça marche pas.");
return;
}
while (fgets(line, sizeof(line), file)) {
int id = atoi(strtok(line, ","));
char *title = strtok(NULL, ",");
char *speaker = strtok(NULL, ",");
int day = atoi(strtok(NULL, ","));
int month = atoi(strtok(NULL, ","));
int year = atoi(strtok(NULL, ","));
addConf(confChain, id, title, speaker, day, month, year);
}
fclose(file);
}
void readListeners(ptListener listenerChain) {
FILE *file = fopen("./data/listeners", "r");
char line[100];
char *token;
if (file == NULL) {
printf("ça marche pas.");
return;
}
while (fgets(line, sizeof(line), file)) {
int id = atoi(strtok(line, ","));
char *name = strtok(NULL, ",");
int day = atoi(strtok(NULL, ","));
int month = atoi(strtok(NULL, ","));
addListener(listenerChain, id, name, day, month);
}
fclose(file);
}
void readRelations(ptConf confChain, ptListener listenerChain) {
FILE *file = fopen("./data/relations", "r");
char line[100];
char *token1;
char *token2;
if (file == NULL) {
printf("ça marche pas.");
return;
}
while (fgets(line, sizeof(line), file)) {
int confId = atoi(strtok(line, ":"));
while ((token1 = strtok(NULL, ";")) != NULL && (token2 = strtok(NULL, ",")) != NULL) {
participateToConf(confChain, listenerChain, confId, atoi(token1), atoi(token2));
}
}
fclose(file);
}
void saveConf(ptConf confChain) {
FILE *file = fopen("./data/confs", "w");
ptConf px = confChain;
while(px -> next != NULL){
fprintf(file,"%d,%s,%s,%d,%d,%d\n",px -> id,px -> title,px -> speaker,px -> day,px -> month,px -> year);
px = px -> next;
}
fclose(file);
}
void saveListeners(ptListener listenerChain) {
FILE *file = fopen("./data/listeners", "w");
ptListener px = listenerChain;
while(px -> next != NULL){
fprintf(file,"%d,%s,%d,%d\n",px -> id,px -> name,px -> age,px -> level);
px = px -> next;
}
fclose(file);
}
void saveRelations(ptConf confChain) {
FILE *file = fopen("./data/relations", "w");
ptConf px = confChain;
ptListenerList py;
printf("test");
while(px -> next != NULL){
printf("test");
py = px -> listeners -> next;
if (py -> next != NULL) {
fprintf(file, "%d:", px -> id);
}
while (py -> next != NULL) {
fprintf(file, "%d;%d", py -> listener -> id, py -> grade);
if (py -> next -> next != NULL) {
fprintf(file, ",");
} else {
fprintf(file, "\n");
}
py = py -> next;
}
px = px -> next;
}
fclose(file);
}