librazor/error.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Feb 11 23:50:26 2012 +0000 (2012-02-11)
changeset 423 6112bcc5d1cf
child 424 8cbc438cc298
permissions -rw-r--r--
Add an error object.
This is intended to dis-entangle the two roles that the atomic
object has evolved into so that atomic need only be used where
atomic actions are actually being undertaken.
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
#endif
ali@423
    66
ali@423
    67
#if HAVE_WINDOWS_KTM
ali@423
    68
struct razor_error *
ali@423
    69
razor_error_new_str(const wchar_t *path, const char *str)
ali@423
    70
#else
ali@423
    71
struct razor_error *
ali@423
    72
razor_error_new_str(const char *path, const char *str)
ali@423
    73
#endif
ali@423
    74
{
ali@423
    75
	struct razor_error *error;
ali@423
    76
ali@423
    77
	error = zalloc(sizeof *error);
ali@423
    78
ali@423
    79
#if HAVE_WINDOWS_KTM
ali@423
    80
	if (path)
ali@423
    81
		error->path = razor_utf16_to_utf8(path, -1);
ali@423
    82
#else
ali@423
    83
	if (path)
ali@423
    84
		error->path = strdup(path);
ali@423
    85
#endif
ali@423
    86
ali@423
    87
	error->str = strdup(str);
ali@423
    88
ali@423
    89
	return error;
ali@423
    90
}
ali@423
    91
ali@423
    92
void razor_error_free(struct razor_error *error)
ali@423
    93
{
ali@423
    94
    free(error->path);
ali@423
    95
    free(error->str);
ali@423
    96
    free(error->msg);
ali@423
    97
    free(error);
ali@423
    98
}