-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobalconfig.cpp
More file actions
247 lines (209 loc) · 7.07 KB
/
globalconfig.cpp
File metadata and controls
247 lines (209 loc) · 7.07 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
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
#include <QDebug>
#include "globalconfig.h"
#include "log.h"
GlobalConfig::GlobalConfig()
{
}
bool GlobalConfig::LoadConfig(QString fileName)
{
QDir dir;
QString currentPath = dir.currentPath();
const char* luaPathStr = QString(currentPath + "/" + fileName).toStdString().c_str();
lua_State *L = luaL_newstate();
if (!L) return false;
qDebug () << "加载服务连接配置 : " << fileName;
int ret = luaL_dofile(L, fileName.toStdString().c_str());
if (ret)
{
qCritical() << lua_tostring(L,-1);
return false;
}
lua_getglobal(L, "Program");
// ParseLuaTable(L);
if (!lua_istable(L, -1))
{
qDebug() << "Program is not a lua table";
return false;
}
//置空栈顶
lua_pushnil(L);
//定制化结构读取,没有必要正对table进行递归,两层table结构
while(lua_next(L, -2)) //把表里面的key-value循环压入栈中
{
// key : 项目名字 value : 项目信息
QString programStr = lua_tostring(L, -2);
// qDebug() << "key = " << programStr;
if (lua_type(L, -1) == LUA_TTABLE)
{
sProgram program;
lua_pushnil(L);
while(lua_next(L, -2))
{
QString sKeyName = lua_tostring(L, -2);
// qDebug() << "sKeyName = " << sKeyName;
if(sKeyName == "name")
{
QString sValueName = lua_tostring(L, -1);
// qDebug() << "sValueName = " << sValueName;
program.sProgramName = sValueName;
}
else if (sKeyName == "serverLists")
{
if (lua_type(L, -1) == LUA_TTABLE)
{
lua_pushnil(L);
// int serverCount = lua_rawlen(L, -2);=
while(lua_next(L, -2))
{
if (lua_type(L, -1) == LUA_TTABLE)
{
sServerInfo serverInfo;
lua_pushnil(L);
while(lua_next(L, -2))
{
// qDebug() << "server param key = " << lua_tostring(L, -2);
// qDebug() << "server param value = " << lua_tostring(L, -1);
QString fieldName = lua_tostring(L, -2);
if (fieldName == "name")
{
serverInfo.name = lua_tostring(L, -1);
}
else if (fieldName == "ip")
{
serverInfo.ip = lua_tostring(L, -1);
}
else if (fieldName == "port")
{
serverInfo.port = lua_tonumber(L, -1);
}
else
{
qDebug() << "unknow key = " << fieldName << ", value = " << lua_tostring(L, -1);
}
lua_pop(L, 1);
}
program.serverList.push_back(serverInfo);
}
lua_pop(L, 1);
}
// qDebug () << program;
}
}
lua_pop(L, 1);
}
m_programs.insert(programStr, program);
// qDebug() << m_programs;
}
else
{
return false;
}
lua_pop(L, 1);
}
lua_close(L);
return true;
}
void GlobalConfig::ParseLuaTable(lua_State *L)
{
if (!lua_istable(L, -1))
{
return;
}
lua_pushnil(L);
while (lua_next(L, -2))
{
fprintf(stdout, "%s : %s ", luaL_typename(L,-2), luaL_typename(L,-1));
int nKeyType = lua_type(L, -2);
int nValueType = lua_type(L, -1);
if (nKeyType == LUA_TNUMBER)
{
fprintf(stdout, "%g,", lua_tonumber(L, -2));
}
else if (nKeyType == LUA_TSTRING)
{
fprintf(stdout, "\"%s\",", lua_tostring(L, -2));
}
fprintf(stdout, " ");
if (nValueType == LUA_TNUMBER)
{
fprintf(stdout, "%g", lua_tonumber(L, -1));
}
else if (nValueType == LUA_TSTRING)
{
fprintf(stdout, "%s", lua_tostring(L, -1));
}
else if (nValueType == LUA_TTABLE)
{
fprintf(stdout, "\n");
ParseLuaTable(L);
}
// qDebug() << "key value = " << lua_tonumber(L, -2);
// qDebug() << "value1 type = " << luaL_typename(L, -1);
// qDebug() << "stack counts = " << lua_gettop(L);
lua_pop(L, 1);
fprintf(stdout, "\n");
}
fflush(stdout);
}
std::string GlobalConfig::doubleToString(double price) {
auto res = std::to_string(price);
const std::string format("$1");
try {
std::regex r("(\\d*)\\.0{6}|");
std::regex r2("(\\d*\\.{1}0*[^0]+)0*");
res = std::regex_replace(res, r2, format);
res = std::regex_replace(res, r, format);
}
catch (const std::exception & e) {
return res;
}
return res;
}
bool GlobalConfig::CheckStrIsCorrectType(QString str, int nType)
{
//先判断是否为纯数字
if(nType == LUA_TNUMBER)
{
if (str.toStdString().find_first_not_of("-.0123456789\"") != std::string::npos)
{
return false;
}
}
lua_State *L = luaL_newstate();
if (L == NULL) return false;
QString sTempTableName = "temp_table = " + str;
int ret = luaL_dostring(L, sTempTableName.toStdString().c_str());
if (ret)
{
return false;
}
lua_getglobal(L, "temp_table");
int nTargetType = lua_type(L, -1);
if (nType == nTargetType)
{
return true;
}
return false;
}
int GlobalConfig::PickStrALuaValueType(QString str)
{
int nType = 0;
if (CheckStrIsCorrectType(str, LUA_TTABLE))
{
nType = LUA_TTABLE;
}
else if (CheckStrIsCorrectType(str, LUA_TNUMBER))
{
nType = LUA_TNUMBER;
}
else if (CheckStrIsCorrectType(str, LUA_TBOOLEAN))
{
nType = LUA_TBOOLEAN;
}
//如果前面的类型都不是,最后检测是否为string,如果string都检测失败,返回0(不是nil,这里表示不存在的类型)
else if (CheckStrIsCorrectType("\"" + str + "\"", LUA_TSTRING))
{
nType = LUA_TSTRING;
}
return nType;
}