Traitement-signal-plantes/Code-C/getArray.c

89 lines
2.2 KiB
C

#include "getArray.h"
#include "fileGestion.h"
//#include <string.h>
long **get(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i;
long **array;
array = (long **) malloc(N*sizeof(long *));
for(i = 0 ; i < N ; i++)
array[i] = (long *) malloc( M*sizeof(long) );
return array;
}
void fillArrayWithRawData(char *rawDataFileName,long** p, int N, int M) {
int i, j;
char *buffer;
size_t bufsize = 200;
buffer = (char *)malloc(bufsize * sizeof(char));
char* token;
FILE *f = fopen(rawDataFileName,"r");
for(i = 0 ; i < N ; i++){
if (!getline(&buffer, &bufsize, f)) break; // condition d'arret de la boucle si fichier fini
//printf("buffer : %s token : ",buffer);
j = 0;
while((token = strsep(&buffer,",")) != NULL){ // séparation valeur par virgule initiale : csv
//printf(token);
p[i][j] = atoi(token);
//printf("%d,", p[i][j]);
//printf("%d, " , p[i][j]);
j++;
}
//printf("\n\n");
}
fclose(f);
}
/**
* @brief print all the element of a bidimensionnal array p of shape : N x M
*/
void printArrayData(long** p, int N, int M) {
int i, j;
for(i = 0 ; i < N ; i++){
printf("line n°%d : " , i);
for(int j = 0 ; j < M ; j++){
printf("%d , " , p[i][j]);
if(j==(M-1)) printf("\n");
}
}
}
/**
* @brief verify if all the element of an array are not NULL, return
*
* @param p
* @param N
* @param M
* @return int
*/
bool checkArrayFullyFill(long **p, int N , int M){
for(int i = 0 ; i < N ; i++){
if(p[i][0] == '\0'){ return false; }
}
return true;
}
void freeArray(long **p, int N) {
int i;
for(i = 0 ; i < N ; i++)
free(p[i]);
free(p);
}
long **getRawDataArray(char* rawDataFileName , int N , int M){
long **p;
p = get(N, M);
fillArrayWithRawData(rawDataFileName,p ,N, M);
//printf("before test\n");
if(checkArrayFullyFill(p,N,M)==0){
//printf("after test 0\n");
clearRawData(N);
return p;
}
else{
//printf("after test 1\n");
return NULL;
}
}