www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Phobos SysError...

reply Regan Heath <regan netwin.co.nz> writes:
------------3XBoBeTjsysaYIp98kCb6Y
Content-Type: text/plain; format=flowed; charset=iso-8859-15
Content-Transfer-Encoding: 8bit

I just noticed the undocumented? SysError class in phobos. It appears to 
be for getting the OS error message associated with an error code.

It is currently a switch statement with some errno constants and constant 
strings.

I think it should be updated to call FormatMessage on windows and strerror 
on *NIX. Attached is my attempt at it, if it meets aproval pls consider it 
for inclusion in Phobos.

Regan.

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
------------3XBoBeTjsysaYIp98kCb6Y
Content-Disposition: attachment; filename=syserror.d
Content-Type: text/plain; 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);
}

class SysError
{
	static char[] msg(uint errcode)
	{
		char[] text;

		version(Windows)
		{
			LPVOID lpMsgBuf;			

			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 
			);
			text = (cast(char *)lpMsgBuf)[0..strlen(cast(char *)lpMsgBuf)];
			LocalFree(cast(HLOCAL)lpMsgBuf);
		}
		else
		{
			char* pemsg;

			pemsg = strerror(errcode);
			text = pemsg[0..strlen(pemsg)];
		}
		
		return text;
	}
}

------------3XBoBeTjsysaYIp98kCb6Y--
Jul 22 2004
next sibling parent reply Regan Heath <regan netwin.co.nz> writes:
I forgot to mention, the Windows functions I needed (FormatMessageA and 
LocalFree) are not declared in std.c.windows.windows, or aywhere else I 
could find.

Is this because they are defined in winbase.h and no-one has done a D stub 
for that yet?

I have declared them in the attached syserror.d but they should be moved.

The attached file may not work on *NIX it may require something like
   extern (C) char *strerror(int errnum);
I'm not sure.

Regan

On Fri, 23 Jul 2004 00:33:48 +1200, Regan Heath <regan netwin.co.nz> wrote:
 I just noticed the undocumented? SysError class in phobos. It appears to
 be for getting the OS error message associated with an error code.

 It is currently a switch statement with some errno constants and constant
 strings.

 I think it should be updated to call FormatMessage on windows and 
 strerror
 on *NIX. Attached is my attempt at it, if it meets aproval pls consider 
 it
 for inclusion in Phobos.

 Regan.

-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 2004
next sibling parent Regan Heath <regan netwin.co.nz> writes:
------------Atl4kGAxSCdt4IW7YxiZvq
Content-Type: text/plain; format=flowed; charset=iso-8859-15
Content-Transfer-Encoding: 8bit

<g> I always forget to polish my code.. this version has a few fixes, like 
the removal of the newline from the error string, and the addition of the 
extern (C) char *strerror(int) etc.

Regan

On Fri, 23 Jul 2004 00:40:53 +1200, Regan Heath <regan netwin.co.nz> wrote:
 I forgot to mention, the Windows functions I needed (FormatMessageA and 
 LocalFree) are not declared in std.c.windows.windows, or aywhere else I 
 could find.

 Is this because they are defined in winbase.h and no-one has done a D 
 stub for that yet?

 I have declared them in the attached syserror.d but they should be moved.

 The attached file may not work on *NIX it may require something like
    extern (C) char *strerror(int errnum);
 I'm not sure.

 Regan

 On Fri, 23 Jul 2004 00:33:48 +1200, Regan Heath <regan netwin.co.nz> 
 wrote:
 I just noticed the undocumented? SysError class in phobos. It appears to
 be for getting the OS error message associated with an error code.

 It is currently a switch statement with some errno constants and 
 constant
 strings.

 I think it should be updated to call FormatMessage on windows and 
 strerror
 on *NIX. Attached is my attempt at it, if it meets aproval pls consider 
 it
 for inclusion in Phobos.

 Regan.


-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ ------------Atl4kGAxSCdt4IW7YxiZvq Content-Disposition: attachment; filename=syserror.d Content-Type: text/plain; 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) { 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]; LocalFree(cast(HLOCAL)lpMsgBuf); } 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]; } return text; } } ------------Atl4kGAxSCdt4IW7YxiZvq--
Jul 22 2004
prev sibling parent "Matthew" <admin.hat stlsoft.dot.org> writes:
"Regan Heath" <regan netwin.co.nz> wrote in message
news:opsbjhifhn5a2sq9 digitalmars.com...
 I forgot to mention, the Windows functions I needed (FormatMessageA and
 LocalFree) are not declared in std.c.windows.windows, or aywhere else I
 could find.

They're included with WindowsException (with various useful techniques for loading string messages from different modules), which Walter's been hanging onto for several months, but is still not in Phobos. I'm still waiting ... sigh ...
 Is this because they are defined in winbase.h and no-one has done a D stub
 for that yet?

 I have declared them in the attached syserror.d but they should be moved.

 The attached file may not work on *NIX it may require something like
    extern (C) char *strerror(int errnum);
 I'm not sure.

 Regan

 On Fri, 23 Jul 2004 00:33:48 +1200, Regan Heath <regan netwin.co.nz> wrote:
 I just noticed the undocumented? SysError class in phobos. It appears to
 be for getting the OS error message associated with an error code.

 It is currently a switch statement with some errno constants and constant
 strings.

 I think it should be updated to call FormatMessage on windows and
 strerror
 on *NIX. Attached is my attempt at it, if it meets aproval pls consider
 it
 for inclusion in Phobos.

 Regan.

-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Jul 23 2004
prev sibling parent reply "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
Nice. Do you mind if I pop this into Mango until the 'official' version
appears? (with credit of course).

- Kris

"Regan Heath" <regan netwin.co.nz> wrote in message
news:opsbjg6mt85a2sq9 digitalmars.com...
 I just noticed the undocumented? SysError class in phobos. It appears to
 be for getting the OS error message associated with an error code.

 It is currently a switch statement with some errno constants and constant
 strings.

 I think it should be updated to call FormatMessage on windows and strerror
 on *NIX. Attached is my attempt at it, if it meets aproval pls consider it
 for inclusion in Phobos.

 Regan.

 --
 Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Jul 22 2004
parent Regan Heath <regan netwin.co.nz> writes:
Not at all, go right ahead.
:)

On Thu, 22 Jul 2004 10:15:18 -0700, Kris 
<someidiot earthlink.dot.dot.dot.net> wrote:

 Nice. Do you mind if I pop this into Mango until the 'official' version
 appears? (with credit of course).

 - Kris

 "Regan Heath" <regan netwin.co.nz> wrote in message
 news:opsbjg6mt85a2sq9 digitalmars.com...
 I just noticed the undocumented? SysError class in phobos. It appears to
 be for getting the OS error message associated with an error code.

 It is currently a switch statement with some errno constants and 
 constant
 strings.

 I think it should be updated to call FormatMessage on windows and 
 strerror
 on *NIX. Attached is my attempt at it, if it meets aproval pls consider 
 it
 for inclusion in Phobos.

 Regan.

 --
 Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 2004