www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [enhancement] Another error code for std.syserror

reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
     case 123: result = "invalid name"; break;
Jan 27 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
------------6TwHaZQ86RZUoFHCkDo9tp
Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15
Content-Transfer-Encoding: 8bit

On Fri, 28 Jan 2005 10:03:23 +1100, Matthew  
<admin stlsoft.dot.dot.dot.dot.org> wrote:
      case 123: result = "invalid name"; break;

Or instead of adding errors as you find them you could simply use the attached code. Regan ------------6TwHaZQ86RZUoFHCkDo9tp 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) { 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 { 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; } } ------------6TwHaZQ86RZUoFHCkDo9tp--
Jan 27 2005
parent reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Regan Heath" <regan netwin.co.nz> wrote in message 
news:opslacfpe123k2f5 ally...
 On Fri, 28 Jan 2005 10:03:23 +1100, Matthew
 <admin stlsoft.dot.dot.dot.dot.org> wrote:
      case 123: result = "invalid name"; break;

Or instead of adding errors as you find them you could simply use the attached code.

Sure, but what about ERROR_NOT_ENOUGH_MEMORY?
Jan 27 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
------------qfWxlDLxjlwCZYmFV2GG0T
Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15
Content-Transfer-Encoding: 8bit

On Fri, 28 Jan 2005 11:31:09 +1100, Matthew  
<admin stlsoft.dot.dot.dot.dot.org> wrote:
 "Regan Heath" <regan netwin.co.nz> wrote in message
 news:opslacfpe123k2f5 ally...
 On Fri, 28 Jan 2005 10:03:23 +1100, Matthew
 <admin stlsoft.dot.dot.dot.dot.org> wrote:
      case 123: result = "invalid name"; break;

Or instead of adding errors as you find them you could simply use the attached code.

Sure, but what about ERROR_NOT_ENOUGH_MEMORY?

Good point. We can simply add a special case static string for that, right? Attached. Regan ------------qfWxlDLxjlwCZYmFV2GG0T 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; } } ------------qfWxlDLxjlwCZYmFV2GG0T--
Jan 27 2005
parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Regan Heath" <regan netwin.co.nz> wrote in message 
news:opslae9qfh23k2f5 ally...
 On Fri, 28 Jan 2005 11:31:09 +1100, Matthew
 <admin stlsoft.dot.dot.dot.dot.org> wrote:
 "Regan Heath" <regan netwin.co.nz> wrote in message
 news:opslacfpe123k2f5 ally...
 On Fri, 28 Jan 2005 10:03:23 +1100, Matthew
 <admin stlsoft.dot.dot.dot.dot.org> wrote:
      case 123: result = "invalid name"; break;

Or instead of adding errors as you find them you could simply use the attached code.

Sure, but what about ERROR_NOT_ENOUGH_MEMORY?

Good point. We can simply add a special case static string for that, right? Attached.

Agreed. :-)
Jan 27 2005