Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / nls / msgcat.c
blobed3c0c9d9abc8af3e6db92352f13ae07aa45f570
1 /*
2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: msgcat.c,v 1.10.2.1 2004/03/09 06:12:04 marka Exp $ */
21 * Principal Author: Bob Halley
24 #include <config.h>
26 #include <stdlib.h>
28 #include <isc/magic.h>
29 #include <isc/msgcat.h>
30 #include <isc/util.h>
32 #ifdef HAVE_CATGETS
33 #include <nl_types.h> /* Required for nl_catd. */
34 #endif
37 * Implementation Notes:
39 * We use malloc() and free() instead of isc_mem_get() and isc_mem_put()
40 * because we don't want to require a memory context to be specified
41 * in order to use a message catalog.
44 struct isc_msgcat {
45 unsigned int magic;
46 #ifdef HAVE_CATGETS
47 nl_catd catalog;
48 #endif
51 #define MSGCAT_MAGIC ISC_MAGIC('M', 'C', 'a', 't')
52 #define VALID_MSGCAT(m) ISC_MAGIC_VALID(m, MSGCAT_MAGIC)
54 void
55 isc_msgcat_open(const char *name, isc_msgcat_t **msgcatp) {
56 isc_msgcat_t *msgcat;
59 * Open a message catalog.
62 REQUIRE(name != NULL);
63 REQUIRE(msgcatp != NULL && *msgcatp == NULL);
65 msgcat = malloc(sizeof *msgcat);
66 if (msgcat == NULL) {
67 *msgcatp = NULL;
68 return;
71 #ifdef HAVE_CATGETS
73 * We don't check if catopen() fails because we don't care.
74 * If it does fail, then when we call catgets(), it will use
75 * the default string.
77 msgcat->catalog = catopen(name, 0);
78 #endif
79 msgcat->magic = MSGCAT_MAGIC;
81 *msgcatp = msgcat;
84 void
85 isc_msgcat_close(isc_msgcat_t **msgcatp) {
86 isc_msgcat_t *msgcat;
89 * Close a message catalog.
92 REQUIRE(msgcatp != NULL);
93 msgcat = *msgcatp;
94 REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
96 if (msgcat != NULL) {
97 #ifdef HAVE_CATGETS
98 if (msgcat->catalog != (nl_catd)(-1))
99 catclose(msgcat->catalog);
100 #endif
101 msgcat->magic = 0;
102 free(msgcat);
105 *msgcatp = NULL;
108 const char *
109 isc_msgcat_get(isc_msgcat_t *msgcat, int set, int message,
110 const char *default_text)
113 * Get message 'message' from message set 'set' in 'msgcat'. If it
114 * is not available, use 'default'.
117 REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
118 REQUIRE(set > 0);
119 REQUIRE(message > 0);
120 REQUIRE(default_text != NULL);
122 #ifdef HAVE_CATGETS
123 if (msgcat == NULL)
124 return (default_text);
125 return (catgets(msgcat->catalog, set, message, default_text));
126 #else
127 return (default_text);
128 #endif