-
Notifications
You must be signed in to change notification settings - Fork 9
/
FileReader.c
140 lines (129 loc) · 3.36 KB
/
FileReader.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
138
139
140
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#include "Prototypes.h"
/*{
struct FileReader_ {
FILE* fd;
char* buffer;
int size;
int used;
char* start;
bool eof;
bool finalEnter;
bool command;
};
}*/
FileReader* FileReader_new(char* filename, bool command) {
FileReader* this = malloc(sizeof(FileReader));
if (command) {
this->fd = popen(filename, "r");
this->command = true;
} else {
this->command = false;
struct stat st = {0};
stat(filename, &st);
if (S_ISDIR(st.st_mode)) {
free(this);
return NULL;
}
this->fd = fopen(filename, "r");
}
if (this->fd == NULL) {
free(this);
return NULL;
}
this->size = 600;
this->buffer = malloc(this->size);
this->start = this->buffer;
this->used = fread(this->buffer, 1, this->size, this->fd);
this->eof = feof(this->fd);
this->finalEnter = false;
return this;
}
void FileReader_delete(FileReader* this) {
if (this->command) {
pclose(this->fd);
} else {
fclose(this->fd);
}
free(this->buffer);
free(this);
}
bool FileReader_eof(FileReader* this) {
return this->eof && !this->finalEnter && this->used == 0;
}
char* FileReader_readAllAndDelete(FileReader* this) {
if (this->start > this->buffer) {
memmove(this->buffer, this->start, this->used);
}
while (!this->eof) {
int free = this->size - this->used;
if (free == 0) {
this->size *= 2;
this->buffer = realloc(this->buffer, this->size);
}
this->used += fread(this->buffer + this->used, 1, this->size - this->used, this->fd);
this->eof = feof(this->fd);
}
if (this->command) {
pclose(this->fd);
} else {
fclose(this->fd);
}
char* buf = this->buffer;
free(this);
return buf;
}
char* FileReader_readLine(FileReader* this) {
int chunkSize = 0;
char* newline = NULL;
while (true) {
assert(this->start + this->used <= this->buffer + this->size);
newline = memchr(this->start, '\n', this->used);
if (newline) {
chunkSize = newline - this->start;
break;
}
assert(this->used <= this->size);
if (!this->eof) {
if (this->used) {
if (this->used < this->size) {
assert(this->start > this->buffer);
memmove(this->buffer, this->start, this->used);
} else {
assert(this->start == this->buffer);
this->size *= 2;
this->buffer = realloc(this->buffer, this->size);
}
}
this->start = this->buffer;
this->used += fread(this->buffer + this->used, 1, this->size - this->used, this->fd);
this->eof = feof(this->fd);
} else {
chunkSize = this->used;
break;
}
}
if (newline || chunkSize) {
assert(chunkSize <= this->used);
char* result = malloc(chunkSize + 1);
memcpy(result, this->start, chunkSize);
result[chunkSize] = '\0';
if (newline)
chunkSize++;
this->start += chunkSize;
this->used -= chunkSize;
if (newline && this->eof && this->used == 0)
this->finalEnter = true;
return result;
}
if (this->finalEnter) {
this->finalEnter = false;
return strdup("");
}
return NULL;
}