librazor/error.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Sep 09 15:04:24 2014 +0100 (2014-09-09)
changeset 446 4277359896dc
parent 439 f28bb31024b4
child 447 0a5e583393e1
permissions -rw-r--r--
Add razor_transaction_unsatisfied()
     1 /*
     2  * Copyright (C) 2011-2012  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include "config.h"
    20 
    21 #ifdef MSWIN_API
    22 #include <windows.h>
    23 #endif
    24 #include <stdlib.h>
    25 #include <string.h>
    26 
    27 #include "razor.h"
    28 #include "razor-internal.h"
    29 
    30 static const char *razor_error_get_path_str(struct razor_error *error)
    31 {
    32 	if (error->path_str)
    33 		return error->path_str;
    34 
    35 	if (error->path) {
    36 		error->path_str = razor_concat(error->path, ": ",
    37 					       error->str, NULL);
    38 		return error->path_str;
    39 	}
    40 
    41 	return error->str;
    42 }
    43 
    44 /**
    45  * razor_error_get_primary_text:
    46  *
    47  * Retrieves the basic information about an error. If a summary has been set
    48  * then this will be returned. Otherwise the error string possibly prefixed
    49  * by the path will be returned instead.
    50  *
    51  * Returns: Primary text of error.
    52  **/
    53 RAZOR_EXPORT const char *
    54 razor_error_get_primary_text(struct razor_error *error)
    55 {
    56 	if (error->summary)
    57 		return error->summary;
    58 
    59 	return razor_error_get_path_str(error);
    60 }
    61 
    62 /**
    63  * razor_error_get_secondary_text:
    64  *
    65  * Retrieves more detailed information about an error, if any. If a summary
    66  * has been set the error string possibly prefixed by the path will be
    67  * returned. Otherwise NULL will be returned.
    68  *
    69  * Returns: Secondary text of error or NULL.
    70  **/
    71 RAZOR_EXPORT const char *
    72 razor_error_get_secondary_text(struct razor_error *error)
    73 {
    74 	if (!error->summary)
    75 		return NULL;
    76 
    77 	return razor_error_get_path_str(error);
    78 }
    79 
    80 RAZOR_EXPORT const char *
    81 razor_error_get_msg(struct razor_error *error)
    82 {
    83 	const char *primary, *secondary;
    84 
    85 	if (error->msg)
    86 		return error->msg;
    87 
    88 	primary = razor_error_get_primary_text(error);
    89 
    90 	secondary = razor_error_get_secondary_text(error);
    91 
    92 	if (secondary) {
    93 		error->msg = razor_concat(primary, "\n", secondary, NULL);
    94 		return error->msg;
    95 	} else
    96 		return primary;
    97 }
    98 
    99 #ifdef MSWIN_API
   100 struct razor_error *
   101 razor_error_new_mswin(const wchar_t *path, DWORD err)
   102 {
   103 	struct razor_error *error;
   104 	wchar_t *buf;
   105 
   106 	error = zalloc(sizeof *error);
   107 
   108 	if (path)
   109 		error->path = razor_utf16_to_utf8(path, -1);
   110 
   111 	FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
   112 		       FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   113 		       NULL, err, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
   114 		       (LPWSTR)&buf, 0, NULL);
   115 	error->str = razor_utf16_to_utf8(buf, -1);
   116 	LocalFree(buf);
   117 
   118 	return error;
   119 }
   120 
   121 struct razor_error *
   122 razor_error_new_str2(const wchar_t *path, const char *str)
   123 {
   124 	struct razor_error *error;
   125 
   126 	error = zalloc(sizeof *error);
   127 
   128 	if (path)
   129 		error->path = razor_utf16_to_utf8(path, -1);
   130 
   131 	error->str = strdup(str);
   132 
   133 	return error;
   134 }
   135 #endif	/* MSWIN_API */
   136 
   137 RAZOR_EXPORT struct razor_error *
   138 razor_error_new_str(const char *path, const char *str)
   139 {
   140 	struct razor_error *error;
   141 
   142 	error = zalloc(sizeof *error);
   143 
   144 	if (path)
   145 		error->path = strdup(path);
   146 
   147 	error->str = strdup(str);
   148 
   149 	return error;
   150 }
   151 
   152 RAZOR_EXPORT struct razor_error *
   153 razor_error_dup(struct razor_error *src, const char *summary)
   154 {
   155 	struct razor_error *error;
   156 
   157 	error = zalloc(sizeof *error);
   158 
   159 	if (summary)
   160 		error->summary = strdup(summary);
   161 	else if (src->summary)
   162 		error->summary = strdup(src->summary);
   163 
   164 	if (src->path)
   165 		error->path = strdup(src->path);
   166 
   167 	if (src->str)
   168 		error->str = strdup(src->str);
   169 
   170 	return error;
   171 }
   172 
   173 RAZOR_EXPORT void
   174 razor_error_free(struct razor_error *error)
   175 {
   176 	free(error->path);
   177 	free(error->str);
   178 	free(error->path_str);
   179 	free(error->summary);
   180 	free(error->msg);
   181 	free(error);
   182 }