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