msgcat.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1999-2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: msgcat.c,v 1.18 2007/06/19 23:47:18 tbox Exp $ */
00019 
00020 /*! \file msgcat.c
00021  *
00022  * \author Principal Author: Bob Halley
00023  */
00024 
00025 #include <config.h>
00026 
00027 #include <stddef.h>
00028 #include <stdlib.h>
00029 
00030 #include <isc/magic.h>
00031 #include <isc/msgcat.h>
00032 #include <isc/util.h>
00033 
00034 #ifdef HAVE_CATGETS
00035 #include <nl_types.h>           /* Required for nl_catd. */
00036 #endif
00037 
00038 /*
00039  * Implementation Notes:
00040  *
00041  *      We use malloc() and free() instead of isc_mem_get() and isc_mem_put()
00042  *      because we don't want to require a memory context to be specified
00043  *      in order to use a message catalog.
00044  */
00045 
00046 struct isc_msgcat {
00047         unsigned int    magic;
00048 #ifdef HAVE_CATGETS
00049         nl_catd         catalog;
00050 #endif
00051 };
00052 
00053 #define MSGCAT_MAGIC                    ISC_MAGIC('M', 'C', 'a', 't')
00054 #define VALID_MSGCAT(m)                 ISC_MAGIC_VALID(m, MSGCAT_MAGIC)
00055 
00056 void
00057 isc_msgcat_open(const char *name, isc_msgcat_t **msgcatp) {
00058         isc_msgcat_t *msgcat;
00059 
00060         /*
00061          * Open a message catalog.
00062          */
00063 
00064         REQUIRE(name != NULL);
00065         REQUIRE(msgcatp != NULL && *msgcatp == NULL);
00066 
00067         msgcat = malloc(sizeof(*msgcat));
00068         if (msgcat == NULL) {
00069                 *msgcatp = NULL;
00070                 return;
00071         }
00072 
00073 #ifdef HAVE_CATGETS
00074         /*
00075          * We don't check if catopen() fails because we don't care.
00076          * If it does fail, then when we call catgets(), it will use
00077          * the default string.
00078          */
00079         msgcat->catalog = catopen(name, 0);
00080 #endif
00081         msgcat->magic = MSGCAT_MAGIC;
00082 
00083         *msgcatp = msgcat;
00084 }
00085 
00086 void
00087 isc_msgcat_close(isc_msgcat_t **msgcatp) {
00088         isc_msgcat_t *msgcat;
00089 
00090         /*
00091          * Close a message catalog.
00092          */
00093 
00094         REQUIRE(msgcatp != NULL);
00095         msgcat = *msgcatp;
00096         REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
00097 
00098         if (msgcat != NULL) {
00099 #ifdef HAVE_CATGETS
00100                 if (msgcat->catalog != (nl_catd)(-1))
00101                         (void)catclose(msgcat->catalog);
00102 #endif
00103                 msgcat->magic = 0;
00104                 free(msgcat);
00105         }
00106 
00107         *msgcatp = NULL;
00108 }
00109 
00110 const char *
00111 isc_msgcat_get(isc_msgcat_t *msgcat, int set, int message,
00112                const char *default_text)
00113 {
00114         /*
00115          * Get message 'message' from message set 'set' in 'msgcat'.  If it
00116          * is not available, use 'default'.
00117          */
00118 
00119         REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
00120         REQUIRE(set > 0);
00121         REQUIRE(message > 0);
00122         REQUIRE(default_text != NULL);
00123 
00124 #ifdef HAVE_CATGETS
00125         if (msgcat == NULL)
00126                 return (default_text);
00127         return (catgets(msgcat->catalog, set, message, default_text));
00128 #else
00129         return (default_text);
00130 #endif
00131 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha