Merge from vendor branch CVS:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / nls / msgcat.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: msgcat.c,v 1.10.2.1 2004/03/09 06:12:04 marka Exp $ */
19
20 /*
21  * Principal Author: Bob Halley
22  */
23
24 #include <config.h>
25
26 #include <stdlib.h>
27
28 #include <isc/magic.h>
29 #include <isc/msgcat.h>
30 #include <isc/util.h>
31
32 #ifdef HAVE_CATGETS
33 #include <nl_types.h>           /* Required for nl_catd. */
34 #endif
35
36 /*
37  * Implementation Notes:
38  *
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.
42  */
43
44 struct isc_msgcat {
45         unsigned int    magic;
46 #ifdef HAVE_CATGETS
47         nl_catd         catalog;
48 #endif
49 };
50
51 #define MSGCAT_MAGIC                    ISC_MAGIC('M', 'C', 'a', 't')
52 #define VALID_MSGCAT(m)                 ISC_MAGIC_VALID(m, MSGCAT_MAGIC)
53
54 void
55 isc_msgcat_open(const char *name, isc_msgcat_t **msgcatp) {
56         isc_msgcat_t *msgcat;
57
58         /*
59          * Open a message catalog.
60          */
61
62         REQUIRE(name != NULL);
63         REQUIRE(msgcatp != NULL && *msgcatp == NULL);
64
65         msgcat = malloc(sizeof *msgcat);
66         if (msgcat == NULL) {
67                 *msgcatp = NULL;
68                 return;
69         }
70
71 #ifdef HAVE_CATGETS
72         /*
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.
76          */
77         msgcat->catalog = catopen(name, 0);
78 #endif
79         msgcat->magic = MSGCAT_MAGIC;
80
81         *msgcatp = msgcat;
82 }
83
84 void
85 isc_msgcat_close(isc_msgcat_t **msgcatp) {
86         isc_msgcat_t *msgcat;
87
88         /*
89          * Close a message catalog.
90          */
91
92         REQUIRE(msgcatp != NULL);
93         msgcat = *msgcatp;
94         REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
95
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);
103         }
104
105         *msgcatp = NULL;
106 }
107
108 const char *
109 isc_msgcat_get(isc_msgcat_t *msgcat, int set, int message,
110                const char *default_text)
111 {
112         /*
113          * Get message 'message' from message set 'set' in 'msgcat'.  If it
114          * is not available, use 'default'.
115          */
116
117         REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
118         REQUIRE(set > 0);
119         REQUIRE(message > 0);
120         REQUIRE(default_text != NULL);
121
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
129 }