forked from ZXCroon/Wordable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.cpp
executable file
·183 lines (155 loc) · 4.02 KB
/
info.cpp
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
#include "info.h"
#include <fstream>
#include <iostream>
FileStrBridge::FileStrBridge(const string& file) : file(file), content("") {
ifstream ifs;
ifs.open(file.c_str());
char tempc;
while ((tempc = ifs.get()) != EOF) {
content += tempc;
}
str::clearEnds(content);
ifs.close();
}
FileStrBridge::~FileStrBridge() {
ofstream ofs;
ofs.open(file.c_str());
ofs << content;
ofs.close();
}
string& FileStrBridge::getContent() {
return content;
}
SimpleInfo::SimpleInfo(FileStrBridge* fsb) :
fsb(fsb) {
transStrToProp(fsb->getContent());
}
SimpleInfo::~SimpleInfo() {
fsb->getContent() = transPropToStr();
}
void SimpleInfo::transStrToProp(const string& infoStr) {
int lm, rm = 0;
for (;;) {
lm = rm;
rm = infoStr.find("!", rm + 1);
if (rm == string::npos) {
bool have = false;
for (int i = lm + 1; i < infoStr.length(); ++i) {
if (infoStr[i] > 32) {
have = true;
break;
}
}
if (have) {
rm = infoStr.length();
}
else {
break;
}
}
string temps = infoStr.substr(lm + 1, rm - 1 - lm);
str::clearEnds(temps);
if (temps != "") {
itemList.push_back(temps);
}
}
}
string SimpleInfo::transPropToStr() {
string res = "";
for (vector <string>::iterator it = itemList.begin(); it != itemList.end(); ++it) {
res += "!" + (*it) + '\n';
}
return res;
}
void SimpleInfo::add(const string& oneInfo) {
itemList.push_back(oneInfo);
}
void SimpleInfo::del(const string& oneInfo) {
int x = search(oneInfo);
if (x == -1) return;
del(x);
}
void SimpleInfo::del(int no) {
if (no > itemList.size()) return;
itemList.erase(itemList.begin() + no -1);
}
string SimpleInfo::getItem(int no) {
if (no > itemList.size()) return "";
return itemList[no - 1];
}
int SimpleInfo::search(const string& oneInfo) {
for (int i = 1; i <= itemList.size(); ++i) {
if (itemList[i - 1] == oneInfo) {
return i;
}
}
return -1;
}
int SimpleInfo::getSize() {
return itemList.size();
}
void SimpleInfo::shuffle() {
srand(time(0));
random_shuffle(itemList.begin(), itemList.end());
}
ComplexInfo::ComplexInfo(FileStrBridge* fsb,
const string& itemName, bool canAdd) :
fsb(fsb),
itemName(itemName),
ok(true),
canAdd(canAdd),
dead(false) {
}
ComplexInfo::~ComplexInfo() {
}
bool ComplexInfo::succeed() {
return ok;
}
void ComplexInfo::suicide() {
dead = true;
}
/**
Virtual function cannot be called in the constructor && destructor.
Therefore construct && destruct are needed to achieve the aim.
**/
// have to be called in derived class' constructor
void ComplexInfo::construct() {
int beg = fsb->getContent().find('*' + itemName + '*');
if (beg == string::npos) {
if (!canAdd) {
ok = false;
return;
}
beg = fsb->getContent().length();
char c = fsb->getContent()[fsb->getContent().length() - 1];
if (c != '\n' && c != '\r') {
fsb->getContent() += '\n';
}
fsb->getContent() += "*" + itemName + "* {\n\n}\n";
}
int pos1 = fsb->getContent().find('{', beg) + 1;
int pos2 = fsb->getContent().find('}', pos1) - 1;
string temps = fsb->getContent().substr(pos1, pos2 - pos1 + 1);
str::clearEnds(temps);
transStrToProp(temps);
}
// have to be called in derived class' destructor
void ComplexInfo::destruct() {
if (!ok) return;
int beg = fsb->getContent().find('*' + itemName + '*');
int pos1 = fsb->getContent().find('{', beg) + 1;
int pos2 = fsb->getContent().find('}', pos1) - 1;
if (!dead) {
string temps = transPropToStr();
str::clearEnds(temps);
fsb->getContent() = fsb->getContent().replace(pos1, pos2 - pos1 + 1, '\n' + temps + '\n');
}
else {
int lp = pos1, rp = pos2;
for (; fsb->getContent()[lp] != '*'; --lp);
for (--lp; fsb->getContent()[lp] != '*'; --lp);
for (; fsb->getContent()[rp] != '}'; ++rp);
for (; rp + 1 < fsb->getContent().length() && fsb->getContent()[rp + 1] <= 32; ++rp);
fsb->getContent().erase(lp, rp - lp + 1);
}
}