📄 stinistm.pas
字号:
procedure TStIniStream.ReadSections(Strings: TStrings);
var
i : Integer;
begin
if not Assigned(Strings) then Exit;
{ omit the pseudo section }
for i := 1 to Pred(FSections.Count) do
Strings.Add(Trim(FSections[i])); {!!.02}
end;
procedure TStIniStream.ReadSection(const Section : AnsiString;
Strings: TStrings);
{ return Name strings for all entries in requested section }
var
SecStrs : TStringList;
i : Integer;
LineVal, Name : AnsiString;
begin
if not Assigned(Strings) then Exit;
SecStrs := TStringList.Create;
try
// ReadSection(Section, SecStrs);
{!!.02 - Rewritten }
Strings.Clear;
{ locate section }
GotoSection(Section);
{ retrieve section contents, comments, blank lines and all }
GetSecStrings(SecStrs);
{ iterate section lines looking for entries }
for i := 0 to Pred(SecStrs.Count) do begin
LineVal := SecStrs[i];
if (Trim(LineVal) <> '') and (Trim(LineVal[1]) <> ';') and (Pos('=', LineVal) > 0) then begin
{ not empty and not a comment and at least superficially resembles a
<Name>=<Value> pair }
SplitNameValue(LineVal, Name, LineVal);
Strings.Add(Trim(Name));
end;
end;
// for i := 0 to Pred(SecStrs.Count) do
// Strings.Add(SecStrs.Names[i]);
{!!.02 - Rewritten End }
finally
SecStrs.Free;
end;
end;
function TStIniStream.ReadString(const Section, Ident,
Default : AnsiString) : AnsiString;
{
return a particular string selected by Ident from Section
if empty or doesn't exist, return Default
}
var
SecStrs : TStringList;
begin
SecStrs := TStringList.Create;
try
ReadSectionValues(Section, SecStrs); {!!.04}
Result := SecStrs.Values[Ident];
if Result = '' then
Result := Default;
finally
SecStrs.Free;
end;
end;
function TStIniStream.SectionExists(const Section : AnsiString): Boolean;
{ returns True if Section exists in section list, False otherwise }
begin
Result := FSections.IndexOf(Section) > -1;
end;
procedure TStIniStream.UpdateSections;
{ refresh Sections list }
var
i : Integer;
Line : AnsiString;
begin
i := 0;
FSections.Clear;
FAnsiStream.SeekLine(0);
{ pseudo section to account for any comments or whitespace prior to first
real section in data }
FSections.AddObject('[]', TObject(0));
{ iterate data looking for section headers: '[blah]' }
while not FAnsiStream.AtEndOfStream do begin
Line := Trim(FAnsiStream.ReadLine);
{ if it looks like a header }
if IsHeader(Line) then
{ add it to the list with a line index }
FSections.AddObject(Copy(Line, 2, Length(Line) - 2), TObject(i));
{ go to next line }
Inc(i);
end;
end;
function TStIniStream.ValueExists(const Section, Ident : AnsiString): Boolean;
{
see if requested section contains requested Ident
implies "<Ident>=" exists in section, not that there's necessarily any
explicit Value associated, i.e. Value may be blank
}
var
SecStrs : TStringList;
i : Integer;
begin
Result := False;
SecStrs := TStringList.Create;
try
{ get section }
ReadSection(Section, SecStrs);
{ see if Ident exists in Names collection }
for i := 0 to Pred(SecStrs.Count) do
if SecStrs.Names[i] = Ident then begin
Result := True;
Break;
end;
finally
SecStrs.Free;
end;
end;
procedure TStIniStream.WriteString(const Section, Ident, Value : AnsiString);
{ write individual string value to IniStream }
var
SecStrs : TStringList;
SecIdx : Integer;
MS : TMemoryStream;
TS : TStAnsiTextStream;
i : Integer;
begin
if SectionExists(Section) then begin
SecStrs := TStringList.Create;
MS := TMemoryStream.Create;
TS := TStAnsiTextStream.Create(MS);
try
{ locate and read section }
GotoSection(Section);
GetSecStrings(SecStrs);
{ 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);
{ insert new value }
SecStrs.Add(Ident + '=' + Value);
{ 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);
finally
SecStrs.Free;
TS.Free;
MS.Free;
end;
end
else begin { no such section exists, append new one }
FAnsiStream.Seek(0, soFromEnd);
WriteSectionName(Section);
WriteValue(Ident, Value);
UpdateSections;
end;
end;
procedure TStIniStream.WriteSectionName(const Section: AnsiString);
{ write section header at current location }
begin
FAnsiStream.WriteLine('[' + Section + ']');
end;
procedure TStIniStream.WriteValue(const Key, Value: AnsiString);
{ write <Name>=<Value> pair at current location }
begin
FAnsiStream.WriteLine(Key + '=' + Value);
end;
procedure TStIniStream.WriteSection(const Section: AnsiString;
Strings: TStrings);
{ write entire section described by Strings }
var
SecStrs : TStringList;
SecIdx : Integer;
MS : TMemoryStream;
TS : TStAnsiTextStream;
i : Integer;
L : LongInt;
Name : AnsiString;
begin
if not Assigned(Strings) then Exit;
if SectionExists(Section) then begin
SecStrs := TStringList.Create;
MS := TMemoryStream.Create;
TS := TStAnsiTextStream.Create(MS);
try
{ locate and read section }
GotoSection(Section);
GetSecStrings(SecStrs);
{ 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);
{ update section }
for i := 0 to Pred(Strings.Count) do begin
Name := Strings.Names[i];
if SecStrs.IndexOfName(Name) > -1 then { entry exists, change value }
SecStrs.Values[Name] := Strings.Values[Name]
else { new entry, just append it }
SecStrs.Add(Strings[i]);
end;
{ 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);
finally
SecStrs.Free;
TS.Free;
MS.Free;
end;
end
else begin { no such section exists, append new one }
L := FAnsiStream.LineCount;
FAnsiStream.Seek(0, soFromEnd);
WriteSectionName(Section);
FSections.AddObject(Section, TObject(L+1));
for i := 0 to Pred(Strings.Count) do
FAnsiStream.WriteLine(Strings[i]);
// UpdateSections;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -