gen_ldc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <stdint.h>
  6. #include <stddef.h>
  7. #ifndef __WCHAR_ENABLED
  8. #warning WHOA!!! __WCHAR_ENABLED is not defined! defining it now...
  9. #define __WCHAR_ENABLED
  10. #endif
  11. #define WANT_DATA
  12. #include "c8tables.h"
  13. #ifndef __CTYPE_HAS_8_BIT_LOCALES
  14. #warning __CTYPE_HAS_8_BIT_LOCALES is not defined...
  15. /* #define __CTYPE_HAS_8_BIT_LOCALES */
  16. #endif
  17. /* #define Cctype_TBL_LEN 328 */
  18. /* #define Cuplow_TBL_LEN 400 */
  19. /* #define Cc2wc_TBL_LEN 1448 */
  20. /* #define Cwc2c_TBL_LEN 3744 */
  21. #define WANT_WCctype_data
  22. #define WANT_WCuplow_data
  23. #define WANT_WCuplow_diff_data
  24. /* #define WANT_WCcomb_data */
  25. /* #define WANT_WCwidth_data */
  26. #include "wctables.h"
  27. #undef WANT_WCctype_data
  28. #undef WANT_WCuplow_data
  29. #undef WANT_WCuplow_diff_data
  30. /* #undef WANT_WCcomb_data */
  31. /* #undef WANT_WCwidth_data */
  32. #define WCctype_TBL_LEN (WCctype_II_LEN + WCctype_TI_LEN + WCctype_UT_LEN)
  33. #define WCuplow_TBL_LEN (WCuplow_II_LEN + WCuplow_TI_LEN + WCuplow_UT_LEN)
  34. #define WCuplow_diff_TBL_LEN (2 * WCuplow_diffs)
  35. /* #define WCcomb_TBL_LEN (WCcomb_II_LEN + WCcomb_TI_LEN + WCcomb_UT_LEN) */
  36. #include "locale_collate.h"
  37. #include "locale_tables.h"
  38. #include "locale_mmap.h"
  39. /* #undef __PASTE2 */
  40. /* #define __PASTE2(A,B) A ## B */
  41. /* #undef __PASTE3 */
  42. /* #define __PASTE3(A,B,C) A ## B ## C */
  43. /* #define MAGIC_SIZE 64 */
  44. /* #define COMMON_MMAP(X) \ */
  45. /* unsigned char __PASTE3(lc_,X,_data)[__PASTE3(__lc_,X,_data_LEN)]; */
  46. /* #define COMMON_MMIDX(X) \ */
  47. /* unsigned char __PASTE3(lc_,X,_rows)[__PASTE3(__lc_,X,_rows_LEN)]; \ */
  48. /* uint16_t __PASTE3(lc_,X,_item_offsets)[__PASTE3(__lc_,X,_item_offsets_LEN)]; \ */
  49. /* uint16_t __PASTE3(lc_,X,_item_idx)[__PASTE3(__lc_,X,_item_idx_LEN)]; \ */
  50. /* ---------------------------------------------------------------------- */
  51. #define COMMON_OFFSETS(X) \
  52. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), \
  53. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), \
  54. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), \
  55. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)) \
  56. static const size_t common_tbl_offsets[CATEGORIES*4] = {
  57. COMMON_OFFSETS(ctype),
  58. COMMON_OFFSETS(numeric),
  59. COMMON_OFFSETS(monetary),
  60. COMMON_OFFSETS(time),
  61. 0, 0, 0, 0, /* collate */
  62. COMMON_OFFSETS(messages),
  63. };
  64. void out_uc(FILE *f, const unsigned char *p, size_t n, char *comment)
  65. {
  66. size_t i;
  67. fprintf(f, "{\t/* %s */", comment);
  68. for (i = 0 ; i < n ; i++) {
  69. if (!(i & 7)) {
  70. fprintf(f, "\n\t");
  71. }
  72. if (p[i]) {
  73. fprintf(f, "%#04x, ", p[i]);
  74. } else {
  75. fprintf(f, "%#4x, ", p[i]);
  76. }
  77. }
  78. fprintf(f, "\n},\n");
  79. }
  80. void out_u16(FILE *f, const uint16_t *p, size_t n, char *comment)
  81. {
  82. size_t i;
  83. fprintf(f, "{\t/* %s */", comment);
  84. for (i = 0 ; i < n ; i++) {
  85. if (!(i & 7)) {
  86. fprintf(f, "\n\t");
  87. }
  88. if (p[i]) {
  89. fprintf(f, "%#06x, ", p[i]);
  90. } else {
  91. fprintf(f, "%#6x, ", p[i]);
  92. }
  93. }
  94. fprintf(f, "\n},\n");
  95. }
  96. void out_i16(FILE *f, const int16_t *p, size_t n, char *comment)
  97. {
  98. size_t i;
  99. fprintf(f, "{\t/* %s */", comment);
  100. for (i = 0 ; i < n ; i++) {
  101. if (!(i & 7)) {
  102. fprintf(f, "\n\t");
  103. }
  104. fprintf(f, "%6d, ", p[i]);
  105. }
  106. fprintf(f, "\n},\n");
  107. }
  108. void out_size_t(FILE *f, const size_t *p, size_t n, char *comment)
  109. {
  110. size_t i;
  111. fprintf(f, "{\t/* %s */", comment);
  112. for (i = 0 ; i < n ; i++) {
  113. if (!(i & 3)) {
  114. fprintf(f, "\n\t");
  115. }
  116. if (p[i]) {
  117. fprintf(f, "%#010zx, ", p[i]);
  118. } else {
  119. fprintf(f, "%#10zx, ", p[i]);
  120. }
  121. }
  122. fprintf(f, "\n},\n");
  123. }
  124. int main(void)
  125. {
  126. FILE *lso; /* static object */
  127. int i;
  128. unsigned char magic[MAGIC_SIZE];
  129. memset(magic, 0, MAGIC_SIZE);
  130. if (!(lso = fopen("locale_data.c", "w"))) {
  131. printf("can't open locale_data.c!\n");
  132. return EXIT_FAILURE;
  133. }
  134. fprintf(lso,
  135. "#include <stddef.h>\n"
  136. "#include <stdint.h>\n"
  137. /* "#define __CTYPE_HAS_8_BIT_LOCALES\n" */
  138. "#ifndef __WCHAR_ENABLED\n"
  139. "#error __WCHAR_ENABLED not defined\n"
  140. "#endif\n"
  141. "#include \"c8tables.h\"\n"
  142. "#include \"wctables.h\"\n"
  143. "#include \"lt_defines.h\"\n"
  144. "#include \"locale_mmap.h\"\n\n"
  145. "static const __locale_mmap_t locale_mmap = {\n\n"
  146. );
  147. out_uc(lso, magic, MAGIC_SIZE, "magic");
  148. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  149. out_uc(lso, Cctype_data, Cctype_TBL_LEN, "tbl8ctype");
  150. out_uc(lso, Cuplow_data, Cuplow_TBL_LEN, "tbl8uplow");
  151. #ifdef __WCHAR_ENABLED
  152. out_u16(lso, Cc2wc_data, Cc2wc_TBL_LEN, "tbl8c2wc");
  153. out_uc(lso, Cwc2c_data, Cwc2c_TBL_LEN, "tbl8wc2c");
  154. /* translit */
  155. #endif /* __WCHAR_ENABLED */
  156. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  157. #ifdef __WCHAR_ENABLED
  158. out_uc(lso, WCctype_data, WCctype_TBL_LEN, "tblwctype");
  159. out_uc(lso, WCuplow_data, WCuplow_TBL_LEN, "tblwuplow");
  160. out_i16(lso, WCuplow_diff_data, WCuplow_diff_TBL_LEN, "tblwuplow_diff");
  161. /* const unsigned char tblwcomb[WCcomb_TBL_LEN]; */
  162. /* width?? */
  163. #endif /* __WCHAR_ENABLED */
  164. out_uc(lso, __lc_ctype_data, __lc_ctype_data_LEN, "lc_ctype_data");
  165. out_uc(lso, __lc_numeric_data, __lc_numeric_data_LEN, "lc_numeric_data");
  166. out_uc(lso, __lc_monetary_data, __lc_monetary_data_LEN, "lc_monetary_data");
  167. out_uc(lso, __lc_time_data, __lc_time_data_LEN, "lc_time_data");
  168. /* TODO -- collate*/
  169. out_uc(lso, __lc_messages_data, __lc_messages_data_LEN, "lc_messages_data");
  170. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  171. fprintf(lso, "{ /* codeset_8_bit array */\n");
  172. for (i = 0 ; i < NUM_CODESETS ; i++) {
  173. fprintf(lso, "{ /* codeset_8_bit[%d] */\n", i);
  174. out_uc(lso, codeset_8_bit[i].idx8ctype, Cctype_IDX_LEN, "idx8ctype");
  175. out_uc(lso, codeset_8_bit[i].idx8uplow, Cuplow_IDX_LEN, "idx8uplow");
  176. out_uc(lso, codeset_8_bit[i].idx8c2wc, Cc2wc_IDX_LEN, "idx8c2wc");
  177. out_uc(lso, codeset_8_bit[i].idx8wc2c, Cwc2c_II_LEN, "idx8wc2c");
  178. fprintf(lso, "},\n");
  179. }
  180. fprintf(lso, "},\n");
  181. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  182. out_uc(lso, __lc_ctype_rows, __lc_ctype_rows_LEN, "lc_ctype_rows");
  183. out_u16(lso, __lc_ctype_item_offsets, __lc_ctype_item_offsets_LEN, "lc_ctype_item_offsets");
  184. out_u16(lso, __lc_ctype_item_idx, __lc_ctype_item_idx_LEN, "lc_ctype_item_idx");
  185. out_uc(lso, __lc_numeric_rows, __lc_numeric_rows_LEN, "lc_numeric_rows");
  186. out_u16(lso, __lc_numeric_item_offsets, __lc_numeric_item_offsets_LEN, "lc_numeric_item_offsets");
  187. out_u16(lso, __lc_numeric_item_idx, __lc_numeric_item_idx_LEN, "lc_numeric_item_idx");
  188. out_uc(lso, __lc_monetary_rows, __lc_monetary_rows_LEN, "lc_monetary_rows");
  189. out_u16(lso, __lc_monetary_item_offsets, __lc_monetary_item_offsets_LEN, "lc_monetary_item_offsets");
  190. out_u16(lso, __lc_monetary_item_idx, __lc_monetary_item_idx_LEN, "lc_monetary_item_idx");
  191. out_uc(lso, __lc_time_rows, __lc_time_rows_LEN, "lc_time_rows");
  192. out_u16(lso, __lc_time_item_offsets, __lc_time_item_offsets_LEN, "lc_time_item_offsets");
  193. out_u16(lso, __lc_time_item_idx, __lc_time_item_idx_LEN, "lc_time_item_idx");
  194. out_uc(lso, __lc_messages_rows, __lc_messages_rows_LEN, "lc_messages_rows");
  195. out_u16(lso, __lc_messages_item_offsets, __lc_messages_item_offsets_LEN, "lc_messages_item_offsets");
  196. out_u16(lso, __lc_messages_item_idx, __lc_messages_item_idx_LEN, "lc_messages_item_idx");
  197. /* collate should be last*/
  198. assert(sizeof(__locale_collate_tbl)/sizeof(__locale_collate_tbl[0]) == __lc_collate_data_LEN) ;
  199. out_u16(lso, __locale_collate_tbl, __lc_collate_data_LEN, "collate_data");
  200. {
  201. unsigned char co_buf[CATEGORIES] = {
  202. __lc_ctype_item_offsets_LEN,
  203. __lc_numeric_item_offsets_LEN,
  204. __lc_monetary_item_offsets_LEN,
  205. __lc_time_item_offsets_LEN,
  206. 0,
  207. __lc_messages_item_offsets_LEN
  208. };
  209. out_uc(lso, co_buf, CATEGORIES, "lc_common_item_offsets_LEN");
  210. }
  211. out_size_t(lso, common_tbl_offsets, CATEGORIES * 4, "lc_common_tbl_offsets");
  212. /* offsets from start of locale_mmap_t */
  213. /* rows, item_offsets, item_idx, data */
  214. #ifdef NUM_LOCALES
  215. out_uc(lso, __locales, NUM_LOCALES * WIDTH_LOCALES, "locales");
  216. out_uc(lso, __locale_names5, 5 * NUM_LOCALE_NAMES, "locale_names5");
  217. #ifdef LOCALE_AT_MODIFIERS_LENGTH
  218. out_uc(lso, __locale_at_modifiers, LOCALE_AT_MODIFIERS_LENGTH, "locale_at_modifiers");
  219. #else
  220. #error LOCALE_AT_MODIFIERS_LENGTH not defined!
  221. #endif /* LOCALE_AT_MODIFIERS_LENGTH */
  222. #endif /* NUM_LOCALES */
  223. out_uc(lso, lc_names, lc_names_LEN, "lc_names");
  224. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  225. out_uc(lso, (const unsigned char*) CODESET_LIST, sizeof(CODESET_LIST), "codeset_list");
  226. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  227. fprintf(lso,
  228. "\n};\n\n"
  229. "const __locale_mmap_t *__locale_mmap = &locale_mmap;\n\n"
  230. );
  231. if (ferror(lso) || fclose(lso)) {
  232. printf("error writing!\n");
  233. return EXIT_FAILURE;
  234. }
  235. return EXIT_SUCCESS;
  236. }
  237. /* ---------------------------------------------------------------------- */
  238. /* TODO:
  239. * collate data (8-bit weighted single char only)
  240. * @ mappings!
  241. * codeset list? yes, since we'll want to be able to inspect them...
  242. * that means putting some header stuff in magic
  243. * fix ctype LEN defines in gen_c8tables
  244. */