-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.Strings.Ext.pas
128 lines (108 loc) · 2.76 KB
/
JPL.Strings.Ext.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
unit JPL.Strings.Ext;
{
Jacek Pazera
http://www.pazera-software.com
}
{$I .\..\jp.inc}
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
interface
uses
SysUtils, Classes, StrUtils;
procedure SplitStrToList(LineToParse: string; var List: TStringList; DataSeparator: string = ',');
function SaveStringToFile(const FileName, FileContent: string; Encoding: TEncoding): Boolean;
function GetStringFromFile(const FileName: string; Default: string = ''): string;
function GetLineStartingWith(List: TStrings; const TextToFind: string; IgnoreCase: Boolean = True; StartIndex: integer = 0): string;
procedure ReverseStrings(List: TStrings);
implementation
function GetLineStartingWith(List: TStrings; const TextToFind: string; IgnoreCase: Boolean = True; StartIndex: integer = 0): string;
var
i: integer;
b: Boolean;
begin
Result := '';
for i := StartIndex to List.Count - 1 do
begin
if IgnoreCase then b := AnsiStartsText(TextToFind, List[i])
else b := AnsiStartsStr(TextToFind, List[i]);
if b then
begin
Result := List[i];
Break;
end;
end;
end;
function SaveStringToFile(const FileName, FileContent: string; Encoding: TEncoding): Boolean;
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Text := FileContent;
try
{$IFDEF DCC}sl.SaveToFile(FileName, Encoding);{$ENDIF}
{$IFDEF FPC}
{$IFDEF HAS_SAVE_WITH_ENCODING}
sl.SaveToFile(FileName, Encoding);
{$ELSE}
sl.SaveToFile(FileName);
{$ENDIF}
{$ENDIF}
Result := True;
except
Result := False;
end;
finally
sl.Free;
end;
end;
function GetStringFromFile(const FileName: string; Default: string = ''): string;
var
sl: TStringList;
begin
Result := '';
if not FileExists(FileName) then Exit;
sl := TStringList.Create;
try
sl.LoadFromFile(FileName);
Result := sl.Text;
finally
sl.Free;
end;
end;
procedure SplitStrToList(LineToParse: string; var List: TStringList; DataSeparator: string = ',');
var
xp: integer;
s: string;
begin
if not Assigned(List) then Exit;
xp := Pos(DataSeparator, LineToParse);
while xp > 0 do
begin
s := Trim(Copy(LineToParse, 1, xp - 1));
List.Add(s);
Delete(LineToParse, 1, xp + Length(DataSeparator) - 1);
LineToParse := Trim(LineToParse);
xp := Pos(DataSeparator, LineToParse);
end;
if LineToParse <> '' then
begin
LineToParse := Trim(LineToParse);
if LineToParse <> '' then List.Add(LineToParse);
end;
end;
procedure ReverseStrings(List: TStrings);
var
i, xCount, xInd: integer;
stemp: string;
begin
xCount := List.Count;
if xCount <= 1 then Exit;
for i := 0 to (xCount div 2) - 1 do
begin
xInd := xCount - 1 - i;
stemp := List[i];
List[i] := List[xInd];
List[xInd] := stemp;
end;
end;
end.