This repository has been archived on 2023-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
GINPA/Modele/fileIO.c

123 lines
2.6 KiB
C

#include "fileIO.h"
ErrEnum loadLogs(const char *logsName, Point **tabPoint, uint32_t *nbLigne)
{
/* Ouverture du fichier de logs */
FILE *logsFile = fopen(logsName, "r");
if(logsFile == NULL)
{
fprintf(stderr, "%s\n", strerror(errno));
return MISSING_FILE;
}
*nbLigne = 0;
char tmp[BUFSIZE] = "";
/* Comptage du nombre de lignes */
while(fgets(tmp, BUFSIZE, logsFile) != NULL)
++(*nbLigne);
/* Allocation de la mémoire en fonction du nombre de lignes */
*tabPoint = calloc(*nbLigne, sizeof(Point));
if(*tabPoint == NULL)
{
fprintf(stderr, "%s\n", strerror(errno));
return ALLOCATION_FAILURE;
}
rewind(logsFile);
/* Conversion du fichier en tableau de Points */
for(uint32_t i = 0; i < *nbLigne; ++i)
{
if(convertLine(fgets(tmp, BUFSIZE, logsFile), &tabPoint[0][i]) == false)
{
fprintf(stderr, "%s\n", strerror(errno));
return CORRUPTED_FILE;
}
}
if(fclose(logsFile) == EOF)
{
fprintf(stderr, "%s\n", strerror(errno));
}
return LOAD_SUCCESS;
}
bool convertLine(char *line, Point *point)
{
char *next = NULL;
char *testConvertion = NULL;
next = strchr(line, ':');
if(next == NULL)
return false;
next++;
point->date = (time_t)strtol(next, &testConvertion, 10);
if(next == testConvertion)
return false;
next = strchr(next, ':');
if(next == NULL)
return false;
next++;
point->pos.lat = strtod(next, &testConvertion);
if(next == testConvertion)
return false;
next = strchr(next, ':');
if(next == NULL)
return false;
next++;
point->pos.lon = strtod(next, &testConvertion);
if(next == testConvertion)
return false;
point->type = NONE;
point->aSupprimer = false;
return true;
}
uint16_t convertLineToFilePaths(const char *const temp, char selectedFiles[MAXSELECTEDFILES][MAXPATHLENGTH])
{
uint16_t i = 0;
// Pré-positionnement du premier pointeur sur le début de chaîne. Le second peut ne jamais être quelque part (si un seul fichier sélectionné) !
char *ptr1 = (char*)temp, *ptr2 = strstr(ptr1, "|");
// Fausse boucle infinie !
while(1)
{
size_t size = strlen(ptr1) - (ptr2 == NULL ? 0 : strlen(ptr2));
if(size > MAXPATHLENGTH)
{
// Notre petite gestion d'erreur
i = 0;
break;
}
strncpy(selectedFiles[i], "", MAXPATHLENGTH);
strncat(selectedFiles[i], ptr1, size);
i++;
// Cas pour gérer un seul fichier entré !
if(ptr2 == NULL)
{
break;
}
// On peut se permettre de faire `+ 1` ci-dessous car la ligne comportera forcément un autre nom de fichier derrière un '|'.
ptr1 = ptr2 + 1;
// On cherche la prochaine occurrence !
ptr2 = strstr(ptr1, "|");
}
return i;
}