few tweaks about changed defines.
[apps/madmutt.git] / intl / explodename.c
1 /* Copyright (C) 1995-1998, 2000-2001, 2003, 2005 Free Software Foundation, Inc.
2    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
3
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU Library General Public License as published
6    by 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 GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17    USA.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26
27 #include "loadinfo.h"
28
29 /* On some strange systems still no definition of NULL is found.  Sigh!  */
30 #ifndef NULL
31 # if defined __STDC__ && __STDC__
32 #  define NULL ((void *) 0)
33 # else
34 #  define NULL 0
35 # endif
36 #endif
37
38 /* @@ end of prolog @@ */
39
40 /* Split a locale name NAME into a leading language part and all the
41    rest.  Return a pointer to the first character after the language,
42    i.e. to the first byte of the rest.  */
43 static char *_nl_find_language (const char *name);
44
45 static char *
46 _nl_find_language (const char *name)
47 {
48   while (name[0] != '\0' && name[0] != '_' && name[0] != '@' && name[0] != '.')
49     ++name;
50
51   return (char *) name;
52 }
53
54
55 int
56 _nl_explode_name (char *name,
57                   const char **language, const char **modifier,
58                   const char **territory, const char **codeset,
59                   const char **normalized_codeset)
60 {
61   char *cp;
62   int mask;
63
64   *modifier = NULL;
65   *territory = NULL;
66   *codeset = NULL;
67   *normalized_codeset = NULL;
68
69   /* Now we determine the single parts of the locale name.  First
70      look for the language.  Termination symbols are `_', '.', and `@'.  */
71   mask = 0;
72   *language = cp = name;
73   cp = _nl_find_language (*language);
74
75   if (*language == cp)
76     /* This does not make sense: language has to be specified.  Use
77        this entry as it is without exploding.  Perhaps it is an alias.  */
78     cp = strchr (*language, '\0');
79   else
80     {
81       if (cp[0] == '_')
82         {
83           /* Next is the territory.  */
84           cp[0] = '\0';
85           *territory = ++cp;
86
87           while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@')
88             ++cp;
89
90           mask |= XPG_TERRITORY;
91         }
92
93       if (cp[0] == '.')
94         {
95           /* Next is the codeset.  */
96           cp[0] = '\0';
97           *codeset = ++cp;
98
99           while (cp[0] != '\0' && cp[0] != '@')
100             ++cp;
101
102           mask |= XPG_CODESET;
103
104           if (*codeset != cp && (*codeset)[0] != '\0')
105             {
106               *normalized_codeset = _nl_normalize_codeset (*codeset,
107                                                            cp - *codeset);
108               if (strcmp (*codeset, *normalized_codeset) == 0)
109                 free ((char *) *normalized_codeset);
110               else
111                 mask |= XPG_NORM_CODESET;
112             }
113         }
114     }
115
116   if (cp[0] == '@')
117     {
118       /* Next is the modifier.  */
119       cp[0] = '\0';
120       *modifier = ++cp;
121
122       if (cp[0] != '\0')
123         mask |= XPG_MODIFIER;
124     }
125
126   if (*territory != NULL && (*territory)[0] == '\0')
127     mask &= ~XPG_TERRITORY;
128
129   if (*codeset != NULL && (*codeset)[0] == '\0')
130     mask &= ~XPG_CODESET;
131
132   return mask;
133 }