pcsxr-1.9.92
[pcsx_rearmed.git] / win32 / intl / bindtextdom.c
1 /* Implementation of the bindtextdomain(3) function
2    Copyright (C) 1995, 1996, 1997, 1998 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.  */
17
18 #include "intlconfig.h"
19
20 #if defined STDC_HEADERS || defined _LIBC
21 # include <stdlib.h>
22 #else
23 # ifdef HAVE_MALLOC_H
24 #  include <malloc.h>
25 # else
26 void free ();
27 # endif
28 #endif
29
30 #if defined HAVE_STRING_H || defined _LIBC
31 # include <string.h>
32 #else
33 # include <strings.h>
34 # ifndef memcpy
35 #  define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
36 # endif
37 #endif
38
39 #ifdef _LIBC
40 # include <libintl.h>
41 #else
42 # include "libgettext.h"
43 #endif
44 #include "gettext.h"
45 #include "gettextP.h"
46
47 /* @@ end of prolog @@ */
48
49 /* Contains the default location of the message catalogs.  */
50 extern const char _nl_default_dirname[];
51
52 /* List with bindings of specific domains.  */
53 extern struct binding *_nl_domain_bindings;
54
55
56 /* Names for the libintl functions are a problem.  They must not clash
57    with existing names and they should follow ANSI C.  But this source
58    code is also used in GNU C Library where the names have a __
59    prefix.  So we have to make a difference here.  */
60 #ifdef _LIBC
61 # define BINDTEXTDOMAIN __bindtextdomain
62 # ifndef strdup
63 #  define strdup(str) __strdup (str)
64 # endif
65 #else
66 # define BINDTEXTDOMAIN bindtextdomain__
67 #endif
68
69 /* Specify that the DOMAINNAME message catalog will be found
70    in DIRNAME rather than in the system locale data base.  */
71 char *
72 BINDTEXTDOMAIN (domainname, dirname_IN) /* FRANCO */
73      const char *domainname;
74      const char *dirname_IN;
75 {
76   struct binding *binding; 
77   /*FRANCO*/
78   char *dirname_MOD,*pos;
79   const char *dirname;
80   size_t  len;
81   dirname_MOD=NULL;
82   pos=NULL;
83   dirname=dirname_IN;
84   len=strlen(dirname);
85   if((len>0) &&  (pos=strchr(dirname,'\\'))){ /* SUBST DOS LIKE \ into UNIX like / */
86 #if defined _LIBC || defined HAVE_STRDUP
87       dirname_MOD = strdup (dirname);
88       if (dirname_MOD == NULL)
89         return NULL;
90 #else
91       size_t len1 = strlen (dirname) + 1;
92       dirname_MOD = (char *) malloc (len1);
93       if (dirname_MOD == NULL)
94         return NULL;
95       memcpy (dirname_MOD, dirname, len1);
96 #endif
97       dirname=dirname_MOD;
98       pos=dirname_MOD;
99       while( pos=strchr(pos,'\\')){
100           *pos='/';
101       }
102   }/*FRANCO END*/
103
104   /* Some sanity checks.  */
105   if (domainname == NULL || domainname[0] == '\0'){
106     if(dirname_MOD)free(dirname_MOD); /*FRANCO*/
107     return NULL;
108   }
109
110   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
111     {
112       int compare = strcmp (domainname, binding->domainname);
113       if (compare == 0)
114         /* We found it!  */
115         break;
116       if (compare < 0)
117         {
118           /* It is not in the list.  */
119           binding = NULL;
120           break;
121         }
122     }
123
124   if (dirname == NULL)
125     /* The current binding has be to returned.  */
126     return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
127
128   if (binding != NULL)
129     {
130       /* The domain is already bound.  If the new value and the old
131          one are equal we simply do nothing.  Otherwise replace the
132          old binding.  */
133       if (strcmp (dirname, binding->dirname) != 0)
134         {
135           char *new_dirname;
136
137           if (strcmp (dirname, _nl_default_dirname) == 0)
138             new_dirname = (char *) _nl_default_dirname;
139           else
140             {
141 #if defined _LIBC || defined HAVE_STRDUP
142               new_dirname = strdup (dirname);
143               if (new_dirname == NULL){
144                 if(dirname_MOD)free(dirname_MOD); /*FRANCO*/
145                 return NULL;
146               }
147 #else
148               size_t len = strlen (dirname) + 1;
149               new_dirname = (char *) malloc (len);
150               if (new_dirname == NULL){
151                 if(dirname_MOD)free(dirname_MOD); /*FRANCO*/
152                 return NULL;
153               }
154
155               memcpy (new_dirname, dirname, len);
156 #endif
157             }
158
159           if (binding->dirname != _nl_default_dirname)
160             free (binding->dirname);
161
162           binding->dirname = new_dirname;
163         }
164     }
165   else
166     {
167       /* We have to create a new binding.  */
168 #if !defined _LIBC && !defined HAVE_STRDUP
169       size_t len;
170 #endif
171       struct binding *new_binding =
172         (struct binding *) malloc (sizeof (*new_binding));
173
174       if (new_binding == NULL){
175         if(dirname_MOD)free(dirname_MOD); /*FRANCO*/
176         return NULL;
177       }
178
179 #if defined _LIBC || defined HAVE_STRDUP
180       new_binding->domainname = strdup (domainname);
181       if (new_binding->domainname == NULL){
182         if(dirname_MOD)free(dirname_MOD); /*FRANCO*/
183         return NULL;
184         }
185 #else
186       len = strlen (domainname) + 1;
187       new_binding->domainname = (char *) malloc (len);
188       if (new_binding->domainname == NULL){
189         if(dirname_MOD)free(dirname_MOD); /*FRANCO*/
190         return NULL;
191         }
192       memcpy (new_binding->domainname, domainname, len);
193 #endif
194
195       if (strcmp (dirname, _nl_default_dirname) == 0)
196         new_binding->dirname = (char *) _nl_default_dirname;
197       else
198         {
199 #if defined _LIBC || defined HAVE_STRDUP
200           new_binding->dirname = strdup (dirname);
201           if (new_binding->dirname == NULL){
202             if(dirname_MOD)free(dirname_MOD); /*FRANCO*/
203             return NULL;
204             }
205 #else
206           len = strlen (dirname) + 1;
207           new_binding->dirname = (char *) malloc (len);
208           if (new_binding->dirname == NULL){
209             if(dirname_MOD)free(dirname_MOD); /*FRANCO*/
210             return NULL;
211             }
212           memcpy (new_binding->dirname, dirname, len);
213 #endif
214         }
215
216       /* Now enqueue it.  */
217       if (_nl_domain_bindings == NULL
218           || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
219         {
220           new_binding->next = _nl_domain_bindings;
221           _nl_domain_bindings = new_binding;
222         }
223       else
224         {
225           binding = _nl_domain_bindings;
226           while (binding->next != NULL
227                  && strcmp (domainname, binding->next->domainname) > 0)
228             binding = binding->next;
229
230           new_binding->next = binding->next;
231           binding->next = new_binding;
232         }
233
234       binding = new_binding;
235     }
236
237   if(dirname_MOD)free(dirname_MOD); /*FRANCO*/
238
239   return binding->dirname;
240 }
241
242 #ifdef _LIBC
243 /* Alias for function name in GNU C Library.  */
244 weak_alias (__bindtextdomain, bindtextdomain);
245 #endif