acpi.4: Add some missing references.
[dragonfly.git] / contrib / bind-9.3 / lib / isc / nls / msgcat.c
1 /*
2  * Copyright (C) 2004, 2005  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.12.6 2005/06/09 23:54:31 marka Exp $ */
19
20 /*
21  * Principal Author: Bob Halley
22  */
23
24 #include <config.h>
25
26 #include <stddef.h>
27 #include <stdlib.h>
28
29 #include <isc/magic.h>
30 #include <isc/msgcat.h>
31 #include <isc/util.h>
32
33 #ifdef HAVE_CATGETS
34 #include <nl_types.h>           /* Required for nl_catd. */
35 #endif
36
37 /*
38  * Implementation Notes:
39  *
40  *      We use malloc() and free() instead of isc_mem_get() and isc_mem_put()
41  *      because we don't want to require a memory context to be specified
42  *      in order to use a message catalog.
43  */
44
45 struct isc_msgcat {
46         unsigned int    magic;
47 #ifdef HAVE_CATGETS
48         nl_catd         catalog;
49 #endif
50 };
51
52 #define MSGCAT_MAGIC                    ISC_MAGIC('M', 'C', 'a', 't')
53 #define VALID_MSGCAT(m)                 ISC_MAGIC_VALID(m, MSGCAT_MAGIC)
54
55 void
56 isc_msgcat_open(const char *name, isc_msgcat_t **msgcatp) {
57         isc_msgcat_t *msgcat;
58
59         /*
60          * Open a message catalog.
61          */
62
63         REQUIRE(name != NULL);
64         REQUIRE(msgcatp != NULL && *msgcatp == NULL);
65
66         msgcat = malloc(sizeof(*msgcat));
67         if (msgcat == NULL) {
68                 *msgcatp = NULL;
69                 return;
70         }
71
72 #ifdef HAVE_CATGETS
73         /*
74          * We don't check if catopen() fails because we don't care.
75          * If it does fail, then when we call catgets(), it will use
76          * the default string.
77          */
78         msgcat->catalog = catopen(name, 0);
79 #endif
80         msgcat->magic = MSGCAT_MAGIC;
81
82         *msgcatp = msgcat;
83 }
84
85 void
86 isc_msgcat_close(isc_msgcat_t **msgcatp) {
87         isc_msgcat_t *msgcat;
88
89         /*
90          * Close a message catalog.
91          */
92
93         REQUIRE(msgcatp != NULL);
94         msgcat = *msgcatp;
95         REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
96
97         if (msgcat != NULL) {
98 #ifdef HAVE_CATGETS
99                 if (msgcat->catalog != (nl_catd)(-1))
100                         (void)catclose(msgcat->catalog);
101 #endif
102                 msgcat->magic = 0;
103                 free(msgcat);
104         }
105
106         *msgcatp = NULL;
107 }
108
109 const char *
110 isc_msgcat_get(isc_msgcat_t *msgcat, int set, int message,
111                const char *default_text)
112 {
113         /*
114          * Get message 'message' from message set 'set' in 'msgcat'.  If it
115          * is not available, use 'default'.
116          */
117
118         REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
119         REQUIRE(set > 0);
120         REQUIRE(message > 0);
121         REQUIRE(default_text != NULL);
122
123 #ifdef HAVE_CATGETS
124         if (msgcat == NULL)
125                 return (default_text);
126         return (catgets(msgcat->catalog, set, message, default_text));
127 #else
128         return (default_text);
129 #endif
130 }