〜 ファイル 〜
INIファイルのデータを取得 (要KERNEL32.DLL)

「*.ini」ファイル内情報を取得するサンプルです。
INIファイルとはWindowsフォルダに入っているwin.iniファイル等と同じものであり、
拡張子こそ通常のテキストファイル「*.txt」とは違いますが中身はそれと同じですので、
指定セクション位置までinstr命令または関数でオフセットをずらした上で、
次のセクションが来るまでの間にある指定キーを探して、それに対応するデータを取得する方法も良いですが、
WinAPIを用いて取得する場合、GetPrivateProfileString関数というものを使用します。
尚、INIの基本書式として、下記のようになっています。

[セクション1] キー1 = データ1 キー2 = データ2 キー3 = データ3   :   : [セクション2] キー1 = データ1 キー2 = データ2 キー3 = データ3   :   :

setini 設定情報配列
設定情報配列INIファイルに設定する情報を文字列型配列で指定する。
各設定要素は、保存ファイル(=0)、セクション(=1)、キー(=2)、値(=3)とする。
尚、設定に失敗したら、statに0が、成功なら1が返る。

getini 取得情報配列
取得情報配列取得対象INIファイル情報を文字列型配列で指定する。
各設定要素は、取得ファイル(=0)、セクション(=1)、キー(=2)、非存在時文字列(=3)とする。
尚、取得テキストはrefstrに最大256バイト代入される。

 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
	ll_libload kernel, "kernel32.dll"
	ll_getproc WritePrivateProfileString, "WritePrivateProfileStringA", kernel
	ll_getproc GetPrivateProfileString, "GetPrivateProfileStringA", kernel

#module
#deffunc setini val
	mref info, 56
	mref stt, 64
	ll_getptr info.0 : ll_ret prm.3
	ll_getptr info.1 : ll_ret prm.0
	ll_getptr info.2 : ll_ret prm.1
	ll_getptr info.3 : ll_ret prm.2
	ll_callfunc prm, 4, WritePrivateProfileString@
	ll_ret prm : stt = prm
	return

#deffunc getini val
	mref info, 56
	mref rstr, 65
	prm.4 = 256
	ll_getptr info.0 : ll_ret prm.5
	ll_getptr info.1 : ll_ret prm.0
	ll_getptr info.2 : ll_ret prm.1
	ll_getptr info.3 : ll_ret prm.2
	ll_getptr rstr   : ll_ret prm.3
	ll_callfunc prm, 6, GetPrivateProfileString@
	return
#global

	// INIファイル作成
	sdim string, 256, 4
	dummy = 3, 5, 3, 4 // section総数, section1のkey数, section2のkey数, section3のkey数
	fname = curdir + "\\" + "hspbc_tips.ini"
	repeat dummy
		i = cnt + 1, cnt
		repeat dummy.i
			string = fname, "Section" + i.1, "Key" + cnt, "Data" + cnt
			setini string
			if stat = 0 {
				dialog "一時INIファイル作成に失敗しました"
				end
			}
			loop
	loop
	// INIファイル構成
	pos  10, 10 : mes "セクション"
	pos 100, 10 : combox index.0, 100, "0\n1\n2"
	pos  10, 40 : mes "キー"
	pos 100, 40 : combox index.2, 100, ""
	pos 100, 70 : button "読込", *getvalue
	gosub *change

*main
	wait 5
	if index ! index.1 : gosub *change
	goto *main

*change
	index.1 = index
	keyinfo = ""
	i = index + 1
	repeat dummy.i
		if keyinfo ! "" : keyinfo += "\n"
		keyinfo += "" + cnt
	loop
	objprm 1, keyinfo
	index.2 = 0
	objprm 1, index.2
	return

*getvalue
	string = fname, "Section" + index, "Key" + index.2, "存在しません"
	getini string
	dialog refstr
	goto *main

成功フラグ = setini(保存ファイル, セクション, キー, 値)
成功フラグ設定失敗(=0)か成功(=1)かのフラグ値受取先を指定する。
保存ファイル保存対象INIファイルを指定する。
セクションINIファイルに設定するセクションを指定する。
キーINIファイルに設定するキーを指定する。
INIファイルに設定する値を指定する。

getini 取得ファイル, セクション, キー, 非存在時文字列, 取得サイズ
取得ファイル取得対象INIファイルを指定する。
セクションINIファイルの取得するセクションを指定する。
キーINIファイルの取得するキーを指定する。
非存在時文字列対象キーが存在せずに取得できなかった場合に返すテキストを指定する。
取得サイズ取得する変数のサイズを指定する。

 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
#uselib "kernel32.dll"
#cfunc global WritePrivateProfileString "WritePrivateProfileStringA" sptr, sptr, sptr, sptr
#func  global GetPrivateProfileString "GetPrivateProfileStringA" sptr, sptr, sptr, var, int, sptr

#module
#defcfunc setini str file, str section, str key, str value, local s
	s = section, key, value, file
	return WritePrivateProfileString(s.0, s.1, s.2, s.3)

#defcfunc getini str file, str section, str key, str defvalue, int maxsize, local s
	sdim s, maxsize, 5
	s = section, key, defvalue, "", file
	GetPrivateProfileString s.0, s.1, s.2, s.3, maxsize, s.4
	return s.3
#global

	// INIファイル作成
	dummy = 3, 5, 3, 4 // section総数, section1のkey数, section2のkey数, section3のkey数
	fname = dir_cur + "\\" + "hspbc_tips.ini"
	repeat dummy
		i = cnt
		repeat dummy(i + 1)
			if setini(fname, "Section" + i, "Key" + cnt, "Data" + cnt) = 0 {
				dialog "一時INIファイル作成に失敗しました"
				end
			}
			loop
	loop
	// INIファイル構成
	pos  10, 10 : mes "セクション"
	pos 100, 10 : combox index.0, 100, "0\n1\n2"
	pos  10, 40 : mes "キー"
	pos 100, 40 : combox index.2, 100, ""
	pos 100, 70 : button gosub "読込", *getvalue
	gosub *change

*main
	wait 5
	if index ! index.1 : gosub *change
	goto *main

*change
	index.1 = index
	keyinfo = ""
	repeat dummy(index + 1)
		if keyinfo ! "" : keyinfo += "\n"
		keyinfo += str(cnt)
	loop
	objprm 1, keyinfo
	index.2 = 0
	objprm 1, index.2
	return

*getvalue
	dialog getini(fname, "Section" + index, "Key" + index.2, "存在しません", 16)
	return