librazor/error.c
changeset 423 6112bcc5d1cf
child 424 8cbc438cc298
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/librazor/error.c	Sat Feb 11 23:50:26 2012 +0000
     1.3 @@ -0,0 +1,98 @@
     1.4 +/*
     1.5 + * Copyright (C) 2011-2012  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + *
     1.7 + * This program is free software; you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU General Public License as published by
     1.9 + * the Free Software Foundation; either version 2 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License along
    1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
    1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.20 + */
    1.21 +
    1.22 +#include "config.h"
    1.23 +
    1.24 +#ifdef MSWIN_API
    1.25 +#include <windows.h>
    1.26 +#endif
    1.27 +#include <stdlib.h>
    1.28 +#include <string.h>
    1.29 +
    1.30 +#include "razor.h"
    1.31 +#include "razor-internal.h"
    1.32 +
    1.33 +RAZOR_EXPORT const char *
    1.34 +razor_error_get_msg(struct razor_error *error)
    1.35 +{
    1.36 +	if (!error->msg) {
    1.37 +		if (error->path)
    1.38 +			error->msg = razor_concat(error->path, ": ", error->str,
    1.39 +						  NULL);
    1.40 +		else
    1.41 +			error->msg = strdup(error->str);
    1.42 +	}
    1.43 +
    1.44 +	return error->msg;
    1.45 +}
    1.46 +
    1.47 +#ifdef MSWIN_API
    1.48 +struct razor_error *
    1.49 +razor_error_new_mswin(const wchar_t *path, DWORD err)
    1.50 +{
    1.51 +	struct razor_error *error;
    1.52 +	wchar_t *buf;
    1.53 +
    1.54 +	error = zalloc(sizeof *error);
    1.55 +
    1.56 +	if (path)
    1.57 +		error->path = razor_utf16_to_utf8(path, -1);
    1.58 +
    1.59 +	FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
    1.60 +		       FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
    1.61 +		       NULL, err, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
    1.62 +		       (LPWSTR)&buf, 0, NULL);
    1.63 +	error->str = razor_utf16_to_utf8(buf, -1);
    1.64 +	LocalFree(buf);
    1.65 +
    1.66 +	return error;
    1.67 +}
    1.68 +#endif
    1.69 +
    1.70 +#if HAVE_WINDOWS_KTM
    1.71 +struct razor_error *
    1.72 +razor_error_new_str(const wchar_t *path, const char *str)
    1.73 +#else
    1.74 +struct razor_error *
    1.75 +razor_error_new_str(const char *path, const char *str)
    1.76 +#endif
    1.77 +{
    1.78 +	struct razor_error *error;
    1.79 +
    1.80 +	error = zalloc(sizeof *error);
    1.81 +
    1.82 +#if HAVE_WINDOWS_KTM
    1.83 +	if (path)
    1.84 +		error->path = razor_utf16_to_utf8(path, -1);
    1.85 +#else
    1.86 +	if (path)
    1.87 +		error->path = strdup(path);
    1.88 +#endif
    1.89 +
    1.90 +	error->str = strdup(str);
    1.91 +
    1.92 +	return error;
    1.93 +}
    1.94 +
    1.95 +void razor_error_free(struct razor_error *error)
    1.96 +{
    1.97 +    free(error->path);
    1.98 +    free(error->str);
    1.99 +    free(error->msg);
   1.100 +    free(error);
   1.101 +}