Something went wrong. Try again.
🪄 dd's hex editor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566/// OS error handling.////// Copyright: dd86k <dd@dax.moe>/// License: MIT/// Authors: $(LINK2 https://github.com/dd86k, dd86k)module os.error;
version (Windows){ import core.sys.windows.winbase; private alias systemerr = GetLastError;}else{ import core.stdc.string; import std.string; import core.stdc.errno : errno; private alias systemerr = errno;}
/// OS error exception.class OSException : Exception{ /// New exception with optional message prefix this(string prefix = null, int code = systemerr(), string _file = __FILE__, size_t _line = __LINE__) { oscode = code; if (prefix) super(prefix~": "~messageFromCode(code), _file, _line); else super(messageFromCode(code), _file, _line); } /// Original OS code. int oscode;}
privatestring messageFromCode(int code){ version (Windows) { enum BUFSZ = 256; __gshared char[BUFSZ] buffer; uint len = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK, null, code, 0, // Default buffer.ptr, BUFSZ, null); import std.conv : text; if (len == 0) throw new Exception(text("FormatMessageA Error=", GetLastError(), " Original=", code)); return cast(string)buffer[0..len]; } else { return cast(string)fromStringz( strerror(code) ); }}