conv_kana.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * this is a small sample script to use libmbfl.
  3. * Rui Hirokawa <hirokawa@php.net>
  4. *
  5. * this file is encoded in EUC-JP.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "mbfl/mbfilter.h"
  11. static void hexdump(const mbfl_string *ptr)
  12. {
  13. unsigned int i;
  14. for (i = 0; i < ptr->len; i++) {
  15. printf("%%%02x", ptr->val[i]);
  16. }
  17. printf(" (%u)\n", ptr->len);
  18. }
  19. int main(int argc, char **argv)
  20. {
  21. enum mbfl_no_encoding no_enc;
  22. const enum mbfl_no_language no_lang = mbfl_no_language_japanese;
  23. mbfl_memory_device dev;
  24. mbfl_string string, result;
  25. int final = 0;
  26. int state = 0;
  27. int mode = 0;
  28. if (argc < 3) {
  29. fprintf(stderr, "Usage: %s encoding flags\n", argv[0]);
  30. return EXIT_FAILURE;
  31. }
  32. if ((no_enc = mbfl_name2no_encoding(argv[1])) ==
  33. mbfl_no_encoding_invalid) {
  34. printf("Unsupported encoding: %s\n", argv[1]);
  35. return EXIT_FAILURE;
  36. }
  37. {
  38. const char *p;
  39. for (p= argv[2] + strlen(argv[2]); p > argv[2]; ) {
  40. switch (*(--p)) {
  41. case 'A':
  42. mode |= 0x1;
  43. break;
  44. case 'a':
  45. mode |= 0x10;
  46. break;
  47. case 'R':
  48. mode |= 0x2;
  49. break;
  50. case 'r':
  51. mode |= 0x20;
  52. break;
  53. case 'N':
  54. mode |= 0x4;
  55. break;
  56. case 'n':
  57. mode |= 0x40;
  58. break;
  59. case 'S':
  60. mode |= 0x8;
  61. break;
  62. case 's':
  63. mode |= 0x80;
  64. break;
  65. case 'K':
  66. mode |= 0x100;
  67. break;
  68. case 'k':
  69. mode |= 0x1000;
  70. break;
  71. case 'H':
  72. mode |= 0x200;
  73. break;
  74. case 'h':
  75. mode |= 0x2000;
  76. break;
  77. case 'V':
  78. mode |= 0x800;
  79. break;
  80. case 'C':
  81. mode |= 0x10000;
  82. break;
  83. case 'c':
  84. mode |= 0x20000;
  85. break;
  86. case 'M':
  87. mode |= 0x100000;
  88. break;
  89. case 'm':
  90. mode |= 0x200000;
  91. break;
  92. }
  93. }
  94. }
  95. do {
  96. mbfl_memory_device_init(&dev, 0, 4096);
  97. mbfl_string_init_set(&string, no_lang, no_enc);
  98. for (;;) {
  99. const int c = fgetc(stdin);
  100. if (c == EOF) {
  101. final = 1;
  102. break;
  103. } else if (c == 10) {
  104. if (state == 1) {
  105. state = 0;
  106. continue;
  107. }
  108. break;
  109. } else if (c == 13) {
  110. state = 1;
  111. break;
  112. }
  113. if (dev.pos >= dev.length) {
  114. if (dev.length + dev.allocsz < dev.length) {
  115. printf("Unable to allocate memory\n");
  116. return EXIT_FAILURE;
  117. }
  118. mbfl_memory_device_realloc(&dev, dev.length + dev.allocsz,
  119. dev.allocsz);
  120. }
  121. dev.buffer[dev.pos++] = (unsigned char)c;
  122. }
  123. mbfl_memory_device_result(&dev, &string);
  124. mbfl_ja_jp_hantozen(&string, &result, mode);
  125. hexdump(&result);
  126. mbfl_string_clear(&result);
  127. mbfl_string_clear(&string);
  128. } while (!final);
  129. return EXIT_SUCCESS;
  130. }