Модуль для работы с ini файлами
Материал из DRKB
Модуль для работы с ini файлами[править | править код]
Вот мой модуль для работы с Ini файлами... Должен всем пригодиться...
// Модуль для работы с данными в конфигурационном файле.
// Функции упрощают использование конфиг. файлов.
// Данный материал можно изменять по Вашему усмотрению...
// При нахождении ошибок пишите на guedda@yandex.ru
unit MyIni.pas
interface
uses
IniFiles;
procedure WriteIniData(Section, Ident, Value: string);
procedure WriteIniDataInt(Section, Ident: string; Value: Integer);
procedure WriteIniDataBool(Section, Ident: string; Value: boolean);
function ReadIniData(Section, Ident: string; Default: string = ''): string;
function ReadIniDataInt(Section, Ident: string; Default: Integer = 0): Integer;
function ReadIniDataBool(Section, Ident: string; Default: boolean = false): boolean;
implementation
var
IniFile: TIniFile;
Path: string;
procedure WriteIniData(Section, Ident, Value: string);
begin
IniFile := TIniFile.Create(Path + '\config.ini');
IniFile.WriteString(Section, Ident, Value);
IniFile.Free;
end;
procedure WriteIniDataInt(Section, Ident: string; Value: Integer);
begin
IniFile := TIniFile.Create(Path + '\config.ini');
IniFile.WriteInteger(Section, Ident, Value);
IniFile.Free;
end;
procedure WriteIniDataBool(Section, Ident: string; Value: boolean);
begin
IniFile := TIniFile.Create(Path + '\config.ini');
IniFile.WriteBool(Section, Ident, Value);
IniFile.Free;
end;
function ReadIniData(Section, Ident: string; Default: string = ''): string;
begin
IniFile := TIniFile.Create(Path + '\config.ini');
Result := IniFile.ReadString(Section, Ident, Default);
IniFile.Free;
end;
function ReadIniDataInt(Section, Ident: string; Default: Integer = 0): Integer;
begin
IniFile := TIniFile.Create(Path + '\config.ini');
Result := IniFile.ReadInteger(Section, Ident, Default);
IniFile.Free;
end;
function ReadIniDataBool(Section, Ident: string; Default: Boolean = False): boolean;
begin
IniFile := TIniFile.Create(Path + '\config.ini');
Result := IniFile.ReadBool(Section, Ident, Default);
IniFile.Free;
end;
initialization
GetDir(0, Path);
end.
Author: Guedda
Source: Взято с Vingrad.ru http://forum.vingrad.ru
ID: 02310
unit USDKINIFiles;
{ From Windows Messages SDK }
interface
uses Windows, SysUtils;
type
TINIFile = class(TObject)
private
FFileName: string;
public
constructor Create(const FileName: string);
destructor Destroy; override;
function ReadString(const Section, Key, Default: string): string;
function ReadInteger(const Section, Key: string; Default: longint): longint;
function ReadBool(const Section, Key: string; Default: boolean): boolean;
function WriteString(const Section, Key, Value: string): boolean;
function WriteInteger(const Section, Key: string; Value: longint): boolean;
function WriteBool(const Section, Key: string; Value: boolean): boolean;
procedure UpdateFile;
property FileName: string read FFileName;
end;
implementation
{ TINIFile }
constructor TIniFile.Create(const FileName: string);
begin
FFileName := FileName;
end;
destructor TIniFile.Destroy;
begin
UpdateFile;
inherited Destroy;
end;
function TIniFile.ReadBool(const Section, Key: string; Default: boolean): boolean;
begin
Result := ReadInteger(Section, Key, Ord(Default)) <> 0;
end;
function TIniFile.ReadInteger(const Section, Key: string; Default: integer): longint;
var
IntStr: string;
begin
IntStr := ReadString(Section, Key, '');
if (Length(IntStr) > 2) and (IntStr[1] = '0') and
((IntStr[2] = 'X') or (IntStr[2] = 'x')) then
IntStr := '$' + Copy(IntStr, 3, MaxInt);
Result := StrToIntDef(IntStr, Default);
end;
function TIniFile.ReadString(const Section, Key, Default: string): string;
var
Buffer: array[0..2047] of char;
begin
SetString(Result, Buffer, GetPrivateProfileString(PChar(Section),
PChar(Key), PChar(Default), Buffer, SizeOf(Buffer), PChar(FFileName)));
end;
procedure TIniFile.UpdateFile;
begin
WritePrivateProfileString(nil, nil, nil, PChar(FFileName));
end;
function TIniFile.WriteBool(const Section, Key: string; Value: boolean): boolean;
const
Values: array[boolean] of string = ('0', '1');
begin
Result := WriteString(Section, Key, Values[Value]);
end;
function TIniFile.WriteInteger(const Section, Key: string; Value: integer): boolean;
begin
Result := WriteString(Section, Key, IntToStr(Value));
end;
function TIniFile.WriteString(const Section, Key, Value: string): boolean;
begin
Result := WritePrivateProfileString(PChar(Section), PChar(Key),
PChar(Value), PChar(FFileName));
end;
end.
Author: Rrader
Source: Взято с Vingrad.ru http://forum.vingrad.ru
ID: 02311