summaryrefslogtreecommitdiff
path: root/src/errors.c
blob: 6b715484012737ebf09971cd6d7e3764d4ae32b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

    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 <http://www.gnu.org/licenses/>.
*/

#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 <windows.h>			/* for GetLastError */
#else
#include <errno.h>			/* 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, _( "<unknown error>" ), 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
}