-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
291 lines (224 loc) · 6.57 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#define SO_EOF -1
#define SEEK_SET 0 /* Seek from beginning of file. */
#define SEEK_CUR 1 /* Seek from current position. */
#define SEEK_END 2 /* Seek from end of file. */
#define DEFAULT_BUF_SIZE 4096
struct _so_file{
char* pathname;
char mode;
int fd;
char buf[DEFAULT_BUF_SIZE];
int size;
int buf_cursor;
int cursor;
bool can_flush;
bool error;
bool eof;
};
typedef struct _so_file SO_FILE;
int min(int a, int b){
if(a < b){
return a;
}
return b;
}
int max(int a, int b){
if(a > b){
return a;
}
return b;
}
SO_FILE *so_fopen(const char *pathname, const char *mode){
int fd = -1;
int mode_len = strlen(mode);
//check the open mode
if(mode[0] == 'r'){
if(mode_len > 1 && mode[1] == '+'){
fd = open(pathname, O_RDWR);
}else{
fd = open(pathname, O_RDONLY);
}
}else if(mode[0] == 'w'){
if(mode_len > 1 && mode[1] == '+'){
fd = open(pathname, O_RDWR | O_CREAT | O_TRUNC, 0644);
}else{
fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
}
}else if(mode[0] == 'a'){
if(mode_len > 1 && mode[1] == '+'){
fd = open(pathname, O_RDWR | O_APPEND | O_CREAT, 0644);
}else{
fd = open(pathname, O_WRONLY | O_APPEND | O_CREAT, 0644);
}
}else{
printf("Invalid mode!");
return NULL;
}
if(fd == -1){
return NULL;
}
//initialize the structure
SO_FILE* file = malloc(sizeof(SO_FILE));
int path_len = strlen(pathname);
file->pathname = malloc(sizeof(char) * path_len + 1);
memcpy(file->pathname, pathname, path_len);
file->pathname[path_len] = '\0';
file->fd = fd;
file->size = 0;
file->buf_cursor = 0;
file->can_flush = false;
file->mode = mode[0];
file->cursor = 0;
file->error = false;
file->eof = false;
return file;
}
int so_fclose(SO_FILE *stream){
int flush_err = 0;
//flush the buffer before closing the file
if(stream->can_flush){
flush_err = so_fflush(stream);
}
int close_err = close(stream->fd);
free(stream->pathname);
free(stream);
//returns an error from the flushing/closing process
return -(close_err || flush_err);
}
int so_fgetc(SO_FILE *stream){
//check the buffer's state
if(stream->size == 0 || stream->buf_cursor == stream->size){
int err = read(stream->fd, stream->buf, sizeof(unsigned char) * BUF_FILL_SIZE);
if(err <= 0){
stream->error = true;
if(err == 0){
stream->eof = true;
}
return SO_EOF;
}
stream->size = err;
//move the cursor to the beginning of the buffer
stream->buf_cursor = 0;
}
//move the file's cursor
++stream->cursor;
return (int)stream->buf[stream->buf_cursor++];
}
int so_fputc(int c, SO_FILE *stream){
stream->buf[stream->size++] = (char)c;
++stream->cursor;
stream->can_flush = true;
//if the buffer got full flush it
if(stream->size == BUF_FILL_SIZE){
so_fflush(stream);
}
return c;
}
size_t so_fread(void *ptr, size_t size, size_t nmemb, SO_FILE *stream){
//how many bytes we already read
int processed = 0;
//count how many bytes we have to read
int total = nmemb * size;
while(total - processed > 0){
//if the buffer is full
if(stream->buf_cursor == stream->size){
int err = read(stream->fd, stream->buf, sizeof(char) * BUF_FILL_SIZE);
if(err <= 0){
stream->error = true;
if(err == 0){
stream->eof = true;
}
return processed;
}
//if the buffer won't get fully filled make sure it won t use data from previous buffers
stream->buf[err] = '\0';
stream->size = err;
stream->buf_cursor = 0;
}
//count how many bytes I will copy depending on the buffer size and on my needs
int copy_len = min(total - processed, stream->size - stream->buf_cursor);
memcpy(&((char*)ptr)[processed], &stream->buf[stream->buf_cursor], copy_len);
//move the cursor to the next byte
stream->buf_cursor += copy_len;
//count how many bytes we processed
processed += copy_len;
}
//move the file's cursor
stream->cursor += processed;
return processed;
}
size_t so_fwrite(const void *ptr, size_t size, size_t nmemb, SO_FILE *stream){
//how many bytes I already wrote
int processed = 0;
//count how many bytes I have to write
int total = nmemb * size;
int err;
while(total - processed > 0){
//flush the buffer if full
if(stream->size == BUF_FILL_SIZE){
err = so_fflush(stream);
stream->can_flush = false;
}
//count how many bytes I will write depending on the buffer size and on my needs
int copy_len = min(total - processed, BUF_FILL_SIZE - stream->size);
memcpy(&stream->buf[stream->size], &((char*)ptr)[processed], copy_len);
stream->size += copy_len;
//move the file's cursor
stream->cursor += copy_len;
stream->can_flush = true;
//count the bytes we've processed
processed += copy_len;
}
return processed;
}
int so_fseek(SO_FILE *stream, long offset, int whence){
//flush the buffer if we wrote last time
if(stream->can_flush){
so_fflush(stream);
}
int err = lseek(stream->fd, offset, whence);
//move the cursor to the new possition
stream->cursor = err;
stream->size = 0;
stream->buf_cursor = 0;
if(err == -1){
stream->error = true;
return SO_EOF;
}
return 0;
}
long so_ftell(SO_FILE *stream){
return stream->cursor;
}
int so_fileno(SO_FILE *stream){
return stream->fd;
}
int so_feof(SO_FILE *stream){
return (int)stream->eof;
}
int so_fflush(SO_FILE *stream){
int err = write(stream->fd, stream->buf, sizeof(char) * (stream->size));
stream->size = 0;
stream->can_flush = false;
if(err <= 0){
stream->error = true;
return SO_EOF;
}
return 0;
}
int so_ferror(SO_FILE *stream){
return (int)stream->error;
}
int main(){
SO_FILE* file = so_fopen("test.txt", "r+");
so_fwrite(file, sizeof(char), 4, file);
so_fclose(file);
return 0;
}