Back to index

glibc  2.9
explodename.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995-2002, 2003, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023 
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <sys/types.h>
00027 
00028 #include "loadinfo.h"
00029 
00030 /* On some strange systems still no definition of NULL is found.  Sigh!  */
00031 #ifndef NULL
00032 # if defined __STDC__ && __STDC__
00033 #  define NULL ((void *) 0)
00034 # else
00035 #  define NULL 0
00036 # endif
00037 #endif
00038 
00039 /* @@ end of prolog @@ */
00040 
00041 static char *_nl_find_language PARAMS ((const char *name));
00042 
00043 static char *
00044 _nl_find_language (name)
00045      const char *name;
00046 {
00047   while (name[0] != '\0' && name[0] != '_' && name[0] != '@' && name[0] != '.')
00048     ++name;
00049 
00050   return (char *) name;
00051 }
00052 
00053 
00054 int
00055 _nl_explode_name (name, language, modifier, territory, codeset,
00056                 normalized_codeset)
00057      char *name;
00058      const char **language;
00059      const char **modifier;
00060      const char **territory;
00061      const char **codeset;
00062      const char **normalized_codeset;
00063 {
00064   char *cp;
00065   int mask;
00066 
00067   *modifier = NULL;
00068   *territory = NULL;
00069   *codeset = NULL;
00070   *normalized_codeset = NULL;
00071 
00072   /* Now we determine the single parts of the locale name.  First
00073      look for the language.  Termination symbols are `_', '.', and `@'.  */
00074   mask = 0;
00075   *language = cp = name;
00076   cp = _nl_find_language (*language);
00077 
00078   if (*language == cp)
00079     /* This does not make sense: language has to be specified.  Use
00080        this entry as it is without exploding.  Perhaps it is an alias.  */
00081     cp = __rawmemchr (*language, '\0');
00082   else if (cp[0] != '@')
00083     {
00084       if (cp[0] == '_')
00085        {
00086          /* Next is the territory.  */
00087          cp[0] = '\0';
00088          *territory = ++cp;
00089 
00090          while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@')
00091            ++cp;
00092 
00093          mask |= XPG_TERRITORY;
00094        }
00095 
00096       if (cp[0] == '.')
00097        {
00098          /* Next is the codeset.  */
00099          cp[0] = '\0';
00100          *codeset = ++cp;
00101 
00102          while (cp[0] != '\0' && cp[0] != '@')
00103            ++cp;
00104 
00105          mask |= XPG_CODESET;
00106 
00107          if (*codeset != cp && (*codeset)[0] != '\0')
00108            {
00109              *normalized_codeset = _nl_normalize_codeset (*codeset,
00110                                                     cp - *codeset);
00111              if (*normalized_codeset == NULL)
00112               return -1;
00113              else if (strcmp (*codeset, *normalized_codeset) == 0)
00114               free ((char *) *normalized_codeset);
00115              else
00116               mask |= XPG_NORM_CODESET;
00117            }
00118        }
00119     }
00120 
00121   if (cp[0] == '@')
00122     {
00123       /* Next is the modifier.  */
00124       cp[0] = '\0';
00125       *modifier = ++cp;
00126 
00127       if (cp[0] != '\0')
00128        mask |= XPG_MODIFIER;
00129     }
00130 
00131   if (*territory != NULL && (*territory)[0] == '\0')
00132     mask &= ~XPG_TERRITORY;
00133 
00134   if (*codeset != NULL && (*codeset)[0] == '\0')
00135     mask &= ~XPG_CODESET;
00136 
00137   return mask;
00138 }