-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
84 lines (73 loc) · 1.3 KB
/
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
#include "file.h"
void freeListChar(ListChar l){
Element *e=l.first,*nxt;
while(e!=NULL){
nxt=e->nxt;
free(e);
e=e->nxt;
}
}
char* ltoa(ListChar l){
char *s=NULL;
int i;
Element *e;
if(l.taille>-1 || (l.taille>0 && l.first)){
s=malloc((l.taille+1)*sizeof(*s));
e=l.first;
i=0;
while(i<l.taille && e){
*(s+i)=e->c;
e=e->nxt;
++i;
}
if(i<l.taille){
free(s);
s=NULL;
}else{
s[i]='\0';
}
}
return s;
}
char* read_str(FILE *stream, int * n,char end){
ListChar l;
char c=EOF,*s=NULL;
Element *e;
if(stream){
/*init de la liste*/
l.taille=-1;
l.first=NULL;
/*lecture*/
c=getc(stream);
if(c!=end){
l.taille=0;
l.first=malloc(sizeof(*(l.first)));
e=l.first;
e->c=c;
l.taille=1;
c=getc(stream);
while(c!=end){
e->nxt=malloc(sizeof(*(e->nxt)));
e=e->nxt;
e->c=c;
++(l.taille);
c=getc(stream);
}
e->nxt=NULL;
}else if(c!=end){
l.taille=1;
l.first=malloc(sizeof(*(l.first)));
l.first->c=0;
l.first->nxt=NULL;
}
/* enregistrement de la taille */
if(n){
*n=l.taille;
}
/*convertion en chaine de caractere*/
s=ltoa(l);
/*liberation de la liste*/
freeListChar(l);
}
return s;
}