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

98 lines
2.5 KiB
C

#include "getArray.h"
#include "fileGestion.h"
#include "initialParameters.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
j = 0;
while((token = strsep(&buffer,",")) != NULL){ // séparation valeur par virgule initiale : csv
p[i][j] = atoi(token);
j++;
}
}
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 array to check
* @param N number of rows in p
* @param M number of columns in p
* @return true if the array contaign no NULL element
* @return false if at least one element is null
*/
bool checkArrayFullyFill(long **p, int N){
for(int i = 0 ; i < N ; i++){
if(p[i][0] == '\0'){ return false; }
}
return true;
}
/**
* @brief free memory allocate to an array p
*
* @param p array to free memory
* @param N number of rows in array
*/
void freeArray(long **p, int N) {
for(int i = 0 ; i < N ; i++){
free(p[i]);
}
free(p);
}
/**
* @brief Get the Raw Data Array object
*
* @param rawDataFileName name of the file to use to file the array
* @param N numbers of rows to have i the array
* @param M numbers of columns to have i the array
* @return long** the array fill with raw data
*/
long **getRawDataArray(char* rawDataFileName){
long **p;
p = get(nRow, nCol);
fillArrayWithRawData(rawDataFileName,p ,nRow, nCol);
//if(checkArrayFullyFill(p,nRow)){
//clearRawData(N);
return p;
/*}
else{
return NULL;
}*/
}