forked from lksj/einstein-puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.cpp
187 lines (156 loc) · 4.61 KB
/
formatter.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
#include "formatter.h"
#include "utils.h"
#include "convert.h"
#define ADD_ARG(t) \
commands[commandsCnt].type = t; \
argNo = readInt(data + offset); \
if (argNo > maxArg) \
maxArg = argNo; \
commands[commandsCnt].data = (void*)argNo; \
commandsCnt++;
Formatter::Formatter(unsigned char *data, int offset)
{
int cnt = readInt(data + offset);
if (! cnt) {
commandsCnt = argsCnt = 0;
commands = NULL;
args = NULL;
}
offset += 4;
commands = new Command[cnt];
commandsCnt = 0;
int maxArg = 0, argNo;
for (int i = 0; i < cnt; i++) {
int type = data[offset];
offset++;
int size = readInt(data + offset);
offset += 4;
switch (type) {
case 1:
commands[commandsCnt].type = TEXT_COMMAND;
commands[commandsCnt].data = new std::wstring(
fromUtf8((char*)data + offset, size));
commandsCnt++;
break;
case 2: ADD_ARG(INT_ARG); break;
case 3: ADD_ARG(STRING_ARG); break;
case 4: ADD_ARG(FLOAT_ARG); break;
case 5: ADD_ARG(DOUBLE_ARG); break;
}
offset += size;
}
argsCnt = maxArg;
if (! argsCnt)
args = NULL;
else {
args = new CmdType[argsCnt];
memset(args, 0, sizeof(CmdType) * argsCnt);
for (int i = 0; i < commandsCnt; i++) {
Command &c = commands[i];
if ((c.type == INT_ARG) || (c.type == STRING_ARG) ||
(c.type == FLOAT_ARG) || (c.type == DOUBLE_ARG))
{
long no = (long)c.data;
args[no - 1] = c.type;
}
}
}
}
Formatter::~Formatter()
{
for (int i = 0; i < commandsCnt; i++)
if (TEXT_COMMAND == commands[i].type)
delete (std::wstring*)(commands[i].data);
if (commands)
delete[] commands;
if (args)
delete[] args;
}
std::wstring Formatter::getMessage() const
{
std::wstring s;
for (int i = 0; i < commandsCnt; i++)
if (TEXT_COMMAND == commands[i].type)
s += *(std::wstring*)(commands[i].data);
return s;
}
class ArgValue
{
public:
virtual ~ArgValue() { };
virtual std::wstring format(Formatter::Command *command) = 0;
};
template <typename T>
class TemplatedArgValue: public ArgValue
{
private:
T value;
public:
TemplatedArgValue(const T &v) { value = v; };
virtual std::wstring format(Formatter::Command *command) {
return toString(value);
};
};
class StrArgValue: public ArgValue
{
private:
std::wstring value;
public:
StrArgValue(const std::wstring &v): value(v) { };
virtual std::wstring format(Formatter::Command *command) {
return value;
};
};
std::wstring Formatter::format(std::vector<ArgValue*> &argValues) const
{
std::wstring s;
long no;
for (int i = 0; i < commandsCnt; i++) {
Command *cmd = &commands[i];
switch (cmd->type) {
case TEXT_COMMAND:
s += *(std::wstring*)(cmd->data);
break;
case STRING_ARG:
case INT_ARG:
no = (long)cmd->data - 1;
if (no < (long)argValues.size())
s += argValues[no]->format(cmd);
break;
default: ;
}
}
return s;
}
std::wstring Formatter::format(va_list ap) const
{
if (! argsCnt)
return getMessage();
std::vector<ArgValue*> argValues;
for (int i = 0; i < argsCnt; i++) {
switch (args[i]) {
case INT_ARG:
argValues.push_back(new TemplatedArgValue<int>
(va_arg(ap, int)));
break;
case STRING_ARG:
argValues.push_back(new StrArgValue(va_arg(ap, wchar_t*)));
break;
case DOUBLE_ARG:
argValues.push_back(new TemplatedArgValue<double>
(va_arg(ap, double)));
break;
case FLOAT_ARG:
argValues.push_back(new TemplatedArgValue<float>
((float)va_arg(ap, double)));
break;
default:
i = argsCnt;
}
}
std::wstring s = format(argValues);
for (std::vector<ArgValue*>::iterator i = argValues.begin();
i != argValues.end(); i++)
delete *i;
return s;
}