Skip to content
Extraits de code Groupes Projets
Valider c133c418 rédigé par Jonathan Twite's avatar Jonathan Twite
Parcourir les fichiers

fileFeature

parent e99931b6
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -10,78 +10,60 @@ ...@@ -10,78 +10,60 @@
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <math.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); //functions:
long parseLong(char *buff, int index); int factorize(char *file_in_memory, FILE *fileOutput);
long ipow(int base, int exp); int factAndWrite(FILE *out, int n);
// bizarrement, quand j'appelle la fonction pow de la librairie math.h, int ipow(int base, int exp);
// on me retourne des erreurs donc j'ai refait une implementation
/** // iterate through a mapped file and write each number found and factorize them
* @param #file le fichier a read qu'on a prealablement charge (on ne charge pas le fichier ici) // the program stop if file_in_memory contains anything else than a digit or a '\n'
* @param #buff un char[x>=20] qui collecte les digits lus byte par byte int factorize(char *file_in_memory, FILE *fileOutput) {
* @return la ligne lue dans file sous forme numérique int start = 0;
*/ int size = strlen(file_in_memory);
long readline(int file, char buff[]) { int i = 0;
if(file == 0) { while (i < size) {
puts("End of file."); if (file_in_memory[i] == '\n' && i > start) { // if we are at the end of the line
return 0; int k = 0;
} for (int j = i - 1; j >= start; j--) // iterate from right to left
k += ((file_in_memory[j] - '0') *ipow(10, i - j - 1)); // record each digit into k
if (file <= 0) { factAndWrite(fileOutput, k); // write and factorize k into fileOutput
printf("Trying to read into %d directory.", file); start = i += 1;
close(file); }
return -1; else if ((file_in_memory[i] > '9' || file_in_memory[i] < '0') && file_in_memory[i] != '\n') {
} else if (buff == NULL) { // char doit être perror("Not a digit.");
puts("Trying to store values into NULL buffer."); return -2;
return -2; }
} else
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++; i++;
} }
return byteReader; return 0;
} }
/** // * write n and its factorization in the file @out *
* @param #buff la ligne lue dans le fichier int factAndWrite(FILE *out, int n) {
* @param index qui signifie les n premiers digits à lire int x = n;
* @return la ligne lue dans file sous forme numérique fprintf(out, "%d", n);
*/ while (n % 2 == 0) {
long parseLong(char *buff, int index) { fprintf(out, " 2");
if (buff == NULL) n /= 2;
return -1;
long k = 0;
for (int i = index - 1 ; i >= 0; i--) {
k += (buff[i] - '0') * ipow(10, index - (i + 1)) ;
} }
return k; for (int i = 3; x >= i*i; i+=2)
while (n % i == 0) {
fprintf(out, " %d", i);
n /= i;
}
if(n > 1 && n < x)
fprintf(out, " %d", n);
fprintf(out, " \n");
return 0;
} }
long ipow(int base, int exp) { int ipow(int base, int exp) {
long result = 1; int result = 1;
for (;;) { for (;;) {
if (exp & 1) if (exp & 1)
result *= base; result *= base;
......
//
// Created by jtwite on 28.03.20.
//
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <string.h>
#include "fileFeatures.h"
// int max value: 2 147 483 647
// long max value: 9223372 036 854 775 807
// char *filepath = "/home/jtwite/Desktop/test";
// Remplacez-le contenu par le chemin relatif du fichier que vous souhaitez tester.
int main(int argc, char *argv[])
{
struct timeval stop, start;
gettimeofday(&start, NULL);
printf("argv[1]: %s\n", argv[1]);
printf("argv[2]: %s\n", argv[2]);
//open the file
int fileInput = open(argv[1], O_RDONLY);
if (fileInput <= 0)
exit(1);
// get the fileInput size
struct stat sb;
fstat(fileInput, &sb);
char *file_in_memory = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fileInput, 0);
close(fileInput);
// Create a new file struct in which the output is writen
FILE *fileOutput = fopen(argv[2], "w");
if(fileOutput == NULL)
exit(-2);
//go through the mapped file to get numbers and write them in fileOutput
factorize(file_in_memory, fileOutput);
//close everything
munmap(file_in_memory, sb.st_size);
fclose(fileOutput);
gettimeofday(&stop, NULL);
printf("took %lu µs\n", (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec);
return 0;
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter