|
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル
"拡張IniFilesのUnitサンプル(長文)"
こんにちわ、みなさん、むむす です。
ちょっと、まだ完成度が低いので満足していませんが公開に踏み切りました。
(もうじきアップ予定のフリーウェアで使用するため)
[拡張項目]
1.Delphi標準のIniFilesでは、1024byte以上のStringのRead/Writeは
できません。そこでWindowsAPIを使って、Read/Writeの操作します。
2.String,Integer,Boolean型は有るのに浮動小数点は有りません。
そこでReadFloat/WriteFloatを作ってみました。他の開発ツールとは、
互換性は、ありませんので注意してください。
[省略項目]
1.IniFilesは、TIniFilesオブジェクトを作るので一度に複数の.iniファイル
が扱えますが、このUnitはオブジェクトでなく関数なので1つのファイル
しか扱えません。
2.Section系の関数は省略しました。
[動作確認]
とりあえず、自分のプログラムでは異常は出ていません。
エラーなどがあれば、教えてください。
[使用例]
uses
MuIniFiles; //usesに追加してください。
procedure TForm1.Button1Click(Sender: TObject);
begin
MuIniFiles.SetFileName('c:\test.ini');
MuIniFiles.SetBufferSize(8192); //8K byteまで拡張
MuIniFiles.WriteString('Section','Ident',Edit1.Text);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MuIniFiles.SetFileName('c:\test.ini');
MuIniFiles.SetBufferSize(8192); //8K byteまで拡張
Edit1.Text := MuIniFiles.ReadString('Section','Ident','Default');
end;
[ユニット ファイル]
MuIniFiles.pasで保存してください。
=====< bof >=====
unit MuIniFiles;
{$R-}
interface
uses
Windows, SysUtils;
var
dwBufferSize: dword = 1024;
strFileName: string;
function SetFileName(FileNmae: string): boolean;
function SetBufferSize(nSize: dword): boolean;
function ReadString(const Section, Ident, Default: string): string;
function WriteString(const Section, Ident, Value: string): boolean;
function ReadInteger(const Section, Ident: string; Default: Longint):
Longint;
function WriteInteger(const Section, Ident: string; Value: Longint):
boolean;
function ReadBool(const Section, Ident: string; Default: boolean):
boolean;
function WriteBool(const Section, Ident: string; Value: boolean):
boolean;
function ReadFloat(const Section, Ident: string; Default: Extended):
Extended;
function WriteFloat(const Section, Ident: string; Value: Extended):
boolean;
function DeleteKey(const Section, Ident: String): boolean;
implementation
function SetFileName(FileNmae: string): boolean;
begin
strFileName := FileNmae;
if FileExists(strFileName) = True then
begin
Result := True;
end
else
begin
strFileName := '';
Result := False;
end;
end;
function SetBufferSize(nSize: dword): boolean;
begin
dwBufferSize := nSize;
Result := True;
end;
function ReadString(const Section, Ident, Default: string): string;
var
strBuffer: string;
i: integer;
begin
strBuffer := '';
for i := 0 to dwBufferSize do
begin
strBuffer := strBuffer + ' ';
end;
windows.GetPrivateProfileString(PChar(Section), PChar(Ident),
PChar(Default), PChar(strBuffer), (dwBufferSize + 1),
PChar(strFileName));
Result := strBuffer;
end;
function WriteString(const Section, Ident, Value: string): boolean;
var
strBuffer: string;
i: integer;
begin
strBuffer := Copy(Value,1,dwBufferSize); //元のデータが長すぎ
た場合、カット
for i := Length(strBuffer) to dwBufferSize do
begin
strBuffer := strBuffer + ' ';
end;
Result := windows.WritePrivateProfileString(PChar(Section),
PChar(Ident), PChar(strBuffer), PChar(strFileName));
end;
function ReadInteger(const Section, Ident: string; Default: Longint):
Longint;
var
dwSize: dword;
strBuffer: string;
i: integer;
strDefault: string;
begin
dwSize := 16; //32bit整数で最大+2147483647なので16桁あれば十分
strBuffer := '';
for i := 0 to dwSize do
begin
strBuffer := strBuffer + ' ';
end;
strDefault := IntToStr(Default);
try
windows.GetPrivateProfileString(PChar(Section), PChar(Ident),
PChar(strDefault), PChar(strBuffer), (dwSize + 1),
PChar(strFileName));
Result := StrtoInt(strBuffer);
except
Result := Default;
end;
end;
function WriteInteger(const Section, Ident: string; Value: Longint):
boolean;
var
dwSize: dword;
strBuffer: string;
i: integer;
begin
dwSize := 16; //32bit整数で最大+2147483647なので16桁あれば十分
strBuffer := IntToStr(Value);
for i := Length(strBuffer) to dwSize do
begin
strBuffer := strBuffer + ' ';
end;
Result := windows.WritePrivateProfileString(PChar(Section),
PChar(Ident), PChar(strBuffer), PChar(strFileName));
end;
function ReadBool(const Section, Ident: string; Default: boolean):
boolean;
var
dwSize: dword;
strBuffer: string;
strDefault: string;
i: integer;
begin
dwSize := 1; //1桁あれば十分
if Default = False then
begin
strDefault := '0';
end
else
begin
strDefault := '1';
end;
strBuffer := '';
for i := 0 to dwSize do
begin
strBuffer := strBuffer + ' ';
end;
try
windows.GetPrivateProfileString(PChar(Section), PChar(Ident),
PChar(strDefault), PChar(strBuffer), (dwSize + 1),
PChar(strFileName));
strBuffer := Copy(strBuffer,1,1); //これを行わないとstrBuffer =
'0'が成立しない
if strBuffer = '0' then
begin
Result := False;
end
else
begin
Result := True;
end;
except
Result := Default;
end;
end;
function WriteBool(const Section, Ident: string; Value: boolean):
boolean;
var
strBuffer: string;
begin
if Value = False then
begin
strBuffer := '0';
end
else
begin
strBuffer := '1';
end;
Result := windows.WritePrivateProfileString(PChar(Section),
PChar(Ident), PChar(strBuffer), PChar(strFileName));
end;
function ReadFloat(const Section, Ident: string; Default: Extended):
Extended;
var
dwSize: dword;
strBuffer: string;
i: integer;
strDefault: string;
begin
dwSize := 64; //64桁あれば、大丈夫かな?
strBuffer := '';
for i := 0 to dwSize do
begin
strBuffer := strBuffer + ' ';
end;
strDefault := FloatToStr(Default);
try
windows.GetPrivateProfileString(PChar(Section), PChar(Ident),
PChar(strDefault), PChar(strBuffer), (dwSize + 1),
PChar(strFileName));
Result := StrToFloat(strBuffer);
except
Result := Default;
end;
end;
function WriteFloat(const Section, Ident: string; Value: Extended):
boolean;
var
dwSize: dword;
strBuffer: string;
i: integer;
begin
dwSize := 64; //64桁あれば、大丈夫かな?
strBuffer := FloatToStr(Value);
for i := Length(strBuffer) to dwSize do
begin
strBuffer := strBuffer + ' ';
end;
Result := windows.WritePrivateProfileString(PChar(Section),
PChar(Ident), PChar(strBuffer), PChar(strFileName));
end;
function DeleteKey(const Section, Ident: String): boolean;
begin
Result := windows.WritePrivateProfileString(PChar(Section),
PChar(Ident), nil, PChar(strFileName));
end;
end.
=====< eof >=====
Original document by むすす 氏 ID:(BYH12101)
ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。
Copyright 1996-2002 Delphi Users' Forum
|