-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFileHandler.h
More file actions
126 lines (94 loc) · 4.11 KB
/
Copy pathFileHandler.h
File metadata and controls
126 lines (94 loc) · 4.11 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
#ifdef __cplusplus
extern "C" {
#endif
char* ReadStr(char* filename, char* id, char* setting, char* defaultvalue);
int IsSet(char* filename, char* id, char* setting);
int FetchIntValue(char* filename, char* id, char* setting, int def);
int EntryExists(char* filename, char* id);
void Write(char* filename, char* id, char* setting, char* value);
void Delete(char* filename, char* id, char* setting);
void DeleteAll(char* filename, char* id);
// Added for support of the new cheat system
char* ReadLine(char* filename, char* id, size_t line_number);
void WriteLine(char* filename, char* id, char* line);
void DeleteLine(char* filename, char* id, char* line);
// Added for supporting renumbering of cheats but this may be useful in other places
void ChangeKey(char* filename, char* id, char* oldkey, char* newkey);
#ifdef __cplusplus
} // extern "C"
#endif
#ifdef __cplusplus
#include <string>
#include <vector>
#include <list>
using namespace std;
#ifndef FHANDLER_H
#define FHANDLER_H
// This struct will hold a header and data
// In the case the header is a simple string (Not encapsulated by [ ] square brackets) then data shall be empty
struct Entry {
string header;
vector<string> data;
size_t unsortable_lines;
// GOOD = The header has been accepted and/or the data has been aded
// FULL = A header has been passed to an already contructed entry, save the current entry and make a new one that contains the header
// UNFORMATTED = This was simple text that was saved, unlike FULL it denotes that the entry was saved so the new entry will not have this text copied to it
enum class read_states { GOOD, FULL, UNFORMATTED };
Entry();
// A normal header is encased in brackets [ ] at the start and may end with a comment
bool IsHeader(const string& str);
// A game header is very specific [XXXXXXXX:XXXXXXXX-C:XX] (Where X represents a Hex number) and may end with a comment
bool IsGameHeader(const string& str);
// The rules are as follows
// The header must always contain something, if it is a Game Header or Header it may contain data (Returns true)
// If the header is neither then it will only contain a string (regular text inside the file) and it cannot store data (Returns false) or be sorted
// Only one header may be contained at a time, therefore attempting to store another header will return false
read_states StoreData(const string& str);
void ClearData();
static bool CompareKeys(const string& key1, const string& key2);
static bool IsCheatKey(const string& key, int& num);
};
// This class will handle the majority of the work when doing file related things
class FileStuff {
public:
FileStuff();
FileStuff(char* _filename);
~FileStuff();
void SetFileName(char* _filename);
string GetFileName();
// Reading from the buffer
string GetValue(char* id, char* setting, char* def, bool fetch);
string GetKeys(char* id);
string GetLine(char* id, size_t line_number);
bool DoesEntryExist(char* id);
// Changes to the buffer
void AddSettingValue(char* id, char* setting, char* value);
void WriteLine(char* id, char* line);
// A special case where the key must be changed
void ChangeCurrentKey(char* id, char* oldkey, char* newkey);
// Removals / Deletions from the buffer
void RemoveSetting(char* id, char* setting);
void RemoveEntry(char* id);
void DeleteLine(char* id, char* line);
// Basic file write
void WriteToFile();
private:
// A copy of the file in memory, some processing will be done when it is loaded
vector<Entry> filebuffer;
// The index of the current entry being examined, mostly only useful when reading multiple times from the same entry
// So whenever the cheats or settings are being loaded
size_t current_entry;
// The name of the file, set by constructor or SetFileName
string filename;
string fullpath;
// The time in seconds to wait before checking the file's date and time
static const size_t check_time = 2;
// Used for checking when the file shouild be re-read and loaded into memory
struct _stat file_time;
time_t last_checked;
void LoadFile();
bool HasChanged();
Entry* FindEntry(string search);
};
#endif // End of #ifdef FHANDLER_H
#endif // End of __cplusplus