-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
178 lines (147 loc) · 4.42 KB
/
Utils.cpp
File metadata and controls
178 lines (147 loc) · 4.42 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
/////////////////////////////////////////////////////////////////////////////
// Some utils - Implementation
/////////////////////////////////////////////////////////////////////////////
#include <wx/mstream.h>
#include "Utils.h"
#include "AppConfig.h"
// A global critical section nedded for logging
wxCriticalSection CSLock;
/////////////////////////////////////////////////////////////////////////////
// Load a png file from ressources
/////////////////////////////////////////////////////////////////////////////
bool GetPngFromRessource(const wxString& strRessourceName_png, wxBitmap& Bitmap)
{
// on y croit :
Bitmap = wxBITMAP_PNG(strRessourceName);
return true;
/*
bool bSuccess = false; // Result of operation
char* pData = NULL; // Image raw data
unsigned long ulDataSize = 0; // Image data size
HGLOBAL hHandle = NULL; // Handle to ressource
HRSRC pResource = NULL; // Pointer to ressource
// Verify that the ressource exist
pResource = FindResource(0, strRessourceName.wchar_str(), RT_RCDATA);
if(NULL != pResource)
{
// Load the ressource
hHandle = LoadResource(NULL, pResource);
if (NULL != hHandle)
{
// Get data and size
pData = (char*)LockResource(hHandle);
ulDataSize = SizeofResource(NULL, pResource);
// Convert to wxImage
wxMemoryInputStream iStream(pData, ulDataSize);
Bitmap = wxBitmap(wxImage(iStream, wxBITMAP_TYPE_PNG, -1), -1);
bSuccess = true;
}
}
return bSuccess;
*/
}
/////////////////////////////////////////////////////////////////////////////
// Display a message
// Note: No lock needed, only called from GUI thread
/////////////////////////////////////////////////////////////////////////////
void DoMessage(wxString strMessage, eMsgLevel MsgLevel)
{
wxString strTitle;
long lStyle = wxOK;
if(MSG_WARNING == MsgLevel)
{
strTitle = "Warning";
lStyle |= wxICON_WARNING;
}
else if(MSG_ERROR == MsgLevel)
{
strTitle = "Error";
lStyle |= wxICON_ERROR;
}
else
{
strTitle = "Info";
lStyle |= wxICON_INFORMATION;
}
wxMessageBox(strMessage, strTitle, lStyle);
}
/////////////////////////////////////////////////////////////////////////////
// Log to file
// Note: Lock is needed, as the function may be called from multiple threads
/////////////////////////////////////////////////////////////////////////////
void DoLog(wxString strMessage, eMsgLevel MsgLevel)
{
wxCriticalSectionLocker Lock(CSLock);
// Type of message (info, warning, error)
wxString strType;
// Text file containing the device config
wxTextFile LogFile("Data/Log/Errors.log");
// Verify if the file exist
if(LogFile.Exists())
{
// If the file exist, open it
if(!LogFile.Open())
{
return;
}
}
else
{
// Create the file
if(!LogFile.Create())
{
return;
}
}
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
char *asct = asctime (timeinfo);
strip(asct);
switch(MsgLevel)
{
case MSG_INFO : strType = wxString::Format("[NFO] [%s] ", asct); break;
case MSG_WARNING : strType = wxString::Format("[WRN] [%s] ", asct); break;
default : strType = wxString::Format("[ERR] [%s] ", asct); break;
}
LogFile.AddLine(strType + strMessage);
// Save the changes
LogFile.Write();
// Close the file
LogFile.Close();
}
void strip(char *s) {
char *p2 = s;
while(*s != '\0') {
if(*s != '\t' && *s != '\n') {
*p2++ = *s++;
} else {
++s;
}
}
*p2 = '\0';
}
/////////////////////////////////////////////////////////////////////////////
// Get a text in current language
/////////////////////////////////////////////////////////////////////////////
wxString GetText(wxString strKey)
{
return CConfig::GetSingleton()->GetText(strKey);
}
/////////////////////////////////////////////////////////////////////////////
// Convert string to long
/////////////////////////////////////////////////////////////////////////////
long ToLong(wxString strValue)
{
long lTmpVal = 0;
strValue.ToLong(&lTmpVal);
return lTmpVal;
}
/////////////////////////////////////////////////////////////////////////////
// Convert long to string
/////////////////////////////////////////////////////////////////////////////
wxString ToString(long lValue)
{
return wxString::Format("%d", lValue);
}