diff -r 000000000000 -r 6112bcc5d1cf librazor/error.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/librazor/error.c Sat Feb 11 23:50:26 2012 +0000 @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2011-2012 J. Ali Harlow + * + * 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 2 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#ifdef MSWIN_API +#include +#endif +#include +#include + +#include "razor.h" +#include "razor-internal.h" + +RAZOR_EXPORT const char * +razor_error_get_msg(struct razor_error *error) +{ + if (!error->msg) { + if (error->path) + error->msg = razor_concat(error->path, ": ", error->str, + NULL); + else + error->msg = strdup(error->str); + } + + return error->msg; +} + +#ifdef MSWIN_API +struct razor_error * +razor_error_new_mswin(const wchar_t *path, DWORD err) +{ + struct razor_error *error; + wchar_t *buf; + + error = zalloc(sizeof *error); + + if (path) + error->path = razor_utf16_to_utf8(path, -1); + + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER| + FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, err, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), + (LPWSTR)&buf, 0, NULL); + error->str = razor_utf16_to_utf8(buf, -1); + LocalFree(buf); + + return error; +} +#endif + +#if HAVE_WINDOWS_KTM +struct razor_error * +razor_error_new_str(const wchar_t *path, const char *str) +#else +struct razor_error * +razor_error_new_str(const char *path, const char *str) +#endif +{ + struct razor_error *error; + + error = zalloc(sizeof *error); + +#if HAVE_WINDOWS_KTM + if (path) + error->path = razor_utf16_to_utf8(path, -1); +#else + if (path) + error->path = strdup(path); +#endif + + error->str = strdup(str); + + return error; +} + +void razor_error_free(struct razor_error *error) +{ + free(error->path); + free(error->str); + free(error->msg); + free(error); +}