/*********************************************************************

  ui_text.c

  Functions used to retrieve text used by MAME, to aid in
  translation.

*********************************************************************/

#include "driver.h"
#include "ui_text.h"

struct lang_struct lang;

char *trans_text[UI_last_entry + 1];

/* All entries in this table must match the enum ordering in "ui_text.h" */
const char * default_text[] =
#ifdef JAPANESE
{
#ifndef MESS
	"MAME",
#else
	"MESS",
#endif

	/* copyright stuff */
	"所有していないROMをエミュレータで使用することは、著作権法によって禁じられています。",
	"「%s」を合法的に遊ぶ権利がなければESCを押してください。",
	"OKと入力すると続けます。",

	/* misc stuff */
	"メインメニューに戻る",
	"一つ前のメニューに戻る",
	"何かキーを押してください",
#ifdef JMAME
	"決定キー/ボタンを押してください",
#endif
	"ON",
	"OFF",
	"なし",
	"OK",
	"無効",
	"(なし)",
	"CPU",
	"アドレス",
	"値",
	"音源",
	"サウンド用",
	"ステレオ",
	"ベクタゲーム",
	"画面解像度",
	"テキスト",
	"音量",
	"相対",
	"全チャネル",
	"明るさ",
	"ガンマ値",
	"ベクタフリッカー",
	"ベクタ明度",
	"オーバークロック",
	"全CPU",
#ifndef MESS
	"ヒストリーが見つかりません",
#else
	"システム情報が見つかりません",
#endif
#ifdef COMMAND_LIST
	"コマンドリストが見つかりません",
#endif

	/* special characters */
	"\x11",
	"\x10",
	"\x18",
	"\x19",
	"\x1a",
	"\x1b",

	/* known problems */
#ifndef MESS
	"このゲームには以下の問題があります:",
#else
	"このシステムには以下の問題があります:",
#endif
	"色の再現が不完全です。",
	"色が完全に間違っています。",
	"ビデオエミュレーションが不完全です。",
	"サウンドエミュレーションが不完全です。",
	"サウンドが再生されません。",
	"カクテルモードの画面反転に対応していません。",
#ifndef MESS
	"※このゲームは全く動作しません※",
#else
	"※このシステムは全く動作しません※",
#endif
	"このゲームはプロテクトのためエミュレーションが不完全です。",
	"このゲームには以下の動作可能なクローンが存在します:",
	"OKと入力すると続けます",
#ifdef MESS
	"このシステムはコンピュータです: ",
	"キーボードエミュレーションが不完全です。",
#endif

	/* main menu */
	"入力設定 (標準)",
	"ディップスイッチ",
	"アナログコントロール",
	"ジョイスティックの調整",
	"インカム情報",
#ifndef MESS
	"入力設定 (このゲーム)",
	"ゲーム情報",
	"ゲームの歴史",
#ifdef JMAME // AUTO_FIRE
	"連射設定",
#endif
#ifdef COMMAND_LIST
	"コマンド一覧/操作説明",
#endif
	"ゲームをリセットする",
	"ゲームに戻る",
#else
	"入力設定 (このマシン)",
	"マシン情報",
	"マシンの使い方と歴史",
	"マシンをリセットする",
	"マシンに戻る",
	"イメージ情報",
	"ファイルマネージャ",
	"テープコントロール",
#endif
	"チート (ゲームの改造)",
	"メモリーカード",

	/* input */
	"Key/Joy 速度",
	"反転",
	"感度",

	/* stats */
	"チケット発券数",
	"コイン",
	"(固定)",

	/* memory card */
	"メモリーカードの読みこみ",
	"メモリーカードの取り出し",
	"メモリーカードの作成",
	"メモリーカードマネージャ呼び出し (リセット)",
	"メモリーカードの読みこみ失敗!",
	"読みこみ完了!",
	"メモリーカードを取り出しました!",
	"メモリーカードを作成しました!",
	"メモリーカードの作成失敗!",
	"(すでに作成済み?)",
	"内部エラーです!!",

	/* cheats */
	"チートを有効/無効にする",
	"チートを追加/編集する",
	"新規チートのサーチ開始",
	"サーチを継続する",
	"最後の結果を表示する",
	"前回の結果に戻す",
	"ウォッチポイントの設定",
	"全般のヘルプ",
	"オプション",
	"データベース再読み込み",
	"ウォッチポイント",
	"無効",
	"チート",
	"ウォッチポイント",
	"補足説明",
	"補足説明:",
	"名前",
	"説明",
	"有効化キー",
	"コード",
	"最大",
	"SET",
	"重複するチートが見つかりました: 無効にします",
	"ヘルプはまだありません",

	/* watchpoints */
	"バイト数",
	"表示形式",
	"ラベルタイプ",
	"ラベル",
	"X座標",
	"Y座標",
	"ウォッチ",

	"16進",
	"10進",
	"2進",

	/* searching */
	"ライフ (決まった値)",
	"タイマー (+/- の値)",
	"エネルギー (値の大小)",
	"状態 (ビットかフラグ)",
	"遅いけど確実 (変化したかしないか)",
	"標準のサーチ速度",
	"速い",
	"普通",
	"遅い",
	"とても遅い",
	"全てのメモリー",
	"メモリー領域を選択",
	"一致する値が見つかりました",
	"サーチが初期化されていません",
	"前回の値が保存されていません",
	"前回の値は既に復帰済みです",
	"値の復帰に成功しました",
	"値の選択",
	"すべての値を保存しました",
	"一致する値が見つかりました - リストに追加します",

#ifdef JMAME // AUTO_FIRE
	/* autofire */
	"    オフ",
	"常時オン",
	"切り替え",
	"連射間隔",
#endif

	NULL
};
#else
{
#ifndef MESS
	"MAME",
#else
	"MESS",
#endif

	/* copyright stuff */
	"Usage of emulators in conjunction with ROMs you don't own is forbidden by copyright law.",
	"IF YOU ARE NOT LEGALLY ENTITLED TO PLAY \"%s\" ON THIS EMULATOR, PRESS ESC.",
	"Otherwise, type OK to continue",

	/* misc stuff */
	"Return to Main Menu",
	"Return to Prior Menu",
	"Press Any Key",
#ifdef JMAME
	"Press Select Key/Button",
#endif
	"On",
	"Off",
	"NA",
	"OK",
	"INVALID",
	"(none)",
	"CPU",
	"Address",
	"Value",
	"Sound",
	"sound",
	"stereo",
	"Vector Game",
	"Screen Resolution",
	"Text",
	"Volume",
	"Relative",
	"ALL CHANNELS",
	"Brightness",
	"Gamma",
	"Vector Flicker",
	"Vector Intensity",
	"Overclock",
	"ALL CPUS",
#ifndef MESS
	"History not available",
#else
	"System Info not available",
#endif
#ifdef COMMAND_LIST
	"Command List not available",
#endif

	/* special characters */
	"\x11",
	"\x10",
	"\x18",
	"\x19",
	"\x1a",
	"\x1b",

	/* known problems */
#ifndef MESS
	"There are known problems with this game:",
#else
	"There are known problems with this system",
#endif
	"The colors aren't 100% accurate.",
	"The colors are completely wrong.",
	"The video emulation isn't 100% accurate.",
	"The sound emulation isn't 100% accurate.",
	"The game lacks sound.",
	"Screen flipping in cocktail mode is not supported.",
#ifndef MESS
	"THIS GAME DOESN'T WORK PROPERLY",
#else
	"THIS SYSTEM DOESN'T WORK PROPERLY",
#endif
	"The game has protection which isn't fully emulated.",
	"There are working clones of this game. They are:",
	"Type OK to continue",
#ifdef MESS
	"The emulated system is a computer: ",
	"The keyboard emulation may not be 100% accurate.",
#endif

	/* main menu */
	"Input (general)",
	"Dip Switches",
	"Analog Controls",
	"Calibrate Joysticks",
	"Bookkeeping Info",
#ifndef MESS
	"Input (this game)",
	"Game Information",
	"Game History",
#ifdef JMAME // AUTO_FIRE
	"Autofire Setting",
#endif
#ifdef COMMAND_LIST
	"Show Command List",
#endif
	"Reset Game",
	"Return to Game",
#else
	"Input (this machine)",
	"Machine Information",
	"Machine Usage & History",
	"Reset Machine",
	"Return to Machine",
	"Image Information",
	"File Manager",
	"Tape Control",
#endif
	"Cheat",
	"Memory Card",

	/* input */
	"Key/Joy Speed",
	"Reverse",
	"Sensitivity",

	/* stats */
	"Tickets dispensed",
	"Coin",
	"(locked)",

	/* memory card */
	"Load Memory Card",
	"Eject Memory Card",
	"Create Memory Card",
	"Call Memory Card Manager (RESET)",
	"Failed To Load Memory Card!",
	"Load OK!",
	"Memory Card Ejected!",
	"Memory Card Created OK!",
	"Failed To Create Memory Card!",
	"(It already exists ?)",
	"DAMN!! Internal Error!",

	/* cheats */
	"Enable/Disable a Cheat",
	"Add/Edit a Cheat",
	"Start a New Cheat Search",
	"Continue Search",
	"View Last Results",
	"Restore Previous Results",
	"Configure Watchpoints",
	"General Help",
	"Options",
	"Reload Database",
	"Watchpoint",
	"Disabled",
	"Cheats",
	"Watchpoints",
	"More Info",
	"More Info for",
	"Name",
	"Description",
	"Activation Key",
	"Code",
	"Max",
	"Set",
	"Cheat conflict found: disabling",
	"Help not available yet",

	/* watchpoints */
	"Number of bytes",
	"Display Type",
	"Label Type",
	"Label",
	"X Position",
	"Y Position",
	"Watch",

	"Hex",
	"Decimal",
	"Binary",

	/* searching */
	"Lives (or another value)",
	"Timers (+/- some value)",
	"Energy (greater or less)",
	"Status (bits or flags)",
	"Slow But Sure (changed or not)",
	"Default Search Speed",
	"Fast",
	"Medium",
	"Slow",
	"Very Slow",
	"All Memory",
	"Select Memory Areas",
	"Matches found",
	"Search not initialized",
	"No previous values saved",
	"Previous values already restored",
	"Restoration successful",
	"Select a value",
	"All values saved",
	"One match found - added to list",

#ifdef JMAME // AUTO_FIRE
	/* autofire */
	"   OFF",
	"    ON",
	"TOGGLE",
	"Autofire Delay",
#endif

	NULL
};
#endif

int uistring_init (void *langfile)
{
	/*
		TODO: This routine needs to do several things:
			- load an external font if needed
			- determine the number of characters in the font
			- deal with multibyte languages

	*/

	int i;
	char curline[255];
	char section[255] = "\0";
	char *ptr;

	/* Clear out any default strings */
	for (i = 0; i <= UI_last_entry; i ++)
	{
		trans_text[i] = NULL;
	}

	memset(&lang, 0, sizeof(lang));

	if (!langfile) return 0;

	while (osd_fgets (curline, 255, langfile) != NULL)
	{
		/* Ignore commented and blank lines */
		if (curline[0] == ';') continue;
		if (curline[0] == '\n') continue;

		if (curline[0] == '[')
		{
			ptr = strtok (&curline[1], "]");
			/* Found a section, indicate as such */
			strcpy (section, ptr);

			/* Skip to the next line */
			continue;
		}

		/* Parse the LangInfo section */
		if (strcmp (section, "LangInfo") == 0)
		{
			ptr = strtok (curline, "=");
			if (strcmp (ptr, "Version") == 0)
			{
				ptr = strtok (NULL, "\n");
				sscanf (ptr, "%d", &lang.version);
			}
			else if (strcmp (ptr, "Language") == 0)
			{
				ptr = strtok (NULL, "\n");
				strcpy (lang.langname, ptr);
			}
			else if (strcmp (ptr, "Author") == 0)
			{
				ptr = strtok (NULL, "\n");
				strcpy (lang.author, ptr);
			}
			else if (strcmp (ptr, "Font") == 0)
			{
				ptr = strtok (NULL, "\n");
				strcpy (lang.fontname, ptr);
			}
		}

		/* Parse the Strings section */
		if (strcmp (section, "Strings") == 0)
		{
			/* Get all text up to the first line ending */
			ptr = strtok (curline, "\n");

			/* Find a matching default string */
			for (i = 0; i < UI_last_entry; i ++)
			{
				if (strcmp (curline, default_text[i]) == 0)
				{
					char transline[255];

					/* Found a match, read next line as the translation */
					osd_fgets (transline, 255, langfile);

					/* Get all text up to the first line ending */
					ptr = strtok (transline, "\n");

					/* Allocate storage and copy the string */
					trans_text[i] = malloc (strlen(transline)+1);
					strcpy (trans_text[i], transline);

					/* Done searching */
					break;
				}
			}
		}
	}

	/* indicate success */
	return 0;
}

void uistring_shutdown (void)
{
	int i;

	/* Deallocate storage for the strings */
	for (i = 0; i <= UI_last_entry; i ++)
	{
		if (trans_text[i])
		{
			free (trans_text[i]);
			trans_text[i] = NULL;
		}
	}
}

const char * ui_getstring (int string_num)
{
	/* Try to use the language file strings first */
	if (trans_text[string_num] != NULL)
		return trans_text[string_num];
	else
		/* That failed, use the default strings */
		return default_text[string_num];
}
