|
| 1 | +/* -*-c++-*- |
| 2 | +Copyright (C) 2016 LiveCode Ltd. |
| 3 | +
|
| 4 | +This file is part of LiveCode. |
| 5 | +
|
| 6 | +LiveCode is free software; you can redistribute it and/or modify it under |
| 7 | +the terms of the GNU General Public License v3 as published by the Free |
| 8 | +Software Foundation. |
| 9 | +
|
| 10 | +LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU General Public License |
| 16 | +along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ |
| 17 | + |
| 18 | +#include "system-private.h" |
| 19 | + |
| 20 | +#include <foundation-auto.h> |
| 21 | + |
| 22 | +#if defined(__WINDOWS__) |
| 23 | +/* ================================================================ |
| 24 | + * Windows implementations |
| 25 | + * ================================================================ */ |
| 26 | +# include <windows.h> |
| 27 | + |
| 28 | +MC_DLLEXPORT_DEF void |
| 29 | +MCSErrorReset () |
| 30 | +{ |
| 31 | + SetLastError (0); |
| 32 | +} |
| 33 | + |
| 34 | +MC_DLLEXPORT_DEF uint32_t |
| 35 | +MCSErrorGetCode () |
| 36 | +{ |
| 37 | + return GetLastError (); |
| 38 | +} |
| 39 | + |
| 40 | +MC_DLLEXPORT_DEF bool |
| 41 | +MCSErrorGetDescription (uint32_t p_code, |
| 42 | + MCStringRef& r_description) |
| 43 | +{ |
| 44 | + DWORD t_code = GetLastError (); |
| 45 | + |
| 46 | + /* No error has been recorded */ |
| 47 | + if (0 == t_code) |
| 48 | + { |
| 49 | + return MCStringCopy (kMCEmptyString, r_description); |
| 50 | + } |
| 51 | + |
| 52 | + LPWSTR t_message_buffer = nil; |
| 53 | + |
| 54 | + DWORD t_message_len = FormatMessageW ( |
| 55 | + (FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 56 | + FORMAT_MESSAGE_FROM_SYSTEM | |
| 57 | + FORMAT_MESSAGE_IGNORE_INSERTS), /* dwFlags */ |
| 58 | + nil, /* lpSource */ |
| 59 | + t_code, /* dwMessageId */ |
| 60 | + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* dwLanguageId */ |
| 61 | + reinterpret_cast<LPWSTR>(&t_message_buffer), /* lpBuffer */ |
| 62 | + 0, /* nSize */ |
| 63 | + nil); /* arguments... */ |
| 64 | + |
| 65 | + bool t_success = true; |
| 66 | + MCAutoStringRef t_description; |
| 67 | + |
| 68 | + if (t_success) |
| 69 | + t_success = MCStringCreateWithChars(t_message_buffer, |
| 70 | + t_message_len, |
| 71 | + &t_description); |
| 72 | + |
| 73 | + LocalFree(t_message_buffer); |
| 74 | + |
| 75 | + return MCStringCopy(*t_description, r_description); |
| 76 | +} |
| 77 | + |
| 78 | +#else /* !__WINDOWS__ */ |
| 79 | +/* ================================================================ |
| 80 | + * POSIX implementations |
| 81 | + * ================================================================ */ |
| 82 | + |
| 83 | +#include <errno.h> |
| 84 | + |
| 85 | +MC_DLLEXPORT_DEF void |
| 86 | +MCSErrorReset () |
| 87 | +{ |
| 88 | + errno = 0; |
| 89 | +} |
| 90 | + |
| 91 | +MC_DLLEXPORT_DEF uint32_t |
| 92 | +MCSErrorGetCode () |
| 93 | +{ |
| 94 | + return errno; |
| 95 | +} |
| 96 | + |
| 97 | +MC_DLLEXPORT_DEF bool |
| 98 | +MCSErrorGetDescription (uint32_t p_code, |
| 99 | + MCStringRef& r_description) |
| 100 | +{ |
| 101 | + return MCStringCreateWithCString(strerror(p_code), |
| 102 | + r_description); |
| 103 | +} |
| 104 | + |
| 105 | +#endif /* !__WINDOWS__ */ |
0 commit comments