www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - SysError.msg()

reply Benjamin Herr <ben 0x539.de> writes:
Hell, brave D world,

why is SysError.msg() (from std.syserror) not implemented like this?

#extern (C) char* strerror(int errnum);
#
#...
#    static char[] msg(uint errcode = getErrno())
#    {
#        char* msg = strerror(errcode);
#        return msg[0 .. strlen(msg)].dup;
#    }

As far as I know, strerror() is supposed to be portable enough.

-ben
Jan 30 2005
next sibling parent reply Benjamin Herr <ben 0x539.de> writes:
Benjamin Herr wrote:
 Hell, brave D world,

Needs more o. Sorry.
Jan 30 2005
parent pragma <pragma_member pathlink.com> writes:
In article <ctjgvp$196f$4 digitaldaemon.com>, Benjamin Herr says...
Benjamin Herr wrote:
 Hell, brave D world,

Needs more o. Sorry.

Actually, its more accurate the way it was. We're still Beta. - EricAnderton at yahoo
Jan 30 2005
prev sibling parent "Regan Heath" <regan netwin.co.nz> writes:
------------epujy7hzvP4DFVhTO1rPl8
Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15
Content-Transfer-Encoding: 8bit

On Sun, 30 Jan 2005 21:36:44 +0100, Benjamin Herr <ben 0x539.de> wrote:
 Hell, brave D world,

 why is SysError.msg() (from std.syserror) not implemented like this?

 #extern (C) char* strerror(int errnum);
 #
 #...
 #    static char[] msg(uint errcode = getErrno())
 #    {
 #        char* msg = strerror(errcode);
 #        return msg[0 .. strlen(msg)].dup;
 #    }

 As far as I know, strerror() is supposed to be portable enough.

Try this one. Regan ------------epujy7hzvP4DFVhTO1rPl8 Content-Disposition: attachment; filename=syserror.d Content-Type: application/octet-stream; name=syserror.d Content-Transfer-Encoding: 8bit module lib.syserror; private import std.string; private import std.c.windows.windows; private import std.c.stdarg; extern (Windows) { DWORD FormatMessageA( DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPTSTR lpBuffer, DWORD nSize, va_list *Arguments ); static uint FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100; static uint FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; static uint FORMAT_MESSAGE_FROM_STRING = 0x00000400; static uint FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; static uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; static uint FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; static uint FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF; WORD MAKELANGID(WORD p, WORD s) { return (((cast(WORD)s) << 10) | cast(WORD)p); } WORD PRIMARYLANGID(WORD lgid) { return (cast(WORD)lgid & 0x3ff); } WORD SUBLANGID(WORD lgid) { return (cast(WORD)lgid >> 10); } static uint LANG_NEUTRAL = 0x00; static uint SUBLANG_DEFAULT = 0x01; alias HGLOBAL HLOCAL; HLOCAL LocalFree(HLOCAL hMem); } extern (C) char *strerror(int); class SysError { static char[] msg(uint errcode) { char[] text; version(Windows) { if (errcode == ERROR_NOT_ENOUGH_MEMORY) text = "not enough memory"; else { LPVOID lpMsgBuf; DWORD r; r = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, null, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language cast(LPTSTR)&lpMsgBuf, 0, null ); /* Remove \r\n from error string */ if (r >= 2) r -= 2; text = (cast(char *)lpMsgBuf)[0..r].dup; LocalFree(cast(HLOCAL)lpMsgBuf); } } else { if (errcode == ENOMEM) text = "not enough memory"; else { char* pemsg; uint r; pemsg = strerror(errcode); r = strlen(pemsg); /* Remove \r\n from error string */ if (pemsg[r-1] == '\n') r--; if (pemsg[r-1] == '\r') r--; text = pemsg[0..r].dup; } } return text; } } ------------epujy7hzvP4DFVhTO1rPl8--
Jan 31 2005