show-ucs-data.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. int
  6. main (int argc, char *argv[])
  7. {
  8. int n;
  9. char *line = NULL;
  10. size_t len = 0;
  11. for (n = 1; n < argc; ++n)
  12. {
  13. FILE *fp = fopen (argv[n], "r");
  14. if (fp == NULL)
  15. continue;
  16. while (! feof (fp))
  17. {
  18. ssize_t cnt = getline (&line, &len, fp);
  19. char *runp;
  20. if (cnt <= 0)
  21. break;
  22. runp = line;
  23. do
  24. {
  25. if (runp[0] == '<' && runp[1] == 'U' && isxdigit (runp[2])
  26. && isxdigit (runp[3]) && isxdigit (runp[4])
  27. && isxdigit (runp[5]) && runp[6] == '>')
  28. {
  29. unsigned int val = strtoul (runp + 2, NULL, 16);
  30. //putchar ('<');
  31. if (val < 128)
  32. putchar (val);
  33. else if (val < 0x800)
  34. {
  35. putchar (0xc0 | (val >> 6));
  36. putchar (0x80 | (val & 0x3f));
  37. }
  38. else
  39. {
  40. putchar (0xe0 | (val >> 12));
  41. putchar (0x80 | ((val >> 6) & 0x3f));
  42. putchar (0x80 | (val & 0x3f));
  43. }
  44. //putchar ('>');
  45. runp += 7;
  46. }
  47. else
  48. putchar (*runp++);
  49. }
  50. while (runp < &line[cnt]);
  51. }
  52. fclose (fp);
  53. }
  54. return 0;
  55. }