]> git.lyx.org Git - lyx.git/blob - intl/bindtextdom.c
Tiny things found while peering at chset code
[lyx.git] / intl / bindtextdom.c
1 /* Implementation of the bindtextdomain(3) function
2    Copyright (C) 1995, 1996, 1997 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 Foundation,
16    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 # ifndef memcpy
37 #  define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
38 # endif
39 #endif
40
41 #ifdef _LIBC
42 # include <libintl.h>
43 #else
44 # include "libgettext.h"
45 #endif
46 #include "gettext.h"
47 #include "gettextP.h"
48
49 /* @@ end of prolog @@ */
50
51 /* Contains the default location of the message catalogs.  */
52 extern const char _nl_default_dirname[];
53
54 /* List with bindings of specific domains.  */
55 extern struct binding *_nl_domain_bindings;
56
57
58 /* Names for the libintl functions are a problem.  They must not clash
59    with existing names and they should follow ANSI C.  But this source
60    code is also used in GNU C Library where the names have a __
61    prefix.  So we have to make a difference here.  */
62 #ifdef _LIBC
63 # define BINDTEXTDOMAIN __bindtextdomain
64 #else
65 # define BINDTEXTDOMAIN bindtextdomain__
66 #endif
67
68 /* Specify that the DOMAINNAME message catalog will be found
69    in DIRNAME rather than in the system locale data base.  */
70 char *
71 BINDTEXTDOMAIN (domainname, dirname)
72      const char *domainname;
73      const char *dirname;
74 {
75   struct binding *binding;
76
77   /* Some sanity checks.  */
78   if (domainname == NULL || domainname[0] == '\0')
79     return NULL;
80
81   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
82     {
83       int compare = strcmp (domainname, binding->domainname);
84       if (compare == 0)
85         /* We found it!  */
86         break;
87       if (compare < 0)
88         {
89           /* It is not in the list.  */
90           binding = NULL;
91           break;
92         }
93     }
94
95   if (dirname == NULL)
96     /* The current binding has be to returned.  */
97     return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
98
99   if (binding != NULL)
100     {
101       /* The domain is already bound.  Replace the old binding.  */
102       char *new_dirname;
103
104       if (strcmp (dirname, _nl_default_dirname) == 0)
105         new_dirname = (char *) _nl_default_dirname;
106       else
107         {
108           size_t len = strlen (dirname) + 1;
109           new_dirname = (char *) malloc (len);
110           if (new_dirname == NULL)
111             return NULL;
112
113           memcpy (new_dirname, dirname, len);
114         }
115
116       if (strcmp (binding->dirname, _nl_default_dirname) != 0)
117         free (binding->dirname);
118
119       binding->dirname = new_dirname;
120     }
121   else
122     {
123       /* We have to create a new binding.  */
124       size_t len;
125       struct binding *new_binding =
126         (struct binding *) malloc (sizeof (*new_binding));
127
128       if (new_binding == NULL)
129         return NULL;
130
131       len = strlen (domainname) + 1;
132       new_binding->domainname = (char *) malloc (len);
133       if (new_binding->domainname == NULL)
134           return NULL;
135       memcpy (new_binding->domainname, domainname, len);
136
137       if (strcmp (dirname, _nl_default_dirname) == 0)
138         new_binding->dirname = (char *) _nl_default_dirname;
139       else
140         {
141           len = strlen (dirname) + 1;
142           new_binding->dirname = (char *) malloc (len);
143           if (new_binding->dirname == NULL)
144             return NULL;
145           memcpy (new_binding->dirname, dirname, len);
146         }
147
148       /* Now enqueue it.  */
149       if (_nl_domain_bindings == NULL
150           || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
151         {
152           new_binding->next = _nl_domain_bindings;
153           _nl_domain_bindings = new_binding;
154         }
155       else
156         {
157           binding = _nl_domain_bindings;
158           while (binding->next != NULL
159                  && strcmp (domainname, binding->next->domainname) > 0)
160             binding = binding->next;
161
162           new_binding->next = binding->next;
163           binding->next = new_binding;
164         }
165
166       binding = new_binding;
167     }
168
169   return binding->dirname;
170 }
171
172 #ifdef _LIBC
173 /* Alias for function name in GNU C Library.  */
174 weak_alias (__bindtextdomain, bindtextdomain);
175 #endif