eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printucddata_cpp.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2012-2017 Reece H. Dunn
  3. *
  4. * This file is part of ucd-tools.
  5. *
  6. * ucd-tools is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * ucd-tools is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "ucd/ucd.h"
  20. #include <string.h>
  21. #include <stdio.h>
  22. void fput_utf8c(FILE *out, ucd::codepoint_t c)
  23. {
  24. if (c < 0x80)
  25. fputc((uint8_t)c, out);
  26. else if (c < 0x800)
  27. {
  28. fputc(0xC0 | (c >> 6), out);
  29. fputc(0x80 + (c & 0x3F), out);
  30. }
  31. else if (c < 0x10000)
  32. {
  33. fputc(0xE0 | (c >> 12), out);
  34. fputc(0x80 + ((c >> 6) & 0x3F), out);
  35. fputc(0x80 + (c & 0x3F), out);
  36. }
  37. else if (c < 0x200000)
  38. {
  39. fputc(0xF0 | (c >> 18), out);
  40. fputc(0x80 + ((c >> 12) & 0x3F), out);
  41. fputc(0x80 + ((c >> 6) & 0x3F), out);
  42. fputc(0x80 + (c & 0x3F), out);
  43. }
  44. }
  45. bool fget_utf8c(FILE *in, ucd::codepoint_t &c)
  46. {
  47. int ch = EOF;
  48. if ((ch = fgetc(in)) == EOF) return false;
  49. if (uint8_t(ch) < 0x80)
  50. c = uint8_t(ch);
  51. else switch (uint8_t(ch) & 0xF0)
  52. {
  53. default:
  54. c = uint8_t(ch) & 0x1F;
  55. if ((ch = fgetc(in)) == EOF) return false;
  56. c = (c << 6) + (uint8_t(ch) & 0x3F);
  57. break;
  58. case 0xE0:
  59. c = uint8_t(ch) & 0x0F;
  60. if ((ch = fgetc(in)) == EOF) return false;
  61. c = (c << 6) + (uint8_t(ch) & 0x3F);
  62. if ((ch = fgetc(in)) == EOF) return false;
  63. c = (c << 6) + (uint8_t(ch) & 0x3F);
  64. break;
  65. case 0xF0:
  66. c = uint8_t(ch) & 0x07;
  67. if ((ch = fgetc(in)) == EOF) return false;
  68. c = (c << 6) + (uint8_t(ch) & 0x3F);
  69. if ((ch = fgetc(in)) == EOF) return false;
  70. c = (c << 6) + (uint8_t(ch) & 0x3F);
  71. if ((ch = fgetc(in)) == EOF) return false;
  72. c = (c << 6) + (uint8_t(ch) & 0x3F);
  73. break;
  74. }
  75. return true;
  76. }
  77. void uprintf_codepoint(FILE *out, ucd::codepoint_t c, char mode)
  78. {
  79. switch (mode)
  80. {
  81. case 'c': // character
  82. switch (c)
  83. {
  84. case '\t': fputs("\\t", out); break;
  85. case '\r': fputs("\\r", out); break;
  86. case '\n': fputs("\\n", out); break;
  87. default: fput_utf8c(out, c); break;
  88. }
  89. break;
  90. case 'h': // hexadecimal (lower)
  91. fprintf(out, "%06x", c);
  92. break;
  93. case 'H': // hexadecimal (upper)
  94. fprintf(out, "%06X", c);
  95. break;
  96. }
  97. }
  98. void uprintf_is(FILE *out, ucd::codepoint_t c, char mode)
  99. {
  100. switch (mode)
  101. {
  102. case 'A': // alpha-numeric
  103. fputc(ucd::isalnum(c) ? '1' : '0', out);
  104. break;
  105. case 'a': // alpha
  106. fputc(ucd::isalpha(c) ? '1' : '0', out);
  107. break;
  108. case 'b': // blank
  109. fputc(ucd::isblank(c) ? '1' : '0', out);
  110. break;
  111. case 'c': // control
  112. fputc(ucd::iscntrl(c) ? '1' : '0', out);
  113. break;
  114. case 'd': // numeric
  115. fputc(ucd::isdigit(c) ? '1' : '0', out);
  116. break;
  117. case 'g': // glyph
  118. fputc(ucd::isgraph(c) ? '1' : '0', out);
  119. break;
  120. case 'l': // lower case
  121. fputc(ucd::islower(c) ? '1' : '0', out);
  122. break;
  123. case 'P': // printable
  124. fputc(ucd::isprint(c) ? '1' : '0', out);
  125. break;
  126. case 'p': // punctuation
  127. fputc(ucd::ispunct(c) ? '1' : '0', out);
  128. break;
  129. case 's': // whitespace
  130. fputc(ucd::isspace(c) ? '1' : '0', out);
  131. break;
  132. case 'u': // upper case
  133. fputc(ucd::isupper(c) ? '1' : '0', out);
  134. break;
  135. case 'x': // xdigit
  136. fputc(ucd::isxdigit(c) ? '1' : '0', out);
  137. break;
  138. }
  139. }
  140. void uprintf(FILE *out, ucd::codepoint_t c, const char *format)
  141. {
  142. while (*format) switch (*format)
  143. {
  144. case '%':
  145. switch (*++format)
  146. {
  147. case 'c': // category
  148. fputs(ucd::get_category_string(ucd::lookup_category(c)), out);
  149. break;
  150. case 'C': // category group
  151. fputs(ucd::get_category_group_string(ucd::lookup_category_group(c)), out);
  152. break;
  153. case 'p': // codepoint
  154. uprintf_codepoint(out, c, *++format);
  155. break;
  156. case 'i': // is*
  157. uprintf_is(out, c, *++format);
  158. break;
  159. case 'L': // lowercase
  160. uprintf_codepoint(out, ucd::tolower(c), *++format);
  161. break;
  162. case 's': // script
  163. fputs(ucd::get_script_string(ucd::lookup_script(c)), out);
  164. break;
  165. case 'T': // titlecase
  166. uprintf_codepoint(out, ucd::totitle(c), *++format);
  167. break;
  168. case 'U': // uppercase
  169. uprintf_codepoint(out, ucd::toupper(c), *++format);
  170. break;
  171. }
  172. ++format;
  173. break;
  174. case '\\':
  175. switch (*++format) {
  176. case 0:
  177. break;
  178. case 't':
  179. fputc('\t', out);
  180. ++format;
  181. break;
  182. case 'r':
  183. fputc('\r', out);
  184. ++format;
  185. break;
  186. case 'n':
  187. fputc('\n', out);
  188. ++format;
  189. break;
  190. default:
  191. fputc(*format, out);
  192. ++format;
  193. break;
  194. }
  195. break;
  196. default:
  197. fputc(*format, out);
  198. ++format;
  199. break;
  200. }
  201. }
  202. void print_file(FILE *in, const char *format)
  203. {
  204. ucd::codepoint_t c = 0;
  205. while (fget_utf8c(in, c))
  206. uprintf(stdout, c, format ? format : "%pc\t%pH\t%s\t%c\t%Uc\t%Lc\t%Tc\t%is\n");
  207. }
  208. int main(int argc, char **argv)
  209. {
  210. FILE *in = NULL;
  211. const char *format = NULL;
  212. for (int argn = 1; argn != argc; ++argn)
  213. {
  214. const char *arg = argv[argn];
  215. if (!strcmp(arg, "--stdin") || !strcmp(arg, "-"))
  216. in = stdin;
  217. else if (!strncmp(arg, "--format=", 9))
  218. format = arg + 9;
  219. else if (in == NULL)
  220. {
  221. in = fopen(arg, "r");
  222. if (!in)
  223. fprintf(stdout, "cannot open `%s`\n", argv[1]);
  224. }
  225. }
  226. if (in == stdin)
  227. print_file(stdin, format);
  228. else if (in != NULL)
  229. {
  230. print_file(in, format);
  231. fclose(in);
  232. }
  233. else
  234. {
  235. for (ucd::codepoint_t c = 0; c <= 0x10FFFF; ++c)
  236. uprintf(stdout, c, format ? format : "%pH %s %C %c %UH %LH %TH %is %iu %il\n");
  237. }
  238. return 0;
  239. }