〜 ファイル 〜
読み込んだデータを独自のスクリプトとして処理

自アプリケーションで使用する設定データを読み込むやり方の応用で、
設定ファイル内の●から■までを1つのデータとして読み込んで、
そのデータ内のコマンド部を元に予め組み込んだ様々な処理に振り分けてあげれば、
データを1つずつ処理するHSPの様なインタプリタとなります。
また、設定データを読み込んで表示、編集、保存できるツールを作成すると、
HSPスクリプトエディタのような専用エディタにできるでしょう。

ココでは、サイズや色、表示内容等を設定データとして読み込んで処理するサンプルを載せておきます。
仕様を軽く説明すると、視認性の面で設定ファイルをテキストエディタでも直接確認・編集しやすいよう、
コマンドのパラメータが増減しても応用しやすく、どんなコマンドも汎用的に使えるよう1行1データです。
先頭部には1桁のコマンド値+スペース1文字、以降を「&」毎に区切ったパラメータとしています。
実際の各コマンド毎の詳細仕様についてはコチラをダウンロード後に下記スクリプトを実行してみてください。
	

getparams 格納先配列, 取得元変数
格納先配列取得元変数内容を「&」毎に区切って格納する、対象の文字列型配列変数を指定する。
取得元変数「&」毎に分割したい対象の文字列型変数を指定する。

cmdrun 対象変数
対象変数コマンドとパラメータを格納した文字列型変数を指定する。
尚、内部でgetparams命令を使用しているため、cmdrun命令より前にgetparams命令を定義すること。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#module
#deffunc getparams val, val
	mref setvar, 56 : mref data, 25
	i = 2
	repeat 4
		strlen i.2, data
		instr i.1, data, "&", i : if i.1 = -1 : i.3 = i.2 - i : else: i.3 = i.1
		strmid setvar.cnt, data, i, i.3
		if i.1 = -1 : break : else : i += i.3 + 1
	loop
	return

#deffunc cmdrun val
	mref data, 24
	sdim tmp, 64, 4 // サンプルモジュールでは64バイト×4のパラメータだけとする
	getparams tmp, data
	repeat 4
		cast = tmp.cnt : int cast : i.cnt = cast
	loop
	strmid command, data, 0, 1
	switch command
		case "1" // 画面クリア
			color i.0, i.1, i.2 : boxf
			pos 0, 0
			swbreak
		case "2" // 位置変更・改行禁止設定
			if tmp.0 ! "" : pos i.0, i.1
			if tmp.2 ! "" : prohibit = i.2
			swbreak
		case "3" // 色変更・フォントサイズ変更
			if tmp.0 ! "" : color i.0, i.1, i.2
			if tmp.3 ! "" : font "", i.3
			swbreak
		case "4" // データ表示・ウェイト
			mes tmp
			ginfo 7
			if prohibit : pos csrx + prmx, csry - prmy
			if tmp.1 ! "" : wait i.1
			swbreak
		case "5" // アプリケーション終了
			if tmp.0 ! "" : wait i
			end
	swend
	return
#global

	// サンプルデータ読み込み
	exist "script.dat"
	if strsize = -1 : dialog "設定データが存在しません" : end
	notesel buf
	noteload "script.dat"
	noteget check, 0
	if check ! "hspbc_sample_command" : dialog "設定ファイルは専用データではありません" : end
	// サンプルデータを1行ずつ解析
	notemax rows
	repeat rows - 1, 1
		noteget tmpdata, cnt
		cmdrun tmpdata
	loop

getparams 格納先配列, 取得元変数
格納先配列取得元変数内容を「&」毎に区切って格納する、対象の文字列型配列変数を指定する。
取得元変数「&」毎に分割したい対象の文字列型変数を指定する。

cmdrun 対象変数
対象変数コマンドとパラメータを格納した文字列型変数を指定する。
尚、内部でgetparams命令を使用しているため、cmdrun命令より前にgetparams命令を定義すること。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#module
#deffunc getparams array setvar, var data, local i
	i = 2
	repeat 4
		i.1 = instr(data, i, "&") : if i.1 = -1 : i.1 = strlen(data) - i
		setvar.cnt = strmid(data, i, i.1)
		if instr(data, i, "&") = -1 : break : else : i += instr(data, i, "&") + 1
	loop
	return

#deffunc cmdrun var data, local tmp, local i
	sdim tmp, 64, 4 // サンプルモジュールでは64バイト×4のパラメータだけとする
	getparams tmp, data
	switch strmid(data, 0, 1)
		case "1" // 画面クリア
			color int(tmp.0), int(tmp.1), int(tmp.2) : boxf
			pos 0, 0
			swbreak
		case "2" // 位置変更・改行禁止設定
			if tmp.0 ! "" : pos int(tmp.0), int(tmp.1)
			if tmp.2 ! "" : prohibit = int(tmp.2)
			swbreak
		case "3" // 色変更・フォントサイズ変更
			if tmp.0 ! "" : color int(tmp.0), int(tmp.1), int(tmp.2)
			if tmp.3 ! "" : font "", int(tmp.3)
			swbreak
		case "4" // データ表示・ウェイト
			mes tmp
			if prohibit : pos ginfo_cx + ginfo_mesx, ginfo_cy - ginfo_mesy
			if tmp.1 ! "" : wait int(tmp.1)
			swbreak
		case "5" // アプリケーション終了
			if tmp.0 ! "" : wait int(tmp.0)
			end
	swend
	return
#global

	// サンプルデータ読み込み
	exist "script.dat"
	if strsize = -1 : dialog "設定データが存在しません" : end
	notesel buf
	noteload "script.dat"
	noteget check, 0
	if check ! "hspbc_sample_command" : dialog "設定ファイルは専用データではありません" : end
	// サンプルデータを1行ずつ解析
	repeat notemax - 1, 1
		noteget tmpdata, cnt
		cmdrun tmpdata
	loop