Home | History | Annotate | Line # | Download | only in tests
      1  1.1  christos /* Fake setlocale - platform independent, for testing purposes.
      2  1.1  christos    Copyright (C) 2001-2002 Free Software Foundation, Inc.
      3  1.1  christos 
      4  1.1  christos    This program is free software; you can redistribute it and/or modify
      5  1.1  christos    it under the terms of the GNU General Public License as published by
      6  1.1  christos    the Free Software Foundation; either version 2, or (at your option)
      7  1.1  christos    any later version.
      8  1.1  christos 
      9  1.1  christos    This program is distributed in the hope that it will be useful,
     10  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  1.1  christos    GNU General Public License for more details.
     13  1.1  christos 
     14  1.1  christos    You should have received a copy of the GNU General Public License
     15  1.1  christos    along with this program; if not, write to the Free Software Foundation,
     16  1.1  christos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
     17  1.1  christos 
     18  1.1  christos #ifdef HAVE_CONFIG_H
     19  1.1  christos # include <config.h>
     20  1.1  christos #endif
     21  1.1  christos 
     22  1.1  christos #include <stdlib.h>
     23  1.1  christos #include <locale.h>
     24  1.1  christos #include <string.h>
     25  1.1  christos 
     26  1.1  christos /* Return string representation of locale CATEGORY.  */
     27  1.1  christos static const char *
     28  1.1  christos category_to_name (int category)
     29  1.1  christos {
     30  1.1  christos   const char *retval;
     31  1.1  christos 
     32  1.1  christos   switch (category)
     33  1.1  christos   {
     34  1.1  christos #ifdef LC_COLLATE
     35  1.1  christos   case LC_COLLATE:
     36  1.1  christos     retval = "LC_COLLATE";
     37  1.1  christos     break;
     38  1.1  christos #endif
     39  1.1  christos #ifdef LC_CTYPE
     40  1.1  christos   case LC_CTYPE:
     41  1.1  christos     retval = "LC_CTYPE";
     42  1.1  christos     break;
     43  1.1  christos #endif
     44  1.1  christos #ifdef LC_MONETARY
     45  1.1  christos   case LC_MONETARY:
     46  1.1  christos     retval = "LC_MONETARY";
     47  1.1  christos     break;
     48  1.1  christos #endif
     49  1.1  christos #ifdef LC_NUMERIC
     50  1.1  christos   case LC_NUMERIC:
     51  1.1  christos     retval = "LC_NUMERIC";
     52  1.1  christos     break;
     53  1.1  christos #endif
     54  1.1  christos #ifdef LC_TIME
     55  1.1  christos   case LC_TIME:
     56  1.1  christos     retval = "LC_TIME";
     57  1.1  christos     break;
     58  1.1  christos #endif
     59  1.1  christos #ifdef LC_MESSAGES
     60  1.1  christos   case LC_MESSAGES:
     61  1.1  christos     retval = "LC_MESSAGES";
     62  1.1  christos     break;
     63  1.1  christos #endif
     64  1.1  christos #ifdef LC_RESPONSE
     65  1.1  christos   case LC_RESPONSE:
     66  1.1  christos     retval = "LC_RESPONSE";
     67  1.1  christos     break;
     68  1.1  christos #endif
     69  1.1  christos #ifdef LC_ALL
     70  1.1  christos   case LC_ALL:
     71  1.1  christos     /* This might not make sense but is perhaps better than any other
     72  1.1  christos        value.  */
     73  1.1  christos     retval = "LC_ALL";
     74  1.1  christos     break;
     75  1.1  christos #endif
     76  1.1  christos   default:
     77  1.1  christos     /* If you have a better idea for a default value let me know.  */
     78  1.1  christos     retval = "LC_XXX";
     79  1.1  christos   }
     80  1.1  christos 
     81  1.1  christos   return retval;
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos /* An implementation of setlocale that always succeeds, but doesn't
     85  1.1  christos    actually change the behaviour of locale dependent functions.
     86  1.1  christos    Assumes setenv()/putenv() is not called.  */
     87  1.1  christos char *
     88  1.1  christos setlocale (int category, SETLOCALE_CONST char *locale)
     89  1.1  christos {
     90  1.1  christos   static char C_string[] = "C";
     91  1.1  christos   static char *current_locale = C_string;
     92  1.1  christos   struct list
     93  1.1  christos   {
     94  1.1  christos     int category;
     95  1.1  christos     char *current_locale;
     96  1.1  christos     struct list *next;
     97  1.1  christos   };
     98  1.1  christos   static struct list *facets = NULL;
     99  1.1  christos   struct list *facetp;
    100  1.1  christos   char *retval;
    101  1.1  christos 
    102  1.1  christos   if (locale != NULL)
    103  1.1  christos     {
    104  1.1  christos       char *copy;
    105  1.1  christos 
    106  1.1  christos       copy = (char *) malloc (strlen (locale) + 1);
    107  1.1  christos       strcpy (copy, locale);
    108  1.1  christos 
    109  1.1  christos       if (category == LC_ALL)
    110  1.1  christos 	{
    111  1.1  christos 	  while ((facetp = facets) != NULL)
    112  1.1  christos 	    {
    113  1.1  christos 	      facets = facetp->next;
    114  1.1  christos 	      free (facetp->current_locale);
    115  1.1  christos 	      free (facetp);
    116  1.1  christos 	    }
    117  1.1  christos 	  if (current_locale != C_string)
    118  1.1  christos 	    free (current_locale);
    119  1.1  christos 	  current_locale = copy;
    120  1.1  christos 	}
    121  1.1  christos       else
    122  1.1  christos 	{
    123  1.1  christos 	  for (facetp = facets; facetp != NULL; facetp = facetp->next)
    124  1.1  christos 	    if (category == facetp->category)
    125  1.1  christos 	      {
    126  1.1  christos 		free (facetp->current_locale);
    127  1.1  christos 		facetp->current_locale = copy;
    128  1.1  christos 		break;
    129  1.1  christos 	      }
    130  1.1  christos 	  if (facetp == NULL)
    131  1.1  christos 	    {
    132  1.1  christos 	      facetp = (struct list *) malloc (sizeof (struct list));
    133  1.1  christos 	      facetp->category = category;
    134  1.1  christos 	      facetp->current_locale = copy;
    135  1.1  christos 	      facetp->next = facets;
    136  1.1  christos 	      facets = facetp;
    137  1.1  christos 	    }
    138  1.1  christos 	}
    139  1.1  christos     }
    140  1.1  christos 
    141  1.1  christos   retval = current_locale;
    142  1.1  christos   for (facetp = facets; facetp != NULL; facetp = facetp->next)
    143  1.1  christos     if (category == facetp->category)
    144  1.1  christos       {
    145  1.1  christos 	retval = facetp->current_locale;
    146  1.1  christos 	break;
    147  1.1  christos       }
    148  1.1  christos 
    149  1.1  christos   if (retval[0] == '\0')
    150  1.1  christos     {
    151  1.1  christos       retval = getenv ("LC_ALL");
    152  1.1  christos       if (retval == NULL || retval[0] == '\0')
    153  1.1  christos 	{
    154  1.1  christos 	  retval = getenv (category_to_name (category));
    155  1.1  christos 	  if (retval == NULL || retval[0] == '\0')
    156  1.1  christos 	    {
    157  1.1  christos 	      retval = getenv ("LANG");
    158  1.1  christos 	      if (retval == NULL || retval[0] == '\0')
    159  1.1  christos 		retval = "C";
    160  1.1  christos 	    }
    161  1.1  christos 	}
    162  1.1  christos     }
    163  1.1  christos   return retval;
    164  1.1  christos }
    165