File: SystemError.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (67 lines) | stat: -rw-r--r-- 1,491 bytes parent folder | download | duplicates (2)
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
#include "stdafx.h"
#include "SystemError.h"
#include "Convert.h"

#ifdef WINDOWS
#undef null
#include <comdef.h>
#define null NULL
#endif

namespace storm {

#if defined(WINDOWS)

	Str *systemErrorMessage(Engine &e, DWORD lastError) {
		wchar *buffer = null;
		size_t size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
									FORMAT_MESSAGE_FROM_SYSTEM |
									FORMAT_MESSAGE_IGNORE_INSERTS,
									NULL,
									lastError,
									// MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
									MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
									(wchar *)&buffer,
									0,
									NULL);
		try {
			Str *msg = new (e) Str(buffer);
			LocalFree(buffer);
			return msg;
		} catch (...) {
			LocalFree(buffer);
			throw;
		}
	}

	Str *hresultErrorMessage(Engine &e, HRESULT result) {
		return new (e) Str(_com_error(result).ErrorMessage());
	}

#endif

#if defined(POSIX)

	// There are two variants of strerror_r with different return values. Detect which one is used
	// and extract the error as appropriate.
	static const char *extract(char *buffer, int result) {
		if (result != 0)
			strcat(buffer, "unknown error");
		return buffer;
	}
	static const char *extract(char *buffer, const char *result) {
		(void)buffer;
		return result;
	}

	Str *systemErrorMessage(Engine &e, int error) {
		const size_t bufsize = 512;
		char buffer[bufsize] = { 0 };

		const char *msg = extract(buffer, strerror_r(error, buffer, bufsize));
		return new (e) Str(toWChar(e, msg));
	}

#endif

}