Skip to content

Commit

Permalink
[m/+] UTF-8, ReadIntegerInRange, IgnoreExceptionsOnSave, Read/Write H…
Browse files Browse the repository at this point in the history
…TML color
  • Loading branch information
jackdp committed Aug 3, 2019
1 parent b56fed2 commit f95e404
Showing 1 changed file with 85 additions and 21 deletions.
106 changes: 85 additions & 21 deletions Base/JPL.IniFile.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
unit JPL.IniFile;
unit JPL.IniFile;

{
Jacek Pazera
Expand All @@ -7,14 +7,16 @@
TJPIniFile - INI file with comments support
// Co mnie napad³o, ¿eby to zrobiæ na Kolekcjach?!
// Co mnie napadło, żeby to zrobić na Kolekcjach?!
}

interface

uses
//Winapi.Windows,
System.SysUtils, System.Classes, System.Generics.Collections, Vcl.Graphics;
System.SysUtils, System.Classes, System.Generics.Collections,
Vcl.Graphics,
JPL.Strings, JPL.Conversion, JPL.Colors;

//System.ZLib, JP.Common.Procs;

Expand Down Expand Up @@ -65,13 +67,15 @@ TJPIniSectionItems = class(TCollection)

function ReadString(const Key, Default: string): string;
function ReadInteger(const Key: string; Default: integer): integer;
function ReadIntegerInRange(const Key: string; const Default, Min, Max: integer): integer;
function ReadBool(const Key: string; Default: Boolean): Boolean;
function ReadFloat(const Key: string; Default: Double): Double;
function ReadDate(const Key: string; Default: TDateTime): TDateTime;
function ReadDateTime(const Key: string; Default: TDateTime): TDateTime;
function ReadTime(const Key: string; Default: TDateTime): TDateTime;
function ReadBinaryStream(const Key: string; Value: TStream): integer;
function ReadColor(const Key: string; Default: TColor): TColor;
function ReadHtmlColor(const Key: string; const Default: TColor): TColor;
function ReadFontStyle(const Key: string; Default: TFontStyles): TFontStyles;

procedure WriteString(const Key, Value: string);
Expand All @@ -84,6 +88,7 @@ TJPIniSectionItems = class(TCollection)
procedure WriteBinaryStream(const Key: string; Value: TStream);

procedure WriteColor(const Key: string; const Value: TColor);
procedure WriteHtmlColor(const Key: string; const Value: TColor);
procedure WriteFontStyle(const Key: string; const Value: TFontStyles);

procedure WriteComment(const Value: string); overload;
Expand Down Expand Up @@ -111,13 +116,15 @@ TJPIniSection = class(TCollectionItem)

function ReadString(const Key, Default: string): string;
function ReadInteger(const Key: string; Default: integer): integer;
function ReadIntegerInRange(const Key: string; const Default, Min, Max: integer): integer;
function ReadBool(const Key: string; Default: Boolean): Boolean;
function ReadFloat(const Key: string; Default: Double): Double;
function ReadDate(const Key: string; Default: TDateTime): TDateTime;
function ReadDateTime(const Key: string; Default: TDateTime): TDateTime;
function ReadTime(const Key: string; Default: TDateTime): TDateTime;
function ReadBinaryStream(const Key: string; Value: TStream): integer;
function ReadColor(const Key: string; Default: TColor): TColor;
function ReadHtmlColor(const Key: string; const Default: TColor): TColor;
function ReadFontStyle(const Key: string; Default: TFontStyles): TFontStyles;

procedure WriteString(const Key, Value: string);
Expand All @@ -130,6 +137,7 @@ TJPIniSection = class(TCollectionItem)
procedure WriteBinaryStream(const Key: string; Value: TStream);

procedure WriteColor(const Key: string; const Value: TColor);
procedure WriteHtmlColor(const Key: string; const Value: TColor);
procedure WriteFontStyle(const Key: string; const Value: TFontStyles);

procedure WriteComment(const Value: string); overload;
Expand Down Expand Up @@ -208,6 +216,7 @@ TJPIniFile = class(TObject)
FCurrentSection: string;
FEncoding: TEncoding;
FUpdateFileOnExit: Boolean;
FIgnoreExceptionsOnSave: Boolean;
procedure LoadFile;
procedure ParseText(Source: string);
procedure SetSectionsSeparator(const Value: string);
Expand All @@ -216,9 +225,11 @@ TJPIniFile = class(TObject)
function GetText: string;
procedure SetEncoding(const Value: TEncoding);
procedure SetUpdateFileOnExit(const Value: Boolean);
procedure SetIgnoreExceptionsOnSave(const Value: Boolean);
public
constructor Create(const FileName: string); overload;
constructor Create(const FileName: string; Encoding: TEncoding); overload;
constructor Create(const FileName: string; Encoding: TEncoding; bIgnoreExceptionsOnSave: Boolean); overload;
destructor Destroy; override;

procedure Clear;
Expand Down Expand Up @@ -326,6 +337,7 @@ TJPIniFile = class(TObject)
property Text: string read GetText write SetText; // Reads/sets INI source
property Encoding: TEncoding read FEncoding write SetEncoding;
property UpdateFileOnExit: Boolean read FUpdateFileOnExit write SetUpdateFileOnExit; // if True, FileName will be saved on Destroy. Default True
property IgnoreExceptionsOnSave: Boolean read FIgnoreExceptionsOnSave write SetIgnoreExceptionsOnSave;
end;
{$endregion}

Expand Down Expand Up @@ -366,31 +378,33 @@ function StrToFontStyles(s: string): TFontStyles;

{$region ' ------------------------------------ TJPIniFile ---------------------------------------- '}

constructor TJPIniFile.Create(const FileName: string);
begin
// inherited Create;
// FFileName := FileName;
// FSectionsSeparator := #13#10;
// FSections := TJPIniSections.Create(TJPIniSection);
// FSections.SectionsSeparator := FSectionsSeparator;
// LoadFile;
Create(FileName, nil);
end;



constructor TJPIniFile.Create(const FileName: string; Encoding: TEncoding);
constructor TJPIniFile.Create(const FileName: string; Encoding: TEncoding; bIgnoreExceptionsOnSave: Boolean);
begin
inherited Create;
FEncoding := Encoding;
FFileName := FileName;
FSectionsSeparator := #13#10;
FSectionsSeparator := ENDL; //#13#10
FSections := TJPIniSections.Create(TJPIniSection);
FSections.SectionsSeparator := FSectionsSeparator;
FUpdateFileOnExit := True;
FIgnoreExceptionsOnSave := bIgnoreExceptionsOnSave;
LoadFile;
end;

constructor TJPIniFile.Create(const FileName: string);
begin
Create(FileName, nil, False);
end;

constructor TJPIniFile.Create(const FileName: string; Encoding: TEncoding);
begin
Create(FileName, Encoding, False);
end;





destructor TJPIniFile.Destroy;
begin
//FSections.Clear;
Expand Down Expand Up @@ -734,6 +748,11 @@ procedure TJPIniFile.SetEncoding(const Value: TEncoding);
FEncoding := Value;
end;

procedure TJPIniFile.SetIgnoreExceptionsOnSave(const Value: Boolean);
begin
FIgnoreExceptionsOnSave := Value;
end;

procedure TJPIniFile.SetSectionsSeparator(const Value: string);
begin
FSectionsSeparator := Value;
Expand Down Expand Up @@ -774,8 +793,15 @@ procedure TJPIniFile.SaveToFile(const FileName: string);
if FileName.Trim = '' then Exit; //raise Exception.Create('File name can not be blank!');
sl := TStringList.Create;
try

sl.Text := Text;
sl.SaveToFile(FileName, FEncoding);
if FIgnoreExceptionsOnSave then
try
sl.SaveToFile(FileName, FEncoding);
except
end
else sl.SaveToFile(FileName, FEncoding);

finally
sl.Free;
end;
Expand Down Expand Up @@ -1278,7 +1304,8 @@ function TJPIniSectionItems.ReadBinaryStream(const Key: string; Value: TStream):
dataLen := Length(Text) div 2;
SetLength(DataBytes, dataLen);
Pos := Stream.Position;
HexToBin(BytesOf(Text), 0, DataBytes, 0, dataLen);

System.Classes.HexToBin(BytesOf(Text), 0, DataBytes, 0, dataLen);
Stream.Write(DataBytes[0], dataLen);
Stream.Position := Pos;
if Value <> Stream then Value.CopyFrom(Stream, dataLen);
Expand Down Expand Up @@ -1315,6 +1342,15 @@ function TJPIniSectionItems.ReadColor(const Key: string; Default: TColor): TColo
Result := xColor;
end;

function TJPIniSectionItems.ReadHtmlColor(const Key: string; const Default: TColor): TColor;
var
s: string;
begin
s := ReadString(Key, '');
if s = '' then Exit(Default);
if not TryHtmlStrToColor(s, Result) then Result := Default;
end;

function TJPIniSectionItems.ReadDate(const Key: string; Default: TDateTime): TDateTime;
var
s: string;
Expand Down Expand Up @@ -1375,6 +1411,14 @@ function TJPIniSectionItems.ReadInteger(const Key: string; Default: integer): in
Result := StrToIntDef(s, Default);
end;

function TJPIniSectionItems.ReadIntegerInRange(const Key: string; const Default, Min, Max: integer): integer;
var
x: integer;
begin
x := ReadInteger(Key, Default);
Result := GetIntInRange(x, Min, Max);
end;

function TJPIniSectionItems.ReadString(const Key, Default: string): string;
var
IniItem: TJPIniItem;
Expand Down Expand Up @@ -1419,7 +1463,7 @@ procedure TJPIniSectionItems.WriteBinaryStream(const Key: string; Value: TStream
Stream.Position := 0;
end;
SetLength(Buffer, Stream.Size * 2);
BinToHex(TBytes(Stream.Memory), Stream.Position, Buffer, 0, Stream.Size - Stream.Position);
System.Classes.BinToHex(TBytes(Stream.Memory), Stream.Position, Buffer, 0, Stream.Size - Stream.Position);
Text := StringOf(Buffer);
finally
if Value <> Stream then Stream.Free;
Expand Down Expand Up @@ -1478,6 +1522,11 @@ procedure TJPIniSectionItems.WriteFontStyle(const Key: string; const Value: TFon
WriteString(Key, FontStylesToStr(Value));
end;

procedure TJPIniSectionItems.WriteHtmlColor(const Key: string; const Value: TColor);
begin
WriteString(Key, ColorToHtmlColorStr(Value, '#', True));
end;

procedure TJPIniSectionItems.WriteInteger(const Key: string; const Value: Integer);
begin
WriteString(Key, Value.ToString);
Expand Down Expand Up @@ -1567,6 +1616,11 @@ function TJPIniSection.ReadColor(const Key: string; Default: TColor): TColor;
Result := SectionItems.ReadColor(Key, Default);
end;

function TJPIniSection.ReadHtmlColor(const Key: string; const Default: TColor): TColor;
begin
Result := SectionItems.ReadHtmlColor(Key, Default);
end;

function TJPIniSection.ReadDate(const Key: string; Default: TDateTime): TDateTime;
begin
Result := SectionItems.ReadDate(Key, Default);
Expand All @@ -1592,6 +1646,11 @@ function TJPIniSection.ReadInteger(const Key: string; Default: integer): integer
Result := SectionItems.ReadInteger(Key, Default);
end;

function TJPIniSection.ReadIntegerInRange(const Key: string; const Default, Min, Max: integer): integer;
begin
Result := SectionItems.ReadIntegerInRange(Key, Default, Min, Max);
end;

function TJPIniSection.ReadString(const Key, Default: string): string;
begin
Result := SectionItems.ReadString(Key, Default);
Expand Down Expand Up @@ -1648,6 +1707,11 @@ procedure TJPIniSection.WriteFontStyle(const Key: string; const Value: TFontStyl
SectionItems.WriteFontStyle(Key, Value);
end;

procedure TJPIniSection.WriteHtmlColor(const Key: string; const Value: TColor);
begin
SectionItems.WriteHtmlColor(Key, Value);
end;

procedure TJPIniSection.WriteInteger(const Key: string; const Value: Integer);
begin
SectionItems.WriteInteger(Key, Value);
Expand Down

0 comments on commit f95e404

Please sign in to comment.