ali@423: /* ali@423: * Copyright (C) 2011-2012 J. Ali Harlow ali@423: * ali@423: * This program is free software; you can redistribute it and/or modify ali@423: * it under the terms of the GNU General Public License as published by ali@423: * the Free Software Foundation; either version 2 of the License, or ali@423: * (at your option) any later version. ali@423: * ali@423: * This program is distributed in the hope that it will be useful, ali@423: * but WITHOUT ANY WARRANTY; without even the implied warranty of ali@423: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ali@423: * GNU General Public License for more details. ali@423: * ali@423: * You should have received a copy of the GNU General Public License along ali@423: * with this program; if not, write to the Free Software Foundation, Inc., ali@423: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ali@423: */ ali@423: ali@423: #include "config.h" ali@423: ali@423: #ifdef MSWIN_API ali@423: #include ali@423: #endif ali@423: #include ali@423: #include ali@423: ali@423: #include "razor.h" ali@423: #include "razor-internal.h" ali@423: ali@423: RAZOR_EXPORT const char * ali@423: razor_error_get_msg(struct razor_error *error) ali@423: { ali@423: if (!error->msg) { ali@423: if (error->path) ali@423: error->msg = razor_concat(error->path, ": ", error->str, ali@423: NULL); ali@423: else ali@423: error->msg = strdup(error->str); ali@423: } ali@423: ali@423: return error->msg; ali@423: } ali@423: ali@423: #ifdef MSWIN_API ali@423: struct razor_error * ali@423: razor_error_new_mswin(const wchar_t *path, DWORD err) ali@423: { ali@423: struct razor_error *error; ali@423: wchar_t *buf; ali@423: ali@423: error = zalloc(sizeof *error); ali@423: ali@423: if (path) ali@423: error->path = razor_utf16_to_utf8(path, -1); ali@423: ali@423: FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER| ali@423: FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, ali@423: NULL, err, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), ali@423: (LPWSTR)&buf, 0, NULL); ali@423: error->str = razor_utf16_to_utf8(buf, -1); ali@423: LocalFree(buf); ali@423: ali@423: return error; ali@423: } ali@423: #endif ali@423: ali@423: #if HAVE_WINDOWS_KTM ali@423: struct razor_error * ali@423: razor_error_new_str(const wchar_t *path, const char *str) ali@423: #else ali@423: struct razor_error * ali@423: razor_error_new_str(const char *path, const char *str) ali@423: #endif ali@423: { ali@423: struct razor_error *error; ali@423: ali@423: error = zalloc(sizeof *error); ali@423: ali@423: #if HAVE_WINDOWS_KTM ali@423: if (path) ali@423: error->path = razor_utf16_to_utf8(path, -1); ali@423: #else ali@423: if (path) ali@423: error->path = strdup(path); ali@423: #endif ali@423: ali@423: error->str = strdup(str); ali@423: ali@423: return error; ali@423: } ali@423: ali@423: void razor_error_free(struct razor_error *error) ali@423: { ali@423: free(error->path); ali@423: free(error->str); ali@423: free(error->msg); ali@423: free(error); ali@423: }