librazor/error.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Oct 04 18:12:58 2014 +0100 (2014-10-04)
changeset 454 56ff755c268c
parent 442 c4bcba8023a9
child 455 df914f383f5c
permissions -rw-r--r--
Only export symbols starting with razor_ in dynamic library.

Apart from being good practice to avoid clashes with higher-level
libraries and the application, this also fixes an obscure bug: The
gnulib library is used both by librazor (the dynamic library) and
by razor (the executable). In doing so, we want to have two separate
copies of the library despite the code duplication this involves.
Without the explicit limit to export only razor_ symbols, the razor
executable under mingw64 was picking up the getopt_long function
from librazor and the optind variable from libgnu which meant that
it did not see optind changing. Hiding librazor's copy of getopt
causes the linker to find libgnu's copy and everything works.

Note that under mingw librazor-#.dll still contains undocumented
(private) razor_ symbols but these will do no harm as long as nobody
tries to use them.
     1 /*
     2  * Copyright (C) 2011-2012, 2014  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 int
    31 razor_error_get_domain(struct razor_error *error)
    32 {
    33 	return error->domain;
    34 }
    35 
    36 RAZOR_EXPORT int
    37 razor_error_get_code(struct razor_error *error)
    38 {
    39 	return error->code;
    40 }
    41 
    42 RAZOR_EXPORT const char *
    43 razor_error_get_object(struct razor_error *error)
    44 {
    45 	return error->object;
    46 }
    47 
    48 static const char *razor_error_get_obj_str(struct razor_error *error)
    49 {
    50 	if (error->obj_str)
    51 		return error->obj_str;
    52 
    53 	if (error->object) {
    54 		error->obj_str = razor_concat(error->object, ": ",
    55 					      error->str, NULL);
    56 		return error->obj_str;
    57 	}
    58 
    59 	return error->str;
    60 }
    61 
    62 /**
    63  * razor_error_get_primary_text:
    64  *
    65  * Retrieves the basic information about an error. If a summary has been set
    66  * then this will be returned. Otherwise the error string possibly prefixed
    67  * by the object will be returned instead.
    68  *
    69  * Returns: Primary text of error.
    70  **/
    71 RAZOR_EXPORT const char *
    72 razor_error_get_primary_text(struct razor_error *error)
    73 {
    74 	if (error->summary)
    75 		return error->summary;
    76 
    77 	return razor_error_get_obj_str(error);
    78 }
    79 
    80 /**
    81  * razor_error_get_secondary_text:
    82  *
    83  * Retrieves more detailed information about an error, if any. If a summary
    84  * has been set the error string possibly prefixed by the object will be
    85  * returned. Otherwise NULL will be returned.
    86  *
    87  * Returns: Secondary text of error or NULL.
    88  **/
    89 RAZOR_EXPORT const char *
    90 razor_error_get_secondary_text(struct razor_error *error)
    91 {
    92 	if (!error->summary)
    93 		return NULL;
    94 
    95 	return razor_error_get_obj_str(error);
    96 }
    97 
    98 RAZOR_EXPORT const char *
    99 razor_error_get_msg(struct razor_error *error)
   100 {
   101 	const char *primary, *secondary;
   102 
   103 	if (error->msg)
   104 		return error->msg;
   105 
   106 	primary = razor_error_get_primary_text(error);
   107 
   108 	secondary = razor_error_get_secondary_text(error);
   109 
   110 	if (secondary) {
   111 		error->msg = razor_concat(primary, "\n", secondary, NULL);
   112 		return error->msg;
   113 	} else
   114 		return primary;
   115 }
   116 
   117 #ifdef MSWIN_API
   118 struct razor_error *
   119 razor_error_new_mswin(const wchar_t *object, DWORD err)
   120 {
   121 	struct razor_error *error;
   122 	wchar_t *buf;
   123 
   124 	error = zalloc(sizeof *error);
   125 
   126 	error->domain = RAZOR_MSWIN_ERROR;
   127 	error->code = err;
   128 
   129 	if (object)
   130 		error->object = razor_utf16_to_utf8(object, -1);
   131 
   132 	FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
   133 		       FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   134 		       NULL, err, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
   135 		       (LPWSTR)&buf, 0, NULL);
   136 	error->str = razor_utf16_to_utf8(buf, -1);
   137 	LocalFree(buf);
   138 
   139 	return error;
   140 }
   141 
   142 struct razor_error *
   143 razor_error_new_str2(int domain, int code, const wchar_t *object,
   144 		     const char *str)
   145 {
   146 	struct razor_error *error;
   147 
   148 	error = zalloc(sizeof *error);
   149 
   150 	error->domain = domain;
   151 	error->code = code;
   152 
   153 	if (object)
   154 		error->object = razor_utf16_to_utf8(object, -1);
   155 
   156 	error->str = strdup(str);
   157 
   158 	return error;
   159 }
   160 #endif	/* MSWIN_API */
   161 
   162 RAZOR_EXPORT struct razor_error *
   163 razor_error_new_str(int domain, int code, const char *object, const char *str)
   164 {
   165 	struct razor_error *error;
   166 
   167 	error = zalloc(sizeof *error);
   168 
   169 	error->domain = domain;
   170 	error->code = code;
   171 
   172 	if (object)
   173 		error->object = strdup(object);
   174 
   175 	error->str = strdup(str);
   176 
   177 	return error;
   178 }
   179 
   180 RAZOR_EXPORT struct razor_error *
   181 razor_error_dup(struct razor_error *src, const char *summary)
   182 {
   183 	struct razor_error *error;
   184 
   185 	error = zalloc(sizeof *error);
   186 
   187 	error->domain = src->domain;
   188 	error->code = src->code;
   189 
   190 	if (summary)
   191 		error->summary = strdup(summary);
   192 	else if (src->summary)
   193 		error->summary = strdup(src->summary);
   194 
   195 	if (src->object)
   196 		error->object = strdup(src->object);
   197 
   198 	if (src->str)
   199 		error->str = strdup(src->str);
   200 
   201 	return error;
   202 }
   203 
   204 RAZOR_EXPORT void
   205 razor_error_free(struct razor_error *error)
   206 {
   207 	free(error->object);
   208 	free(error->str);
   209 	free(error->obj_str);
   210 	free(error->summary);
   211 	free(error->msg);
   212 	free(error);
   213 }