-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_file.c
138 lines (118 loc) · 2.72 KB
/
read_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* read_file.c
*
* Created on: 2 nov. 2012
* Author: Hugo
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//def
#include "read_file.h"
int open_file(char * file_path,FILE **pfile) {
*pfile = fopen(file_path, "rb");
if (*pfile == NULL ) {
fprintf(stderr,"Impossible d'ouvrir le fichier\n");
return 1;
}
return 0;
}
int get_line_file(FILE * file, char **line, size_t * size) {
//alloc mem for line
if (*line == NULL ) {
*line = calloc(*size, sizeof(char));
}
if (*line == NULL ) {
fprintf(stderr,"Impossible de créer cette merde en RAM\n");
exit(EXIT_FAILURE);
}
if (fgets(*line, *size, file) == NULL ) {
return 1;
} else {
//verify all the line is read
char *endline = strchr(*line, '\n');
if (endline != NULL ) {
/* remove \n */
*endline = '\0';
} else {
/* else, read the all line */
if (!feof(file)) {
char c;
size_t maxsize = *size;
size_t currentsize = *size;
while ((c = fgetc(file)) != '\n' && c != EOF) {
currentsize++;
/* if overflow realloc x2 the buffer line*/
if (currentsize > maxsize - 1) {
maxsize = maxsize +100;
*line = (char *) realloc(*line, maxsize * sizeof(char));
if (*line == NULL ) {
int dodo;
fprintf(stderr,"Impossible de creer cette merde en RAM\n");
scanf("%d",&dodo);
dodo = 2 ;
exit(EXIT_FAILURE);
}
}
*(*line + currentsize - 2) = c;
}
*(*line + currentsize - 1) = '\0';
*size = maxsize;
}
}
}
return 0;
}
int get_line_file_nd(FILE * file, char **line, size_t * size) {
if (feof(file)) {
return 1;
}
//alloc mem for line
if (*line == NULL ) {
*line = calloc(*size, sizeof(char));
}
if (*line == NULL ) {
fprintf(stderr,"Impossible de creer cette merde en RAM\n");
exit(EXIT_FAILURE);
}
char c;
size_t maxsize = *size;
size_t currentsize = 0;
while ((c = fgetc(file)) != '\0' && c != EOF) {
currentsize++;
/* if overflow realloc x2 the buffer line*/
if (currentsize > maxsize - 1) {
maxsize = maxsize * 2;
*line = realloc(*line, maxsize * sizeof(char));
}
*(*line + currentsize - 1) = c;
}
*(*line + currentsize) = '\0';
*size = maxsize;
return 0;
}
char *get_all_file(char *file_path) {
FILE *file = NULL;
if(open_file(file_path,&file)){
exit(EXIT_FAILURE);
}
size_t size = 50;
char c;
char *line = calloc(size, sizeof(char));
;
size_t maxsize = size;
size_t currentsize = 0;
while ((c = fgetc(file)) != EOF) {
currentsize++;
/* if overflow realloc x2 the buffer line*/
if (currentsize > maxsize - 1) {
maxsize = maxsize * 2;
line = realloc(line, maxsize * sizeof(char));
}
line[currentsize - 1] = c;
}
line[currentsize] = '\0';
size = maxsize;
fclose(file);
return line;
}