Back to index

plt-scheme  4.2.1
bindtextdom.c
Go to the documentation of this file.
00001 /* bindtextdom.c -- implementation of the bindtextdomain(3) function
00002    Copyright (C) 1995 Free Software Foundation, Inc.
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2, or (at your option)
00007 any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00017 
00018 #ifdef HAVE_CONFIG_H
00019 # include <config.h>
00020 #endif
00021 
00022 #if defined STDC_HEADERS || defined _LIBC
00023 # include <stdlib.h>
00024 #else
00025 # ifdef HAVE_MALLOC_H
00026 #  include <malloc.h>
00027 # else
00028 void free ();
00029 # endif
00030 #endif
00031 
00032 #if defined HAVE_STRING_H || defined _LIBC
00033 # include <string.h>
00034 #else
00035 # include <strings.h>
00036 #endif
00037 
00038 #ifdef _LIBC
00039 # include <libintl.h>
00040 #else
00041 # include "libgettext.h"
00042 #endif
00043 #include "gettext.h"
00044 #include "gettextP.h"
00045 
00046 /* @@ end of prolog @@ */
00047 
00048 /* Contains the default location of the message catalogs.  */
00049 extern const char _nl_default_dirname[];
00050 
00051 /* List with bindings of specific domains.  */
00052 extern struct binding *_nl_domain_bindings;
00053 
00054 
00055 /* Names for the libintl functions are a problem.  They must not clash
00056    with existing names and they should follow ANSI C.  But this source
00057    code is also used in GNU C Library where the names have a __
00058    prefix.  So we have to make a difference here.  */
00059 #ifdef _LIBC
00060 # define BINDTEXTDOMAIN __bindtextdomain
00061 #else
00062 # define BINDTEXTDOMAIN bindtextdomain__
00063 #endif
00064 
00065 /* Specify that the DOMAINNAME message catalog will be found
00066    in DIRNAME rather than in the system locale data base.  */
00067 char *
00068 BINDTEXTDOMAIN (domainname, dirname)
00069      const char *domainname;
00070      const char *dirname;
00071 {
00072   struct binding *binding;
00073 
00074   /* Some sanity checks.  */
00075   if (domainname == NULL || domainname[0] == '\0')
00076     return NULL;
00077 
00078   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
00079     {
00080       int compare = strcmp (domainname, binding->domainname);
00081       if (compare == 0)
00082        /* We found it!  */
00083        break;
00084       if (compare < 0)
00085        {
00086          /* It is not in the list.  */
00087          binding = NULL;
00088          break;
00089        }
00090     }
00091 
00092   if (dirname == NULL)
00093     /* The current binding has be to returned.  */
00094     return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
00095 
00096   if (binding != NULL)
00097     {
00098       /* The domain is already bound.  Replace the old binding.  */
00099       char *new_dirname;
00100 
00101       if (strcmp (dirname, _nl_default_dirname) == 0)
00102        new_dirname = (char *) _nl_default_dirname;
00103       else
00104        {
00105          size_t len = strlen (dirname) + 1;
00106          new_dirname = (char *) malloc (len);
00107          if (new_dirname == NULL)
00108            return NULL;
00109 
00110          memcpy (new_dirname, dirname, len);
00111        }
00112 
00113       if (strcmp (binding->dirname, _nl_default_dirname) != 0)
00114         free (binding->dirname);
00115 
00116       binding->dirname = new_dirname;
00117     }
00118   else
00119     {
00120       /* We have to create a new binding.  */
00121       size_t len;
00122       struct binding *new_binding =
00123        (struct binding *) malloc (sizeof (*new_binding));
00124 
00125       if (new_binding == NULL)
00126        return NULL;
00127 
00128       len = strlen (domainname) + 1;
00129       new_binding->domainname = (char *) malloc (len);
00130       if (new_binding->domainname == NULL)
00131          return NULL;
00132       memcpy (new_binding->domainname, domainname, len);
00133 
00134       if (strcmp (dirname, _nl_default_dirname) == 0)
00135        new_binding->dirname = (char *) _nl_default_dirname;
00136       else
00137        {
00138          len = strlen (dirname) + 1;
00139          new_binding->dirname = (char *) malloc (len);
00140          if (new_binding->dirname == NULL)
00141            return NULL;
00142          memcpy (new_binding->dirname, dirname, len);
00143        }
00144 
00145       /* Now enqueue it.  */
00146       if (_nl_domain_bindings == NULL
00147          || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
00148        {
00149          new_binding->next = _nl_domain_bindings;
00150          _nl_domain_bindings = new_binding;
00151        }
00152       else
00153        {
00154          binding = _nl_domain_bindings;
00155          while (binding->next != NULL
00156                && strcmp (domainname, binding->next->domainname) > 0)
00157            binding = binding->next;
00158 
00159          new_binding->next = binding->next;
00160          binding->next = new_binding;
00161        }
00162 
00163       binding = new_binding;
00164     }
00165 
00166   return binding->dirname;
00167 }
00168 
00169 #ifdef _LIBC
00170 /* Alias for function name in GNU C Library.  */
00171 weak_alias (__bindtextdomain, bindtextdomain);
00172 #endif