/* Copyright (C) 2008 Andreas Baumann This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "port/string.h" /* for strncpy, strerror_r */ #include "errors.h" #include "i18n/gettext.h" /* for localization */ #if defined _WIN32 #define WIN32_MEAN_AND_LEAN #include /* for GetLastError */ #else #include /* for errno */ #endif /* defined _WIN32 */ char *wolf_error_msg( const wolf_error_t error, char *buf, size_t buflen ) { switch( error ) { case WOLF_OK: strncpy( buf, _( "No error" ), buflen ); break; case WOLF_ERR_OUT_OF_MEMORY: strncpy( buf, _( "Out of memory" ), buflen ); break; case WOLF_ERR_INVALID_STATE: strncpy( buf, _( "Invalid state" ), buflen ); break; case WOLF_ERR_INVALID_VALUE: strncpy( buf, _( "Invalid value" ), buflen ); break; case WOLF_ERR_INTERNAL: strncpy( buf, _( "Internal error" ), buflen ); break; case WOLF_ERR_PROGRAMMING: strncpy( buf, _( "Programming error" ), buflen ); break; case WOLF_ERR_NOT_IMPLEMENTED: strncpy( buf, _( "Not implemented" ), buflen ); break; case WOLF_ERR_TIMEOUT: strncpy( buf, _( "Timeout" ), buflen ); break; default: strncpy( buf, _( "" ), buflen ); } return buf; } char *wolf_system_error_msg( char *buf, size_t buflen ) { #ifdef _WIN32 LPTSTR errbuf; DWORD errbuf_len; DWORD res; DWORD last_error; last_error = GetLastError( ); res = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, /* message is from system */ last_error, /* there is a message with that id */ 0, /* default language preference */ (LPTSTR)&errbuf, /* buffer allocated internally with LocalAlloc */ 0, /* minimum allocation size */ NULL ); /* no arguments */ if( res == 0 ) { strlcpy( buf, _( "No message available" ), buflen ); } else { strlcpy( buf, errbuf, buflen ); LocalFree( errbuf ); } return buf; #else (void)strerror_r( errno, buf, buflen ); return buf; #endif }