home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / id-utils-3.2-src.tgz / tar.out / fsf / id-utils / intl / bindtextdom.c next >
C/C++ Source or Header  |  1996-09-28  |  5KB  |  173 lines

  1. /* bindtextdom.c -- implementation of the bindtextdomain(3) function
  2.    Copyright (C) 1995 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21.  
  22. #if defined STDC_HEADERS || defined _LIBC
  23. # include <stdlib.h>
  24. #else
  25. # ifdef HAVE_MALLOC_H
  26. #  include <malloc.h>
  27. # else
  28. void free ();
  29. # endif
  30. #endif
  31.  
  32. #if defined HAVE_STRING_H || defined _LIBC
  33. # include <string.h>
  34. #else
  35. # include <strings.h>
  36. #endif
  37.  
  38. #ifdef _LIBC
  39. # include <libintl.h>
  40. #else
  41. # include "libgettext.h"
  42. #endif
  43. #include "gettext.h"
  44. #include "gettextP.h"
  45.  
  46. /* @@ end of prolog @@ */
  47.  
  48. /* Contains the default location of the message catalogs.  */
  49. extern const char _nl_default_dirname[];
  50.  
  51. /* List with bindings of specific domains.  */
  52. extern struct binding *_nl_domain_bindings;
  53.  
  54.  
  55. /* Names for the libintl functions are a problem.  They must not clash
  56.    with existing names and they should follow ANSI C.  But this source
  57.    code is also used in GNU C Library where the names have a __
  58.    prefix.  So we have to make a difference here.  */
  59. #ifdef _LIBC
  60. # define BINDTEXTDOMAIN __bindtextdomain
  61. #else
  62. # define BINDTEXTDOMAIN bindtextdomain__
  63. #endif
  64.  
  65. /* Specify that the DOMAINNAME message catalog will be found
  66.    in DIRNAME rather than in the system locale data base.  */
  67. char *
  68. BINDTEXTDOMAIN (domainname, dirname)
  69.      const char *domainname;
  70.      const char *dirname;
  71. {
  72.   struct binding *binding;
  73.  
  74.   /* Some sanity checks.  */
  75.   if (domainname == NULL || domainname[0] == '\0')
  76.     return NULL;
  77.  
  78.   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  79.     {
  80.       int compare = strcmp (domainname, binding->domainname);
  81.       if (compare == 0)
  82.     /* We found it!  */
  83.     break;
  84.       if (compare < 0)
  85.     {
  86.       /* It is not in the list.  */
  87.       binding = NULL;
  88.       break;
  89.     }
  90.     }
  91.  
  92.   if (dirname == NULL)
  93.     /* The current binding has be to returned.  */
  94.     return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
  95.  
  96.   if (binding != NULL)
  97.     {
  98.       /* The domain is already bound.  Replace the old binding.  */
  99.       char *new_dirname;
  100.  
  101.       if (strcmp (dirname, _nl_default_dirname) == 0)
  102.     new_dirname = (char *) _nl_default_dirname;
  103.       else
  104.     {
  105.       size_t len = strlen (dirname) + 1;
  106.       new_dirname = (char *) malloc (len);
  107.       if (new_dirname == NULL)
  108.         return NULL;
  109.  
  110.       memcpy (new_dirname, dirname, len);
  111.     }
  112.  
  113.       if (strcmp (binding->dirname, _nl_default_dirname) != 0)
  114.         free (binding->dirname);
  115.  
  116.       binding->dirname = new_dirname;
  117.     }
  118.   else
  119.     {
  120.       /* We have to create a new binding.  */
  121.       size_t len;
  122.       struct binding *new_binding =
  123.     (struct binding *) malloc (sizeof (*new_binding));
  124.  
  125.       if (new_binding == NULL)
  126.     return NULL;
  127.  
  128.       len = strlen (domainname) + 1;
  129.       new_binding->domainname = (char *) malloc (len);
  130.       if (new_binding->domainname == NULL)
  131.       return NULL;
  132.       memcpy (new_binding->domainname, domainname, len);
  133.  
  134.       if (strcmp (dirname, _nl_default_dirname) == 0)
  135.     new_binding->dirname = (char *) _nl_default_dirname;
  136.       else
  137.     {
  138.       len = strlen (dirname) + 1;
  139.       new_binding->dirname = (char *) malloc (len);
  140.       if (new_binding->dirname == NULL)
  141.         return NULL;
  142.       memcpy (new_binding->dirname, dirname, len);
  143.     }
  144.  
  145.       /* Now enqueue it.  */
  146.       if (_nl_domain_bindings == NULL
  147.       || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
  148.     {
  149.       new_binding->next = _nl_domain_bindings;
  150.       _nl_domain_bindings = new_binding;
  151.     }
  152.       else
  153.     {
  154.       binding = _nl_domain_bindings;
  155.       while (binding->next != NULL
  156.          && strcmp (domainname, binding->next->domainname) > 0)
  157.         binding = binding->next;
  158.  
  159.       new_binding->next = binding->next;
  160.       binding->next = new_binding;
  161.     }
  162.  
  163.       binding = new_binding;
  164.     }
  165.  
  166.   return binding->dirname;
  167. }
  168.  
  169. #ifdef _LIBC
  170. /* Alias for function name in GNU C Library.  */
  171. weak_alias (__bindtextdomain, bindtextdomain);
  172. #endif
  173.