forked from akerou/fvp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.cpp
265 lines (212 loc) · 6.95 KB
/
parser.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
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
#include "parser.h"
QStringList *Parser::getScript()
{
return script;
}
void Parser::loadScript(const QString filename)
{
if(script != NULL)
delete script;
script = readShiftJis(filename);
}
void Parser::loadScriptUTF8(const QString filename)
{
if(script != NULL)
delete script;
// script = readShiftJis(filename);
script = readUTF8(filename);
}
void Parser::prepareScript()
{
int index;
QString tag;
QStringList *part;
QStringList *tmp = new QStringList();
while((index = script->indexOf(QRegExp("<part.+"))) != -1)
{
tag = script->at(index);
script->removeAt(index);
part = importPart(tag.section("filename=\"", 1).section("\"", 0, 0));
if(part!=NULL)
{
tmp->append(*part);
delete part;
}
}
delete script;
script = tmp;
}
QStringList *Parser::importPart(const QString filename)
{
QFile f(filename);
//QRegExp rx("(\n|\r)*");
QStringList *part = new QStringList;
if(f.exists())
{
f.open(QIODevice::ReadOnly | QIODevice::Text);
QStringList tmp = QTextCodec::codecForName("Shift-JIS")->toUnicode(f.readAll().data()).split("\n");
f.close();
foreach(QString line, tmp) {
if(!line.indexOf("#") == 0 && !line.contains("【"))
part->append(line);
}
return part;
}
delete part;
return NULL;
}
int Parser::writeShiftJis(const QString filename, const QStringList part)
{
QFile f(filename);
f.open(QIODevice::WriteOnly | QIODevice::Text);
f.write(QTextCodec::codecForName("Shift-JIS")->fromUnicode(part.join("\n")));
f.close();
return 0;
}
QStringList *Parser::readShiftJis(const QString filename)
{
QStringList *file = new QStringList();
QFile f(filename);
f.open(QIODevice::ReadOnly | QIODevice::Text);
QStringList tmp = QTextCodec::codecForName("Shift-JIS")->toUnicode(f.readAll().data()).split("\n");
f.close();
file->append(tmp);
return file;
}
QStringList *Parser::readUTF8(const QString filename)
{
QStringList *file = new QStringList();
QFile f(filename);
f.open(QIODevice::ReadOnly | QIODevice::Text);
// QStringList tmp = QTextCodec::codecForName("Shift-JIS")->toUnicode(f.readAll().data()).split("\n");
QStringList tmp = QTextCodec::codecForName("UTF-8")->toUnicode(f.readAll().data()).split("\n");
f.close();
file->append(tmp);
return file;
}
int Parser::exportParts(const QString filename)
{
loadScript(filename);
QString tag;
int startIndex, endIndex;
while((startIndex = script->indexOf(QRegExp("<part.+"))) != -1)
{
tag = script->at(startIndex);
script->removeAt(startIndex);
if((endIndex = script->indexOf("</part>")) == -1)
return -1;
script->removeAt(endIndex);
writeShiftJis(tag.section("filename=\"", 1).section("\"", 0, 0), script->mid(startIndex, endIndex));
}
return 0;
}
int Parser::dumpStrings(const char *in)
{
QString in_file = in;
QStringList *script_lines = readShiftJis(in_file);
QStringList output;
output.append("");
int start_index = script_lines->indexOf("SCENE_PROLOGUE:");
if(start_index == -1) start_index = 0;
QString str_token = "pushstring ";
for(int i = start_index +1; i < script_lines->length(); i++)
{
QString line = script_lines->at(i);
if(line.contains("#") && line.contains("SPEAK"))
output.append(parse_name(line.section("#", 1)));
if(line.contains(str_token) && line.length() > str_token.length() + 1)
output.append(line.section(str_token, 1) + "\n");
}
writeShiftJis(in_file.section(".", 0, 0) + "_strings.txt", output);
delete script_lines;
return 0;
}
QByteArray Parser::insertStrings(const QString strings, const QString script, int char_limit)
{
// loadScript(strings);
loadScriptUTF8(strings);
prepareScript();
QStringList *tl_lines = getScript();
// QStringList *script_lines = readShiftJis(script);
QStringList *script_lines = readUTF8(script);
QStringList output;
QString line, tmp;
int start_index = script_lines->indexOf("SCENE_PROLOGUE:");
if(start_index == -1) start_index = 0;
output.append(script_lines->mid(0, start_index + 1));
for(int i = start_index + 1; i < script_lines->length(); i++)
{
line = script_lines->at(i);
if(line.contains("pushstring") && tl_lines->length() > 0)
{
tmp = line.section("pushstring ", 1);
int index = -1;
if(tmp.length() > 0)
index = tl_lines->indexOf(tmp);
if(index != -1 && tl_lines->at(index+1).length() > 0)
{
if(tl_lines->at(index+1).indexOf("@") == 0)
output.append("\tpushstring " + tl_lines->at(index+1).section("@", 1));
else
output.append("\tpushstring " + wordWrap(tl_lines->at(index+1), line_limit, char_limit));
tl_lines->removeAt(index);
tl_lines->removeAt(index);
tl_lines->removeAt(index);
continue;
}
else
output.append(line);
}
else
output.append(line);
}
delete script_lines;
// return QTextCodec::codecForName("Shift-JIS")->fromUnicode(output.join("\n"));
QTextCodec *sjis = QTextCodec::codecForName("Shift-JIS");
QTextCodec *gbk = QTextCodec::codecForName("GBK");
QByteArray result;
QList<QString>::Iterator it = output.begin(), itend = output.end();
int cnt = 0;
for (; it != itend; it++, cnt++) {
// line = output.at(cnt);
// if (line)
// printf("output insert %s\n", line.toStdString().c_str());
if (it->contains("@")) {
QString tmp = *it;
tmp = tmp.replace("@", "");
result.append(gbk->fromUnicode(tmp));
// printf("out ori %s\n", it->toStdString().c_str());
// printf("out next %s\n", tmp.toStdString().c_str());
// printf("out gbk %s\n", gbk->fromUnicode(tmp).toStdString().c_str());
} else {
result.append(sjis->fromUnicode(*it));
// printf("out shift-jis %s\n", it->toStdString().c_str());
// result.append(gbk->fromUnicode(*it));
}
result.append("\n");
}
return result;
}
const QString Parser::wordWrap(const QString str, int line_limit, int char_limit)
{
QString line = str;
int rest_size = str.length();
int index = 0;
while(--line_limit > 0 && rest_size > char_limit)
{
index = line.lastIndexOf(" ", index + char_limit);
line.insert(index+1, "~");
rest_size = line.length() - (index + 2);
}
return line;
}
QString Parser::parse_name(QString name_line)
{
QString name = "【" + name_line.remove(" ").remove(" ") + "】";
return name;
}
Parser::~Parser()
{
if(script != NULL)
delete script;
}