-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyFILE.cpp
More file actions
498 lines (469 loc) · 14.6 KB
/
myFILE.cpp
File metadata and controls
498 lines (469 loc) · 14.6 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include "mystdio.h"
#include <stdlib.h>
vector<string> myFILEs_open;
vector<simple_file *> myFILEs;
myFILE::myFILE(/* args */)
{
this->file = new simple_file();
this->file->permition = PERMISSION_UNSET;
this->file->file_num = 0;
this->file->current_offset = 0;
this->file->used_size = 0;
this->file->size = 0;
this->file->first_block = 0;
strcpy(this->file->name, "");
this->file->type = FILE_TYPE_UNSET;
};
myFILE::~myFILE(){
};
myFILE *myfopen(const char *pathname, const char *mode)
{
myFILE *myfile_ptr = new myFILE();
if (mode[0] == 'r' && strlen(mode) == 1)
{
int file_number = myopen(pathname, PERMISSION_READ);
if (file_number == -1)
{
return NULL;
}
myfile_ptr->file->permition = PERMISSION_READ;
myfile_ptr->file->file_num = file_number;
myfile_ptr->file->current_offset = 0;
myfile_ptr->file->used_size = inodes[file_number].used_size;
myfile_ptr->file->size = inodes[file_number].size;
myfile_ptr->file->first_block = inodes[file_number].first_block;
strcpy(myfile_ptr->file->name, inodes[file_number].name);
myfile_ptr->file->type = inodes[file_number].type;
myFILEs_open.push_back(inodes[file_number].name);
// myread(myfile_ptr->file->file_num, (void *)myfile_ptr->file_buffer, myfile_ptr->file->used_size);
return myfile_ptr;
}
else if (mode[0] == 'w' && strlen(mode) == 1)
{
int file_number = myopen(pathname, PERMISSION_WRITE);
if (file_number == -1)
{
return NULL;
}
myfile_ptr->file->permition = PERMISSION_WRITE;
myfile_ptr->file->file_num = file_number;
myfile_ptr->file->current_offset = 0;
myfile_ptr->file->used_size = inodes[file_number].used_size;
myfile_ptr->file->size = inodes[file_number].size;
myfile_ptr->file->first_block = inodes[file_number].first_block;
strcpy(myfile_ptr->file->name, inodes[file_number].name);
myfile_ptr->file->type = inodes[file_number].type;
myFILEs_open.push_back(inodes[file_number].name);
myread(myfile_ptr->file->file_num, (void *)myfile_ptr->file_buffer, myfile_ptr->file->used_size);
}
else if (mode[0] == 'a' && strlen(mode) == 1)
{
int file_number = myopen(pathname, PERMISSION_APPEND);
if (file_number == -1)
{
return NULL;
}
myfile_ptr->file->permition = PERMISSION_APPEND;
myfile_ptr->file->current_offset = myfile_ptr->file->used_size;
myfile_ptr->file->file_num = file_number;
myfile_ptr->file->used_size = inodes[file_number].used_size;
myfile_ptr->file->size = inodes[file_number].size;
myfile_ptr->file->first_block = inodes[file_number].first_block;
strcpy(myfile_ptr->file->name, inodes[file_number].name);
myfile_ptr->file->type = inodes[file_number].type;
myFILEs_open.push_back(inodes[file_number].name);
myread(myfile_ptr->file->file_num, (void *)myfile_ptr->file_buffer, myfile_ptr->file->used_size);
}
else if (mode[0] == 'r' && mode[1] == '+')
{
int file_number = myopen(pathname, PERMISSION_READ_WRITE);
if (file_number == -1)
{
return NULL;
}
myfile_ptr->file->permition = PERMISSION_READ_WRITE;
myfile_ptr->file->file_num = file_number;
myfile_ptr->file->current_offset = 0;
myfile_ptr->file->used_size = inodes[file_number].used_size;
myfile_ptr->file->size = inodes[file_number].size;
myfile_ptr->file->first_block = inodes[file_number].first_block;
strcpy(myfile_ptr->file->name, inodes[file_number].name);
myfile_ptr->file->type = inodes[file_number].type;
myFILEs_open.push_back(inodes[file_number].name);
myread(myfile_ptr->file->file_num, (void *)myfile_ptr->file_buffer, myfile_ptr->file->used_size);
}
else
{
myfile_ptr->file->permition = PERMISSION_UNSET;
}
return myfile_ptr;
}
int myfclose(myFILE *stream)
{
int file_number = stream->file->file_num;
myFILEs_open.erase(std::remove(myFILEs_open.begin(), myFILEs_open.end(), inodes[file_number].name), myFILEs_open.end());
myclose(file_number);
return file_number;
}
size_t myfread(void *ptr, size_t size, size_t nmemb, myFILE *stream)
{
// check if the file is opened in write mode break myFILE.cpp:118
if (stream->file->permition != PERMISSION_READ && stream->file->permition != PERMISSION_READ_WRITE && stream->file->permition != PERMISSION_APPEND)
{
return 0;
}
// check if the file is in filesopen
if (std::find(myFILEs_open.begin(), myFILEs_open.end(), inodes[stream->file->file_num].name) == myFILEs_open.end())
{
return 0;
}
int current_offset = inodes[stream->file->file_num].current_offset;
int used_size = inodes[stream->file->file_num].used_size;
int size_to_read = size * nmemb;
int size_to_read_from_buffer = 0;
if (current_offset + size_to_read > used_size)
{
size_to_read_from_buffer = used_size - current_offset;
}
else
{
size_to_read_from_buffer = size_to_read;
}
myread(stream->file->file_num, ptr, size_to_read_from_buffer);
// memcpy(ptr, stream->file_buffer + current_offset, size_to_read_from_buffer);
stream->file->current_offset += size_to_read_from_buffer;
return size_to_read_from_buffer;
}
size_t myfwrite(const void *ptr, size_t size, size_t nmemb, myFILE *stream)
{
// check if the file is opened in write mode
if (stream->file->permition != PERMISSION_WRITE && stream->file->permition != PERMISSION_READ_WRITE && stream->file->permition != PERMISSION_APPEND)
{
return 0;
}
// check if the file is in filesopen
if (std::find(myFILEs_open.begin(), myFILEs_open.end(), inodes[stream->file->file_num].name) == myFILEs_open.end())
{
return 0;
}
// int current_offset = inodes[stream->file->file_num].current_offset;
// int used_size = inodes[stream->file->file_num].used_size;
int size_to_write = size * nmemb;
int size_to_write_from_buffer = 0;
size_to_write_from_buffer = size_to_write;
// use mywrite to write to the file
mywrite(stream->file->file_num, (void *)ptr, size_to_write_from_buffer);
// break myFILE.cpp:164
// memcpy(stream->file_buffer + current_offset, ptr, size_to_write_from_buffer);
// stream->file->current_offset += size_to_write_from_buffer;
return size_to_write_from_buffer;
}
int myfseek(myFILE *stream, long offset, int whence)
{
// check if the file is opened in write mode
if (stream->file->permition != PERMISSION_READ && stream->file->permition != PERMISSION_READ_WRITE && stream->file->permition != PERMISSION_APPEND)
{
return -1;
}
// check if the file is in filesopen
if (std::find(myFILEs_open.begin(), myFILEs_open.end(), inodes[stream->file->file_num].name) == myFILEs_open.end())
{
return -1;
}
int current_offset = inodes[stream->file->file_num].current_offset;
int used_size = inodes[stream->file->file_num].used_size;
int size_to_seek = offset;
int size_to_seek_from_buffer = 0;
if (whence == SEEK_SET)
{
if (size_to_seek > used_size)
{
return -1;
}
else
{
size_to_seek_from_buffer = size_to_seek;
}
}
else if (whence == SEEK_CUR)
{
if (current_offset + size_to_seek > used_size)
{
return -1;
}
else
{
size_to_seek_from_buffer = current_offset + size_to_seek;
}
}
else if (whence == SEEK_END)
{
if (used_size + size_to_seek > used_size)
{
return -1;
}
else
{
size_to_seek_from_buffer = used_size + size_to_seek;
}
}
else
{
return -1;
}
stream->file->current_offset = size_to_seek_from_buffer;
inodes[stream->file->file_num].current_offset = size_to_seek_from_buffer;
return 0;
}
int myfscanf(myFILE *stream, const char *format, ...)
{
myfseek(stream, 0, SEEK_SET);
// check if the file is opened in write mode
if (stream->file->permition != PERMISSION_READ && stream->file->permition != PERMISSION_READ_WRITE && stream->file->permition != PERMISSION_APPEND)
{
return -1;
}
// check if the file is in filesopen
if (std::find(myFILEs_open.begin(), myFILEs_open.end(), inodes[stream->file->file_num].name) == myFILEs_open.end())
{
return -1;
}
// need to be like myfprintf but the opposite
// so first we start with myread
int current_offset = 0;
int used_size = inodes[stream->file->file_num].used_size;
int size_to_read = used_size - current_offset;
char *buffer = (char *)malloc(size_to_read);
// break myFILE.cpp:273
myread(stream->file->file_num, buffer, size_to_read);
// now we start to check which format we have
char tmp[20];
int i = 0, j = 0;
va_list vl;
va_start(vl, format);
while (format[i] != '\0')
{
if (format[i] == '%')
{
i++;
if (format[i] == 'd')
{
bzero(tmp, 20);
int *p = va_arg(vl, int *);
int x = 0;
while (buffer[j] != ' ' && buffer[j] != '\n' && buffer[j] != '\0')
{
tmp[x] = buffer[j];
j++;
x++;
}
tmp[x] = '\0';
// break myFILE.cpp:301
//
*p = atoi(tmp);
j++;
}
else if (format[i] == 's')
{
bzero(tmp, 20);
char *p = va_arg(vl, char *);
int x = 0;
while (buffer[j] != ' ' && buffer[j] != '\n' && buffer[j] != '\0')
{
tmp[x] = buffer[j];
j++;
x++;
}
tmp[x] = '\0';
strcpy(p, tmp);
j++;
}
else if (format[i] == 'c')
{
bzero(tmp, 20);
char *p = va_arg(vl, char *);
int x = 0;
while (buffer[j] != ' ' && buffer[j] != '\n' && buffer[j] != '\0')
{
tmp[j] = buffer[j];
j++;
x++;
}
tmp[x] = '\0';
*p = tmp[0];
j++;
}
else if (format[i] == 'f')
{
bzero(tmp, 20);
float *p = va_arg(vl, float *);
int x = 0;
while (buffer[j] != ' ' && buffer[j] != '\n' && buffer[j] != '\0')
{
tmp[x] = buffer[j];
j++;
x++;
}
tmp[x] = '\0';
*p = atof(tmp);
j++;
}
else
{
return -1;
}
}
else
{
i++;
}
}
return 0;
}
int mystrlen(char *str)
{
int i = 0;
while (str[i] != '\0')
{
i++;
}
return i;
}
int myfprintf(myFILE *stream, const char *format, ...)
{
// check if the file is opened in write mode
if (stream->file->permition != PERMISSION_WRITE && stream->file->permition != PERMISSION_READ_WRITE && stream->file->permition != PERMISSION_APPEND)
{
return -1;
}
// check if the file is in filesopen
if (std::find(myFILEs_open.begin(), myFILEs_open.end(), inodes[stream->file->file_num].name) == myFILEs_open.end())
{
return -1;
}
char buff[100] = {0}, tmp[20];
int s_amount = 0;
int i = 0, j = 0;
va_list vl;
va_start(vl, format);
while (format && format[i])
{
if (format[i] == '%')
{
i++;
switch (format[i])
{
/* Convert char */
case 'c':
{
bzero(tmp, 20);
buff[j] = (char)va_arg(vl, int);
j++;
break;
}
/* Convert decimal */
case 'd':
{
bzero(tmp, 20);
_itoa(va_arg(vl, int), tmp, 10);
strcpy(&buff[j], tmp);
j += strlen(tmp);
break;
}
case 'f':
{
bzero(tmp, 20);
float *p = va_arg(vl, float *);
// copy the float to tmp
sprintf(tmp, "%f", *p);
int x = 0;
while (tmp[x] != ' ' && tmp[x] != '\n' && tmp[x] != '\0' && tmp[x] != '0')
{
buff[j] = tmp[x];
j++;
x++;
}
// tmp[x] = '\0';
// *p = atof(tmp);
j++;
break;
}
/* copy string */
case 's':
{
// copy the string from the va_arg
s_amount++;
// strcpy(&buff[j], va_arg(vl, char *));
bzero(tmp, 20);
char *p = va_arg(vl, char *);
int x = 0;
while (p[x] != ' ' && p[x] != '\n' && p[x] != '\0')
{
buff[j] = p[x];
j++;
x++;
}
buff[j] = ' ';
// tmp[x] = '\0';
// strcpy(p, tmp);
j++;
break;
}
}
}
else
{
buff[j] = format[i];
j++;
}
i++;
}
size_t size_to_write = mystrlen(buff);
// break myFILE.cpp:357
mywrite(stream->file->file_num, buff, size_to_write + s_amount);
va_end(vl);
return mystrlen(buff);
}
char *_itoa(int i, char *strout, int base)
{
char *str = strout;
int digit, sign = 0;
if (i < 0)
{
sign = 1;
i *= -1;
}
while (i)
{
digit = i % base;
*str = (digit > 9) ? ('A' + digit - 10) : '0' + digit;
i = i / base;
str++;
}
if (sign)
{
*str++ = '-';
}
*str = '\0';
_strrev(strout);
return strout;
}
char *_strrev(char *str)
{
int i;
int len = 0;
char c;
if (!str)
return NULL;
while (str[len] != '\0')
{
len++;
}
for (i = 0; i < (len / 2); i++)
{
c = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = c;
}
return str;
}