Back to index

glibc  2.9
show-ucs-data.c
Go to the documentation of this file.
00001 #include <ctype.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <sys/types.h>
00005 
00006 int
00007 main (int argc, char *argv[])
00008 {
00009   int n;
00010   char *line = NULL;
00011   size_t len = 0;
00012 
00013   if (argc == 1)
00014     {
00015       static char *new_argv[] = { NULL, (char *) "/dev/stdin", NULL };
00016       argv = new_argv;
00017       argc = 2;
00018     }
00019 
00020   for (n = 1; n < argc; ++n)
00021     {
00022       FILE *fp = fopen (argv[n], "r");
00023       if (fp == NULL)
00024        continue;
00025 
00026       while (! feof (fp))
00027        {
00028          ssize_t cnt = getline (&line, &len, fp);
00029          char *runp;
00030          if (cnt <= 0)
00031            break;
00032 
00033          runp = line;
00034          do
00035            {
00036              if (runp[0] == '<' && runp[1] == 'U' && isxdigit (runp[2])
00037                 && isxdigit (runp[3]) && isxdigit (runp[4])
00038                 && isxdigit (runp[5]) && runp[6] == '>')
00039               {
00040                 unsigned int val = strtoul (runp + 2, NULL, 16);
00041 
00042                 //putchar ('<');
00043                 if (val < 128)
00044                   putchar (val);
00045                 else if (val < 0x800)
00046                   {
00047                     putchar (0xc0 | (val >> 6));
00048                     putchar (0x80 | (val & 0x3f));
00049                   }
00050                 else
00051                   {
00052                     putchar (0xe0 | (val >> 12));
00053                     putchar (0x80 | ((val >> 6) & 0x3f));
00054                     putchar (0x80 | (val & 0x3f));
00055                   }
00056                 //putchar ('>');
00057                 runp += 7;
00058               }
00059              else
00060               putchar (*runp++);
00061            }
00062          while (runp < &line[cnt]);
00063        }
00064 
00065       fclose (fp);
00066     }
00067 
00068   return 0;
00069 }