-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStorage.mqh
102 lines (88 loc) · 7.52 KB
/
Storage.mqh
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
//+------------------------------------------------------------------+
//| Storage.mqh |
//| Copyright 2021, Homma.tech |
//| https://www.homma.tech |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Homma.tech"
#property link "https://www.homma.tech"
class CLocalStorage {
private:
struct state_s {
string key[];
string value[];
} state;
string fileName_;
int fileHandle;
int KeyExists(string key);
public:
CLocalStorage(string fileName);
~CLocalStorage();
void SetState(string key, string value);
string GetState(string key);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CLocalStorage::CLocalStorage(string fileName) {
fileName_ = fileName;
ResetLastError();
fileHandle = FileOpen(fileName_ + ".csv", FILE_WRITE | FILE_READ | FILE_ANSI | FILE_CSV | FILE_COMMON, ';');
int index = 0;
while(!FileIsEnding(fileHandle)) {
ArrayResize(state.key, ArraySize(state.key) + 1);
ArrayResize(state.value, ArraySize(state.value) + 1);
state.key[index] = FileReadString(fileHandle);
state.value[index] = FileReadString(fileHandle);
index++;
}
if(fileHandle == INVALID_HANDLE)
Print(":: Não foi possível abrir o arquivo " + fileName + " (Erro: " + (string)GetLastError() + ") ::");
FileClose(fileHandle);
FileDelete(fileName);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CLocalStorage::~CLocalStorage() {
fileHandle = FileOpen(fileName_ + ".csv", FILE_WRITE | FILE_READ | FILE_ANSI | FILE_CSV | FILE_COMMON, ';');
for(int i = 0; i < ArraySize(state.key); i++) {
FileWriteString(fileHandle, state.key[i] + ";" + state.value[i] + "\n");
}
FileClose(fileHandle);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CLocalStorage :: SetState(string key,
string value) {
int index = ArraySize(state.key);
int keyPosition = KeyExists(key);
if(keyPosition == -1) {
ArrayResize(state.key, ArraySize(state.key) + 1);
ArrayResize(state.value, ArraySize(state.value) + 1);
state.key[index] = key;
state.value[index] = value;
} else {
state.value[keyPosition] = value;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string CLocalStorage :: GetState(string key) {
int keyPosition = KeyExists(key);
if(keyPosition != -1)
return state.value[keyPosition];
return "";
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CLocalStorage :: KeyExists(string key) {
for(int i = 0; i < ArraySize(state.key); i++) {
if(state.key[i] == key)
return i;
}
return -1;
}
//+------------------------------------------------------------------+