pcsxr-1.9.92
[pcsx_rearmed.git] / win32 / intl / finddomain.c
1 /* Handle list of needed message catalogs
2    Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3    Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.  */
18
19 #include "intlconfig.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25
26 #if defined STDC_HEADERS || defined _LIBC
27 # include <stdlib.h>
28 #else
29 # ifdef HAVE_MALLOC_H
30 #  include <malloc.h>
31 # else
32 void free ();
33 # endif
34 #endif
35
36 #if defined HAVE_STRING_H || defined _LIBC
37 # include <string.h>
38 #else
39 # include <strings.h>
40 # ifndef memcpy
41 #  define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
42 # endif
43 #endif
44 #if !HAVE_STRCHR && !defined _LIBC
45 # ifndef strchr
46 #  define strchr index
47 # endif
48 #endif
49
50 #if defined HAVE_UNISTD_H || defined _LIBC
51 # include <unistd.h>
52 #endif
53
54 #include "gettext.h"
55 #include "gettextP.h"
56 #ifdef _LIBC
57 # include <libintl.h>
58 #else
59 # include "libgettext.h"
60 #endif
61
62 /* @@ end of prolog @@ */
63 /* List of already loaded domains.  */
64 static struct loaded_l10nfile *_nl_loaded_domains;
65
66
67 /* Return a data structure describing the message catalog described by
68    the DOMAINNAME and CATEGORY parameters with respect to the currently
69    established bindings.  */
70 struct loaded_l10nfile *
71 internal_function
72 _nl_find_domain (dirname, locale, domainname)
73      const char *dirname;
74      char *locale;
75      const char *domainname;
76 {
77   struct loaded_l10nfile *retval;
78   const char *language;
79   const char *modifier;
80   const char *territory;
81   const char *codeset;
82   const char *normalized_codeset;
83   const char *special;
84   const char *sponsor;
85   const char *revision;
86   const char *alias_value;
87   int mask;
88
89   /* LOCALE can consist of up to four recognized parts for the XPG syntax:
90
91                 language[_territory[.codeset]][@modifier]
92
93      and six parts for the CEN syntax:
94
95         language[_territory][+audience][+special][,[sponsor][_revision]]
96
97      Beside the first part all of them are allowed to be missing.  If
98      the full specified locale is not found, the less specific one are
99      looked for.  The various parts will be stripped off according to
100      the following order:
101                 (1) revision
102                 (2) sponsor
103                 (3) special
104                 (4) codeset
105                 (5) normalized codeset
106                 (6) territory
107                 (7) audience/modifier
108    */
109
110   /* If we have already tested for this locale entry there has to
111      be one data set in the list of loaded domains.  */
112   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
113                                strlen (dirname) + 1, 0, locale, NULL, NULL,
114                                NULL, NULL, NULL, NULL, NULL, domainname, 0);
115   if (retval != NULL)
116     {
117       /* We know something about this locale.  */
118       int cnt;
119
120       if (retval->decided == 0)
121         _nl_load_domain (retval);
122
123       if (retval->data != NULL)
124         return retval;
125
126       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
127         {
128           if (retval->successor[cnt]->decided == 0)
129             _nl_load_domain (retval->successor[cnt]);
130
131           if (retval->successor[cnt]->data != NULL)
132             break;
133         }
134       return cnt >= 0 ? retval : NULL;
135       /* NOTREACHED */
136     }
137
138   /* See whether the locale value is an alias.  If yes its value
139      *overwrites* the alias name.  No test for the original value is
140      done.  */
141   alias_value = _nl_expand_alias (locale);
142   if (alias_value != NULL)
143     {
144 #if defined _LIBC || defined HAVE_STRDUP
145       locale = strdup (alias_value);
146       if (locale == NULL)
147         return NULL;
148 #else
149       size_t len = strlen (alias_value) + 1;
150       locale = (char *) malloc (len);
151       if (locale == NULL)
152         return NULL;
153
154       memcpy (locale, alias_value, len);
155 #endif
156     }
157
158   /* Now we determine the single parts of the locale name.  First
159      look for the language.  Termination symbols are `_' and `@' if
160      we use XPG4 style, and `_', `+', and `,' if we use CEN syntax.  */
161   mask = _nl_explode_name (locale, &language, &modifier, &territory,
162                            &codeset, &normalized_codeset, &special,
163                            &sponsor, &revision);
164
165   /* Create all possible locale entries which might be interested in
166      generalization.  */
167   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
168                                strlen (dirname) + 1, mask, language, territory,
169                                codeset, normalized_codeset, modifier, special,
170                                sponsor, revision, domainname, 1);
171   if (retval == NULL)
172     /* This means we are out of core.  */
173     return NULL;
174
175   if (retval->decided == 0)
176     _nl_load_domain (retval);
177   if (retval->data == NULL)
178     {
179       int cnt;
180       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
181         {
182           if (retval->successor[cnt]->decided == 0)
183             _nl_load_domain (retval->successor[cnt]);
184           if (retval->successor[cnt]->data != NULL)
185             break;
186         }
187     }
188
189   /* The room for an alias was dynamically allocated.  Free it now.  */
190   if (alias_value != NULL)
191     free (locale);
192
193   return retval;
194 }
195
196
197 #ifdef _LIBC
198 static void __attribute__ ((unused))
199 free_mem (void)
200 {
201   struct loaded_l10nfile *runp = _nl_loaded_domains;
202
203   while (runp != NULL)
204     {
205       struct loaded_l10nfile *here = runp;
206       if (runp->data != NULL)
207         _nl_unload_domain ((struct loaded_domain *) runp->data);
208       runp = runp->next;
209       free (here);
210     }
211 }
212
213 text_set_element (__libc_subfreeres, free_mem);
214 #endif