2 * Copyright (C) 2011-2012 J. Ali Harlow <ali@juiblex.co.uk>
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.
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.
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.
28 #include "razor-internal.h"
30 static const char *razor_error_get_path_str(struct razor_error *error)
33 return error->path_str;
36 error->path_str = razor_concat(error->path, ": ",
38 return error->path_str;
45 * razor_error_get_primary_text:
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.
51 * Returns: Primary text of error.
53 RAZOR_EXPORT const char *
54 razor_error_get_primary_text(struct razor_error *error)
57 return error->summary;
59 return razor_error_get_path_str(error);
63 * razor_error_get_secondary_text:
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.
69 * Returns: Secondary text of error or NULL.
71 RAZOR_EXPORT const char *
72 razor_error_get_secondary_text(struct razor_error *error)
77 return razor_error_get_path_str(error);
80 RAZOR_EXPORT const char *
81 razor_error_get_msg(struct razor_error *error)
83 const char *primary, *secondary;
88 primary = razor_error_get_primary_text(error);
90 secondary = razor_error_get_secondary_text(error);
93 error->msg = razor_concat(primary, "\n", secondary, NULL);
101 razor_error_new_mswin(const wchar_t *path, DWORD err)
103 struct razor_error *error;
106 error = zalloc(sizeof *error);
109 error->path = razor_utf16_to_utf8(path, -1);
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);
122 razor_error_new_str2(const wchar_t *path, const char *str)
124 struct razor_error *error;
126 error = zalloc(sizeof *error);
129 error->path = razor_utf16_to_utf8(path, -1);
131 error->str = strdup(str);
135 #endif /* MSWIN_API */
137 RAZOR_EXPORT struct razor_error *
138 razor_error_new_str(const char *path, const char *str)
140 struct razor_error *error;
142 error = zalloc(sizeof *error);
145 error->path = strdup(path);
147 error->str = strdup(str);
152 RAZOR_EXPORT struct razor_error *
153 razor_error_dup(struct razor_error *src, const char *summary)
155 struct razor_error *error;
157 error = zalloc(sizeof *error);
160 error->summary = strdup(summary);
161 else if (src->summary)
162 error->summary = strdup(src->summary);
165 error->path = strdup(src->path);
168 error->str = strdup(src->str);
174 razor_error_free(struct razor_error *error)
178 free(error->path_str);
179 free(error->summary);