home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 April / Chip_1997-04_cd.bin / prezent / cb / data.z / INIFILES.PAS < prev    next >
Pascal/Delphi Source File  |  1997-01-16  |  5KB  |  186 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Delphi Visual Component Library                 }
  5. {                                                       }
  6. {       Copyright (c) 1995,96 Borland International     }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit IniFiles;
  11.  
  12. {$R-}
  13.  
  14. interface
  15.  
  16. uses Windows, Classes;
  17.  
  18. type
  19.   TIniFile = class(TObject)
  20.   private
  21.     FFileName: string;
  22.   public
  23.     constructor Create(const FileName: string);
  24.     function ReadString(const Section, Ident, Default: string): string;
  25.     procedure WriteString(const Section, Ident, Value: String);
  26.     function ReadInteger(const Section, Ident: string;
  27.       Default: Longint): Longint;
  28.     procedure WriteInteger(const Section, Ident: string; Value: Longint);
  29.     function ReadBool(const Section, Ident: string;
  30.       Default: Boolean): Boolean;
  31.     procedure WriteBool(const Section, Ident: string; Value: Boolean);
  32.     procedure ReadSection(const Section: string; Strings: TStrings);
  33.     procedure ReadSections(Strings: TStrings);
  34.     procedure ReadSectionValues(const Section: string; Strings: TStrings);
  35.     procedure EraseSection(const Section: string);
  36.     procedure DeleteKey(const Section, Ident: String);
  37.     property FileName: string read FFileName;
  38.   end;
  39.  
  40. implementation
  41.  
  42. uses SysUtils, Consts;
  43.  
  44. constructor TIniFile.Create(const FileName: string);
  45. begin
  46.   FFileName := FileName;
  47. end;
  48.  
  49. function TIniFile.ReadString(const Section, Ident, Default: string): string;
  50. var
  51.   Buffer: array[0..1023] of Char;
  52. begin
  53.   SetString(Result, Buffer, GetPrivateProfileString(PChar(Section),
  54.     PChar(Ident), PChar(Default), Buffer, SizeOf(Buffer), PChar(FFileName)));
  55. end;
  56.  
  57. procedure TIniFile.WriteString(const Section, Ident, Value: string);
  58. begin
  59.   if not WritePrivateProfileString(PChar(Section), PChar(Ident),
  60.     PChar(Value), PChar(FFileName)) then
  61.     raise Exception.CreateResFmt(SIniFileWriteError, [FileName]);
  62. end;
  63.  
  64. function TIniFile.ReadInteger(const Section, Ident: string;
  65.   Default: Longint): Longint;
  66. var
  67.   IntStr: string;
  68. begin
  69.   IntStr := ReadString(Section, Ident, '');
  70.   if (Length(IntStr) > 2) and (IntStr[1] = '0') and
  71.     ((IntStr[2] = 'X') or (IntStr[2] = 'x')) then
  72.     IntStr := '$' + Copy(IntStr, 3, Maxint);
  73.   Result := StrToIntDef(IntStr, Default);
  74. end;
  75.  
  76. procedure TIniFile.WriteInteger(const Section, Ident: string; Value: Longint);
  77. begin
  78.   WriteString(Section, Ident, IntToStr(Value));
  79. end;
  80.  
  81. function TIniFile.ReadBool(const Section, Ident: string;
  82.   Default: Boolean): Boolean;
  83. begin
  84.   Result := ReadInteger(Section, Ident, Ord(Default)) <> 0;
  85. end;
  86.  
  87. procedure TIniFile.WriteBool(const Section, Ident: string; Value: Boolean);
  88. const
  89.   Values: array[Boolean] of string = ('0', '1');
  90. begin
  91.   WriteString(Section, Ident, Values[Value]);
  92. end;
  93.  
  94. procedure TIniFile.ReadSections(Strings: TStrings);
  95. const
  96.   BufSize = 8192;
  97. var
  98.   Buffer, P: PChar;
  99. begin
  100.   GetMem(Buffer, BufSize);
  101.   try
  102.     Strings.BeginUpdate;
  103.     try
  104.       Strings.Clear;
  105.       if GetPrivateProfileString(nil, nil, nil, Buffer, BufSize,
  106.         PChar(FFileName)) <> 0 then
  107.       begin
  108.         P := Buffer;
  109.         while P^ <> #0 do
  110.         begin
  111.           Strings.Add(P);
  112.           Inc(P, StrLen(P) + 1);
  113.         end;
  114.       end;
  115.     finally
  116.       Strings.EndUpdate;
  117.     end;
  118.   finally
  119.     FreeMem(Buffer, BufSize);
  120.   end;
  121. end;
  122.  
  123. procedure TIniFile.ReadSection(const Section: string; Strings: TStrings);
  124. const
  125.   BufSize = 8192;
  126. var
  127.   Buffer, P: PChar;
  128. begin
  129.   GetMem(Buffer, BufSize);
  130.   try
  131.     Strings.BeginUpdate;
  132.     try
  133.       Strings.Clear;
  134.       if GetPrivateProfileString(PChar(Section), nil, nil, Buffer, BufSize,
  135.         PChar(FFileName)) <> 0 then
  136.       begin
  137.         P := Buffer;
  138.         while P^ <> #0 do
  139.         begin
  140.           Strings.Add(P);
  141.           Inc(P, StrLen(P) + 1);
  142.         end;
  143.       end;
  144.     finally
  145.       Strings.EndUpdate;
  146.     end;
  147.   finally
  148.     FreeMem(Buffer, BufSize);
  149.   end;
  150. end;
  151.  
  152. procedure TIniFile.ReadSectionValues(const Section: string; Strings: TStrings);
  153. var
  154.   KeyList: TStringList;
  155.   I: Integer;
  156. begin
  157.   KeyList := TStringList.Create;
  158.   try
  159.     ReadSection(Section, KeyList);
  160.     Strings.BeginUpdate;
  161.     try
  162.       for I := 0 to KeyList.Count - 1 do
  163.         Strings.Values[KeyList[I]] := ReadString(Section, KeyList[I], '');
  164.     finally
  165.       Strings.EndUpdate;
  166.     end;
  167.   finally
  168.     KeyList.Free;
  169.   end;
  170. end;
  171.  
  172. procedure TIniFile.EraseSection(const Section: string);
  173. begin
  174.   if not WritePrivateProfileString(PChar(Section), nil, nil,
  175.     PChar(FFileName)) then
  176.     raise Exception.CreateResFmt(SIniFileWriteError, [FileName]);
  177. end;
  178.  
  179. procedure TIniFile.DeleteKey(const Section, Ident: String);
  180. begin
  181.   WritePrivateProfileString(PChar(Section), PChar(Ident), nil,
  182.      PChar(FFileName));
  183. end;
  184.  
  185. end.
  186.