Back to index

glibc  2.9
tst-langinfo.c
Go to the documentation of this file.
00001 /* Test program for nl_langinfo() function.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <langinfo.h>
00022 #include <locale.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 
00026 
00027 struct map
00028 {
00029   const char *str;
00030   int val;
00031 } map[] =
00032 {
00033 #define VAL(name) { #name, name }
00034   VAL (ABDAY_1),
00035   VAL (ABDAY_2),
00036   VAL (ABDAY_3),
00037   VAL (ABDAY_4),
00038   VAL (ABDAY_5),
00039   VAL (ABDAY_6),
00040   VAL (ABDAY_7),
00041   VAL (ABMON_1),
00042   VAL (ABMON_10),
00043   VAL (ABMON_11),
00044   VAL (ABMON_12),
00045   VAL (ABMON_2),
00046   VAL (ABMON_3),
00047   VAL (ABMON_4),
00048   VAL (ABMON_5),
00049   VAL (ABMON_6),
00050   VAL (ABMON_7),
00051   VAL (ABMON_8),
00052   VAL (ABMON_9),
00053   VAL (ALT_DIGITS),
00054   VAL (AM_STR),
00055   VAL (CRNCYSTR),
00056   VAL (CURRENCY_SYMBOL),
00057   VAL (DAY_1),
00058   VAL (DAY_2),
00059   VAL (DAY_3),
00060   VAL (DAY_4),
00061   VAL (DAY_5),
00062   VAL (DAY_6),
00063   VAL (DAY_7),
00064   VAL (DECIMAL_POINT),
00065   VAL (D_FMT),
00066   VAL (D_T_FMT),
00067   VAL (ERA),
00068   VAL (ERA_D_FMT),
00069   VAL (ERA_D_T_FMT),
00070   VAL (ERA_T_FMT),
00071   VAL (ERA_YEAR),
00072   VAL (FRAC_DIGITS),
00073   VAL (GROUPING),
00074   VAL (INT_CURR_SYMBOL),
00075   VAL (INT_FRAC_DIGITS),
00076   VAL (MON_1),
00077   VAL (MON_10),
00078   VAL (MON_11),
00079   VAL (MON_12),
00080   VAL (MON_2),
00081   VAL (MON_3),
00082   VAL (MON_4),
00083   VAL (MON_5),
00084   VAL (MON_6),
00085   VAL (MON_7),
00086   VAL (MON_8),
00087   VAL (MON_9),
00088   VAL (MON_DECIMAL_POINT),
00089   VAL (MON_GROUPING),
00090   VAL (MON_THOUSANDS_SEP),
00091   VAL (NEGATIVE_SIGN),
00092   VAL (NOEXPR),
00093   VAL (NOSTR),
00094   VAL (N_CS_PRECEDES),
00095   VAL (N_SEP_BY_SPACE),
00096   VAL (N_SIGN_POSN),
00097   VAL (PM_STR),
00098   VAL (POSITIVE_SIGN),
00099   VAL (P_CS_PRECEDES),
00100   VAL (P_SEP_BY_SPACE),
00101   VAL (P_SIGN_POSN),
00102   VAL (RADIXCHAR),
00103   VAL (THOUSANDS_SEP),
00104   VAL (THOUSEP),
00105   VAL (T_FMT),
00106   VAL (T_FMT_AMPM),
00107   VAL (YESEXPR),
00108   VAL (YESSTR)
00109 };
00110 
00111 
00112 static int
00113 map_paramstr (const char *str)
00114 {
00115   int low = 0;
00116   int high = sizeof (map) / sizeof (map[0]);
00117 
00118   while (low < high)
00119     {
00120       int med = (low + high) / 2;
00121       int cmpres;
00122 
00123       cmpres = strcmp (str, map[med].str);
00124       if (cmpres == 0)
00125        return map[med].val;
00126       else if (cmpres > 0)
00127        low = med + 1;
00128       else
00129        high = med;
00130     }
00131 
00132   return -1;
00133 }
00134 
00135 
00136 #ifdef DEBUG
00137 # define REASON(str) printf ("\"%s\" ignored: %s\n", buf, str)
00138 #else
00139 # define REASON(str)
00140 #endif
00141 
00142 int
00143 main (void)
00144 {
00145   int result = 0;
00146 
00147   while (! feof (stdin))
00148     {
00149       char buf[1000];
00150       char *rp;
00151       char *locale;
00152       char *paramstr;
00153       char *expected;
00154       char *actual;
00155       int param;
00156 
00157       if (fgets (buf, sizeof (buf), stdin) == NULL)
00158        break;
00159 
00160       /* Split the fields.   There are three is them:
00161         1. locale
00162         2. langinfo() parameter
00163         3. expected result; this can be a string with white space etc.
00164       */
00165       rp = buf;
00166       while (*rp == ' ' || *rp == '\t')
00167        ++rp;
00168 
00169       if  (*rp == '#')
00170        {
00171          /* It's a comment line.  Ignore it.  */
00172          REASON ("comment");
00173          continue;
00174        }
00175       locale = rp;
00176 
00177       while (*rp != '\0' && *rp != ' ' && *rp != '\t' && *rp != '\n')
00178        ++rp;
00179       if (*rp == '\0' || *rp == '\n')
00180        {
00181          /* Incomplete line.  */
00182          REASON ("incomplete line");
00183          continue;
00184        }
00185       *rp++ = '\0';
00186 
00187       while (*rp == ' ' || *rp == '\t')
00188        ++rp;
00189       paramstr = rp;
00190 
00191       while (*rp != '\0' && *rp != ' ' && *rp != '\t' && *rp != '\n')
00192        ++rp;
00193       if (*rp == '\0' || *rp == '\n')
00194        {
00195          /* Incomplete line.  */
00196          REASON ("incomplete line");
00197          continue;
00198        }
00199       *rp++ = '\0';
00200 
00201       while (*rp == ' ' || *rp == '\t')
00202        ++rp;
00203 
00204       if (*rp == '"')
00205        {
00206          char *wp;
00207 
00208          expected = wp = ++rp;
00209          while (*rp != '"' && *rp != '\n' && *rp != '\0')
00210            {
00211              if (*rp == '\\')
00212               {
00213                 ++rp;
00214                 if (*rp == '\0')
00215                   break;
00216                 if (*rp >= '0' && *rp <= '9')
00217                   {
00218                     int val = *rp - '0';
00219                     if (rp[1] >= '0' && rp[1] <= '9')
00220                      {
00221                        ++rp;
00222                        val *= 10;
00223                        val += *rp - '0';
00224                        if (rp[1] >= '0' && rp[1] <= '9')
00225                          {
00226                            ++rp;
00227                            val *= 10;
00228                            val += *rp - '0';
00229                          }
00230                      }
00231                     *rp = val;
00232                   }
00233               }
00234              *wp++ = *rp++;
00235            }
00236 
00237          if (*rp != '"')
00238            {
00239              REASON ("missing '\"'");
00240              continue;
00241            }
00242 
00243          *wp = '\0';
00244        }
00245       else
00246        {
00247          expected = rp;
00248          while (*rp != '\0' && *rp != '\n')
00249            ++rp;
00250          *rp = '\0';
00251        }
00252 
00253       param = map_paramstr (paramstr);
00254       if (param == -1)
00255        {
00256          /* Invalid parameter.  */
00257          REASON ("invalid parameter");
00258          continue;
00259        }
00260 
00261       /* Set the locale and check whether it worked.  */
00262       printf ("LC_ALL=%s nl_langinfo(%s)", locale, paramstr);
00263       setlocale (LC_ALL, locale);
00264       if (strcmp (locale, setlocale (LC_ALL, NULL)) != 0)
00265        {
00266          puts (": failed to set locale");
00267          result = 1;
00268          continue;
00269        }
00270 
00271       actual = nl_langinfo (param);
00272       printf (" = \"%s\", ", actual);
00273 
00274       if (strcmp (actual, expected) == 0)
00275        puts ("OK");
00276       else
00277        {
00278          printf ("FAILED (expected: %s)\n", expected);
00279          result = 1;
00280        }
00281     }
00282 
00283   return result;
00284 }