gen_mmap.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #define WANT_DATA
  8. #include "c8tables.h"
  9. /* #define Cctype_TBL_LEN 328 */
  10. /* #define Cuplow_TBL_LEN 400 */
  11. /* #define Cc2wc_TBL_LEN 1448 */
  12. /* #define Cwc2c_TBL_LEN 3744 */
  13. #define WANT_WCctype_data
  14. #define WANT_WCuplow_data
  15. #define WANT_WCuplow_diff_data
  16. /* #define WANT_WCcomb_data */
  17. /* #define WANT_WCwidth_data */
  18. #include "wctables.h"
  19. #undef WANT_WCctype_data
  20. #undef WANT_WCuplow_data
  21. #undef WANT_WCuplow_diff_data
  22. /* #undef WANT_WCcomb_data */
  23. /* #undef WANT_WCwidth_data */
  24. /* #define WCctype_TBL_LEN (WCctype_II_LEN + WCctype_TI_LEN + WCctype_UT_LEN) */
  25. /* #define WCuplow_TBL_LEN (WCuplow_II_LEN + WCuplow_TI_LEN + WCuplow_UT_LEN) */
  26. /* #define WCuplow_diff_TBL_LEN (2 * WCuplow_diffs) */
  27. /* #define WCcomb_TBL_LEN (WCcomb_II_LEN + WCcomb_TI_LEN + WCcomb_UT_LEN) */
  28. #include "locale_tables.h"
  29. #include "locale_mmap.h"
  30. /* #undef __PASTE2 */
  31. /* #define __PASTE2(A,B) A ## B */
  32. /* #undef __PASTE3 */
  33. /* #define __PASTE3(A,B,C) A ## B ## C */
  34. /* #define MAGIC_SIZE 64 */
  35. /* #define COMMON_MMAP(X) \ */
  36. /* unsigned char __PASTE3(lc_,X,_data)[__PASTE3(__lc_,X,_data_LEN)]; */
  37. /* #define COMMON_MMIDX(X) \ */
  38. /* unsigned char __PASTE3(lc_,X,_rows)[__PASTE3(__lc_,X,_rows_LEN)]; \ */
  39. /* uint16_t __PASTE3(lc_,X,_item_offsets)[__PASTE3(__lc_,X,_item_offsets_LEN)]; \ */
  40. /* uint16_t __PASTE3(lc_,X,_item_idx)[__PASTE3(__lc_,X,_item_idx_LEN)]; \ */
  41. #define WRITE_COMMON_MMAP(X) \
  42. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)), SEEK_SET); \
  43. for (i=0 ; i < __PASTE3(__lc_,X,_data_LEN) ; i++) { \
  44. putc(__PASTE3(__lc_,X,_data)[i], fp); \
  45. }
  46. #define WRITE_COMMON_MMIDX(X) \
  47. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), SEEK_SET); \
  48. for (i=0 ; i < __PASTE3(__lc_,X,_rows_LEN) ; i++) { \
  49. putc(__PASTE3(__lc_,X,_rows)[i], fp); \
  50. } \
  51. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), SEEK_SET); \
  52. for (i=0 ; i < __PASTE3(__lc_,X,_item_offsets_LEN) ; i++) { \
  53. putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_offsets)[i]))[0], fp); \
  54. putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_offsets)[i]))[1], fp); \
  55. } \
  56. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), SEEK_SET); \
  57. for (i=0 ; i < __PASTE3(__lc_,X,_item_idx_LEN) ; i++) { \
  58. putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_idx)[i]))[0], fp); \
  59. putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_idx)[i]))[1], fp); \
  60. }
  61. #define WRITE_WC_DATA(X) \
  62. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tblw,X)), SEEK_SET); \
  63. for (i=0 ; i < __PASTE3(WC,X,_TBL_LEN) ; i++) { \
  64. putc(__PASTE3(WC,X,_data)[i], fp); \
  65. }
  66. #define WRITE_WC_I16_DATA(X) \
  67. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tblw,X)), SEEK_SET); \
  68. for (i=0 ; i < __PASTE3(WC,X,_TBL_LEN) ; i++) { \
  69. putc( ((unsigned char *) &(__PASTE3(WC,X,_data)[i]))[0], fp); \
  70. putc( ((unsigned char *) &(__PASTE3(WC,X,_data)[i]))[1], fp); \
  71. }
  72. #define WRITE_C_DATA(X) \
  73. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tbl8,X)), SEEK_SET); \
  74. for (i=0 ; i < __PASTE3(C,X,_TBL_LEN) ; i++) { \
  75. putc(__PASTE3(C,X,_data)[i], fp); \
  76. }
  77. #define WRITE_C_U16_DATA(X) \
  78. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tbl8,X)), SEEK_SET); \
  79. for (i=0 ; i < __PASTE3(C,X,_TBL_LEN) ; i++) { \
  80. putc( ((unsigned char *) &(__PASTE3(C,X,_data)[i]))[0], fp); \
  81. putc( ((unsigned char *) &(__PASTE3(C,X,_data)[i]))[1], fp); \
  82. }
  83. /**********************************************************************/
  84. #define COMMON_OFFSETS(X) \
  85. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), \
  86. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), \
  87. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), \
  88. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)) \
  89. static const size_t common_tbl_offsets[CATEGORIES*4] = {
  90. 0, 0, 0, 0, /* ctype */
  91. COMMON_OFFSETS(numeric),
  92. COMMON_OFFSETS(monetary),
  93. COMMON_OFFSETS(time),
  94. 0, 0, 0, 0, /* collate */
  95. COMMON_OFFSETS(messages),
  96. };
  97. int main(void)
  98. {
  99. FILE *fp;
  100. size_t i;
  101. unsigned char *p;
  102. if (!(fp = fopen("locale.mmap", "w"))) {
  103. printf("cannot open locale.mmap for writing!");
  104. return EXIT_FAILURE;
  105. }
  106. for (i=0 ; i < sizeof(__locale_mmap_t) ; i++) {
  107. putc(0, fp); /* Zero out the file. */
  108. }
  109. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  110. WRITE_C_DATA(ctype);
  111. WRITE_C_DATA(uplow);
  112. #ifdef __WCHAR_ENABLED
  113. WRITE_C_U16_DATA(c2wc);
  114. WRITE_C_DATA(wc2c);
  115. /* translit */
  116. #endif /* __WCHAR_ENABLED */
  117. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  118. #ifdef __WCHAR_ENABLED
  119. WRITE_WC_DATA(ctype);
  120. WRITE_WC_DATA(uplow);
  121. WRITE_WC_I16_DATA(uplow_diff);
  122. /* WRITE_WC_DATA(comb); */
  123. /* width?? */
  124. #endif /* __WCHAR_ENABLED */
  125. WRITE_COMMON_MMAP(numeric);
  126. WRITE_COMMON_MMAP(monetary);
  127. WRITE_COMMON_MMAP(time);
  128. /* TODO -- collate*/
  129. WRITE_COMMON_MMAP(messages);
  130. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  131. fseek(fp, (long) offsetof(__locale_mmap_t, codeset_8_bit), SEEK_SET); \
  132. p = (unsigned char *) codeset_8_bit;
  133. for (i=0 ; i < sizeof(codeset_8_bit) ; i++) {
  134. putc(p[i], fp);
  135. }
  136. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  137. WRITE_COMMON_MMIDX(numeric);
  138. WRITE_COMMON_MMIDX(monetary);
  139. WRITE_COMMON_MMIDX(time);
  140. /* TODO -- collate*/
  141. WRITE_COMMON_MMIDX(messages);
  142. fseek(fp, (long) offsetof(__locale_mmap_t, lc_common_item_offsets_LEN), SEEK_SET);
  143. putc(1, fp); /* ctype -- (codeset) handled specially */
  144. putc(__lc_numeric_item_offsets_LEN, fp);
  145. putc(__lc_monetary_item_offsets_LEN, fp);
  146. putc(__lc_time_item_offsets_LEN, fp);
  147. putc(0, fp); /* collate */
  148. putc(__lc_messages_item_offsets_LEN, fp);
  149. fseek(fp, (long) offsetof(__locale_mmap_t, lc_common_tbl_offsets), SEEK_SET);
  150. for (i=0 ; i < sizeof(common_tbl_offsets) ; i++) {
  151. putc(((unsigned char *)common_tbl_offsets)[i], fp);
  152. }
  153. #ifdef NUM_LOCALES
  154. fseek(fp, (long) offsetof(__locale_mmap_t, locales), SEEK_SET);
  155. for (i=0 ; i < (NUM_LOCALES * WIDTH_LOCALES) ; i++) {
  156. putc(__locales[i], fp);
  157. }
  158. fseek(fp, (long) offsetof(__locale_mmap_t, locale_names5), SEEK_SET);
  159. for (i=0 ; i < 5 * NUM_LOCALE_NAMES ; i++) {
  160. putc(__locale_names5[i], fp);
  161. }
  162. #ifdef LOCALE_AT_MODIFIERS_LENGTH
  163. fseek(fp, (long) offsetof(__locale_mmap_t, locale_at_modifiers), SEEK_SET);
  164. for (i=0 ; i < LOCALE_AT_MODIFIERS_LENGTH ; i++) {
  165. putc(__locale_at_modifiers[i], fp);
  166. }
  167. #endif /* LOCALE_AT_MODIFIERS_LENGTH */
  168. #endif /* NUM_LOCALES */
  169. fseek(fp, (long) offsetof(__locale_mmap_t, lc_names), SEEK_SET);
  170. for (i=0 ; i < lc_names_LEN ; i++) {
  171. putc(lc_names[i], fp);
  172. }
  173. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  174. fseek(fp, (long) offsetof(__locale_mmap_t, codeset_list), SEEK_SET);
  175. for (i=0 ; i < sizeof(CODESET_LIST) ; i++) {
  176. putc((unsigned char)(CODESET_LIST[i]), fp);
  177. }
  178. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  179. if (ferror(fp) || (fclose(fp) == EOF)) {
  180. printf("error - stream in error state or fclose failed!");
  181. return EXIT_FAILURE;
  182. }
  183. printf("sizeof(__locale_mmap_t) = %zd\n", sizeof(__locale_mmap_t));
  184. return EXIT_SUCCESS;
  185. }
  186. /* TODO:
  187. * collate data (8-bit weighted single char only)
  188. * @ mappings!
  189. * codeset list? yes, since we'll want to be able to inspect them...
  190. * that means putting some header stuff in magic
  191. * fix ctype LEN defines in gen_c8tables
  192. */