[go: up one dir, main page]

Menu

[r469]: / trunk / src / utils.cpp  Maximize  Restore  History

Download this file

327 lines (272 with data), 8.4 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
/*
This file is part of VVV (Virtual Volumes View)
Copyright (C) 2007, the VVV Development team
Author: Fulvio Senore
VVV is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
VVV is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VVV; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __WXMSW__
#include<errno.h>
#endif
#include "utils.h"
#include <wx/stdpaths.h>
#include <wx/filename.h>
#include <wx/filefn.h>
#include <iostream>
wxString CUtils::applicationName = wxT("VVV");
wxString CUtils::applicationVersion = wxT("1.5.0.0");
int CUtils::expectedDatabaseVersion = 24;
int CUtils::firstUnicodeDatabaseVersion = 14;
wxString CUtils::strucUpdateDbName = wxT("vvv-struct-update.fdb");
// will be changed to the correct value by the first function call
wxChar CUtils::m_thousandsSeparator = 'x';
//-----------------------------------------------------------------------------
//! converts wxString to std::string
std::string CUtils::wx2std(const wxString& input, wxMBConv* conv)
{
if (input.empty())
return "";
if (!conv)
conv = wxConvCurrent;
const wxWX2MBbuf buf(input.mb_str(*conv));
// conversion may fail and return 0, which isn't a safe value to pass
// to std:string constructor
if (!buf)
return "";
return std::string(buf);
}
//-----------------------------------------------------------------------------
//! converts std:string to wxString
wxString CUtils::std2wx(const std::string& input, wxMBConv* conv)
{
if (input.empty())
return wxEmptyString;
if (!conv)
conv = wxConvCurrent;
return wxString(input.c_str(), *conv);
}
// string conversion for database access
// the database uses UTF8 encoding
std::string CUtils::DBwx2std( const wxString& input ) {
std::string s = wx2std( input, &wxConvUTF8 );
return wx2std( input, &wxConvUTF8 );
}
wxString CUtils::DBstd2wx( const std::string& input ) {
return std2wx( input, &wxConvUTF8 );
}
wxString CUtils::UTF162wx( const char *strUTF16 ) {
wxString retVal;
#if SIZEOF_WCHAR_T == 2
wxString s( (wxChar*) strUTF16 );
retVal = s;
#else
wxMBConvUTF16 converter ;
wchar_t *uniChars = new wchar_t[1024];
int n = converter.MB2WC( uniChars, (const char *) strUTF16, 1000 );
wxString s( (wxChar*) uniChars );
retVal = s;
delete uniChars;
#endif
return retVal;
}
void CUtils::MsgErr( wxString errMsg ){
wxMessageDialog dialog( NULL, errMsg, applicationName, wxOK|wxICON_ERROR );
dialog.ShowModal();
}
void CUtils::MsgStderr( wxString errMsg ){
#ifdef __WXMSW__
MsgErr( errMsg );
#else
std::cerr << wx2std( errMsg ) << "\n";
#endif
}
void CUtils::MsgInfo( wxString infoMsg ){
wxMessageDialog dialog( NULL, infoMsg, applicationName, wxOK|wxICON_INFORMATION );
dialog.ShowModal();
}
bool CUtils::MsgAskNo( wxString msg ){
wxMessageDialog dialog( NULL, msg, applicationName, wxYES_NO|wxNO_DEFAULT|wxICON_EXCLAMATION );
return dialog.ShowModal() == wxID_YES;
}
bool CUtils::MsgAskYes( wxString msg ){
wxMessageDialog dialog( NULL, msg, applicationName, wxYES_NO|wxYES_DEFAULT|wxICON_EXCLAMATION );
return dialog.ShowModal() == wxID_YES;
}
wxString CUtils::HumanReadableFileSize( wxLongLong size ) {
wxString retVal;
if( size > 1024*1024 ) {
size = size / (1024*1024);
retVal = AddThousandsSeparators(size.ToString()) + wxT(" MB");
}
else {
if( size > 1024 ) {
size = size / 1024;
retVal = AddThousandsSeparators(size.ToString()) + wxT(" KB");
}
else {
retVal = AddThousandsSeparators(size.ToString());
}
}
return retVal;
}
wxString CUtils::GetApplicationName(void) {
return applicationName;
}
wxString CUtils::GetApplicationVersion(void) {
return applicationVersion;
}
int CUtils::GetExpectedDatabaseVersion(void) {
return expectedDatabaseVersion;
}
int CUtils::GetFirstUnicodeDatabaseVersion(void) {
return firstUnicodeDatabaseVersion;
}
wxString CUtils::GetStructUpdateDbName(void) {
wxString appPath = wxStandardPaths::Get().GetExecutablePath();
wxFileName fn( appPath );
fn.SetFullName( strucUpdateDbName );
wxString fullName = fn.GetFullPath();
return fullName;
}
// converts a long to a string
wxString CUtils::long2string( long val ) {
wxString retVal;
retVal.Printf( wxT("%ld"), val );
return retVal;
}
wxString CUtils::Encrypt( wxString s ) {
wxString key = "w2m5t7i0g4ndnra5s4lfdpvlyrepfjemwksdjfrpwuanrtogbjentruglk60947235128";
wxString sout( ' ', s.Len() );
size_t k;
for( k = 0; k < s.Len(); k++ ) {
char ch = static_cast<char>(s[k]) ^ static_cast<char>(key[k % key.Len()]);
sout[k] = ch;
}
return sout;
}
wxString CUtils::GetHelpFileName(void) {
wxString appPath = wxStandardPaths::Get().GetExecutablePath();
wxFileName fn( appPath );
fn.SetFullName( wxT("vvv") );
wxString fullName = fn.GetFullPath();
return fullName;
}
bool CUtils::FileIsLink( wxString fileName ) {
#ifdef __WXMSW__
// Windows does not have symlinks
return false;
#else
wxStructStat buf;
// remove trailing path separator if any
wxString sep( wxFileName::GetPathSeparator() );
if( fileName.EndsWith(sep) ) {
fileName = fileName.Left( fileName.Len() - 1 );
}
if( wxLstat(fileName, &buf) == 0 ) {
bool r = S_ISLNK(buf.st_mode);
return r;
}
else
return false;
#endif
}
wxBitmap CUtils::EnlargeBitmap( wxBitmap bmpIn, int w, int h )
{
wxImage img = bmpIn.ConvertToImage();
int wOrg = img.GetWidth();
int hOrg = img.GetHeight();
wxASSERT( w > wOrg );
wxASSERT( h > hOrg );
wxSize size( w, h );
wxPoint pos( (w - wOrg)/2, (h - hOrg)/2 ); // center original bitmap in the new image
img.Resize( size, pos );
return wxBitmap( img );
}
wxString CUtils::ExpandSingleQuotes( wxString txt ) {
// doubles single-quote characters in a string, used to create SQL statements
txt.Replace( wxT("'"), wxT("''"), true );
return txt;
}
wxString CUtils::ConvertSecondsToTimeString( int seconds )
{
int h, m, s;
h = m = 0;
s = seconds;
while( s >= 60 ) {
s -= 60;
m++;
}
while( m >= 60 ) {
m -= 60;
h++;
}
wxDateTime dt( h, m, s );
wxString sl = dt.FormatTime();
wxString stmp;
if( sl.StartsWith(wxT("0.0"), &stmp) )
sl = stmp;
else {
if( sl.StartsWith(wxT("0."), &stmp) )
sl = stmp;
}
return sl;
}
wxIcon CUtils::GetIconForPane( const char *xpm[], const wxArtID &id )
{
wxIcon r;
wxIcon icoProv = wxArtProvider::GetIcon( id, wxART_OTHER, wxSize(16, 16) );
if( icoProv.IsOk() ) {
r = icoProv;
}
else {
wxIcon ico( xpm );
r = ico;
}
return r;
}
void CUtils::SetThousandsSeparators()
{
if( m_thousandsSeparator == 'x' )
{
// discover the right decimal separator for this computer's settings
wxChar m_decimalSeparator;
wxString stmp = wxString::Format( wxT("%.1f"), 1.2 );
m_decimalSeparator = stmp[1];
// set the thousands separator (hope this way is right)
if( m_decimalSeparator == '.' )
m_thousandsSeparator = ',';
else
m_thousandsSeparator = '.';
}
}
wxString CUtils::AddThousandsSeparators( const wxString &s )
{
SetThousandsSeparators();
wxString intPart = s;
int n3 = 0;
wxString intPartNew = wxEmptyString;
int minIdx = 0;
if( intPart[0] == '-' ) minIdx = 1;
for( int k = intPart.Len() - 1; k >= minIdx; k-- )
{
intPartNew = intPart[k] + intPartNew;
n3++;
if( n3 == 3 )
{
if( k > minIdx )
intPartNew = m_thousandsSeparator + intPartNew;
n3 = 0;
}
}
if( minIdx == 1 ) intPartNew = intPart[0] + intPartNew;
return intPartNew;
}