conf-database/fileManager.c
2023-11-26 23:10:30 +01:00

139 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 *tokens[6];
char *token;
if (file == NULL) {
printf("ça marche pas.");
return;
}
while (fgets(line, sizeof(line), file)) {
token = strtok(line, ",");
int i = 0;
while (token != NULL && i < 6) {
tokens[i] = token;
i ++;
token = strtok(NULL, ",");
}
addConf(confChain, atoi(tokens[0]), tokens[1], tokens[2], atoi(tokens[3]), atoi(tokens[4]), atoi(tokens[5]));
}
fclose(file);
}
void readListeners(ptListener listenerChain) {
FILE *file = fopen("./data/listeners", "r");
char line[100];
char *tokens[4];
char *token;
if (file == NULL) {
printf("ça marche pas.");
return;
}
while (fgets(line, sizeof(line), file)) {
token = strtok(line, ",");
int i = 0;
while (token != NULL && i < 4) {
tokens[i] = token;
i ++;
token = strtok(NULL, ",");
}
addListener(listenerChain, atoi(tokens[0]), tokens[1], atoi(tokens[2]), atoi(tokens[3]));
}
fclose(file);
}
void readRelations(ptConf confChain, ptListener listenerChain) {
FILE *file = fopen("./data/relations", "r");
char line[100];
char *tokens[50];
char *token;
int i;
int j;
if (file == NULL) {
printf("ça marche pas.");
return;
}
while (fgets(line, sizeof(line), file)) {
token = strtok(line, ":");
printf("%d\n", atoi(token));
i = 0;
while (token != NULL) {
tokens[i] = token;
i ++;
token = strtok(NULL, ",");
}
}
for (j = 1; j < i; j++) {
participateToConf(confChain, listenerChain, atoi(tokens[0]), atoi(tokens[j]));
}
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;
while(px -> next != NULL){
py = px -> listeners;
if (py -> next != NULL) {
fprintf(file, "%d:", px -> id);
}
while (py -> next != NULL) {
fprintf(file, "%d", py -> listener -> id);
if (py -> next -> next != NULL) {
fprintf(file, ",");
} else {
fprintf(file, "\n");
}
py = py -> next;
}
px = px -> next;
}
fclose(file);
}