-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtilities.cpp
More file actions
329 lines (288 loc) · 8.33 KB
/
Utilities.cpp
File metadata and controls
329 lines (288 loc) · 8.33 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
isxSQLite is an extension for InnerSpace (http://www.lavishsoft.com).
Copyright 2011-2016 isxGames.com (http://www.isxgames.com)
Permission to use the source code in this file is granted under the
Creative Commons Attribution 3.0 Unported (CC BY 3.0) license. Visit
http://creativecommons.org/licenses/by/3.0/ for a summary of what
rights are granted under this license.
*/
//--------------------------------
// This file contains "utility" functions. See Utilities.h for
// declarations
//--------------------------------
#include <fstream>
#include "isxSQLite.h"
#pragma region SQLite Related
CppSQLite3DB* OpenDatabase(std::string Name, std::string FileName, LavishScript2::LS2Exception **ppException)
{
// NOTE: Calling function should be responsible for determining if the database is already opened and located
// in gDatabases.
CppSQLite3DB *pDatabase = new CppSQLite3DB();
char FullPathAndFileName[MAX_PATH] = {0};
bool bFileExists;
// Note: FileName is UTF-8, while ModulePath is ANSI
// And this should technically be using LavishScript->SetRoot, to adhere to LavishScript the filesystem... but... okay.
if (!IsAbsolutePath(FileName.c_str()))
_snprintf_s(FullPathAndFileName,MAX_PATH,"%s\\%s",ModulePath,FileName.c_str());
else
strncpy(FullPathAndFileName,FileName.c_str(),MAX_PATH);
FullPathAndFileName[MAX_PATH-1]=0;
if (FileName == ":Memory:" || FileName == ":memory:")
bFileExists = false;
else
{
std::ifstream infile(FullPathAndFileName);
bFileExists = infile.good();
}
try
{
pDatabase->open(FullPathAndFileName);
}
catch (CppSQLite3Exception& e)
{
#pragma region isxSQLite_onErrorMsg
std::string ErrCode = format("%d",e.errorCode());
std::string s = format("OpenDB:: Error Opening %s (Error: %s)",FullPathAndFileName,e.errorMessage());
if (!gQuietMode)
printf(s.c_str());
char *argv[] = {
(char*)ErrCode.c_str(),
(char*)s.c_str()
};
pISInterface->ExecuteEvent(isxSQLite_onErrorMsg,0,2,argv,0);
#pragma endregion
delete pDatabase;
return nullptr;
}
if (!bFileExists)
{
try
{
pDatabase->execDML( "PRAGMA encoding = 'UTF-8';"
"PRAGMA auto_vacuum = 1;"
"PRAGMA cache_size = 2048;"
"PRAGMA page_size = 4096;"
"PRAGMA synchronous = NORMAL;"
"PRAGMA journal_mode = OFF;"
"PRAGMA temp_store = MEMORY;"
"PRAGMA synchronous = OFF;" );
}
catch (CppSQLite3Exception& e)
{
#pragma region isxSQLite_onErrorMsg
std::string ErrCode = format("%d",e.errorCode());
std::string s = format("OpenDatabase:: Error setting defaults on new database file (Error: \"%s\")",e.errorMessage());
if (!gQuietMode)
printf(s.c_str());
char *argv[] = {
(char*)ErrCode.c_str(),
(char*)s.c_str()
};
pISInterface->ExecuteEvent(isxSQLite_onErrorMsg,0,2,argv,0);
#pragma endregion
return nullptr;
}
}
gDatabases.insert(make_pair(Name,pDatabase));
return pDatabase;
}
void CloseAllDatabases()
{
for (std::tr1::unordered_map<std::string,CppSQLite3DB*>::iterator It = gDatabases.begin(); It != gDatabases.end(); ++It)
{
CppSQLite3DB *pDB = It->second;
if (pDB && pDB->IsOpen())
pDB->close();
}
}
void FinalizeAllQueries()
{
for (std::map<int,CppSQLite3Query*>::iterator It = gQueries.begin(); It != gQueries.end(); ++It)
{
CppSQLite3Query *pQuery = It->second;
if (pQuery)
pQuery->finalize();
}
}
void FinalizeAllTables()
{
for (std::map<int,CppSQLite3Table*>::iterator It = gTables.begin(); It != gTables.end(); ++It)
{
CppSQLite3Table *pTable = It->second;
if (pTable)
pTable->finalize();
}
}
int OpenTable(CppSQLite3DB *pDB, const char *name, LavishScript2::LS2Exception **ppException)
{
CppSQLite3Buffer bufSQL;
CppSQLite3Table *pTable = new CppSQLite3Table();
try
{
if (pDB->tableExists(name))
{
bufSQL.format("select * from %s order by 1;", name);
*pTable = pDB->getTable(bufSQL);
}
else
*pTable = pDB->getTable(name);
}
catch (CppSQLite3Exception& e)
{
#pragma region isxSQLite_onErrorMsg
std::string ErrCode = format("%d",e.errorCode());
std::string s;
if (pDB->tableExists(name))
s = format("SQLiteDBType.GetTable:: Error getting table '%s'. (Error: %d:%s)",name, e.errorCode(),e.errorMessage());
else
s = format("SQLiteDBType.GetTable:: Error getting table using custom DML Statment '%s'. (Error: %d:%s)",name, e.errorCode(),e.errorMessage());
if (!gQuietMode)
printf(s.c_str());
char *argv[] = {
(char*)ErrCode.c_str(),
(char*)s.c_str()
};
pISInterface->ExecuteEvent(isxSQLite_onErrorMsg,0,2,argv,0);
#pragma endregion
pTable->finalize();
delete pTable;
return false;
}
gTablesCounter++;
gTables.insert(make_pair(gTablesCounter,pTable));
return gTablesCounter;
}
int ExecQuery(CppSQLite3DB *pDB, const char *sql, LavishScript2::LS2Exception **ppException)
{
CppSQLite3Query *q = new CppSQLite3Query();
try
{
*q = pDB->execQuery(sql);
}
catch (CppSQLite3Exception& e)
{
#pragma region isxSQLite_onErrorMsg
std::string ErrCode = format("%d",e.errorCode());
std::string s = format("SQLiteDBType.ExecQuery:: Error executing DML. (Error: %d:%s)",e.errorCode(),e.errorMessage());
if (!gQuietMode)
printf(s.c_str());
char *argv[] = {
(char*)ErrCode.c_str(),
(char*)s.c_str()
};
pISInterface->ExecuteEvent(isxSQLite_onErrorMsg,0,2,argv,0);
#pragma endregion
delete q;
return false;
}
if (q->eof())
{
#pragma region isxSQLite_onErrorMsg
if (!gQuietMode)
printf("SQLiteDBType.ExecQuery:: Query returned no results.");
char *argv[] = {
(char*)"-1",
(char*)"SQLiteDBType.ExecQuery:: Query returned no results."
};
pISInterface->ExecuteEvent(isxSQLite_onErrorMsg,0,2,argv,0);
#pragma endregion
q->finalize();
delete q;
return false;
}
gQueriesCounter++;
gQueries.insert(make_pair(gQueriesCounter,q));
return gQueriesCounter;
}
bool CloseDatabase(const char *name, CppSQLite3DB* pDB, LavishScript2::LS2Exception **ppException)
{
try
{
pDB->close();
}
catch (CppSQLite3Exception& e)
{
#pragma region isxSQLite_onErrorMsg
std::string ErrCode = format("%d",e.errorCode());
std::string s = format("SQLiteDB.Close:: Error Closing '%s' (Error: %s)",name,e.errorMessage());
if (!gQuietMode)
printf(s.c_str());
char *argv[] = {
(char*)ErrCode.c_str(),
(char*)s.c_str()
};
pISInterface->ExecuteEvent(isxSQLite_onErrorMsg,0,2,argv,0);
#pragma endregion
return false;
}
delete pDB;
gDatabases.erase(name);
#pragma region isxSQLite_onStatusMsg
std::string s = format("SQLiteDB.Close:: Database '%s' closed.",name);
if (!gQuietMode)
printf(s.c_str());
char *argv[] = {
(char*)s.c_str()
};
pISInterface->ExecuteEvent(isxSQLite_onStatusMsg,0,1,argv,0);
#pragma endregion
return true;
}
bool ExecDML(CppSQLite3DB *pDB, const char *dml, LavishScript2::LS2Exception **ppException)
{
try
{
pDB->execDML(dml);
}
catch (CppSQLite3Exception& e)
{
#pragma region isxSQLite_onErrorMsg
std::string ErrCode = format("%d",e.errorCode());
std::string s = format("SQLiteDB.ExecDML:: Error executing SQLite DML statement \"%s\". (Error: %d:%s)", dml, e.errorCode(),e.errorMessage());
if (!gQuietMode)
printf(s.c_str());
char *argv[] = {
(char*)ErrCode.c_str(),
(char*)s.c_str()
};
pISInterface->ExecuteEvent(isxSQLite_onErrorMsg,0,2,argv,0);
#pragma endregion
return false;
}
return true;
}
#pragma endregion
#pragma region isxSQLite related
void ProcessMainXMLSettings()
{
unsigned int GeneralSetID = pISInterface->FindSet(MainXMLFileID,"General");
////////
// Process Main Section
pISInterface->GetSetting(MainXMLFileID,"Use Test Version",gUseTestVersion);
////////
pISInterface->ExportSet(MainXMLFileID,XMLFileName);
return;
}
#pragma endregion
#pragma region Misc. Utility Functions
// Honestly, I would prefer to use boost for formatting with std::string; however, the goal is for isxSQLite to compile
// "out of the box" without any additional packages. For now anyway....
std::string format_arg_list(const char *fmt, va_list args)
{
if (!fmt) return "";
int result = -1, length = _vscprintf(fmt,args);
char *buffer = new char [length + 1];
result = _vsnprintf_s(buffer, length, _TRUNCATE, fmt, args);
std::string s(buffer);
delete [] buffer;
return s;
}
std::string format(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
std::string s = format_arg_list(fmt, args);
va_end(args);
return s;
}
#pragma endregion