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@439: const char *razor_error_get_path_str(struct razor_error *error) ali@439: { ali@439: if (error->path_str) ali@439: return error->path_str; ali@439: ali@439: if (error->path) { ali@439: error->path_str = razor_concat(error->path, ": ", ali@439: error->str, NULL); ali@439: return error->path_str; ali@439: } ali@439: ali@439: return error->str; ali@439: } ali@439: ali@439: /** ali@439: * razor_error_get_primary_text: ali@439: * ali@439: * Retrieves the basic information about an error. If a summary has been set ali@439: * then this will be returned. Otherwise the error string possibly prefixed ali@439: * by the path will be returned instead. ali@439: * ali@439: * Returns: Primary text of error. ali@439: **/ ali@439: RAZOR_EXPORT const char * ali@439: razor_error_get_primary_text(struct razor_error *error) ali@439: { ali@439: if (error->summary) ali@439: return error->summary; ali@439: ali@439: return razor_error_get_path_str(error); ali@439: } ali@439: ali@439: /** ali@439: * razor_error_get_secondary_text: ali@439: * ali@439: * Retrieves more detailed information about an error, if any. If a summary ali@439: * has been set the error string possibly prefixed by the path will be ali@439: * returned. Otherwise NULL will be returned. ali@439: * ali@439: * Returns: Secondary text of error or NULL. ali@439: **/ ali@439: RAZOR_EXPORT const char * ali@439: razor_error_get_secondary_text(struct razor_error *error) ali@439: { ali@439: if (!error->summary) ali@439: return NULL; ali@439: ali@439: return razor_error_get_path_str(error); ali@439: } ali@439: ali@423: RAZOR_EXPORT const char * ali@423: razor_error_get_msg(struct razor_error *error) ali@423: { ali@439: const char *primary, *secondary; ali@423: ali@439: if (error->msg) ali@439: return error->msg; ali@439: ali@439: primary = razor_error_get_primary_text(error); ali@439: ali@439: secondary = razor_error_get_secondary_text(error); ali@439: ali@439: if (secondary) { ali@439: error->msg = razor_concat(primary, "\n", secondary, NULL); ali@439: return error->msg; ali@439: } else ali@439: return primary; 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: ali@423: struct razor_error * ali@424: razor_error_new_str2(const wchar_t *path, const char *str) ali@423: { ali@423: struct razor_error *error; 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@424: ali@424: error->str = strdup(str); ali@424: ali@424: return error; ali@424: } ali@424: #endif /* MSWIN_API */ ali@424: ali@424: RAZOR_EXPORT struct razor_error * ali@424: razor_error_new_str(const char *path, const char *str) ali@424: { ali@424: struct razor_error *error; ali@424: ali@424: error = zalloc(sizeof *error); ali@424: ali@423: if (path) ali@423: error->path = strdup(path); ali@423: ali@423: error->str = strdup(str); ali@423: ali@423: return error; ali@423: } ali@423: ali@439: RAZOR_EXPORT struct razor_error * ali@439: razor_error_dup(struct razor_error *src, const char *summary) ali@439: { ali@439: struct razor_error *error; ali@439: ali@439: error = zalloc(sizeof *error); ali@439: ali@439: if (summary) ali@439: error->summary = strdup(summary); ali@439: else if (src->summary) ali@439: error->summary = strdup(src->summary); ali@439: ali@439: if (src->path) ali@439: error->path = strdup(src->path); ali@439: ali@439: if (src->str) ali@439: error->str = strdup(src->str); ali@439: ali@439: return error; ali@439: } ali@439: ali@424: RAZOR_EXPORT void ali@424: razor_error_free(struct razor_error *error) ali@423: { ali@424: free(error->path); ali@424: free(error->str); ali@439: free(error->path_str); ali@439: free(error->summary); ali@424: free(error->msg); ali@424: free(error); ali@423: }