ali@423: /* ali@447: * Copyright (C) 2011-2012, 2014 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@455: #ifndef ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED ali@455: #define ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED 6832L ali@455: #endif 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@447: RAZOR_EXPORT int ali@447: razor_error_get_domain(struct razor_error *error) ali@439: { ali@447: return error->domain; ali@447: } ali@439: ali@447: RAZOR_EXPORT int ali@447: razor_error_get_code(struct razor_error *error) ali@447: { ali@447: return error->code; ali@447: } ali@447: ali@499: void ali@499: razor_error_set_object(struct razor_error *error, const char *object) ali@499: { ali@499: if (!error) ali@499: return; ali@499: ali@499: free(error->object); ali@499: ali@499: if (object) ali@499: error->object = strdup(object); ali@499: else ali@499: error->object = NULL; ali@499: ali@499: if (error->obj_str) { ali@499: free(error->obj_str); ali@499: error->obj_str = NULL; ali@499: } ali@499: ali@499: if (error->msg) { ali@499: free(error->msg); ali@499: error->msg = NULL; ali@499: } ali@499: } ali@499: ali@447: RAZOR_EXPORT const char * ali@447: razor_error_get_object(struct razor_error *error) ali@447: { ali@447: return error->object; ali@447: } ali@447: ali@447: static const char *razor_error_get_obj_str(struct razor_error *error) ali@447: { ali@447: if (error->obj_str) ali@447: return error->obj_str; ali@447: ali@447: if (error->object) { ali@447: error->obj_str = razor_concat(error->object, ": ", ali@447: error->str, NULL); ali@447: return error->obj_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@447: * by the object 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@447: return razor_error_get_obj_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@447: * has been set the error string possibly prefixed by the object 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@447: return razor_error_get_obj_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@447: razor_error_new_mswin(const wchar_t *object, 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@447: error->domain = RAZOR_MSWIN_ERROR; ali@447: error->code = err; ali@447: ali@447: if (object) ali@447: error->object = razor_utf16_to_utf8(object, -1); ali@423: ali@455: switch(err) { ali@455: case ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED: ali@455: /* ali@455: * Attempting to include files in a transaction on a filesystem ali@455: * that doesn't support them (only NTFS?) produces this error ali@455: * for which the default text isn't very informative. Try and ali@455: * give more useful information. ali@455: */ ali@455: error->str = strdup("Not allowed (perhaps the filesystem " ali@455: "doesn't support transactions)"); ali@455: break; ali@455: default: ali@455: FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER| ali@455: FORMAT_MESSAGE_FROM_SYSTEM| ali@455: FORMAT_MESSAGE_IGNORE_INSERTS, ali@455: NULL, err, ali@455: MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), ali@455: (LPWSTR)&buf, 0, NULL); ali@455: error->str = razor_utf16_to_utf8(buf, -1); ali@455: LocalFree(buf); ali@455: break; ali@455: } ali@423: ali@423: return error; ali@423: } ali@423: ali@423: struct razor_error * ali@447: razor_error_new_str2(int domain, int code, const wchar_t *object, ali@447: const char *str) ali@423: { ali@423: struct razor_error *error; ali@423: ali@423: error = zalloc(sizeof *error); ali@423: ali@447: error->domain = domain; ali@447: error->code = code; ali@447: ali@447: if (object) ali@447: error->object = razor_utf16_to_utf8(object, -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@447: razor_error_new_str(int domain, int code, const char *object, const char *str) ali@424: { ali@424: struct razor_error *error; ali@424: ali@424: error = zalloc(sizeof *error); ali@424: ali@447: error->domain = domain; ali@447: error->code = code; ali@447: ali@447: if (object) ali@447: error->object = strdup(object); 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@447: error->domain = src->domain; ali@447: error->code = src->code; ali@447: 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@447: if (src->object) ali@447: error->object = strdup(src->object); 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@447: free(error->object); ali@424: free(error->str); ali@447: free(error->obj_str); ali@439: free(error->summary); ali@424: free(error->msg); ali@424: free(error); ali@423: }