-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilereader.c
executable file
·179 lines (150 loc) · 4 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* filereader.c
Copyright 2008 University of Bergen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc.,
51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA */
/* Written July and August, 2008 by Dag Hovland */
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUF_SIZE 16384
char buf[BUF_SIZE];
int fd;
bool file_opened = false;
char * glob_filename = "stdin";
char* line;
size_t cur_buf_size, buf_pos;
size_t line_size;
bool file_finished;
void init_all(){
file_finished = false;
buf_pos = 0;
cur_buf_size = 0;
line_size = 1024;
line = malloc(line_size);
return;
}
void inc_line_size(size_t new_size){
assert(new_size > line_size);
line_size = new_size;
line = realloc(line, line_size);
}
bool init_readfile(char* filename){
glob_filename = filename;
if(file_opened)
close(fd);
fd = open(filename, O_RDONLY);
if(fd == -1){
int error_num = errno;
if(error_num == EACCES){
fprintf(stderr, "grep: %s: Ikke tilgang\n", filename);
return false;
}
fprintf(stderr, "file open error on %s: ", filename);
perror("");
abort();
}
file_opened = true;
init_all();
return true;
}
bool init_readstd(){
fd = STDIN_FILENO;
init_all();
return true;
}
void fill_buffer(){
ssize_t read_ret_val = -1;
buf_pos = 0;
assert(!file_finished);
while(read_ret_val < 0){
read_ret_val = read(fd, buf, BUF_SIZE);
if(read_ret_val < 0){
int error_num = errno;
if(error_num == EISDIR)
read_ret_val = 0;
else if (errno != EINTR){
fprintf(stderr, "file read error on %s: ", glob_filename);
perror("");
abort();
}
}
}
cur_buf_size = (size_t) read_ret_val;
if (cur_buf_size == 0) {
file_finished = true;
return;
}
return;
}
bool get_file_finished(){
return file_finished;
}
/**
line_start is the number of chars that should be skipped before copying text into line
line_len is set to the number of chars that are read into it,
such that position of end is line_start + line_len
line_end is the number of characters that should be allocated at the end of the string
**/
char* nextline(size_t line_start, size_t* line_len, size_t line_end){
size_t line_pos = line_start;
if(file_finished){
*line_len = 0;
return line;
}
if(buf_pos >= cur_buf_size){
fill_buffer();
}
while(buf[buf_pos] != '\n' && buf[buf_pos] != '\r'){
while(line_pos < line_size && buf[buf_pos] != '\n' && buf[buf_pos] != '\r' && buf_pos < cur_buf_size){
line[line_pos++] = buf[buf_pos++];
}
if(file_finished)
break;
if(buf_pos >= cur_buf_size)
fill_buffer();
while(line_pos + line_end >= line_size)
inc_line_size( 2 * line_size );
}
if(!file_finished){
while(!file_finished && buf_pos >= cur_buf_size)
fill_buffer();
if(buf[buf_pos] == '\n')
buf_pos++;
else if(buf[buf_pos] == '\r'){
buf_pos++;
while(!file_finished && buf_pos >= cur_buf_size)
fill_buffer();
if(buf_pos < cur_buf_size && buf[buf_pos] == '\n')
buf_pos++;
}
}
assert(line_pos - line_start >= 0);
if(line_pos > line_start || buf_pos != line_pos){
line[line_pos] = 0;
*line_len = line_pos - line_start;
} else {
*line_len = -1;
return NULL;
}
return line;
}
void end_readfile(){
close(fd);
free(line);
}