-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSfilesystem.pas
111 lines (93 loc) · 2.94 KB
/
JSfilesystem.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
unit JSfilesystem;
interface
uses
windows, System.IOUtils, System.strutils, System.types, System.SysUtils,
classes;
procedure StringtoFileUTF8(Filename, Line: string; const Append: boolean = false);
function BrowseFolder(folder: string): widestring;
function appendText(fn, text: string): boolean;
function LoadFileToStr(const FileName: TFileName): string;
implementation
function appendText(fn, text: string): boolean;
begin
if not FileExists(fn) then
FileCreate(fn);
TFile.AppendAllText(fn, text);
result := True;
end;
function BrowseFolder(folder: string): widestring;
var
LList: TStringDynArray;
I: Integer;
LSearchOption: TSearchOption;
begin
{ Select the search option }
// if cbDoRecursive.Checked then
// LSearchOption := TSearchOption.soAllDirectories
// else
LSearchOption := TSearchOption.soTopDirectoryOnly;
try
LList := TDirectory.GetFiles(folder, '*.*', LSearchOption);
// LList := TDirectory.GetFileSystemEntries(edtPath.Text, LSearchOption, nil);
// LList := TDirectory.GetDirectories(edtPath.Text, edtFileMask.Text, LSearchOption);
except { Catch the possible exceptions }
// MessageDlg('Incorrect path or search mask', mtError, [mbOK], 0);
Exit;
end;
Result := '[';
for I := 0 to Length(LList) - 1 do
Result := Result + '"' + ReplaceStr(LList[I], '\', '/') + '"' + ',';
delete(Result, Length(Result), 1);
Result := Result + ']';
end;
function LoadFileToStr(const FileName: TFileName): string;
var
FileStream: TFileStream;
Bytes: TBytes;
begin
Result := '';
FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
if FileStream.Size > 0 then
begin
SetLength(Bytes, FileStream.Size);
FileStream.Read(Bytes[0], FileStream.Size);
end;
Result := TEncoding.UTF8.GetString(Bytes);
finally
FileStream.Free;
end;
end;
procedure StringtoFileUTF8(Filename, Line: string; const Append: boolean = false);
var
fs: TFileStream;
preamble: TBytes;
outpututf8: RawByteString;
amode: Integer;
begin
if Append and FileExists(Filename) then
amode := fmOpenReadWrite
else
amode := fmCreate;
fs := TFileStream.Create(Filename, { mode } amode, fmShareDenyWrite);
{ sharing mode allows read during our writes }
try
{ internal Char (UTF16) codepoint, to UTF8 encoding conversion: }
outpututf8 := Utf8Encode(Line);
// this converts UnicodeString to WideString, sadly.
if (amode = fmCreate) then
begin
// preamble := TEncoding.UTF8.GetPreamble;
// fs.WriteBuffer( PAnsiChar(preamble)^, Length(preamble));
end
else
begin
fs.Seek(fs.Size, 0); { go to the end, append }
end;
// outpututf8 := outpututf8 + AnsiChar(#13) + AnsiChar(#10);
fs.WriteBuffer(PAnsiChar(outpututf8)^, Length(outpututf8));
finally
fs.Free;
end;
end;
end.