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.
     1 /*
     2  * Copyright (C) 2011-2012  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 
    19 #include "config.h"
    20 
    21 #ifdef MSWIN_API
    22 #include <windows.h>
    23 #endif
    24 #include <stdlib.h>
    25 #include <string.h>
    26 
    27 #include "razor.h"
    28 #include "razor-internal.h"
    29 
    30 RAZOR_EXPORT const char *
    31 razor_error_get_msg(struct razor_error *error)
    32 {
    33 	if (!error->msg) {
    34 		if (error->path)
    35 			error->msg = razor_concat(error->path, ": ", error->str,
    36 						  NULL);
    37 		else
    38 			error->msg = strdup(error->str);
    39 	}
    40 
    41 	return error->msg;
    42 }
    43 
    44 #ifdef MSWIN_API
    45 struct razor_error *
    46 razor_error_new_mswin(const wchar_t *path, DWORD err)
    47 {
    48 	struct razor_error *error;
    49 	wchar_t *buf;
    50 
    51 	error = zalloc(sizeof *error);
    52 
    53 	if (path)
    54 		error->path = razor_utf16_to_utf8(path, -1);
    55 
    56 	FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
    57 		       FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
    58 		       NULL, err, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
    59 		       (LPWSTR)&buf, 0, NULL);
    60 	error->str = razor_utf16_to_utf8(buf, -1);
    61 	LocalFree(buf);
    62 
    63 	return error;
    64 }
    65 #endif
    66 
    67 #if HAVE_WINDOWS_KTM
    68 struct razor_error *
    69 razor_error_new_str(const wchar_t *path, const char *str)
    70 #else
    71 struct razor_error *
    72 razor_error_new_str(const char *path, const char *str)
    73 #endif
    74 {
    75 	struct razor_error *error;
    76 
    77 	error = zalloc(sizeof *error);
    78 
    79 #if HAVE_WINDOWS_KTM
    80 	if (path)
    81 		error->path = razor_utf16_to_utf8(path, -1);
    82 #else
    83 	if (path)
    84 		error->path = strdup(path);
    85 #endif
    86 
    87 	error->str = strdup(str);
    88 
    89 	return error;
    90 }
    91 
    92 void razor_error_free(struct razor_error *error)
    93 {
    94     free(error->path);
    95     free(error->str);
    96     free(error->msg);
    97     free(error);
    98 }