/usr/lib/gcc/x86_64-linux-gnu/5/include/d/std/windows/syserror.d is in libphobos-5-dev 5.5.0-12ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // Written in the D programming language.
/**
* Convert Win32 error code to string.
*
* Copyright: Copyright Digital Mars 2006 - 2013.
* License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: $(WEB digitalmars.com, Walter Bright)
* Credits: Based on code written by Regan Heath
*
* Copyright Digital Mars 2006 - 2013.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module std.windows.syserror;
import std.traits : isSomeString;
version (StdDdoc)
{
private
{
alias DWORD = uint;
enum LANG_NEUTRAL = 0, SUBLANG_DEFAULT = 1;
}
/// Query the text for a Windows error code (as returned by $(LINK2
/// http://msdn.microsoft.com/en-us/library/windows/desktop/ms679360.aspx,
/// $(D GetLastError))) as a D string.
string sysErrorString(
DWORD errCode,
// MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) is the user's default language
int langId = LANG_NEUTRAL,
int subLangId = SUBLANG_DEFAULT) @trusted;
/*********************
* Thrown if errors that set $(LINK2
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms679360.aspx,
* $(D GetLastError)) occur.
*/
class WindowsException : Exception
{
private alias DWORD = int;
final @property DWORD code(); /// $(D GetLastError)'s return value.
this(DWORD code, string str=null, string file = null, size_t line = 0) @trusted;
}
/++
If $(D !!value) is true, $(D value) is returned. Otherwise,
$(D new WindowsException(GetLastError(), msg)) is thrown.
$(D WindowsException) assumes that the last operation set
$(D GetLastError()) appropriately.
Example:
--------------------
wenforce(DeleteFileA("junk.tmp"), "DeleteFile failed");
--------------------
+/
T wenforce(T, S)(T value, lazy S msg = null,
string file = __FILE__, size_t line = __LINE__) @safe
if (isSomeString!S);
}
else:
version (Windows):
import std.windows.charset;
import std.array : appender;
import std.conv : to;
import std.format : formattedWrite;
import core.sys.windows.windows;
string sysErrorString(
DWORD errCode,
// MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) is the user's default language
int langId = LANG_NEUTRAL,
int subLangId = SUBLANG_DEFAULT) @trusted
{
auto buf = appender!string();
if (!putSysError(errCode, buf, MAKELANGID(langId, subLangId)))
{
throw new Exception(
"failed getting error string for WinAPI error code: " ~
sysErrorString(GetLastError()));
}
return buf.data;
}
bool putSysError(Writer)(DWORD code, Writer w, /*WORD*/int langId = 0)
{
wchar *lpMsgBuf = null;
auto res = FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
null,
code,
langId,
cast(LPWSTR)&lpMsgBuf,
0,
null);
scope(exit) if (lpMsgBuf) LocalFree(lpMsgBuf);
if (lpMsgBuf)
{
import std.string : strip;
w.put(lpMsgBuf[0..res].strip());
return true;
}
else
return false;
}
class WindowsException : Exception
{
import core.sys.windows.windows;
final @property DWORD code() { return _code; } /// $(D GetLastError)'s return value.
private DWORD _code;
this(DWORD code, string str=null, string file = null, size_t line = 0) @trusted
{
_code = code;
auto buf = appender!string();
if (str != null)
{
buf.put(str);
if (code)
buf.put(": ");
}
if (code)
{
auto success = putSysError(code, buf);
formattedWrite(buf, success ? " (error %d)" : "Error %d", code);
}
super(buf.data, file, line);
}
}
T wenforce(T, S)(T value, lazy S msg = null,
string file = __FILE__, size_t line = __LINE__) if (isSomeString!S)
{
if (!value)
throw new WindowsException(GetLastError(), to!string(msg), file, line);
return value;
}
version(Windows)
unittest
{
import std.exception;
import std.string;
import std.algorithm : startsWith, endsWith;
auto e = collectException!WindowsException(
DeleteFileA("unexisting.txt").wenforce("DeleteFile")
);
assert(e.code == ERROR_FILE_NOT_FOUND);
assert(e.msg.startsWith("DeleteFile: "));
// can't test the entire message, as it depends on Windows locale
assert(e.msg.endsWith(" (error 2)"));
// Test code zero
e = new WindowsException(0);
assert(e.msg == "");
e = new WindowsException(0, "Test");
assert(e.msg == "Test");
}
|