home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / MIFILES.PAS < prev    next >
Pascal/Delphi Source File  |  1997-02-14  |  7KB  |  274 lines

  1. //---------------------------------------------------------------------------
  2. // Borland C++Builder
  3. // Copyright (c) 1987, 1997 Borland International Inc.  All Rights Reserved.
  4. //---------------------------------------------------------------------------
  5. // Mifiles.pas
  6. //
  7. // VCL Class Browser
  8. //---------------------------------------------------------------------------
  9. unit MIFiles;
  10.  
  11. interface
  12.  
  13. uses SysUtils, Classes;
  14.  
  15. type
  16.   TMemIniFile = class(TObject)
  17.   private
  18.     FFileName: string;
  19.     FSections: TStringList;
  20.     function AddSection(const Section: string): TStrings;
  21.   public
  22.     constructor Create(const FileName: string);
  23.     destructor Destroy; override;
  24.     procedure Clear;
  25.     procedure DeleteKey(const Section, Ident: String);
  26.     procedure EraseSection(const Section: string);
  27.     procedure GetStrings(List: TStrings);
  28.     function ReadBool(const Section, Ident: string;
  29.       Default: Boolean): Boolean;
  30.     function ReadInteger(const Section, Ident: string;
  31.       Default: Longint): Longint;
  32.     procedure ReadSection(const Section: string; Strings: TStrings);
  33.     procedure ReadSections(Strings: TStrings);
  34.     procedure ReadSectionValues(const Section: string; Strings: TStrings);
  35.     function ReadString(const Section, Ident, Default: string): string;
  36.     procedure SetStrings(List: TStrings);
  37.     procedure UpdateFile;
  38.     procedure WriteBool(const Section, Ident: string; Value: Boolean);
  39.     procedure WriteInteger(const Section, Ident: string; Value: Longint);
  40.     procedure WriteString(const Section, Ident, Value: String);
  41.     property FileName: string read FFileName;
  42.   end;
  43.  
  44. implementation
  45.  
  46. constructor TMemIniFile.Create(const FileName: string);
  47. var
  48.   List: TStringList;
  49. begin
  50.   FFileName := FileName;
  51.   FSections := TStringList.Create;
  52.   if (FileName <> '') and FileExists(FileName) then
  53.   begin
  54.     List := TStringList.Create;
  55.     try
  56.       List.LoadFromFile(FileName);
  57.       SetStrings(List);
  58.     finally
  59.       List.Free;
  60.     end;
  61.   end;
  62. end;
  63.  
  64. destructor TMemIniFile.Destroy;
  65. begin
  66.   if FSections <> nil then Clear;
  67.   FSections.Free;
  68. end;
  69.  
  70. function TMemIniFile.AddSection(const Section: string): TStrings;
  71. begin
  72.   Result := TStringList.Create;
  73.   try
  74.     FSections.AddObject(Section, Result);
  75.   except
  76.     Result.Free;
  77.   end;
  78. end;
  79.  
  80. procedure TMemIniFile.Clear;
  81. var
  82.   I: Integer;
  83. begin
  84.   for I := 0 to FSections.Count - 1 do
  85.     TStrings(FSections.Objects[I]).Free;
  86. end;
  87.  
  88. procedure TMemIniFile.DeleteKey(const Section, Ident: String);
  89. var
  90.   I, J: Integer;
  91.   Strings: TStrings;
  92. begin
  93.   I := FSections.IndexOf(Section);
  94.   if I >= 0 then
  95.   begin
  96.     Strings := TStrings(FSections.Objects[I]);
  97.     J := Strings.IndexOfName(Ident);
  98.     if J >= 0 then Strings.Delete(J);
  99.   end;
  100. end;
  101.  
  102. procedure TMemIniFile.EraseSection(const Section: string);
  103. var
  104.   I: Integer;
  105. begin
  106.   I := FSections.IndexOf(Section);
  107.   if I >= 0 then
  108.   begin
  109.     TStrings(FSections.Objects[I]).Free;
  110.     FSections.Delete(I);
  111.   end;
  112. end;
  113.  
  114. procedure TMemIniFile.GetStrings(List: TStrings);
  115. var
  116.   I, J: Integer;
  117.   Strings: TStrings;
  118. begin
  119.   List.BeginUpdate;
  120.   try
  121.     for I := 0 to FSections.Count - 1 do
  122.     begin
  123.       List.Add('[' + FSections[I] + ']');
  124.       Strings := TStrings(FSections.Objects[I]);
  125.       for J := 0 to Strings.Count - 1 do List.Add(Strings[J]);
  126.       List.Add('');
  127.     end;
  128.   finally
  129.     List.EndUpdate;
  130.   end;
  131. end;
  132.  
  133. function TMemIniFile.ReadBool(const Section, Ident: string;
  134.   Default: Boolean): Boolean;
  135. begin
  136.   Result := ReadInteger(Section, Ident, Ord(Default)) <> 0;
  137. end;
  138.  
  139. function TMemIniFile.ReadInteger(const Section, Ident: string;
  140.   Default: Longint): Longint;
  141. var
  142.   IntStr: string;
  143. begin
  144.   IntStr := ReadString(Section, Ident, '');
  145.   if (Length(IntStr) > 2) and (IntStr[1] = '0') and
  146.     ((IntStr[2] = 'X') or (IntStr[2] = 'x')) then
  147.     IntStr := '$' + Copy(IntStr, 3, Maxint);
  148.   Result := StrToIntDef(IntStr, Default);
  149. end;
  150.  
  151. procedure TMemIniFile.ReadSection(const Section: string;
  152.   Strings: TStrings);
  153. var
  154.   I, J: Integer;
  155.   SectionStrings: TStrings;
  156. begin
  157.   Strings.BeginUpdate;
  158.   try
  159.     Strings.Clear;
  160.     I := FSections.IndexOf(Section);
  161.     if I >= 0 then
  162.     begin
  163.       SectionStrings := TStrings(FSections.Objects[I]);
  164.       for J := 0 to SectionStrings.Count - 1 do
  165.         Strings.Add(SectionStrings.Names[J]);
  166.     end;
  167.   finally
  168.     Strings.EndUpdate;
  169.   end;
  170. end;
  171.  
  172. procedure TMemIniFile.ReadSections(Strings: TStrings);
  173. begin
  174.   Strings.Assign(FSections);
  175. end;
  176.  
  177. procedure TMemIniFile.ReadSectionValues(const Section: string;
  178.   Strings: TStrings);
  179. var
  180.   I: Integer;
  181. begin
  182.   Strings.BeginUpdate;
  183.   try
  184.     Strings.Clear;
  185.     I := FSections.IndexOf(Section);
  186.     if I >= 0 then Strings.Assign(TStrings(FSections.Objects[I]));
  187.   finally
  188.     Strings.EndUpdate;
  189.   end;
  190. end;
  191.  
  192. function TMemIniFile.ReadString(const Section, Ident,
  193.   Default: string): string;
  194. var
  195.   I: Integer;
  196.   Strings: TStrings;
  197. begin
  198.   I := FSections.IndexOf(Section);
  199.   if I >= 0 then
  200.   begin
  201.     Strings := TStrings(FSections.Objects[I]);
  202.     I := Strings.IndexOfName(Ident);
  203.     if I >= 0 then
  204.     begin
  205.       Result := Copy(Strings[I], Length(Ident) + 2, Maxint);
  206.       Exit;
  207.     end;
  208.   end;
  209.   Result := Default;
  210. end;
  211.  
  212. procedure TMemIniFile.SetStrings(List: TStrings);
  213. var
  214.   I: Integer;
  215.   S: string;
  216.   Strings: TStrings;
  217. begin
  218.   Clear;
  219.   Strings := nil;
  220.   for I := 0 to List.Count - 1 do
  221.   begin
  222.     S := List[I];
  223.     if (S <> '') and (S[1] <> ';') then
  224.       if (S[1] = '[') and (S[Length(S)] = ']') then
  225.         Strings := AddSection(Copy(S, 2, Length(S) - 2))
  226.       else
  227.         if Strings <> nil then Strings.Add(S);
  228.   end;
  229. end;
  230.  
  231. procedure TMemIniFile.UpdateFile;
  232. var
  233.   List: TStringList;
  234. begin
  235.   List := TStringList.Create;
  236.   try
  237.     GetStrings(List);
  238.     List.SaveToFile(FFileName);
  239.   finally
  240.     List.Free;
  241.   end;
  242. end;
  243.  
  244. procedure TMemIniFile.WriteBool(const Section, Ident: string;
  245.   Value: Boolean);
  246. const
  247.   Values: array[Boolean] of string = ('0', '1');
  248. begin
  249.   WriteString(Section, Ident, Values[Value]);
  250. end;
  251.  
  252. procedure TMemIniFile.WriteInteger(const Section, Ident: string;
  253.   Value: Longint);
  254. begin
  255.   WriteString(Section, Ident, IntToStr(Value));
  256. end;
  257.  
  258. procedure TMemIniFile.WriteString(const Section, Ident, Value: String);
  259. var
  260.   I: Integer;
  261.   S: string;
  262.   Strings: TStrings;
  263. begin
  264.   I := FSections.IndexOf(Section);
  265.   if I >= 0 then
  266.     Strings := TStrings(FSections.Objects[I]) else
  267.     Strings := AddSection(Section);
  268.   S := Ident + '=' + Value;
  269.   I := Strings.IndexOfName(Ident);
  270.   if I >= 0 then Strings[I] := S else Strings.Add(S);
  271. end;
  272.  
  273. end.
  274.