librazor/error.c
author ali <j.a.harlow@letterboxes.org>
Fri Mar 23 20:24:09 2012 +0000 (2012-03-23)
changeset 433 fe27cdbd76f2
parent 423 6112bcc5d1cf
child 439 f28bb31024b4
permissions -rw-r--r--
Release version 0.5.5
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@423
    30
RAZOR_EXPORT const char *
ali@423
    31
razor_error_get_msg(struct razor_error *error)
ali@423
    32
{
ali@423
    33
	if (!error->msg) {
ali@423
    34
		if (error->path)
ali@423
    35
			error->msg = razor_concat(error->path, ": ", error->str,
ali@423
    36
						  NULL);
ali@423
    37
		else
ali@423
    38
			error->msg = strdup(error->str);
ali@423
    39
	}
ali@423
    40
ali@423
    41
	return error->msg;
ali@423
    42
}
ali@423
    43
ali@423
    44
#ifdef MSWIN_API
ali@423
    45
struct razor_error *
ali@423
    46
razor_error_new_mswin(const wchar_t *path, DWORD err)
ali@423
    47
{
ali@423
    48
	struct razor_error *error;
ali@423
    49
	wchar_t *buf;
ali@423
    50
ali@423
    51
	error = zalloc(sizeof *error);
ali@423
    52
ali@423
    53
	if (path)
ali@423
    54
		error->path = razor_utf16_to_utf8(path, -1);
ali@423
    55
ali@423
    56
	FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
ali@423
    57
		       FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
ali@423
    58
		       NULL, err, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
ali@423
    59
		       (LPWSTR)&buf, 0, NULL);
ali@423
    60
	error->str = razor_utf16_to_utf8(buf, -1);
ali@423
    61
	LocalFree(buf);
ali@423
    62
ali@423
    63
	return error;
ali@423
    64
}
ali@423
    65
ali@423
    66
struct razor_error *
ali@424
    67
razor_error_new_str2(const wchar_t *path, const char *str)
ali@423
    68
{
ali@423
    69
	struct razor_error *error;
ali@423
    70
ali@423
    71
	error = zalloc(sizeof *error);
ali@423
    72
ali@423
    73
	if (path)
ali@423
    74
		error->path = razor_utf16_to_utf8(path, -1);
ali@424
    75
ali@424
    76
	error->str = strdup(str);
ali@424
    77
ali@424
    78
	return error;
ali@424
    79
}
ali@424
    80
#endif	/* MSWIN_API */
ali@424
    81
ali@424
    82
RAZOR_EXPORT struct razor_error *
ali@424
    83
razor_error_new_str(const char *path, const char *str)
ali@424
    84
{
ali@424
    85
	struct razor_error *error;
ali@424
    86
ali@424
    87
	error = zalloc(sizeof *error);
ali@424
    88
ali@423
    89
	if (path)
ali@423
    90
		error->path = strdup(path);
ali@423
    91
ali@423
    92
	error->str = strdup(str);
ali@423
    93
ali@423
    94
	return error;
ali@423
    95
}
ali@423
    96
ali@424
    97
RAZOR_EXPORT void
ali@424
    98
razor_error_free(struct razor_error *error)
ali@423
    99
{
ali@424
   100
	free(error->path);
ali@424
   101
	free(error->str);
ali@424
   102
	free(error->msg);
ali@424
   103
	free(error);
ali@423
   104
}