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