Back to index

glibc  2.9
Classes | Defines | Functions | Variables
tst-langinfo.c File Reference
#include <langinfo.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  map

Defines

#define VAL(name)   { #name, name }
#define REASON(str)

Functions

static int map_paramstr (const char *str)
int main (void)

Variables

struct map map []

Class Documentation

struct map

Definition at line 32 of file ksc5601.h.

Class Members
const char * str
uint16_t ucs
int val
char val

Define Documentation

#define REASON (   str)

Definition at line 139 of file tst-langinfo.c.

#define VAL (   name)    { #name, name }

Function Documentation

int main ( void  )

Definition at line 143 of file tst-langinfo.c.

{
  int result = 0;

  while (! feof (stdin))
    {
      char buf[1000];
      char *rp;
      char *locale;
      char *paramstr;
      char *expected;
      char *actual;
      int param;

      if (fgets (buf, sizeof (buf), stdin) == NULL)
       break;

      /* Split the fields.   There are three is them:
        1. locale
        2. langinfo() parameter
        3. expected result; this can be a string with white space etc.
      */
      rp = buf;
      while (*rp == ' ' || *rp == '\t')
       ++rp;

      if  (*rp == '#')
       {
         /* It's a comment line.  Ignore it.  */
         REASON ("comment");
         continue;
       }
      locale = rp;

      while (*rp != '\0' && *rp != ' ' && *rp != '\t' && *rp != '\n')
       ++rp;
      if (*rp == '\0' || *rp == '\n')
       {
         /* Incomplete line.  */
         REASON ("incomplete line");
         continue;
       }
      *rp++ = '\0';

      while (*rp == ' ' || *rp == '\t')
       ++rp;
      paramstr = rp;

      while (*rp != '\0' && *rp != ' ' && *rp != '\t' && *rp != '\n')
       ++rp;
      if (*rp == '\0' || *rp == '\n')
       {
         /* Incomplete line.  */
         REASON ("incomplete line");
         continue;
       }
      *rp++ = '\0';

      while (*rp == ' ' || *rp == '\t')
       ++rp;

      if (*rp == '"')
       {
         char *wp;

         expected = wp = ++rp;
         while (*rp != '"' && *rp != '\n' && *rp != '\0')
           {
             if (*rp == '\\')
              {
                ++rp;
                if (*rp == '\0')
                  break;
                if (*rp >= '0' && *rp <= '9')
                  {
                    int val = *rp - '0';
                    if (rp[1] >= '0' && rp[1] <= '9')
                     {
                       ++rp;
                       val *= 10;
                       val += *rp - '0';
                       if (rp[1] >= '0' && rp[1] <= '9')
                         {
                           ++rp;
                           val *= 10;
                           val += *rp - '0';
                         }
                     }
                    *rp = val;
                  }
              }
             *wp++ = *rp++;
           }

         if (*rp != '"')
           {
             REASON ("missing '\"'");
             continue;
           }

         *wp = '\0';
       }
      else
       {
         expected = rp;
         while (*rp != '\0' && *rp != '\n')
           ++rp;
         *rp = '\0';
       }

      param = map_paramstr (paramstr);
      if (param == -1)
       {
         /* Invalid parameter.  */
         REASON ("invalid parameter");
         continue;
       }

      /* Set the locale and check whether it worked.  */
      printf ("LC_ALL=%s nl_langinfo(%s)", locale, paramstr);
      setlocale (LC_ALL, locale);
      if (strcmp (locale, setlocale (LC_ALL, NULL)) != 0)
       {
         puts (": failed to set locale");
         result = 1;
         continue;
       }

      actual = nl_langinfo (param);
      printf (" = \"%s\", ", actual);

      if (strcmp (actual, expected) == 0)
       puts ("OK");
      else
       {
         printf ("FAILED (expected: %s)\n", expected);
         result = 1;
       }
    }

  return result;
}

Here is the call graph for this function:

static int map_paramstr ( const char *  str) [static]

Definition at line 113 of file tst-langinfo.c.

{
  int low = 0;
  int high = sizeof (map) / sizeof (map[0]);

  while (low < high)
    {
      int med = (low + high) / 2;
      int cmpres;

      cmpres = strcmp (str, map[med].str);
      if (cmpres == 0)
       return map[med].val;
      else if (cmpres > 0)
       low = med + 1;
      else
       high = med;
    }

  return -1;
}

Here is the caller graph for this function:


Variable Documentation

struct map map[]