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

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