📄 stinistm.pas
字号:
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is TurboPower SysTools
*
* The Initial Developer of the Original Code is
* TurboPower Software
*
* Portions created by the Initial Developer are Copyright (C) 1996-2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** *)
{*********************************************************}
{* SysTools: StIniStm.pas 4.03 *}
{*********************************************************}
{* SysTools: .INI file-like stream class *}
{*********************************************************}
{$include StDefine.inc}
unit StIniStm;
interface
uses
Windows, SysUtils, Classes, StStrms;
type
TStIniStream = class(TObject)
private
FAnsiStream : TStAnsiTextStream;
FSections : TStringList;
procedure GetSecStrings(Strs: TStrings);
protected
procedure GotoSection(const Section : AnsiString);
procedure UpdateSections;
procedure WriteSectionName(const Section : AnsiString);
procedure WriteValue(const Key, Value : AnsiString);
public
constructor Create(aStream : TStream);
destructor Destroy; override;
function SectionExists(const Section : AnsiString): Boolean;
function ReadString(const Section, Ident, Default : AnsiString) : AnsiString;
procedure WriteString(const Section, Ident, Value : AnsiString);
procedure WriteSection(const Section : AnsiString; Strings: TStrings);
procedure ReadSection(const Section : AnsiString; Strings: TStrings);
procedure ReadSections(Strings: TStrings);
procedure ReadSectionValues(const Section : AnsiString; Strings: TStrings);
procedure EraseSection(const Section : AnsiString);
procedure DeleteKey(const Section, Ident : AnsiString);
function ValueExists(const Section, Ident : AnsiString): Boolean;
end;
procedure SplitNameValue(const Line : string; var Name, Value : string); {!!.04}
implementation
{!!.04 - Added }
procedure SplitNameValue(const Line : string; var Name, Value : string);
var
P : Integer;
begin
P := Pos('=', Line);
if P < 1 then begin
Name := Line;
Value := '';
Exit;
end;
Name := Copy(Line, 1, P-1);
Value := Copy(Line, P+1, Length(Line) - P);
end;
{!!.04 - Added End}
function IsHeader(const AString : AnsiString) : Boolean;
{ see if passed in text looks like an .INI header }
var
Temp : AnsiString;
begin
if AString = '' then begin
Result := False;
Exit;
end;
Temp := Trim(AString);
Result := (Temp[1] = '[') and (Temp[Length(Temp)] = ']')
end;
{ TStIniStream }
constructor TStIniStream.Create(aStream: TStream);
begin
inherited Create;
FAnsiStream := TStAnsiTextStream.Create(aStream);
FSections := TStringList.Create;
FSections.Sorted := True;
FSections.Duplicates := dupIgnore;
if aStream.Size > 0 then { not an empty stream }
UpdateSections;
end;
destructor TStIniStream.Destroy;
begin
FSections.Free;
FAnsiStream.Free;
inherited Destroy;
end;
procedure TStIniStream.DeleteKey(const Section, Ident : AnsiString);
{ delete specified item from Section }
var
SecStrs : TStringList;
SecIdx : Integer;
MS : TMemoryStream;
TS : TStAnsiTextStream;
i, Idx : Integer;
begin
SecStrs := TStringList.Create;
MS := TMemoryStream.Create;
TS := TStAnsiTextStream.Create(MS);
try
{ locate and read section }
GotoSection(Section);
GetSecStrings(SecStrs);
Idx := SecStrs.IndexOfName(Ident);
if Idx > - 1 then begin
{ remove desired key }
SecStrs.Delete(Idx);
{ locate subsequent section }
SecIdx := FSections.IndexOf(Section);
if SecIdx < Pred(FSections.Count) then begin
GotoSection(FSections[SecIdx+1]);
{ copy remaining sections }
while not FAnsiStream.AtEndOfStream do
TS.WriteLine(FAnsiStream.ReadLine);
end;
{ else this is the last section }
{ seek back and truncate }
GotoSection(Section);
FAnsiStream.Size := FAnsiStream.Position;
// FAnsiStream.SetSize(FAnsiStream.Position);
{ write updated section }
WriteSectionName(Section);
for i := 0 to Pred(SecStrs.Count) do
FAnsiStream.WriteLine(SecStrs[i]);
FAnsiStream.Stream.Seek(0, soFromEnd);
{ append saved subsequent sections }
TS.SeekLine(0);
while not TS.AtEndOfStream do
FAnsiStream.WriteLine(TS.ReadLine);
end; { if Ident > -1 }
{ else the Ident doesn't exist so don't alter anything }
finally
SecStrs.Free;
TS.Free;
MS.Free;
end;
end;
procedure TStIniStream.EraseSection(const Section : AnsiString);
{ erase specified section from Ini data }
var
SecIdx : Integer;
MS : TMemoryStream;
TS : TStAnsiTextStream;
begin
MS := TMemoryStream.Create;
TS := TStAnsiTextStream.Create(MS);
{ locate section }
SecIdx := FSections.IndexOf(Section);
{ if section found }
if SectionExists(Section) then begin
try
{ if this is not the last section }
if (SecIdx < Pred(FSections.Count)) then begin
{ locate subsequent section }
GotoSection(FSections[SecIdx+1]);
{ copy remaining sections to temporary stream}
while not FAnsiStream.AtEndOfStream do
TS.WriteLine(FAnsiStream.ReadLine);
end;
{ else this is the last section }
{ locate section to delete and truncate }
GotoSection(Section);
FAnsiStream.Size := FAnsiStream.Position;
// FAnsiStream.SetSize(FAnsiStream.Position);
{ append saved subsequent sections }
TS.SeekLine(0);
while not TS.AtEndOfStream do
FAnsiStream.WriteLine(TS.ReadLine);
finally
TS.Free;
MS.Free;
end;
UpdateSections;
end;
{ else section doesn't exist, do nothing }
end;
procedure TStIniStream.GetSecStrings(Strs : TStrings);
{ read strings from a section, preserving comments and blanks }
var
LineVal : AnsiString;
begin
{ assume we're at the start of a section }
FAnsiStream.ReadLine; { skip section header }
LineVal := FAnsiStream.ReadLine;
while not (FAnsiStream.AtEndOfStream) and not (IsHeader(LineVal)) do begin
Strs.Add(LineVal); { add it to the list }
LineVal := FAnsiStream.ReadLine; { get next line }
end;
end;
procedure TStIniStream.GotoSection(const Section: AnsiString);
{ position stream to requested section header }
var
Idx : Integer;
begin
Idx := FSections.IndexOf(Section);
if Idx > -1 then
FAnsiStream.SeekLine(Integer(FSections.Objects[Idx]));
end;
procedure TStIniStream.ReadSectionValues(const Section : AnsiString;
Strings: TStrings);
{ return <Name>=<Value> pairs of requested Section in Strings }
var
Strs : TStringList;
LineVal : AnsiString;
i : Integer;
begin
if not Assigned(Strings) then Exit;
Strs := TStringList.Create;
if SectionExists(Section) then begin { section exists }
Strings.Clear;
try
{ locate section }
GotoSection(Section);
{ retrieve section contents, comments, blank lines and all }
GetSecStrings(Strs);
{ iterate section lines looking for entries }
for i := 0 to Pred(Strs.Count) do begin
LineVal := Strs[i];
if (Trim(LineVal) <> '') and (Trim(LineVal[1]) <> ';') and (Pos('=', LineVal) > 0) then {!!.02}
{ not empty and not a comment and at least superficially resembles a
<Name>=<Value> pair }
Strings.Add(Trim(LineVal)); { add it to the list } {!!.02}
end;
finally
Strs.Free;
end;
end;
{ else section doesn't exist, do nothing }
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -