-
Notifications
You must be signed in to change notification settings - Fork 7
/
Common.cpp
417 lines (355 loc) · 11.8 KB
/
Common.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Common.cpp: implementation of the Common class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DMSpec.h"
#include "Common.h"
#include <utility>
#include <vector>
#include <string>
#include <MobileDoasLib/GpsData.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
// ---------------------------------------------------------------
// ---------------- GLOBAL FUNCTIONS -----------------------------
// ---------------------------------------------------------------
int Equals(const CString& str1, const CString& str2) {
return (0 == _tcsnicmp(str1, str2, max(strlen(str1), strlen(str2))));
}
int Equals(const CString& str1, const CString& str2, unsigned int nCharacters) {
return (0 == _tcsnicmp(str1, str2, min(nCharacters, max(strlen(str1), strlen(str2)))));
}
int IsExistingFile(const CString* fileName) {
FILE* f = fopen(*fileName, "r");
if (f < (FILE*)1)
return(0);
fclose(f);
return(1);
}
int IsExistingFile(const CString& fileName) {
return IsExistingFile(&fileName);
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Common::Common()
{
}
Common::~Common()
{
}
void Common::WriteLogFile(CString filename, CString txt)
{
FILE* f;
f = fopen(filename, "a+");
if (f < (FILE*)1) {
CString tmpStr;
tmpStr.Format("Could not write to log file: %s. Not enough free space?", (LPCTSTR)filename);
MessageBox(NULL, tmpStr, "Big Error", MB_OK);
return;
}
fprintf(f, txt + "\n");
fclose(f);
}
void Common::GetExePath()
{
TCHAR exeFullPath[MAX_PATH];
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
m_exePath = (CString)exeFullPath;
int position = m_exePath.ReverseFind('\\');
m_exePath = m_exePath.Left(position + 1);
}
void Common::GuessSpecieName(const CString& fileName, CString& specie) {
specie.Format("");
CString spc[] = { "SO2", "NO2", "O3", "O4", "HCHO", "RING", "H2O", "CLO", "BRO", "CHOCHO", "Glyoxal", "Formaldehyde" };
int nSpecies = 12;
int index = fileName.ReverseFind('\\');
if (index == 0)
return;
CString fil;
fil.Format("%s", (LPCTSTR)fileName.Right((int)strlen(fileName) - index - 1));
fil.MakeUpper();
for (int i = 0; i < nSpecies; ++i) {
if (strstr(fil, spc[i])) {
specie.Format("%s", (LPCTSTR)spc[i]);
return;
}
}
// nothing found
return;
}
bool FormatErrorCode(DWORD error, CString& string) {
/* from System Error Codes */
switch (error) {
case ERROR_FILE_NOT_FOUND:
string.Format("File not found"); return true;
case ERROR_PATH_NOT_FOUND:
string.Format("Path not found"); return true;
case ERROR_TOO_MANY_OPEN_FILES:
string.Format("Too many open files"); return true;
case ERROR_ACCESS_DENIED:
string.Format("Access denied"); return true;
case ERROR_NOT_ENOUGH_MEMORY:
string.Format("Not enough memory"); return true;
case ERROR_OUTOFMEMORY:
string.Format("Out of memory"); return true;
case ERROR_WRITE_PROTECT:
string.Format("The media is write protected"); return true;
case ERROR_SEEK:
string.Format("The drive cannot locate a specific area or track on the disk."); return true;
case ERROR_WRITE_FAULT:
string.Format("The system cannot write to the specified device"); return true;
case ERROR_READ_FAULT:
string.Format("The system cannot read from the specified device"); return true;
case ERROR_HANDLE_DISK_FULL:
case ERROR_DISK_FULL:
string.Format("The disk is full"); return true;
case ERROR_CANNOT_MAKE:
string.Format("The directory or file cannot be created"); return true;
case ERROR_BUFFER_OVERFLOW:
string.Format("The file name is too long"); return true;
case ERROR_INVALID_NAME:
string.Format("The filename, directory name, or volume label syntax is incorrect"); return true;
case ERROR_DIRECTORY:
string.Format("The directory name is invalid"); return true;
case ERROR_DISK_TOO_FRAGMENTED:
string.Format("The volume is too fragmented to complete this operation"); return true;
case ERROR_ARITHMETIC_OVERFLOW:
string.Format("Arithmetic result exceeded 32 bits"); return true;
case ERROR_ALREADY_EXISTS:
string.Format("The file already exists"); return true;
}
return false;
}
bool Common::BrowseForDirectory(CString& folder)
{
IFileOpenDialog* pfd;
// CoCreate the dialog object.
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pfd));
bool success = false;
if (SUCCEEDED(hr))
{
DWORD dwOptions;
hr = pfd->GetOptions(&dwOptions);
if (SUCCEEDED(hr))
{
hr = pfd->SetOptions(dwOptions | FOS_PICKFOLDERS);
}
if (SUCCEEDED(hr))
{
// Show the Open dialog.
if (!folder.IsEmpty()) {
IShellItem* dir;
hr = SHCreateItemFromParsingName(CT2CW(folder), NULL, IID_IShellItem, (void**)&dir);
if (SUCCEEDED(hr)) {
pfd->SetFolder(dir);
}
}
hr = pfd->Show(NULL);
if (SUCCEEDED(hr))
{
// Obtain the result of the user interaction.
IShellItem* file;
hr = pfd->GetResult(&file);
if (SUCCEEDED(hr))
{
LPWSTR dn;
file->GetDisplayName(SIGDN_FILESYSPATH, &dn);
folder = dn;
success = true;
file->Release();
}
}
}
pfd->Release();
}
return success;
}
std::vector<std::string> Common::ListFilesInDirectory(const char* directory, const char* fileNameFilter)
{
std::vector<std::string> result;
char searchPattern[MAX_PATH];
if (fileNameFilter == nullptr)
{
sprintf(searchPattern, "%s*", directory);
}
else
{
sprintf(searchPattern, "%s%s", directory, fileNameFilter);
}
WIN32_FIND_DATA FindFileData;
HANDLE hFile = FindFirstFile(searchPattern, &FindFileData);
if (hFile == INVALID_HANDLE_VALUE)
{
return result; // no files found
}
do {
if (Equals(FindFileData.cFileName, ".") || Equals(FindFileData.cFileName, ".."))
{
continue;
}
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// This is a directory that we've found. Skip
}
else
{
// Get the full name of the file that we've just found...
CString fileName;
fileName.Format("%s%s", directory, FindFileData.cFileName);
result.push_back(std::string(fileName));
}
} while (0 != FindNextFile(hFile, &FindFileData));
FindClose(hFile);
return result;
}
std::vector<CString> Common::BrowseForFiles()
{
std::vector<CString> filenames;
IFileOpenDialog* pfd;
// CoCreate the dialog object.
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pfd));
if (SUCCEEDED(hr))
{
DWORD dwOptions;
// Specify multiselect.
hr = pfd->GetOptions(&dwOptions);
if (SUCCEEDED(hr))
{
hr = pfd->SetOptions(dwOptions | FOS_ALLOWMULTISELECT);
}
if (SUCCEEDED(hr))
{
COMDLG_FILTERSPEC aFileTypes[] = {
{ L"Text files", L"*.txt" }
};
hr = pfd->SetFileTypes(ARRAYSIZE(aFileTypes), aFileTypes);
}
if (SUCCEEDED(hr))
{
// Show the Open dialog.
hr = pfd->Show(NULL);
if (SUCCEEDED(hr))
{
// Obtain the result of the user interaction.
IShellItemArray* results;
hr = pfd->GetResults(&results);
if (SUCCEEDED(hr))
{
DWORD numFiles;
hr = results->GetCount(&numFiles);
filenames.reserve(numFiles);
if (SUCCEEDED(hr)) {
for (DWORD j = 0; j < numFiles; j++) {
IShellItem* file;
results->GetItemAt(j, &file);
LPWSTR dn;
file->GetDisplayName(SIGDN_FILESYSPATH, &dn);
CString name(dn);
filenames.push_back(name);
CoTaskMemFree(dn);
}
return filenames;
}
//
// You can add your own code here to handle the results.
//
results->Release();
}
}
}
pfd->Release();
}
return filenames;
}
// open a browser window and let the user search for a file
bool Common::BrowseForFile(const char* filter, CString& fileName) {
static char szFile[4096];
szFile[0] = 0;
OPENFILENAME ofn; // common dialog box structure
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = nullptr;
ofn.hInstance = AfxGetInstanceHandle();
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = filter;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = nullptr;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = nullptr;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
if (GetOpenFileName(&ofn) == TRUE) {
fileName.Format(szFile);
return SUCCESS;
}
fileName.Format("");
return FAIL;
}
bool Common::BrowseForEvaluationLog(CString& fileName)
{
TCHAR filter[512];
int n = _stprintf(filter, "Evaluation Logs\0");
n += _stprintf(filter + n + 1, "*.txt;\0");
filter[n + 2] = 0;
return Common::BrowseForFile(filter, fileName);
}
// open a browser window and let the user search for a file
bool Common::BrowseForFile_SaveAs(TCHAR* filter, CString& fileName)
{
return BrowseForFile_SaveAs(filter, fileName, nullptr);
}
bool Common::BrowseForFile_SaveAs(TCHAR* filter, CString& fileName, int* filterType)
{
static TCHAR szFile[4096];
sprintf(szFile, "%s", (LPCTSTR)fileName);
OPENFILENAME ofn; // common dialog box structure
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = nullptr;
ofn.hInstance = AfxGetInstanceHandle();
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = filter;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = nullptr;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = nullptr;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_EXPLORER;
if (GetSaveFileName(&ofn) == TRUE)
{
fileName.Format(szFile);
if (filterType != nullptr)
{
*filterType = ofn.nFilterIndex;
}
return true;
}
fileName.Format("");
return false;
}
/** Take out the exe name from a long path
@param fileName path of the exe file */
void Common::GetFileName(CString& fileName)
{
int position = fileName.ReverseFind('\\');
int length = CString::StringLength(fileName);
fileName = fileName.Right(length - position - 1);
}
/** Take out the directory from a long path name.
@param fileName - the complete path of the file */
void Common::GetDirectory(CString& fileName) {
int position = fileName.ReverseFind('\\');
fileName = fileName.Left(position + 1);
}