librazor/error.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Feb 17 18:35:10 2012 +0000 (2012-02-17)
changeset 426 2e896ad9754b
parent 423 6112bcc5d1cf
child 439 f28bb31024b4
permissions -rw-r--r--
razor_rpm_open() should take an error parameter, not an atomic
     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 
    66 struct razor_error *
    67 razor_error_new_str2(const wchar_t *path, const char *str)
    68 {
    69 	struct razor_error *error;
    70 
    71 	error = zalloc(sizeof *error);
    72 
    73 	if (path)
    74 		error->path = razor_utf16_to_utf8(path, -1);
    75 
    76 	error->str = strdup(str);
    77 
    78 	return error;
    79 }
    80 #endif	/* MSWIN_API */
    81 
    82 RAZOR_EXPORT struct razor_error *
    83 razor_error_new_str(const char *path, const char *str)
    84 {
    85 	struct razor_error *error;
    86 
    87 	error = zalloc(sizeof *error);
    88 
    89 	if (path)
    90 		error->path = strdup(path);
    91 
    92 	error->str = strdup(str);
    93 
    94 	return error;
    95 }
    96 
    97 RAZOR_EXPORT void
    98 razor_error_free(struct razor_error *error)
    99 {
   100 	free(error->path);
   101 	free(error->str);
   102 	free(error->msg);
   103 	free(error);
   104 }