OSDN Git Service

コンパイル時にNKFを使用して文字コードを変換するようにした
authorHabu <habu@users.sourceforge.jp>
Mon, 10 Mar 2014 13:55:27 +0000 (22:55 +0900)
committerHabu <habu@users.sourceforge.jp>
Mon, 10 Mar 2014 13:55:27 +0000 (22:55 +0900)
古い環境のiconvではEUC-JP-MSをサポートしていないので、
-fexec-charset=EUC-JP-MS が使えずコンパイルができないため、NKFを使
用してソースコードを変換するようにした。
また、内部でのiconvの変換はEUC-JPに戻し、iconvに渡す前にCP932から
Unicodeマッピングした文字コードをJISのUnicodeに変換するようにした。

configure.ac
src/Makefile.am
src/gcc-wrap [new file with mode: 0755]
src/japanese.c

index d8f815c..5796924 100644 (file)
@@ -49,11 +49,6 @@ AC_ARG_ENABLE(worldscore,
 AC_ARG_ENABLE(chuukei,
 [  --enable-chuukei        enable internet chuukei support], AC_DEFINE(CHUUKEI, 1, [Chuukei mode]))
 
-dnl -fno-strength-reduce prevents a bug in some versions of gcc
-if test "$GCC" = yes; then
-  CFLAGS="$CFLAGS -fno-strength-reduce --exec-charset=EUC-JP-MS"
-fi
-
 dnl Checks for libraries.
 dnl Replace `main' with a function in -lncurses:
 AC_CHECK_LIB(ncursesw, initscr, [AC_DEFINE(USE_GCU, 1, [Allow -mGCU environment]) AC_DEFINE(USE_NCURSES, 1, [Use ncurses]) LIBS="$LIBS -lncursesw"])
@@ -101,6 +96,13 @@ if test "$have_x" = yes; then
   fi
 fi
 
+if test "$use_japanese" != no; then
+  AC_CHECK_PROG(NKF, nkf, yes)
+  if test x"$NKF" != x"yes"; then
+    AC_MSG_ERROR([nkf is not found. Please install nkf.])
+  fi
+fi
+
 AC_HEADER_STDC
 AC_CHECK_HEADERS(fcntl.h strings.h sys/file.h sys/ioctl.h sys/time.h termio.h unistd.h stdint.h)
 
index 5261f7b..ad99aa8 100644 (file)
@@ -31,6 +31,9 @@ EXTRA_hengband_SOURCES = \
        makefile.bcc makefile.std makefile.dos makefile.ibm \
        readdib.c wall.bmp
 
+COMPILE = ./gcc-wrap $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
+       $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+
 install-exec-hook:
 if SET_GID
        chgrp "@GAMEGROUP@" "$(DESTDIR)$(bindir)/hengband"
diff --git a/src/gcc-wrap b/src/gcc-wrap
new file mode 100755 (executable)
index 0000000..97102da
--- /dev/null
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+jp=`grep "#define JP" autoconf.h | wc -l`
+
+if test $jp -eq 1; then
+    eval src_file=\${$#}
+
+    trap '' INT
+    backup=`mktemp`
+    mv $src_file $backup
+    nkf -e $backup > $src_file
+    $@
+    mv $backup $src_file
+else
+    $@
+fi
index 932abf6..ae058d9 100644 (file)
@@ -398,6 +398,40 @@ static bool is_utf8_str(cptr str)
 
 #if defined(EUC)
 #include <iconv.h>
+
+static const struct ms_to_jis_unicode_conv_t {
+       char from[3];
+       char to[3];
+} ms_to_jis_unicode_conv[] = {
+       {{0xef, 0xbd, 0x9e}, {0xe3, 0x80, 0x9c}}, /* FULLWIDTH TILDE -> WAVE DASH */
+       {{0xef, 0xbc, 0x8d}, {0xe2, 0x88, 0x92}}, /* FULLWIDTH HYPHEN-MINUS -> MINUS SIGN */
+};
+
+static void ms_to_jis_unicode(char* str)
+{
+       unsigned char* p;
+       for (p = (unsigned char*)str; *p; p++) {
+               int subseq_num = 0;
+               if (0x00 < *p && *p <= 0x7f) continue;
+
+               if ((*p & 0xe0) == 0xc0) subseq_num = 1;
+               if ((*p & 0xf0) == 0xe0) {
+                       int i;
+                       for (i = 0; i < sizeof(ms_to_jis_unicode_conv) / sizeof(ms_to_jis_unicode_conv[0]); ++ i) {
+                               const struct ms_to_jis_unicode_conv_t *c = &ms_to_jis_unicode_conv[i];
+                               if (memcmp(p, c->from, 3) == 0) {
+                                       printf("hoge\n");
+                                       memcpy(p, c->to, 3);
+                               }
+                       }
+                       subseq_num = 2;
+                }
+               if ((*p & 0xf8) == 0xf0) subseq_num = 3;
+
+               p += subseq_num;
+       }
+}
+
 #elif defined(SJIS) && defined(WINDOWS)
 #include <Windows.h>
 #endif
@@ -412,12 +446,15 @@ static bool utf8_to_sys(char* utf8_str, char* sys_str_buffer, size_t sys_str_buf
 {
 #if defined(EUC)
 
-        iconv_t cd = iconv_open("EUC-JP-MS", "UTF-8");
-        size_t utf8_len = strlen(utf8_str) + 1; /* include termination character */
-        char *from = utf8_str;
-        int ret = iconv(cd, &from, &utf8_len, &sys_str_buffer, &sys_str_buflen);
-        iconv_close(cd);
-        return (ret >= 0);
+       iconv_t cd = iconv_open("EUC-JP", "UTF-8");
+       size_t utf8_len = strlen(utf8_str) + 1; /* include termination character */
+       char *from = utf8_str;
+       int ret;
+
+       ms_to_jis_unicode(utf8_str);
+       ret = iconv(cd, &from, &utf8_len, &sys_str_buffer, &sys_str_buflen);
+       iconv_close(cd);
+       return (ret >= 0);
 
 #elif defined(SJIS) && defined(WINDOWS)