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