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

88 lines
2.1 KiB
C
Raw Normal View History

2022-06-01 12:32:04 +02:00
#include "getArray.h"
#include "fileGestion.h"
2022-06-03 17:02:49 +02:00
//#include <string.h>
2022-06-01 12:32:04 +02:00
2022-06-03 17:02:49 +02:00
long **get(int N, int M) /* Allocate the array */
2022-06-01 12:32:04 +02:00
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i;
2022-06-03 17:02:49 +02:00
long **array;
array = (long **) malloc(N*sizeof(long *));
2022-06-01 12:32:04 +02:00
for(i = 0 ; i < N ; i++)
2022-06-03 17:02:49 +02:00
array[i] = (long *) malloc( M*sizeof(long) );
2022-06-01 12:32:04 +02:00
return array;
}
2022-06-09 17:34:24 +02:00
void fillArrayWithRawData(long** p, int N, int M) {
2022-06-01 12:32:04 +02:00
int i, j;
2022-06-03 17:02:49 +02:00
char *buffer;
size_t bufsize = 200;
buffer = (char *)malloc(bufsize * sizeof(char));
char* token;
2022-06-01 12:32:04 +02:00
2022-06-03 17:02:49 +02:00
FILE *f = fopen("rawData.csv","r");
for(i = 0 ; i < N ; i++){
if (!getline(&buffer, &bufsize, f)) break; // condition d'arret de la boucle si fichier fini
2022-06-03 18:04:36 +02:00
//printf("buffer : %s token : ",buffer);
2022-06-03 17:02:49 +02:00
j = 0;
while((token = strsep(&buffer,",")) != NULL){ // séparation valeur par virgule initiale : csv
2022-06-03 18:04:36 +02:00
//printf(token);
2022-06-03 17:02:49 +02:00
p[i][j] = atoi(token);
2022-06-03 18:04:36 +02:00
//printf("%d,", p[i][j]);
//printf("%d, " , p[i][j]);
2022-06-03 17:02:49 +02:00
j++;
}
2022-06-03 18:04:36 +02:00
//printf("\n\n");
2022-06-01 12:32:04 +02:00
}
}
2022-06-09 17:34:24 +02:00
/**
* @brief print all the element of a bidimensionnal array p of shape : N x M
*/
2022-06-03 17:02:49 +02:00
void printArrayData(long** p, int N, int M) {
2022-06-01 12:32:04 +02:00
int i, j;
2022-06-03 17:02:49 +02:00
for(i = 0 ; i < N ; i++){
2022-06-09 17:34:24 +02:00
printf("line n°%d : " , i);
for(int j = 0 ; j < M ; j++){
printf("%d , " , p[i][j]);
if(j==(M-1)) printf("\n");
2022-06-03 17:02:49 +02:00
}
2022-06-09 17:34:24 +02:00
}
2022-06-01 12:32:04 +02:00
}
2022-06-09 17:34:24 +02:00
/**
* @brief verify if all the element of an array are not NULL, return
*
* @param p
* @param N
* @param M
* @return int
*/
2022-06-09 17:56:45 +02:00
bool checkArrayFullyFill(long **p, int N , int M){
2022-06-07 14:28:32 +02:00
for(int i = 0 ; i < N ; i++){
2022-06-09 17:56:45 +02:00
if(p[i][0] == '\0'){ return false; }
2022-06-07 14:28:32 +02:00
}
2022-06-09 17:56:45 +02:00
return true;
2022-06-07 14:28:32 +02:00
}
2022-06-03 17:02:49 +02:00
void freeArray(long **p, int N) {
2022-06-01 12:32:04 +02:00
int i;
for(i = 0 ; i < N ; i++)
free(p[i]);
free(p);
}
2022-06-03 17:02:49 +02:00
long **getRawDataArray(int N , int M){
long **p;
2022-06-01 18:02:32 +02:00
p = get(N, M);
fillRawData(p ,N, M);
2022-06-07 14:28:32 +02:00
//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;
}
2022-06-01 12:32:04 +02:00
}