diff --git a/fileFeatures.h b/fileFeatures.h new file mode 100644 index 0000000000000000000000000000000000000000..39acef6491b297fbc2af3a19f40068430cc5e012 --- /dev/null +++ b/fileFeatures.h @@ -0,0 +1,97 @@ +// +// Created by jtwite on 30.03.20. +// + +#ifndef PROJET_Q4_GROUPE_O4_FILEFEATURES_H +#define PROJET_Q4_GROUPE_O4_FILEFEATURES_H + + +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> + +/** + * A SAVOIR pour la conversion de char en int: + * Si 'x' ∈ ['0', '1', '2', ..., '9'] alors + * alors (int) x = x - '0' + */ +// Mes fonctions: + +long readLine(int file, char *buff); +long parseLong(char *buff, int index); +long ipow(int base, int exp); +// bizarrement, quand j'appelle la fonction pow de la librairie math.h, +// on me retourne des erreurs donc j'ai refait une implementation + +/** + * @param #file le fichier a read qu'on a prealablement charge (on ne charge pas le fichier ici) + * @param #buff un char[x>=20] qui collecte les digits lus byte par byte + * @return la ligne lue dans file sous forme numérique + */ +long readline(int file, char buff[]) { + if(file == 0) { + puts("End of file."); + return 0; + } + + if (file <= 0) { + printf("Trying to read into %d directory.", file); + close(file); + return -1; + } else if (buff == NULL) { // char doit être + puts("Trying to store values into NULL buffer."); + return -2; + } + int byteReader = read(file, &(*buff), 1); // In order to call the read() iteratively, we have to call it once before + if (buff[0] > '9') { + puts("Illegal argument."); + return -3; + } else if(buff[0] < '0') { + puts("End of file"); + return 0; + } + int i = 1; + while (byteReader = read(file, &buff[i], 1) > 0) { // while datas are still remaining + if (buff[i] == '\n') { // if this is the end of line + return parseLong(buff, i); // return the long value + } else if (buff[i] > '9' || buff[i] < '0') { // if the readed value isn't a digit + printf("Illegal argument (%d)..", i); // stop the program + return -3; + } else // otherwise keep iterating + i++; + } + return byteReader; +} + +/** + * @param #buff la ligne lue dans le fichier + * @param index qui signifie les n premiers digits à lire + * @return la ligne lue dans file sous forme numérique + */ +long parseLong(char *buff, int index) { + if (buff == NULL) + return -1; + long k = 0; + for (int i = index - 1 ; i >= 0; i--) { + k += (buff[i] - '0') * ipow(10, index - (i + 1)) ; + } + return k; +} + + +long ipow(int base, int exp) { + long result = 1; + for (;;) { + if (exp & 1) + result *= base; + exp >>= 1; + if (!exp) + break; + base *= base; + } + return result; +} + +#endif //PROJET_Q4_GROUPE_O4_FILEFEATURES_H +