Skip to content

Instantly share code, notes, and snippets.

@hanazuki
Last active December 17, 2015 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanazuki/5591704 to your computer and use it in GitHub Desktop.
Save hanazuki/5591704 to your computer and use it in GitHub Desktop.
Patch for Hengband SVN r3405 to run in non-EUC-JP terminals (such as UTF-8) with curses (-mgcu) / 変愚蛮怒をUTF-8なターミナルで動作させるためのパッチです
diff --git a/src/main-gcu.c b/src/main-gcu.c
index 0c7f244..069d434 100644
--- a/src/main-gcu.c
+++ b/src/main-gcu.c
@@ -158,6 +158,7 @@
#include "angband.h"
+#include <iconv.h>
#ifdef USE_GCU
@@ -189,6 +190,7 @@ struct term_data
static term_data data[MAX_TERM_DATA];
+static iconv_t iconvd;
/*
* Hack -- try to guess which systems use what commands
@@ -1095,7 +1097,7 @@ static errr Term_text_gcu(int x, int y, int n, byte a, cptr s)
int i;
- char text[81];
+ char text[80 * 3 + 1]; // is enough for any coding?
#ifdef USE_NCURSES_ACS
/* do we have colors + 16 ? */
@@ -1107,8 +1109,19 @@ static errr Term_text_gcu(int x, int y, int n, byte a, cptr s)
}
#endif
- /* Obtain a copy of the text */
- for (i = 0; i < n; i++) text[i] = s[i]; text[n] = 0;
+ size_t inlen = n, outlen = sizeof text;
+ char *inbuf = s, *outbuf = text;
+
+ size_t res;
+ res = iconv(iconvd, 0, 0, 0, 0);
+ if(res == (size_t)-1) return (-1);
+ res = iconv(iconvd, &inbuf, &inlen, &outbuf, &outlen);
+ if(res == (size_t)-1) return (-1);
+ res = iconv(iconvd, 0, 0, &outbuf, &outlen);
+ if(res == (size_t)-1) return (-1);
+
+ if(outlen == 0) return (-1);
+ *outbuf = '\0';
/* Move the cursor and dump the string */
wmove(td->win, y, x);
@@ -1183,6 +1196,8 @@ static void hook_quit(cptr str)
/* Exit curses */
endwin();
+
+ iconv_close(iconvd);
}
@@ -1207,6 +1222,8 @@ errr init_gcu(int argc, char *argv[])
setlocale(LC_ALL, "");
+ iconvd = iconv_open("", "EUC-JP");
+ if(iconvd == (iconv_t)-1) return (-1);
#ifdef USE_SOUND
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment