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

222 lines
5.3 KiB
C
Raw Permalink Normal View History

2022-12-01 17:29:47 +01:00
#include "include/getArray.h"
#include "include/fileGestion.h"
#include "include/initialParameters.h"
2022-06-03 17:02:49 +02:00
//#include <string.h>
2022-06-01 12:32:04 +02:00
2022-06-16 14:35:02 +02:00
long **getlongArray(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;
2022-06-21 17:47:27 +02:00
array = (long **)malloc(N * sizeof(long *));
for (i = 0; i < N; i++)
array[i] = (long *)malloc(M * sizeof(long));
2022-06-01 12:32:04 +02:00
return array;
}
2022-06-16 14:35:02 +02:00
double **getDoubleArray(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i;
double **array;
2022-06-21 17:47:27 +02:00
array = (double **)malloc(N * sizeof(double *));
for (i = 0; i < N; i++)
array[i] = (double *)malloc(M * sizeof(double));
2022-06-16 14:35:02 +02:00
return array;
}
2022-06-21 17:47:27 +02:00
void fillArrayWithRawData(char *rawDataFileName, long **p, int N, int M)
{
int i = 0, j;
2022-06-03 17:02:49 +02:00
char *buffer;
size_t bufsize = 200;
buffer = (char *)malloc(bufsize * sizeof(char));
2022-06-24 17:22:33 +02:00
char *token;
2022-06-01 12:32:04 +02:00
2022-06-21 17:47:27 +02:00
FILE *f = fopen(rawDataFileName, "r");
2022-06-13 17:11:16 +02:00
long t, c1, c2, c3, c4, c5, c6, c7, c8;
switch (M)
2022-06-21 17:47:27 +02:00
{
case 2:
while (fscanf(f, "%ld,%ld[^\n] ", &t, &c1) != EOF)
{
p[i][0] = t;
p[i][1] = c1;
i++;
}
break;
case 3:
while (fscanf(f, "%ld,%ld,%ld[^\n] ", &t, &c1, &c2) != EOF)
{
p[i][0] = t;
p[i][1] = c1;
p[i][2] = c2;
i++;
}
break;
case 4:
while (fscanf(f, "%ld,%ld,%ld,%ld[^\n] ", &t, &c1, &c2, &c3) != EOF)
{
2022-06-24 17:22:33 +02:00
p[i][0] = t;
p[i][1] = c1;
p[i][2] = c2;
p[i][3] = c3;
i++;
2022-06-03 17:02:49 +02:00
}
break;
case 5:
while (fscanf(f, "%ld,%ld,%ld,%ld,%ld[^\n] ", &t, &c1, &c2, &c3, &c4) != EOF)
{
p[i][0] = t;
p[i][1] = c1;
p[i][2] = c2;
p[i][3] = c3;
p[i][4] = c4;
i++;
}
break;
case 6:
while (fscanf(f, "%ld,%ld,%ld,%ld,%ld,%ld[^\n] ", &t, &c1, &c2, &c3, &c4, &c5) != EOF)
{
p[i][0] = t;
p[i][1] = c1;
p[i][2] = c2;
p[i][3] = c3;
p[i][4] = c4;
p[i][5] = c5;
i++;
}
break;
case 7:
while (fscanf(f, "%ld,%ld,%ld,%ld,%ld,%ld,%ld[^\n] ", &t, &c1, &c2, &c3, &c4, &c5, &c6) != EOF)
{
p[i][0] = t;
p[i][1] = c1;
p[i][2] = c2;
p[i][3] = c3;
p[i][4] = c4;
p[i][5] = c5;
p[i][6] = c6;
i++;
}
break;
case 8:
while (fscanf(f, "%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld[^\n] ", &t, &c1, &c2, &c3, &c4, &c5, &c6, &c7) != EOF)
{
p[i][0] = t;
p[i][1] = c1;
p[i][2] = c2;
p[i][3] = c3;
p[i][4] = c4;
p[i][5] = c5;
p[i][6] = c6;
p[i][7] = c7;
i++;
}
break;
case 9:
while (fscanf(f, "%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld[^\n] ", &t, &c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8) != EOF)
{
p[i][0] = t;
p[i][1] = c1;
p[i][2] = c2;
p[i][3] = c3;
p[i][4] = c4;
p[i][5] = c5;
p[i][6] = c6;
p[i][7] = c7;
p[i][8] = c8;
i++;
}
break;
default:
printf("bad column size -> time + nbr captor\n");
2022-06-01 12:32:04 +02:00
}
/************** Debug part **************/
// for (int ii = 0; ii < nRowRawData; ii++)
// {
// for (int y = 0; y < nCol; y++)
// {
// printf("%ld,", p[ii][y]);
// if (y == nCol - 1)
// printf("lol\n");
// }
// }
2022-06-10 17:22:01 +02:00
fclose(f);
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-21 17:47:27 +02:00
void printArrayData(long **p, int N, int M)
{
for (int i = 0; i < N; i++)
{
printf("line n°%d : ", i);
for (int j = 0; j < M; j++)
{
printf("%ld , ", 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
/**
2022-06-21 17:47:27 +02:00
* @brief verify if all the element of an array are not NULL, return
*
2022-06-14 10:46:12 +02:00
* @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
2022-06-09 17:34:24 +02:00
*/
2022-06-21 17:47:27 +02:00
bool checkArrayFullyFill(long **p, int N)
{
for (int i = 0; i < N; i++)
{
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-14 10:46:12 +02:00
/**
* @brief free memory allocate to an array p
2022-06-21 17:47:27 +02:00
*
2022-06-14 10:46:12 +02:00
* @param p array to free memory
* @param N number of rows in array
*/
2022-06-21 17:47:27 +02:00
void freeArray(long **p, int N)
{
for (int i = 0; i < N; i++)
{
2022-06-01 12:32:04 +02:00
free(p[i]);
2022-06-15 10:49:21 +02:00
}
2022-06-01 12:32:04 +02:00
free(p);
}
2022-06-14 10:46:12 +02:00
/**
* @brief Get the Raw Data Array object
2022-06-21 17:47:27 +02:00
*
2022-06-14 10:46:12 +02:00
* @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
*/
2022-06-21 17:47:27 +02:00
long **getRawDataArray(char *rawDataFileName)
{
2022-06-03 17:02:49 +02:00
long **p;
2022-06-16 17:05:12 +02:00
p = getlongArray(nRowRawData, nCol);
2022-06-21 17:47:27 +02:00
fillArrayWithRawData(rawDataFileName, p, nRowRawData, nCol);
// if(checkArrayFullyFill(p,nRow)){
// clearRawData(N);
return p;
2022-06-15 10:49:21 +02:00
/*}
2022-06-07 14:28:32 +02:00
else{
return NULL;
2022-06-15 10:49:21 +02:00
}*/
2022-06-01 12:32:04 +02:00
}