-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.h
808 lines (605 loc) · 16 KB
/
file.h
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
#pragma once
#include "func.h"
#include "time_show.h"
class File
{
public:
std::string dialogue[200];
std::string choice[200][5];
std::string path_dialogue;
std::string path_choice;
std::string path_save_1;
std::string path_save_2;
std::string path_save_3;
std::string path_save_4;
// OLD:
bool FindWord(const std::string filename, const std::string word);
bool FindWord2(std::string word);
int Rewrite(std::string file_path, std::string file_text);
int Write(std::string file_path, std::string file_text);
int Read(std::string file_path);
int Read_write(std::string file_path);
int FindDialogueOLD(std::string mark);
int ShowDialogueOLD(std::string mark);
int FindChoiceOLD(std::string mark);
int CheckChoiceOLD(std::string mark, std::string& indicator);
int ShowChoiceOLD(std::string mark);
int FindGoOLD(std::string mark);
int FindWayOLD(std::string& chapter);
// TEST:
int FindDialogue(std::string& dialogue_buffer, int chapter);
int FindChoice(std::string& choice_buffer, int chapter, int number);
int CheckChoice(int chapter, int number, std::string& indicator);
int FindWay(int& chapter, int number);
int SaveChapter(int chapter, int SAVE_SLOT);
int SaveRoom(std::string room_name, int check_room, int SAVE_SLOT);
int SaveInventory(std::string inventory_slot, int slot_number, int SAVE_SLOT);
int LoadChapter(int& save_chapter, int SAVE_SLOT);
std::string LoadSavedName(int SAVE_SLOT);
int LoadRoom(std::string room_name, int& save_room, int SAVE_SLOT);
int LoadInventory(std::string& save_inventory, int slot_number, int SAVE_SLOT);
File();
~File();
};
File::File(void)
{
//cerr << "Object is being created" << endl;
}
File::~File(void)
{
//cerr << "Object is being deleted" << endl;
}
bool File::FindWord(const std::string filename, const std::string word) //
{
std::string tmp;
bool result = false;
std::ifstream file(filename);
while (file >> tmp)
{
if (word == tmp)
{
result = true;
}
}
return result;
}
bool File::FindWord2(std::string word) //
{
std::string tmp;
bool result = false;
std::ifstream file("Game\\dialogue\\scenario.txt");
while (file >> tmp)
{
//if (word == tmp)
if (tmp == word)
{
result = true;
}
}
file.close();
return result;
}
int File::Rewrite(std::string file_path, std::string file_text) //
{
std::string path = file_path;
std::string text = file_text;
std::ofstream out; // ïîòîê äëÿ çàïèñè
out.open(path); // îêðûâàåì ôàéë äëÿ çàïèñè
if (out.is_open())
{
out << text << std::endl;
}
out.close();
return 0;
}
int File::Write(std::string file_path, std::string file_text) //
{
std::string path = file_path;
std::string text = file_text;
std::ofstream out(path, std::ios::app);
if (out.is_open())
{
out << text << std::endl;
}
out.close();
return 0;
}
int File::Read(std::string file_path) //
{
std::string path = file_path;
std::string line;
std::ifstream in(path); // îêðûâàåì ôàéë äëÿ ÷òåíèÿ
if (in.is_open())
{
while (getline(in, line))
{
std::cout << "\n ";
std::cout << line;
}
}
in.close(); // çàêðûâàåì ôàéë
return 0;
}
int File::Read_write(std::string file_path) //
{
std::string path = file_path;
std::string line;
std::ifstream in(path); // îêðûâàåì ôàéë äëÿ ÷òåíèÿ
if (in.is_open())
{
while (getline(in, line))
{
std::cout << "\n ";
//write(line);
}
}
in.close(); // çàêðûâàåì ôàéë
return 0;
}
int File::FindDialogueOLD(std::string mark) //
{
std::string input = "";
std::string temp = " ";
std::string s = '@' + mark + '@';
std::string f = '#' + mark + '#';
std::string path = "Game\\dialogue\\scenario.txt";
std::string path_choice = "Game\\dialogue\\dialogue.txt";
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp + "\n ";
}
fin.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
Rewrite(path_choice, "\n\n " + input);
return 0;
}
int File::ShowDialogueOLD(std::string mark) //
{
std::string input = "";
std::string temp = " ";
std::string s = '@' + mark + '@';
std::string f = '#' + mark + '#';
std::string path = "Game\\dialogue\\scenario.txt";
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp + "\n ";
}
fin.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
//write("\n\n " + input);
return 0;
}
int File::FindChoiceOLD(std::string mark) //
{
std::string input = "";
std::string temp = " ";
std::string s = '$' + mark + '$';
std::string f = '%' + mark + '%';
std::string path = "Game\\dialogue\\scenario.txt";
std::string path_choice = "Game\\dialogue\\choice.txt";
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
Rewrite(path_choice, input);
return 0;
}
int File::CheckChoiceOLD(std::string mark, std::string& indicator) //
{
FindChoiceOLD(mark);
std::string input = "";
std::string temp = " ";
std::string f = "&";
std::string path = "Game\\dialogue\\choice.txt";
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(input.find(f), input.length() - 1);
indicator = input;
return 0;
}
int File::ShowChoiceOLD(std::string mark) //
{
FindChoiceOLD(mark);
std::string input = "";
std::string temp = " ";
std::string f = "&";
std::string path = "Game\\dialogue\\choice.txt";
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(input.find(f), input.length() - 1);
std::cout << "\n\n " << "> " << input;
return 0;
}
int File::FindGoOLD(std::string mark) //
{
std::string input = "";
std::string temp = " ";
std::string s = '{' + mark + '}';
std::string f = '[' + mark + ']';
std::string path = "Game\\dialogue\\scenario.txt";
std::string path_choice = "Game\\dialogue\\choice.txt";
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
Rewrite(path_choice, input);
return 0;
}
int File::FindWayOLD(std::string& chapter) //
{
std::string input = "";
std::string temp = " ";
std::string s = "&";
std::string path = "Game\\dialogue\\choice.txt";
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(0, input.find(s) + s.length());
chapter = input;
return 0;
}
//==========================================================================(TEST)============
int File::FindDialogue(std::string& dialogue_buffer, int chapter) //
{
std::string input = "";
std::string temp = " ";
std::string s = '@' + std::to_string(chapter) + '@';
std::string f = '#' + std::to_string(chapter) + '#';
std::ifstream in(path_dialogue);
if (in.fail())
{
// I need error message here, like "File not found" etc. -
// the reason of the failure
std::cerr << "ERROR: FAILED TO LOAD " << path_dialogue;
return 1;
}
while (!in.eof())
{
getline(in, temp);
input = input + temp + "\n ";
}
in.close();
input.erase(0, input.find(s) + s.length()); // HIDE NUMBER
//input.erase(0, input.find(s)); // SHOW NUMBER
input.erase(input.find(f), input.length() - 1);
dialogue_buffer = "\n\n " + input;
return 0;
}
int File::FindChoice(std::string& choice_buffer, int chapter, int number) //
{
std::string input = "";
std::string temp = " ";
std::string s = '@' + std::to_string(chapter) + '@' + '(' + std::to_string(number) + ')';
std::string f = '#' + std::to_string(chapter) + '#';
std::string f2 = "&";
std::ifstream in(path_choice);
if (in.fail())
{
// I need error message here, like "File not found" etc. -
// the reason of the failure
std::cerr << "ERROR: FAILED TO LOAD " << path_choice;
return 1;
}
while (!in.eof())
{
getline(in, temp);
input = input + temp;
}
in.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
input.erase(input.find(f2), input.length() - 1); // HIDE WAY
choice_buffer = "> " + input;
return 0;
}
int File::CheckChoice(int chapter, int number, std::string& indicator) //
{
std::string input = "";
std::string temp = " ";
std::string s = '@' + std::to_string(chapter) + '@' + '(' + std::to_string(number) + ')';
std::string f = '#' + std::to_string(chapter) + '#';
std::string f2 = "&";
std::ifstream in(path_choice);
while (!in.eof())
{
getline(in, temp);
input = input + temp;
}
in.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
input.erase(input.find(f2), input.length() - 1);
indicator = input;
return 0;
}
int File::FindWay(int& chapter, int number) //
{
std::string input = "";
std::string temp = " ";
std::string s = '@' + std::to_string(chapter) + '@' + '(' + std::to_string(number) + ')';
std::string f = '#' + std::to_string(chapter) + '#';
std::string f2 = "&";
std::string path = path_choice;
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
input.erase(0, input.find(f2) + f2.length());
chapter = std::stoi(input);
return 0;
}
int File::SaveChapter(int chapter, int SAVE_SLOT) //
{
std::string path;
if (SAVE_SLOT == 1)
{
path = path_save_1;
}
else if (SAVE_SLOT == 2)
{
path = path_save_2;
}
else if (SAVE_SLOT == 3)
{
path = path_save_3;
}
else if (SAVE_SLOT == 4)
{
path = path_save_4;
}
std::string save_chapter = "@" + std::to_string(chapter) + "#";
std::ofstream out; // ïîòîê äëÿ çàïèñè
out.open(path); // îêðûâàåì ôàéë äëÿ çàïèñè
if (out.is_open())
{
//out << "D" << return_current_time_and_date << "T" << "\n" << save_chapter << "\n";
out << "D" << show_current_time() << "T" << "\n" << save_chapter << "\n";
}
out.close();
return 0;
}
int File::SaveRoom(std::string room_name, int check_room, int SAVE_SLOT) //
{
std::string path;
if (SAVE_SLOT == 1)
{
path = path_save_1;
}
else if (SAVE_SLOT == 2)
{
path = path_save_2;
}
else if (SAVE_SLOT == 3)
{
path = path_save_3;
}
else if (SAVE_SLOT == 4)
{
path = path_save_4;
}
std::string save_room = "$" + room_name + "$" + std::to_string(check_room) + "%" + room_name + "%";
std::ofstream out(path, std::ios::app);
if (out.is_open())
{
out << save_room << "\n";
}
out.close();
return 0;
}
int File::SaveInventory(std::string inventory_slot, int slot_number, int SAVE_SLOT) //
{
std::string path;
if (SAVE_SLOT == 1)
{
path = path_save_1;
}
else if (SAVE_SLOT == 2)
{
path = path_save_2;
}
else if (SAVE_SLOT == 3)
{
path = path_save_3;
}
else if (SAVE_SLOT == 4)
{
path = path_save_4;
}
std::string save_inventory_slot;
save_inventory_slot = "[" + std::to_string(slot_number) + "]" + inventory_slot;
save_inventory_slot = save_inventory_slot + "{" + std::to_string(slot_number) + "}";
std::ofstream out(path, std::ios::app);
if (out.is_open())
{
out << save_inventory_slot << "\n";
}
out.close();
return 0;
}
int File::LoadChapter(int& save_chapter, int SAVE_SLOT) //
{
std::string input = "";
std::string temp = " ";
std::string s = "@";
std::string f = "#";
std::string path;
if (SAVE_SLOT == 1)
{
path = path_save_1;
}
else if (SAVE_SLOT == 2)
{
path = path_save_2;
}
else if (SAVE_SLOT == 3)
{
path = path_save_3;
}
else if (SAVE_SLOT == 4)
{
path = path_save_4;
}
std::ifstream fin(path);
if (fin.fail())
{
// I need error message here, like "File not found" etc. -
// the reason of the failure
//std::cerr << "\tNo saved data";
return 1;
}
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
save_chapter = std::stoi(input);
return 0;
}
std::string File::LoadSavedName(int SAVE_SLOT) //
{
std::string input = "";
std::string temp = " ";
std::string s = "D";
std::string f = "T";
std::string path;
if (SAVE_SLOT == 1)
{
path = path_save_1;
}
else if (SAVE_SLOT == 2)
{
path = path_save_2;
}
else if (SAVE_SLOT == 3)
{
path = path_save_3;
}
else if (SAVE_SLOT == 4)
{
path = path_save_4;
}
std::ifstream fin(path);
if (fin.fail())
{
// I need error message here, like "File not found" etc. -
// the reason of the failure
return "Wrong DT";
}
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
return input;
}
int File::LoadRoom(std::string room_name, int& save_room, int SAVE_SLOT) //
{
std::string input = "";
std::string temp = " ";
std::string s = "$" + room_name + "$";
std::string f = "%" + room_name + "%";
std::string path;
if (SAVE_SLOT == 1)
{
path = path_save_1;
}
else if (SAVE_SLOT == 2)
{
path = path_save_2;
}
else if (SAVE_SLOT == 3)
{
path = path_save_3;
}
else if (SAVE_SLOT == 4)
{
path = path_save_4;
}
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
save_room = std::stoi(input);
return 0;
}
int File::LoadInventory(std::string& save_inventory, int slot_number, int SAVE_SLOT) //
{
std::string input = "";
std::string temp = " ";
std::string s = '[' + std::to_string(slot_number) + ']';
std::string f = '{' + std::to_string(slot_number) + '}';
std::string path;
if (SAVE_SLOT == 1)
{
path = path_save_1;
}
else if (SAVE_SLOT == 2)
{
path = path_save_2;
}
else if (SAVE_SLOT == 3)
{
path = path_save_3;
}
else if (SAVE_SLOT == 4)
{
path = path_save_4;
}
std::ifstream fin(path);
while (!fin.eof())
{
getline(fin, temp);
input = input + temp;
}
fin.close();
input.erase(0, input.find(s) + s.length());
input.erase(input.find(f), input.length() - 1);
save_inventory = input;
return 0;
}