blob: ddee552ff884c41ee72c2010f8b22107e33221be (
plain)
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
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#ifndef SYSTEMFUNCTIONS_H_INCLUDED
#define SYSTEMFUNCTIONS_H_INCLUDED
#include <string>
#include "utf.h"
#include "i18n.h"
#ifdef FFS_WIN
#include "win.h" //includes "windows.h"
#elif defined FFS_LINUX || defined FFS_MAC
#include <cstring>
#include <cerrno>
#endif
namespace zen
{
//evaluate GetLastError()/errno and assemble specific error message
#ifdef FFS_WIN
typedef DWORD ErrorCode;
#elif defined FFS_LINUX || defined FFS_MAC
typedef int ErrorCode;
#endif
std::wstring getLastErrorFormatted(ErrorCode lastError = 0);
ErrorCode getLastError();
bool errorCodeForNotExisting(ErrorCode lastError); //check for "not existing" aliases
//######################## implementation ########################
inline
ErrorCode getLastError()
{
#ifdef FFS_WIN
return ::GetLastError();
#elif defined FFS_LINUX || defined FFS_MAC
return errno; //don't use "::", errno is a macro!
#endif
}
inline
std::wstring getLastErrorFormatted(ErrorCode lastError)
{
//determine error code if none was specified
if (lastError == 0)
lastError = getLastError();
std::wstring output = _("Error Code %x:");
replace(output, L"%x", numberTo<std::wstring>(lastError));
#ifdef FFS_WIN
LPWSTR buffer = nullptr;
if (::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_MAX_WIDTH_MASK |
FORMAT_MESSAGE_IGNORE_INSERTS | //important: without this flag ::FormatMessage() will fail if message contains placeholders
FORMAT_MESSAGE_ALLOCATE_BUFFER, nullptr, lastError, 0, reinterpret_cast<LPWSTR>(&buffer), 0, nullptr) != 0)
{
if (buffer) //"don't trust nobody"
{
output += L" ";
output += buffer;
::LocalFree(buffer);
}
}
::SetLastError(lastError); //restore last error
#elif defined FFS_LINUX || defined FFS_MAC
output += L" ";
output += utfCvrtTo<std::wstring>(::strerror(lastError));
errno = lastError; //restore errno
#endif
return output;
}
inline
bool errorCodeForNotExisting(ErrorCode lastError)
{
#ifdef FFS_WIN
return lastError == ERROR_FILE_NOT_FOUND ||
lastError == ERROR_PATH_NOT_FOUND ||
lastError == ERROR_BAD_NETPATH ||
lastError == ERROR_NETNAME_DELETED;
#elif defined FFS_LINUX || defined FFS_MAC
return lastError == ENOENT;
#endif
}
}
#endif // SYSTEMFUNCTIONS_H_INCLUDED
|