-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIO.c
67 lines (60 loc) · 1.69 KB
/
IO.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char * readLine(FILE * fPointer){
int charP = 200;
char * buffer = calloc(200, sizeof(char));
int charC = 0;
if(fgets(buffer, charP, fPointer)){
while(buffer[charP - 2] && buffer[charP - 2] != '\n'){
/* while string in buffer is not complete... reallocate and read more */
charC = charP;
charP *= 2;
buffer = realloc(buffer, charP * sizeof(char));
if(!buffer){ // error in realloc - returns NULL
return NULL;
} else {
buffer[charP - 2] = '\0';
if(fgets(buffer + charC - 1, charC + 1, fPointer) != (buffer + charC -1)){ //fgets did not succed...
free(buffer);
return NULL;
}
}
}
} else {
return NULL;
}
return buffer;
}
FILE * positionStream(FILE * fPointer, char * string, int doRewind){
char * bLine;
if(doRewind){
rewind(fPointer);
}
while((bLine = readLine(fPointer))){
char * occurence = strstr(bLine, string);
if(occurence){
fseek(fPointer, (occurence - bLine) - strlen(bLine), SEEK_CUR);
return fPointer;
}
free(bLine);
}
return NULL;
}
char * seekLine(FILE * fPointer, char * string, int doRewind){
char * bLine;
if(doRewind){
rewind(fPointer);
}
while((bLine = readLine(fPointer))){
char * occurence = strstr(bLine, string);
if(occurence){
return bLine;
}
free(bLine);
}
return NULL;
}
char * nextLine(FILE * fPointer){
return readLine(fPointer);
}