All pastes #1396162 Raw Edit

gocha

public c v1 · immutable
#1396162 ·published 2009-04-19 01:29 UTC
rendered paste body
/** * SNES Madou Monogatari - Text Converter. */#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>#include <fcntl.h>#include <io.h>#include <assert.h>#ifndef _setmode#define _setmode setmode#endif#ifndef countof#define countof(a) (sizeof(a)/sizeof(a[0]))#endif/** application entrypoint */int main(int argc, char *argv[]) {	char *cmdName = argv[0];	int argi;	int c, d;	// display about	fprintf(stderr, "SNES Madou Monogatari - Text Converter.\n");	fprintf(stderr, "converts raw text from stdin, then output converted text to stdout.\n");	fprintf(stderr, "\n");	// skip command name	argc--, argv++;	// set stdin to binary mode	if (_setmode(_fileno(stdin), _O_BINARY) == -1) {		fprintf(stderr, "Error: stdin couldn't be opened in binary mode.\n");		return EXIT_FAILURE;	}	// convert it until die	c = fgetc(stdin);	while (c != EOF) {		/* [Special bytes]		 * \xf9 = newline		 * \xfb xx = double-byte character (=\x01xx)		 * \xfc xx = set text color to xx		 * \xfe = newpage		 */		if (c == 0xfb) { // double-byte char			if ((d = fgetc(stdin)) == EOF)				break;			c = 0x100 + d;		}		if (c == 0xf8) {			if ((d = fgetc(stdin)) == EOF)				break;			printf("<confirm %02xh>", d);		}		else if (c == 0xf9) // newline			putchar('\n');		else if (c == 0xfa) { // wait			if ((d = fgetc(stdin)) == EOF)				break;			printf("<wait %d>", d);		}		else if (c == 0xfc) { // color			const char *colorName[] = {				"white",    // RGB15(31, 31, 31)				"cyan",     // RGB15(16, 31, 31)				"magenta",  // RGB15(31, 16, 31)				"green",    // RGB15(16, 31, 16)			};			if ((d = fgetc(stdin)) == EOF)				break;			if (d < countof(colorName))				printf("<color %s>", colorName[d]);			else				printf("<color %d>", d);		}		else if (c == 0xfd) // sound mode			printf("<sound>\n");		else if (c == 0xfe) // newpage			printf("<clear>\n");		else if (c == 0xff) // end of text			printf("<end>\n");		else if (c <= 0xf7 || c >= 0x100) { // char			const char hanamaruCharTable[] = " 0123456789、。.・ー~()ABCDEFGHIJKLMNOPQRSTUVWXYZrぁあいぃうぅえぇおぉかがきぎくぐけげこごさざしじすずせぜそぞただちつっづてでとどなにぬねのはばぱひびぴふぶぷへべぺほぼぽまみむめもやゃゆゅよょらりるれろわをんァアィイウエオカガキギクグケゲコサザシジスズセソゾタダチッツテデトドナニヌネノハバパヒビピフブプベペホポボマミムメモヤャユュヨョラリルレロワン「」!?体力少回復魔導酒水晶何時活能天草攻撃上球法光々起旅使呪文必大打用元気指輪炎美行電生世歩薬度金杖強<%¥^&@#$手紙宝地図玉王形石花森虫卵墓場集的葉認定書幼稚園卒試験塔死谷部色室奥物足滝耳狼近道母休達家今友中入閉走前村若男言知合困悪人父有名向日子教青赤屋畑緑昔黄井戸白巨城泣配長先話信苦年切分動注意守勉発高主『』笛反応弱戦銀読開好竜目絵不思議師心格通取引売闘別方仙山下場所一数女助君本見聞間証空神出番門買北南東西肉軍団町相記ォ↑→↓←会員全自重私結婚者帰密児像魚約立楽許揺様歌精遺殿岸同消彼/倍早期実問明最字事典席願叶印乱質秘岸池跡観川袋歌声老ゼ顔良客匹務ゴ音族双岩妖 闇紫湖眼霊三十聖木火剣士ェ商改 辞          ";			char s[3] = { '\0' };			assert(c <= 0x1ff);			strncpy(s, &hanamaruCharTable[c*2], 2); // FIXME: offset might depend on charset			printf("%s", s);		}		else // unknown			printf("\\x%02x", c);		if (c == 0xff)			while (c != EOF && c == 0xff)				c = fgetc(stdin); // compress them for certain reasons		else			c = fgetc(stdin);	}	return EXIT_SUCCESS;}