Created
August 9, 2019 05:05
-
-
Save plonk/3aff6b9aa8eba1173d91e43b59ad6415 to your computer and use it in GitHub Desktop.
SFCトルネコの名前付け。引数で渡された名前をキー操作に変換
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define LAYER_H 0 | |
#define LAYER_K 1 | |
/* 6*10 */ | |
const char* htbl[6][10] = { | |
{ "あ","い","う","え","お","は","ひ","ふ","へ","ほ" }, | |
{ "か","き","く","け","こ","ま","み","む","め","も" }, | |
{ "さ","し","す","せ","そ","や"," ","ゆ"," ","よ" }, | |
{ "た","ち","つ","て","と","ら","り","る","れ","ろ" }, | |
{ "な","に","ぬ","ね","の","わ","を","ん","゛","゜" }, | |
{ "ぁ","ぃ","ぅ","ぇ","ぉ","ゃ","ゅ","ょ","っ","ー" }, | |
}; | |
const char* ktbl[6][10] = { | |
{ "ア","イ","ウ","エ","オ","ハ","ヒ","フ","ヘ","ホ" }, | |
{ "カ","キ","ク","ケ","コ","マ","ミ","ム","メ","モ" }, | |
{ "サ","シ","ス","セ","ソ","ヤ"," ","ユ"," ","ヨ" }, | |
{ "タ","チ","ツ","テ","ト","ラ","リ","ル","レ","ロ" }, | |
{ "ナ","ニ","ヌ","ネ","ノ","ワ","ヲ","ン","゛","゜" }, | |
{ "ァ","ィ","ゥ","ェ","ォ","ャ","ュ","ョ","ッ","ー" }, | |
}; | |
const char* dtbl[][2] = { | |
{ "ば","は" }, | |
{ "び","ひ" }, | |
{ "ぶ","ふ" }, | |
{ "べ","へ" }, | |
{ "ぼ","ほ" }, | |
{ "が","か" }, | |
{ "ぎ","き" }, | |
{ "ぐ","く" }, | |
{ "げ","け" }, | |
{ "ご","こ" }, | |
{ "ざ","さ" }, | |
{ "じ","し" }, | |
{ "ず","す" }, | |
{ "ぜ","せ" }, | |
{ "ぞ","そ" }, | |
{ "だ","た" }, | |
{ "ぢ","ち" }, | |
{ "づ","つ" }, | |
{ "で","て" }, | |
{ "ど","と" }, | |
{ "ヴ","ウ" }, | |
{ "バ","ハ" }, | |
{ "ビ","ヒ" }, | |
{ "ブ","フ" }, | |
{ "ベ","ヘ" }, | |
{ "ボ","ホ" }, | |
{ "ガ","カ" }, | |
{ "ギ","キ" }, | |
{ "グ","ク" }, | |
{ "ゲ","ケ" }, | |
{ "ゴ","コ" }, | |
{ "ザ","サ" }, | |
{ "ジ","シ" }, | |
{ "ズ","ス" }, | |
{ "ゼ","セ" }, | |
{ "ゾ","ソ" }, | |
{ "ダ","タ" }, | |
{ "ヂ","チ" }, | |
{ "ヅ","ツ" }, | |
{ "デ","テ" }, | |
{ "ド","ト" }, | |
}; | |
const char* hdtbl[][2] = { | |
{ "ぱ","は" }, | |
{ "ぴ","ひ" }, | |
{ "ぷ","ふ" }, | |
{ "ぺ","へ" }, | |
{ "ぽ","ほ" }, | |
{ "パ","ハ" }, | |
{ "ピ","ヒ" }, | |
{ "プ","フ" }, | |
{ "ペ","ヘ" }, | |
{ "ポ","ホ" }, | |
}; | |
int g_x; | |
int g_y; | |
int g_layer = LAYER_H; | |
char * | |
next_char(char *s) | |
{ | |
unsigned char fb = *s; | |
if (fb >= 0x00 && fb <= 0x7F) | |
return s+1; | |
else if (fb >= 0xC2 && fb <= 0xDF) | |
return s+2; | |
else if (fb >= 0xE0 && fb <= 0xEF) | |
return s+3; | |
else if (fb >= 0xF0 && fb <= 0xF7) | |
return s+4; | |
else | |
abort(); | |
} | |
int | |
getcoords(c, px, py, tbl) | |
char* c; | |
int* px; | |
int* py; | |
char* tbl[6][10]; | |
{ | |
int clen; | |
int y, x; | |
clen = next_char(c) - c; | |
for (y = 0; y < 6; y++) | |
for (x = 0; x < 10; x++) | |
if (strlen(tbl[y][x]) == clen && | |
strncmp(tbl[y][x], c, clen) == 0) { | |
*px = x; | |
*py = y; | |
return 1; | |
} | |
return 0; | |
} | |
void | |
go(tx, ty) | |
int tx; | |
int ty; | |
{ | |
while (g_x != tx) { | |
if (g_x > tx) { | |
printf("h"); | |
g_x--; | |
} else { | |
printf("l"); | |
g_x++; | |
} | |
} | |
while (g_y != ty) { | |
if (g_y > ty) { | |
printf("k"); | |
g_y--; | |
} else { | |
printf("j"); | |
g_y++; | |
} | |
} | |
} | |
void | |
changel(int layer) | |
{ | |
if (g_layer != layer) { | |
go(0,0); | |
printf("kzj"); | |
g_layer = layer; | |
} | |
} | |
char* | |
lookup_char(ch, tbl, nrow) | |
char *ch; | |
char *tbl[][2]; | |
int nrow; | |
{ | |
int chlen = next_char(ch) - ch; | |
int i; | |
for (i = 0; i < nrow; i++) | |
if (strlen(tbl[i][0]) == chlen && | |
strncmp(tbl[i][0], ch, chlen) == 0) { | |
return tbl[i][1]; | |
} | |
return NULL; | |
} | |
int | |
main(argc, argv) | |
int argc; | |
char **argv; | |
{ | |
char *p; | |
if (argc != 2) { | |
fprintf(stderr, "Usage: torneco-name STRING\n"); | |
exit(1); | |
} | |
printf("j"); /* 「あ」に降りる */ | |
p = argv[1]; | |
while (*p != '\0') { | |
int dakuon = 0, handakuon = 0; | |
int tx, ty; | |
char *ch; | |
if (ch = lookup_char(p, dtbl, sizeof(dtbl)/sizeof(dtbl[0]))) { | |
dakuon = 1; | |
} else if (ch = lookup_char(p, hdtbl, sizeof(hdtbl)/sizeof(hdtbl[0]))) { | |
handakuon = 1; | |
} else { | |
ch = p; | |
} | |
if (getcoords(ch, &tx, &ty, htbl)) { | |
changel(LAYER_H); | |
} else if (getcoords(ch, &tx, &ty, ktbl)) { | |
changel(LAYER_K); | |
} else { | |
fprintf(stderr, "No such char: "); | |
{ | |
char *st; | |
char *end; | |
st = p; | |
end = next_char(p); | |
while (st != end) | |
fputc(*st++, stderr); | |
fputc('\n', stderr); | |
exit(1); | |
} | |
} | |
go(tx, ty); | |
printf("z"); | |
if (dakuon) { | |
go(8,4); /* ゛ */ | |
printf("z"); | |
} | |
if (handakuon) { | |
go(9,4); /* ゜ */ | |
printf("z"); | |
} | |
p = next_char(p); | |
} | |
go(8,0); /* へ */ | |
printf("kz\n"); /* おわる */ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment