|
bigg
(Gast)
n/a Beiträge |
#7
Falls es dich interessiert, ich habe dazu eine Unit geschrieben, da mir die Funktionen von Borland nicht ausreichten. (nicht objektorientiert =)
Delphi-Quellcode:
unit MyIni;
interface uses Windows, SysUtils, Classes; procedure ReadSections(Ini, Sections: TStringList); function ReadSection(Ini, Section: TStringList; SectionName: String): Integer; function SectionExists(Sections: TStringList; SectionName: String): Boolean; function ValueExists(Section: TStringList; Value: String): Boolean; function IsValue(const Input, ValueName: String): Boolean; function ReadString(Section: TStringList; Value: String): String; function ReadInteger(Section: TStringList; Value: String): Integer; function ReadBool(Section: TStringList; Value: String): Boolean; procedure WriteType(Ini: TStringList; SectionName, ValueName, Value: String); procedure WriteString(Ini: TStringList; SectionName, ValueName, Value: String); procedure WriteInteger(Ini: TStringList; SectionName, ValueName: String; Value: Integer); procedure WriteBool(Ini: TStringList; SectionName, ValueName: String; Value: Boolean); function TrimIni(Ini: TStringList): TStringList; implementation //////////////////////////////////////////////////////////////////////////////// // Entfernt Tabs und Leerzeichen an Anfang und Ende eines // jeden Eintrags in einer Liste //////////////////////////////////////////////////////////////////////////////// function TrimIni(Ini: TStringList): TStringList; var i: Integer; begin Result := Ini; for i := 0 to Ini.Count -1 do begin if Ini[i] <> '' then begin Ini[i] := Trim(Ini[i]); end; end; end; //////////////////////////////////////////////////////////////////////////////// // Alle Gruppen auslesen //////////////////////////////////////////////////////////////////////////////// procedure ReadSections(Ini, Sections: TStringList); var i, p: Integer; begin Sections.Clear; for i := 0 to Ini.Count -1 do begin if Ini[i] <> '' then begin if Ini[i][1] = '[' then begin p := pos(']', Ini[i]); if p > 2 then Sections.Add(Copy(Ini[i], 1, p)); end; end; end; end; //////////////////////////////////////////////////////////////////////////////// // Alle Werte einer Section auslesen //////////////////////////////////////////////////////////////////////////////// function ReadSection(Ini, Section: TStringList; SectionName: String): Integer; var i, p: Integer; var s, sn: String; begin Section.Clear; Result := -1; if SectionName <> '' then begin sn := LowerCase(SectionName); for i := 0 to Ini.Count -1 do begin if Ini[i] <> '' then begin if Ini[i][1] = '[' then begin p := pos(']', Ini[i]); if p > 2 then begin s := LowerCase(Copy(Ini[i], 2, p - 2)); if s = sn then begin Result := i + 1; Break; end; end; end; end; end; end; // Kopiere bis zur nächsten Gruppe if Result > -1 then begin for i := Result to Ini.Count - 1 do begin if Ini[i] <> '' then begin if Ini[i][1] <> '[' then Section.Add(Ini[i]) else Break; end; end; end; end; //////////////////////////////////////////////////////////////////////////////// // Existiert eine Gruppe? //////////////////////////////////////////////////////////////////////////////// function SectionExists(Sections: TStringList; SectionName: String): Boolean; var i, p: Integer; var s, sn: String; begin Result := False; if SectionName <> '' then begin sn := LowerCase(SectionName); for i := 0 to Sections.Count -1 do begin if Sections[i] <> '' then begin if Sections[i][1] = '[' then begin p := pos(']', Sections[i]); if p > 2 then begin s := LowerCase(Copy(Sections[i], 2, p - 2)); if s = sn then begin Result := True; Break; end; end; end; end; end; end; end; //////////////////////////////////////////////////////////////////////////////// // Existiert ein Wert in einer Gruppe? //////////////////////////////////////////////////////////////////////////////// function ValueExists(Section: TStringList; Value: String): Boolean; var i: Integer; var s: String; begin Result := False; if Value <> '' then begin s := LowerCase(Value); for i := 0 to Section.Count -1 do begin if IsValue(Section[i], Value) then begin Result := True; Break; end; end; end; end; //////////////////////////////////////////////////////////////////////////////// // Ermittelt, ob in einem String ein bestimmter Wert existiert //////////////////////////////////////////////////////////////////////////////// function IsValue(const Input, ValueName: String): Boolean; var p: Integer; var s, i: String; begin Result := False; if (ValueName <> '') and (Input <> '') then begin p := Pos('=', Input); if p <> 0 then begin s := LowerCase(Trim(Copy(Input, 1, p -1))); if s <> '' then begin i := LowerCase(ValueName); if s = i then Result := True; end; end; end; end; //////////////////////////////////////////////////////////////////////////////// // Schreibt einen Wert in eine Ini // Existiert die Gruppe nicht, wird sie angelegt //////////////////////////////////////////////////////////////////////////////// procedure WriteType(Ini: TStringList; SectionName, ValueName, Value: String); var i, p, f: Integer; var s, Key: String; var Sections: TStringList; begin if ValueName <> '' then begin if SectionName <> '' then begin Sections := TStringList.Create; try ReadSections(Ini, Sections); Key := ValueName + '=' + Value; if SectionExists(Sections, SectionName) then begin for i := 0 to Ini.Count -1 do begin if Ini[i] <> '' then begin if Ini[i][1] = '[' then // Gruppe suchen begin p := Pos(']', Ini[i]); if p <> 0 then begin s := LowerCase(Copy(Ini[i], 2, p -2)); if s = LowerCase(SectionName) then begin // Gruppe gefunden for f := i + 1 to Ini.Count -1 do begin if f = Ini.Count -1 then begin if IsValue(Ini[f], ValueName) then begin // Wert gefunden Ini[f] := Key; // Spalte überschreiben Break; end else Ini.Add(Key); // Wert konnte nicht gefunden werden, schreibe ihn ans Dateiende end else begin if Ini[f] <> '' then begin if Ini[f][1] <> '[' then begin if IsValue(Ini[f], ValueName) then begin // Wert innerhalb der Gruppe gefunden Ini[f] := Key; // Spalte überschreiben Break; end; end else begin // Gruppen-Ende Ini.Insert(f, Key); // Wert ans Ende der Gruppe einfügen Break; end; end; end; end; Break; end; end; end; end; end; end else // SectionExists begin // Gruppe existiert nicht Ini.Add(''); Ini.Add('[' + SectionName + ']'); Ini.Add(Key); end; finally Sections.Free; end; end; end; end; //////////////////////////////////////////////////////////////////////////////// // Schreibt einen String-Wert in eine Ini (TStringList) //////////////////////////////////////////////////////////////////////////////// procedure WriteString(Ini: TStringList; SectionName, ValueName, Value: String); begin WriteType(Ini, SectionName, ValueName, '"' + Value + '"'); end; //////////////////////////////////////////////////////////////////////////////// // Schreibt einen Integer-Wert in eine Ini (TStringList) //////////////////////////////////////////////////////////////////////////////// procedure WriteInteger(Ini: TStringList; SectionName, ValueName: String; Value: Integer); var s: String; begin try s := IntToStr(Value); except s := '0'; end; WriteType(Ini, SectionName, ValueName, s); end; //////////////////////////////////////////////////////////////////////////////// // Schreibt einen Boolean-Wert in eine Ini (TStringList) //////////////////////////////////////////////////////////////////////////////// procedure WriteBool(Ini: TStringList; SectionName, ValueName: String; Value: Boolean); var s: String; begin if Value then s := '1' else s := '0'; WriteType(Ini, SectionName, ValueName, s); end; //////////////////////////////////////////////////////////////////////////////// // Liest einen Wert aus einer Gruppe aus // ReadString(Gruppe, Wert): Rückgabewert //////////////////////////////////////////////////////////////////////////////// function ReadString(Section: TStringList; Value: String): String; var i, p: Integer; var s: String; begin Result := ''; if Value <> '' then begin s := LowerCase(Value); for i := 0 to Section.Count -1 do begin p := Pos('=', Section[i]); if p <> 0 then begin if s = Trim(LowerCase(Copy(Section[i], 1, p -1))) then begin Result := Trim(Copy(Section[i], p + 1, Length(Section[i]) - p)); if Result <> '' then begin if Result[1] = '"' then begin for p := 2 to Length(Result) do begin if Result[p] = '"' then begin Result := Copy(Result, 2, p -2); Break; end; end; end; end; Break; end; end; end; end; end; //////////////////////////////////////////////////////////////////////////////// // Einen Integer-Wert einer Gruppe auslesen //////////////////////////////////////////////////////////////////////////////// function ReadInteger(Section: TStringList; Value: String): Integer; begin try Result := StrToInt(ReadString(Section, Value)); except Result := -1; end; end; procedure ReadIntegerEx(Section: TStringList; Value: String; Input: Integer); begin if ValueExists(Section, Value) then Input := ReadInteger(Section, Value); end; //////////////////////////////////////////////////////////////////////////////// // Einen Boolschen-Wert einer Gruppe auslesen //////////////////////////////////////////////////////////////////////////////// function ReadBool(Section: TStringList; Value: String): Boolean; var i: Integer; begin i := ReadInteger(Section, Value); if i = 0 then Result := False else Result := True; end; procedure ReadBoolEx(Section: TStringList; Value: String; Input: Boolean); begin if ValueExists(Section, Value) then Input := ReadBool(Section, Value); end; end. |
![]() |
Ansicht |
![]() |
![]() |
![]() |
ForumregelnEs ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.
BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus. Trackbacks are an
Pingbacks are an
Refbacks are aus
|
|
Nützliche Links |
Heutige Beiträge |
Sitemap |
Suchen |
Code-Library |
Wer ist online |
Alle Foren als gelesen markieren |
Gehe zu... |
LinkBack |
![]() |
![]() |