-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.cpp
More file actions
1130 lines (938 loc) · 32 KB
/
Copy pathFileHandler.cpp
File metadata and controls
1130 lines (938 loc) · 32 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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "FileHandler.h"
namespace FileObj
{
ostream& operator<<(ostream& os, const file_data& data)
{
string last_move_data = "Unknown";
if (data.last_move == READ_OP)
last_move_data = "Read";
else if (data.last_move == WRITE_OP)
last_move_data = "Write";
os << "The file at: " << data.file_path << " ~(" << data.file_access << ")~ --> At a length of: " << data.file_len <<
"\n\t-- File's name: " << data.file_name << " - File's extension: " << data.extension <<
"\n\t-- Buffer's Type: " << data.buffer_type << " (" << data.buffer_type_number << ")" <<
"\n\t-- Buffer's Size: " << data.buffer_size <<
"\n\t-- File's last move was: " << last_move_data << " Operation!" << std::endl;
return os;
}
/*
The function checks the type of the file and return the right string for the opening mode.
*/
string FileHandler::getFileStreamType(const openFileModes& file_mode) const noexcept
{
switch (file_mode)
{
case openFileModes::read: { return "r"; }
case openFileModes::read_b: { return "rb"; }
case openFileModes::read_p: { return "r+"; }
case openFileModes::read_bp: { return "rb+"; }
case openFileModes::write: { return "w"; }
case openFileModes::write_b: { return "wb"; }
case openFileModes::write_p: { return "w+"; }
case openFileModes::write_bp: { return "wb+"; }
case openFileModes::append: { return "a"; }
case openFileModes::append_b: { return "ab"; }
case openFileModes::append_p: { return "a+"; }
case openFileModes::append_bp: { return "ab+"; }
default:
return DEFUALT_MODE;
}
return DEFUALT_MODE;
}
void FileHandler::getLineWithPromise(promise<retObj<string>>&& retVal, unsigned int numline, const int pos, unsigned int buff_size, const bool& auto_rewind, const bool& flush_file) noexcept
{
if (this->file != NULL)
{
if (this->file_access == openFileModes::read || this->file_access == openFileModes::read_p ||
this->file_access == openFileModes::write_p || this->file_access == openFileModes::append_p ||
this->file_access == openFileModes::read_b || this->file_access == openFileModes::read_bp ||
this->file_access == openFileModes::write_bp || this->file_access == openFileModes::append_bp)
{
this->last_move = READ_OP;
if (flush_file && this->buffer_type != bufferType::non_buffer) { fflush(this->file); }
if (buff_size < MIN_BUFFER_GETLINE_SIZE || buff_size > MAX_BUFFER_GETLINE_SIZE) { buff_size = DFLT_BUFF_GLINE_SIZE; }
file_mutex.lock();
if (pos >= 0)
{
this->moveCursorInFile(filePosSet::start_file, pos);
}
else
{
this->moveCursorInFile(filePosSet::start_file);
}
if (feof(this->file)) { retVal.set_value({ "", ra_succss }); this->rewindFileOneStep(); file_mutex.unlock(); return; }
char tmp = 0;
char* data = new char[buff_size + 1]();
unsigned int ccount = 0, buffcount = 1;
while (!feof(this->file))
{
tmp = fgetc(this->file);
if (tmp == EOF || tmp == '\0' || (numline <= 0 && tmp == '\n')) break;
if (tmp == '\n') numline--;
if (tmp == '\r') continue;
if (numline <= 0 && tmp != '\n') data[ccount++] = tmp;
if (ccount > buff_size * buffcount)
{
char* ndata = new char[buff_size * (++buffcount) + 1]();
memcpy(ndata, data, buff_size * (buffcount - 1) + 1);
delete data;
data = ndata;
}
}
this->rewindFileOneStep();
file_mutex.unlock();
if ((tmp == EOF || tmp == '\0') && numline > 0) { retVal.set_value({ "", ra_endoffile_fail }); return; }
if (!this->clearCharsCanUse)
{
char* ndata = new char[ccount + 1]();
for (unsigned int i = 0, j = 0; i < ccount; i++)
{
const char ch = data[i];
if (this->charsCanUse[(int)ch])
{
ndata[j] = ch;
j++;
}
}
delete data;
data = ndata;
}
string str = data;
delete data;
data = nullptr;
retVal.set_value({ str, ra_succss });
return;
}
}
retVal.set_value({ "", ra_fileisclosed_fail });
}
/*
The function fixes the string path by replacing back slash to forward slash.
@ It is a static function.
*/
void FileHandler::fixPath(string& path) noexcept
{
std::replace(path.begin(), path.end(), '\\', '/');
}
/*
The function gets file's name out of the path.
@ It is a static function.
*/
string FileHandler::getFileName(const string& path) noexcept
{
auto pos1 = path.find_last_of("/");
auto pos2 = path.find_last_of(".");
if (pos1 != string::npos && pos2 != string::npos)
{
return path.substr(pos1 + 1, pos2 - pos1);
}
return "";
}
/*
The function constructs a FileHandler object.
*/
FileHandler::FileHandler() noexcept : file(NULL), file_buffer(NULL), thread_safe(false), file_buffer_size(0), buffer_type(DEFUALT_BUFFER),
file_access(DEFUALT_MODE_ENUM), last_move(0), last_file_place(SEEK_SET), clearCharsCanUse(true)
{
for (int i = 0; i < MAX_CHAR_CAPACITY; i++)
{
this->charsCanUse[i] = true;
}
}
/*
The function constructs a FileHandler object by trying to open a file.
*/
FileHandler::FileHandler(const string& path, const openFileModes& file_mode, const bool thread_safe, const bufferType& buff_type, size_t buff_size)
: file(NULL), file_buffer(NULL), thread_safe(thread_safe), file_buffer_size(0), buffer_type(DEFUALT_BUFFER), file_access(DEFUALT_MODE_ENUM), last_move(0), last_file_place(SEEK_SET), clearCharsCanUse(true)
{
if (!(this->openFile(path, file_mode, this->thread_safe, buffer_type, buff_size))) { throw FileHandlerException("Error - FileHandler: File couldn't be opened!"); }
for (int i = 0; i < MAX_CHAR_CAPACITY; i++)
{
this->charsCanUse[i] = true;
}
}
/*
The function gets file's extension out of the path.
@ It is a static function.
*/
string FileHandler::getFileExtenstion(const string& path) noexcept
{
auto pos = path.find_last_of(".");
if (pos != string::npos)
{
return path.substr(pos + 1, path.size() - pos);
}
return "";
}
/*
The function gets the file's length.
--> Might be slow so try to use the function only once and then store the value!
*/
long FileHandler::getFilesLength() noexcept
{
if (this->file == NULL)
return -1;
long curr_pos = ftell(this->file);
fseek(this->file, 0, SEEK_END);
long file_size = ftell(this->file);
fseek(this->file, curr_pos, SEEK_SET);
return file_size;
}
/*
The function enables the option to write data into the file.
--> The function is throwable!
*/
FileHandler& FileHandler::operator<<(const char* str)
{
if (this->file != NULL)
{
if (this->file_access == openFileModes::write || this->file_access == openFileModes::write_p ||
this->file_access == openFileModes::append || this->file_access == openFileModes::append_p ||
this->file_access == openFileModes::read_p || this->file_access == openFileModes::write_b ||
this->file_access == openFileModes::write_bp || this->file_access == openFileModes::append_b ||
this->file_access == openFileModes::append_bp || this->file_access == openFileModes::read_bp)
{
this->last_move = WRITE_OP;
if (str == nullptr) { return *this; }
unsigned int length = strlen(str);
if (length <= 0) { return *this; }
if (!this->clearCharsCanUse)
{
char* ndata = new char[length + 1]();
unsigned int nsize = 0;
for (unsigned int i = 0; i < length + 1; i++)
{
const char ch = str[i];
if (this->charsCanUse[(int)ch])
{
ndata[nsize] = ch;
nsize++;
}
}
bool val = fwrite(ndata, sizeof(char), nsize, this->file) == nsize;
delete ndata;
ndata = nullptr;
if (val) return *this;
}
else if (fwrite(str, sizeof(char), length, this->file) == length) return *this;
throw FileHandlerException("Error - FileHandler: Failed to write into the file!", ra_writefile_fail);
}
throw FileHandlerException("Error - FileHandler: The file access type doesn't allow to write into the file!", ra_fileaccesstype_fail);
}
throw FileHandlerException("Error - FileHandler: The file isn't opened!", ra_fileisclosed_fail);
}
/*
The function enables the option to write data into the file.
--> The function is throwable!
*/
FileHandler& FileHandler::operator<<(const string& str)
{
if (this->file != NULL)
{
if (this->file_access == openFileModes::write || this->file_access == openFileModes::write_p ||
this->file_access == openFileModes::append || this->file_access == openFileModes::append_p ||
this->file_access == openFileModes::read_p || this->file_access == openFileModes::write_b ||
this->file_access == openFileModes::write_bp || this->file_access == openFileModes::append_b ||
this->file_access == openFileModes::append_bp || this->file_access == openFileModes::read_bp)
{
this->last_move = WRITE_OP;
if (str.empty()) { return *this; }
if (!this->clearCharsCanUse)
{
char* ndata = new char[str.size() + 1]();
unsigned int nsize = 0;
for (unsigned int i = 0; i < str.size() + 1; i++)
{
const char ch = str[i];
if (this->charsCanUse[(int)ch])
{
ndata[nsize] = ch;
nsize++;
}
}
bool val = fwrite(ndata, sizeof(char), nsize, this->file) == nsize;
delete ndata;
ndata = nullptr;
if (val) return *this;
}
else if (fwrite(str.c_str(), sizeof(char), str.size(), this->file) == str.size()) return *this;
throw FileHandlerException("Error - FileHandler: Failed to write into the file!", ra_writefile_fail);
}
throw FileHandlerException("Error - FileHandler: The file access type doesn't allow to write into the file!", ra_fileaccesstype_fail);
}
throw FileHandlerException("Error - FileHandler: The file isn't opened!", ra_fileisclosed_fail);
}
/*
The function enables the option to write data into the file.
--> The function is throwable!
*/
FileHandler& FileHandler::operator<<(string&& str)
{
if (this->file != NULL)
{
if (this->file_access == openFileModes::write || this->file_access == openFileModes::write_p ||
this->file_access == openFileModes::append || this->file_access == openFileModes::append_p ||
this->file_access == openFileModes::read_p || this->file_access == openFileModes::write_b ||
this->file_access == openFileModes::write_bp || this->file_access == openFileModes::append_b ||
this->file_access == openFileModes::append_bp || this->file_access == openFileModes::read_bp)
{
this->last_move = WRITE_OP;
if (str.empty()) { return *this; }
if (!this->clearCharsCanUse)
{
char* ndata = new char[str.size() + 1]();
unsigned int nsize = 0;
for (unsigned int i = 0; i < str.size() + 1; i++)
{
const char ch = str[i];
if (this->charsCanUse[(int)ch])
{
ndata[nsize] = ch;
nsize++;
}
}
bool val = fwrite(ndata, sizeof(char), nsize, this->file) == nsize;
delete ndata;
ndata = nullptr;
if (val) return *this;
}
else if (fwrite(str.c_str(), sizeof(char), str.size(), this->file) == str.size()) return *this;
throw FileHandlerException("Error - FileHandler: Failed to write into the file!", ra_writefile_fail);
}
throw FileHandlerException("Error - FileHandler: The file access type doesn't allow to write into the file!", ra_fileaccesstype_fail);
}
throw FileHandlerException("Error - FileHandler: The file isn't opened!", ra_fileisclosed_fail);
}
/*
The function enables the option to read data from the file.
--> The function is throwable!
*/
FileHandler& FileHandler::operator>>(char* str)
{
if (this->file != NULL)
{
this->last_move = READ_OP;
if (feof(this->file)) { throw FileHandlerException("Error - FileHandler: Failed to read from file -> End of file was reached!", ra_endoffile_fail); }
if (this->file_access == openFileModes::read || this->file_access == openFileModes::read_p ||
this->file_access == openFileModes::write_p || this->file_access == openFileModes::append_p ||
this->file_access == openFileModes::read_b || this->file_access == openFileModes::read_bp ||
this->file_access == openFileModes::write_bp || this->file_access == openFileModes::append_bp)
{
char tmp = 0;
char* data = new char[DFLT_BUFF_GLINE_SIZE + 1]();
unsigned int ccount = 0, buffcount = 1;
while (!feof(this->file))
{
tmp = fgetc(this->file);
if (tmp == EOF || tmp == '\0') break;
if (tmp == '\r') continue;
data[ccount++] = tmp;
if (ccount > DFLT_BUFF_GLINE_SIZE * buffcount)
{
char* ndata = new char[DFLT_BUFF_GLINE_SIZE * (++buffcount) + 1]();
memcpy(ndata, data, DFLT_BUFF_GLINE_SIZE * (buffcount - 1) + 1);
delete data;
data = ndata;
}
if (tmp == '\n') break;
}
if (!this->clearCharsCanUse)
{
char* ndata = new char[ccount + 1]();
for (unsigned int i = 0, j = 0; i < ccount; i++)
{
const char ch = data[i];
if (this->charsCanUse[(int)ch])
{
ndata[j] = ch;
j++;
}
}
delete data;
data = ndata;
}
str = data;
return *this;
}
throw FileHandlerException("Error - FileHandler: The file access type doesn't allow to read from the file!", ra_fileaccesstype_fail);
}
throw FileHandlerException("Error - FileHandler: The file isn't opened!", ra_fileisclosed_fail);
}
/*
The function enables the option to read data from the file.
--> The function is throwable!
*/
FileHandler& FileHandler::operator>>(string& str)
{
if (this->file != NULL)
{
this->last_move = READ_OP;
if (feof(this->file)) { throw FileHandlerException("Error - FileHandler: Failed to read from file -> End of file was reached!", ra_endoffile_fail); }
if (this->file_access == openFileModes::read || this->file_access == openFileModes::read_p ||
this->file_access == openFileModes::write_p || this->file_access == openFileModes::append_p ||
this->file_access == openFileModes::read_b || this->file_access == openFileModes::read_bp ||
this->file_access == openFileModes::write_bp || this->file_access == openFileModes::append_bp)
{
char tmp = 0;
char* data = new char[DFLT_BUFF_GLINE_SIZE + 1]();
unsigned int ccount = 0, buffcount = 1;
while (!feof(this->file))
{
tmp = fgetc(this->file);
if (tmp == EOF || tmp == '\0') break;
if (tmp == '\r') continue;
data[ccount++] = tmp;
if (ccount > DFLT_BUFF_GLINE_SIZE * buffcount)
{
char* ndata = new char[DFLT_BUFF_GLINE_SIZE * (++buffcount) + 1]();
memcpy(ndata, data, DFLT_BUFF_GLINE_SIZE * (buffcount - 1) + 1);
delete data;
data = ndata;
}
if (tmp == '\n') break;
}
if (!this->clearCharsCanUse)
{
char* ndata = new char[ccount + 1]();
for (unsigned int i = 0, j = 0; i < ccount; i++)
{
const char ch = data[i];
if (this->charsCanUse[(unsigned int)ch])
{
ndata[j] = ch;
j++;
}
}
delete data;
data = ndata;
}
str += data;
delete data;
data = nullptr;
return *this;
}
throw FileHandlerException("Error - FileHandler: The file access type doesn't allow to read from the file!", ra_fileaccesstype_fail);
}
throw FileHandlerException("Error - FileHandler: The file isn't opened!", ra_fileisclosed_fail);
}
/*
The function enables to get the wanted index of the ignoring table.
--> The functions may throw.
*/
bool& FileHandler::operator[](const unsigned int index)
{
if (index >= MAX_CHAR_CAPACITY) { throw FileHandlerException("Error - FileHandler: The wanted ignoring index is out of range!", ra_outofrange_fail); }
return charsCanUse[index];
}
/*
The function enables to get the file buffer, with on updating options.
*/
char const* const* const FileHandler::operator()() const noexcept
{
return &(this->file_buffer);
}
/*
The function opens the wanted file by giving the right data.
@ If a file is already opened then it will try to close the stored file, but if it fails
it will return false and won't keep on going.
*/
bool FileHandler::openFile(const string& f_path, const openFileModes& file_mode, const bool thread_safe, const bufferType& buff_type, size_t buff_size) noexcept
{
string open_mode = getFileStreamType(file_mode);
string fnew_path = f_path;
fixPath(fnew_path);
if (this->file != NULL) // Closing old file and if can't close it then return false
{
if (!(this->closeFile())) { return false; }
}
if ((this->file = fopen(fnew_path.c_str(), open_mode.c_str())) != NULL)
{
this->file_access = file_mode;
this->thread_safe = thread_safe;
if (buff_size > MAX_BUFFER_SIZE)
{
buff_size = MAX_BUFFER_SIZE;
}
else if (buff_size < MIN_BUFFER_SIZE)
{
buff_size = MIN_BUFFER_SIZE;
}
if (this->file_buffer == NULL && buff_type != bufferType::non_buffer) // Buffer allocation check
{
this->file_buffer = new char[buff_size];
this->file_buffer_size = buff_size * sizeof(char);
if (this->file_buffer == NULL) // Secondary checking that the allocation/reallocation worked
{
fclose(this->file);
this->file = NULL;
this->file_buffer_size = 0;
return false;
}
}
else if (buff_type != bufferType::non_buffer)
{
delete[] this->file_buffer;
this->file_buffer = new char[buff_size];
this->file_buffer_size = buff_size * sizeof(char);
if (this->file_buffer == NULL) // Secondary checking that the allocation/reallocation worked
{
fclose(this->file);
this->file = NULL;
this->file_buffer_size = 0;
return false;
}
}
else if (this->file_buffer != NULL)
{
delete[] this->file_buffer;
this->file_buffer = NULL;
this->file_buffer_size = 0;
}
switch (buff_type)
{
case bufferType::non_buffer: { setvbuf(this->file, this->file_buffer, _IONBF, buff_size); break; }
case bufferType::line_buffer: { setvbuf(this->file, this->file_buffer, _IOLBF, buff_size); break; }
case bufferType::full_buffer: { setvbuf(this->file, this->file_buffer, _IOFBF, buff_size); break; }
default: { setvbuf(this->file, this->file_buffer, DEFUALT_BUFFER_NAME, buff_size); }
}
this->file_path = fnew_path;
this->file_name = getFileName(this->file_path);
this->extension = getFileExtenstion(this->file_path);
return true;
}
return false;
}
/*
The function is writing data into a file by the given parameters.
*/
bool FileHandler::writeToFile(const string& data, const int& pos, const bool& auto_rewind, const bool& flush_file) noexcept
{
if (this->file != NULL)
{
if (this->file_access == openFileModes::write || this->file_access == openFileModes::write_p ||
this->file_access == openFileModes::append || this->file_access == openFileModes::append_p ||
this->file_access == openFileModes::read_p || this->file_access == openFileModes::write_b ||
this->file_access == openFileModes::write_bp || this->file_access == openFileModes::append_b ||
this->file_access == openFileModes::append_bp || this->file_access == openFileModes::read_bp)
{
this->last_move = WRITE_OP;
if (flush_file) { this->flushFile(); }
if (data.size() <= 0) { return true; }
if (pos >= 0)
{
this->moveCursorInFile(filePosSet::start_file, pos);
}
bool val = true;
if (!this->clearCharsCanUse)
{
unsigned int nsize = data.size();
char* ndata = new char[data.size()]();
for (unsigned int i = 0, j = 0; i < data.size(); i++)
{
const char ch = data[i];
if (this->charsCanUse[(int)ch])
{
ndata[j] = ch;
j++;
}
else
{
nsize--;
}
}
val = fwrite(ndata, sizeof(char), nsize, this->file) == nsize;
delete ndata;
ndata = nullptr;
}
else
{
val = fwrite(data.c_str(), sizeof(char), data.size(), this->file) == data.size();
}
if (pos >= 0 && auto_rewind)
{
this->rewindFileOneStep();
}
return val;
}
}
return false;
}
/*
The function is reading data from the file by the given parameters.
*/
retObj<string> FileHandler::readFromFile(const size_t& count, const int& pos, const bool& auto_rewind, const bool& flush_file) noexcept
{
if (this->file != NULL)
{
if (this->file_access == openFileModes::read || this->file_access == openFileModes::read_p ||
this->file_access == openFileModes::write_p || this->file_access == openFileModes::append_p ||
this->file_access == openFileModes::read_b || this->file_access == openFileModes::read_bp ||
this->file_access == openFileModes::write_bp || this->file_access == openFileModes::append_bp)
{
this->last_move = READ_OP;
if (flush_file && this->buffer_type != bufferType::non_buffer) { fflush(this->file); }
if (count <= 0) { return { "", ra_succss }; }
if (pos >= 0)
{
this->moveCursorInFile(filePosSet::start_file, pos);
}
auto rewind_check = [&]()
{
if (pos >= 0 && auto_rewind)
{
this->rewindFileOneStep();
}
};
char* temp_str = new char[(unsigned int)count + 1]();
bool read_work = fread(temp_str, sizeof(char), count, this->file) == (count);
if (!this->clearCharsCanUse)
{
char* ndata = new char[(unsigned int)count + 1]();
for (unsigned int i = 0, j = 0; i < (unsigned int)count + 1; i++)
{
const char ch = temp_str[i];
if (this->charsCanUse[(int)ch])
{
ndata[j] = ch;
j++;
}
}
delete temp_str;
temp_str = ndata;
}
string str = temp_str;
delete temp_str;
temp_str = nullptr;
char tmpv[2] = { EOF, 0 };
if (!read_work && feof(this->file))
{
rewind_check();
return { string(tmpv), ra_succss };
}
else if (!read_work)
{
rewind_check();
return { "", ra_readfile_fail };
}
else if (feof(this->file))
{
rewind_check();
return { str.append(tmpv), ra_succss };
}
rewind_check();
return { str, ra_succss };
}
}
return { "", ra_fileisclosed_fail };
}
/*
The function gets a line out of a file.
@ This file returns error if during the getting line, the file met \0 of EOF operators and the wanted line wasn't reached yet.
*/
retObj<string> FileHandler::getLine(unsigned int numline, const int& pos, unsigned int buff_size, const bool& auto_rewind, const bool& flush_file) noexcept
{
if (this->file != NULL)
{
if (this->file_access == openFileModes::read || this->file_access == openFileModes::read_p ||
this->file_access == openFileModes::write_p || this->file_access == openFileModes::append_p ||
this->file_access == openFileModes::read_b || this->file_access == openFileModes::read_bp ||
this->file_access == openFileModes::write_bp || this->file_access == openFileModes::append_bp)
{
this->last_move = READ_OP;
if (flush_file && this->buffer_type != bufferType::non_buffer) { fflush(this->file); }
if (buff_size < MIN_BUFFER_GETLINE_SIZE || buff_size > MAX_BUFFER_GETLINE_SIZE) { buff_size = DFLT_BUFF_GLINE_SIZE; }
if (pos >= 0)
{
this->moveCursorInFile(filePosSet::start_file, pos);
}
else
{
this->moveCursorInFile(filePosSet::start_file);
}
if (feof(this->file)) { return { "", ra_succss }; }
char tmp = 0;
char* data = new char[buff_size + 1]();
unsigned int ccount = 0, buffcount = 1;
while (!feof(this->file))
{
tmp = fgetc(this->file);
if (tmp == EOF || tmp == '\0' || (numline <= 0 && tmp == '\n')) break;
if (tmp == '\n') numline--;
if (tmp == '\r') continue;
if (numline <= 0 && tmp != '\n') data[ccount++] = tmp;
if (ccount > buff_size * buffcount)
{
char* ndata = new char[buff_size * (++buffcount) + 1]();
memcpy(ndata, data, buff_size * (buffcount - 1) + 1);
delete data;
data = ndata;
}
}
this->rewindFileOneStep();
if ((tmp == EOF || tmp == '\0') && numline > 0) return { "", ra_endoffile_fail };
if (!this->clearCharsCanUse)
{
char* ndata = new char[ccount + 1]();
for (unsigned int i = 0, j = 0; i < ccount; i++)
{
const char ch = data[i];
if (this->charsCanUse[(int)ch])
{
ndata[j] = ch;
j++;
}
}
delete data;
data = ndata;
}
string str = data;
delete data;
data = nullptr;
return { str, ra_succss };
}
}
return { "", ra_fileisclosed_fail };
}
/*
The function gets out of a file.
@ This file returns error if during the getting line, the file met \0 of EOF operators and the wanted line wasn't reached yet.
*/
void FileHandler::getLineMultiThreaded(retObj<map<pair<unsigned int, int>, retObj<string>>>& retObject, const vector<pair<unsigned int, int>>& lines_pos, unsigned int buff_size, const bool& auto_rewind, const bool& flush_file)
{
if (this->file != NULL)
{
if (!this->thread_safe) { retObject = { {}, ra_notthreadsafe_fail }; return; }
if (lines_pos.size() <= 0) { retObject = { {}, ra_succss }; return; }
future<retObj<string>>* ftr = new future<retObj<string>>[lines_pos.size()];
thread** thds = new thread*[lines_pos.size()];
for (int i = 0; i < lines_pos.size(); i++)
{
promise<retObj<string>> p;
ftr[i] = p.get_future();
thds[i] = new thread(&FileHandler::getLineWithPromise, this, std::move(p), lines_pos[i].first, lines_pos[i].second, buff_size, std::cref(auto_rewind), std::cref(flush_file));
}
for (int i = 0; i < lines_pos.size(); i++)
{
retObject.obj.insert(pair<pair<unsigned int, int>, retObj<string>>(pair<unsigned int, int>(lines_pos[i].first, lines_pos[i].second), ftr[i].get()));
}
for (int i = 0; i < lines_pos.size(); i++)
{
thds[i]->join();
delete thds[i];
thds[i] = nullptr;
}
delete[] ftr;
delete[] thds;
ftr = nullptr;
thds = nullptr;
retObject.statusObj = ra_succss;
return;
}
retObject = { {}, ra_fileisclosed_fail };
}
/*
The function is closing a file and the buffer if opened.
*/
bool FileHandler::closeFile() noexcept { if (this->file_buffer != NULL) { delete[] this->file_buffer; this->file_buffer = NULL; this->file_buffer_size = 0; } file_path = ""; file_name = ""; extension = ""; thread_safe = false; if (this->file != NULL) { fclose(this->file); this->file = NULL; return true; } return false; }
/*
The function deletes the function from the computer.
*/
bool FileHandler::removeFile() noexcept { closeFile(); return !remove(this->file_path.c_str()); }
/*
The function flushed the buffer if existst and if in writing mode, else just return true.
*/
bool FileHandler::flushFile() noexcept
{
if (this->buffer_type == bufferType::non_buffer) { return true; }
if (this->file != NULL && (this->file_access == openFileModes::write || this->file_access == openFileModes::write_b ||
this->file_access == openFileModes::append || this->file_access == openFileModes::append_b || this->last_move == WRITE_OP))
{
return !fflush(this->file);
}
return false;
}
/*
The function changes the file buffer type and size.
*/
bool FileHandler::changeFileBuffer(const bufferType& buff_type, size_t buff_size) noexcept
{
if (file != NULL)
{
if (buff_size > MAX_BUFFER_SIZE)
{
buff_size = MAX_BUFFER_SIZE;
}
else if (buff_size < MIN_BUFFER_SIZE)
{
buff_size = MIN_BUFFER_SIZE;
}
if (this->file_buffer == NULL && buff_type != bufferType::non_buffer) // Buffer allocation check
{
this->file_buffer = new char[buff_size];
this->file_buffer_size = buff_size * sizeof(char);
if (this->file_buffer == NULL) // Secondary checking that the allocation/reallocation worked
{
fclose(this->file);
this->file = NULL;
this->file_buffer_size = 0;
return false;
}
}
else if (buff_type != bufferType::non_buffer)
{
delete[] this->file_buffer;
this->file_buffer = new char[buff_size];
this->file_buffer_size = buff_size * sizeof(char);
if (this->file_buffer == NULL) // Secondary checking that the allocation/reallocation worked
{
fclose(this->file);
this->file = NULL;
this->file_buffer_size = 0;
return false;
}
}
else if (this->file_buffer != NULL)
{
delete[] this->file_buffer;
this->file_buffer = NULL;
this->file_buffer_size = 0;
}
switch (buff_type)
{
case bufferType::non_buffer: { setvbuf(this->file, this->file_buffer, _IONBF, buff_size); break; }
case bufferType::line_buffer: { setvbuf(this->file, this->file_buffer, _IOLBF, buff_size); break; }
case bufferType::full_buffer: { setvbuf(this->file, this->file_buffer, _IOFBF, buff_size); break; }
default: { setvbuf(this->file, this->file_buffer, DEFUALT_BUFFER_NAME, buff_size); }
}
return true;
}
return false;
}
/*
The function gets the file's state.
*/
file_data FileHandler::getFileState() noexcept
{
unsigned int file_len = 0;
unsigned int buffer_type_number = 0;
string buffer_type;
switch (this->buffer_type)
{
case bufferType::non_buffer: { buffer_type = "No Buffering"; buffer_type_number = _IONBF; break; }
case bufferType::line_buffer: { buffer_type = "Line Buffering"; buffer_type_number = _IOLBF; break; }
case bufferType::full_buffer: { buffer_type = "Full Buffering"; buffer_type_number = _IOFBF; break; }
default:
{
buffer_type = DEFUALT_BUFFER_NAME_TXT;
buffer_type_number = DEFUALT_BUFFER_NAME;
}
}
return { this->file_path, this->file_name, this->extension, this->file_buffer, buffer_type_number, buffer_type,
getFileStreamType(this->file_access), getFilesLength(), this->file_buffer_size, this->last_move };
}