Menu

[r3207]: / trunk / Src / UIOUtils.pas  Maximize  Restore  History

Download this file

281 lines (248 with data), 8.9 kB

  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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2009-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Provides a container for assisting with common file operations.
}
unit UIOUtils;
interface
uses
// Delphi
SysUtils, Classes, Types;
type
/// <summary>
/// Container for methods that assist with common file operations.
/// </summary>
/// <remarks>
/// TFileIO is used instead of IOUtils.TFile because the assumptions TFile
/// makes about the use of byte order marks with encoded text files are not
/// compatible with the needs of this program.
/// </remarks>
TFileIO = record
strict private
class function CheckBOM(const Bytes: TBytes; const Encoding: TEncoding):
Boolean; static;
/// <summary>
/// Appends whole contents of a byte array to a stream.
/// </summary>
class procedure BytesToStream(const Bytes: TBytes; const Stream: TStream);
static;
/// <summary>
/// Copies content of a whole stream into a byte array.
/// </summary>
class function StreamToBytes(const Stream: TStream): TBytes; static;
public
/// <summary>
/// Writes all the bytes from a byte array to a file.
/// </summary>
/// <param name="FileName">string [in] Name of file.</param>
/// <param name="Bytes">TBytes [in] Array of bytes to be written to file.
/// </param>
class procedure WriteAllBytes(const FileName: string; const Bytes: TBytes);
static;
/// <summary>
/// Writes text to a file.
/// </summary>
/// <param name="FileName">string [in] Name of file.</param>
/// <param name="Content">string [in] Text to be written to file.</param>
/// <param name="Encoding">TEncoding [in] Encoding to be used for text in
/// file.</param>
/// <param name="UseBOM">Boolean [in] Flag indicating whether BOM to be
/// written to file. If Encoding has no BOM then UseBOM has no effect.
/// </param>
class procedure WriteAllText(const FileName, Content: string;
const Encoding: TEncoding; const UseBOM: Boolean = False); static;
/// <summary>
/// Writes lines of text to a text file with lines separated by CRLF.
/// </summary>
/// <param name="FileName">string [in] Name of file.</param>
/// <param name="Lines">array of string [in] Array of lines of text to be
/// written.</param>
/// <param name="Encoding">TEncoding [in] Encoding to be used by file.
/// </param>
/// <param name="UseBOM">Boolean [in] Flag indicating whether BOM to be
/// written to file. If Encoding has no BOM then UseBOM has no effect.
/// </param>
class procedure WriteAllLines(const FileName: string;
const Lines: array of string; const Encoding: TEncoding;
const UseBOM: Boolean = False); static;
/// <summary>
/// Reads all bytes from a file into a byte array.
/// </summary>
/// <param name="FileName">string [in] Name of file.</param>
/// <returns>TBytes array containing the file's contents.</returns>
class function ReadAllBytes(const FileName: string): TBytes; static;
/// <summary>
/// Reads all the text from a text file.
/// </summary>
/// <param name="FileName">string [in] Name of file.</param>
/// <param name="Encoding">TEncoding [in] Text encoding used by file.
/// </param>
/// <param name="HasBOM">Boolean [in] Flag indicating if file has a byte
/// order mark. Ignored if Encoding has no BOM.</param>
/// <returns>String containing contents of file.</returns>
/// <remarks>When HasBOM is true and Encoding has a BOM then the BOM must
/// begin the file, otherwise an exception is raised.</remarks>
class function ReadAllText(const FileName: string;
const Encoding: TEncoding; const HasBOM: Boolean = False): string; static;
/// <summary>
/// Reads all the lines of text from a text file.
/// </summary>
/// <param name="FileName">string [in] Name of file.</param>
/// <param name="Encoding">TEncoding [in] Text encoding used by file.
/// </param>
/// <param name="HasBOM">Boolean [in] Flag indicating if file has a byte
/// order mark. Ignored if Encoding has no BOM.</param>
/// <returns>TStringDynArray containing lines from file.</returns>
/// <remarks>When HasBOM is true and Encoding has a BOM then the BOM must
/// begin the file, otherwise an exception is raised.</remarks>
class function ReadAllLines(const FileName: string;
const Encoding: TEncoding; const HasBOM: Boolean = False):
TStringDynArray; static;
/// <summary>
/// Copies content of one file to another.
/// </summary>
/// <param name="SrcFileName">string [in] Name of file to be copied.
/// </param>
/// <param name="DestFileName">string [in] Name of file to receive
/// contents of file named in SrcFileName.</param>
/// <remarks>SrcFileName and DestFileName must be different. SrcFileName
/// must exist. DestFileName is overwritten if it already exists.</remarks>
class procedure CopyFile(const SrcFileName, DestFileName: string); static;
end;
type
/// <summary>Class of exception raised by UIOUtils code.</summary>
EIOUtils = class(Exception);
implementation
resourcestring
// Error messages
sBadBOM = 'Preamble of file %s does not match expected encoding';
{ TFileIO }
class procedure TFileIO.BytesToStream(const Bytes: TBytes;
const Stream: TStream);
begin
if Length(Bytes) > 0 then
Stream.WriteBuffer(Pointer(Bytes)^, Length(Bytes));
end;
class function TFileIO.CheckBOM(const Bytes: TBytes; const Encoding: TEncoding):
Boolean;
var
Preamble: TBytes;
I: Integer;
begin
Preamble := Encoding.GetPreamble;
if Length(Preamble) = 0 then
Exit(False);
if Length(Bytes) < Length(Preamble) then
Exit(False);
for I := 0 to Pred(Length(Preamble)) do
if Bytes[I] <> Preamble[I] then
Exit(False);
Result := True;
end;
class procedure TFileIO.CopyFile(const SrcFileName, DestFileName: string);
begin
TFileIO.WriteAllBytes(DestFileName, TFileIO.ReadAllBytes(SrcFileName));
end;
class function TFileIO.ReadAllBytes(const FileName: string): TBytes;
var
FS: TFileStream;
begin
FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
Result := StreamToBytes(FS);
finally
FS.Free;
end;
end;
class function TFileIO.ReadAllLines(const FileName: string;
const Encoding: TEncoding; const HasBOM: Boolean): TStringDynArray;
var
Lines: TStrings;
I: Integer;
begin
Assert(Assigned(Encoding), 'TFileIO.ReadAllLines: Encoding is nil');
Lines := TStringList.Create;
try
Lines.Text := ReadAllText(FileName, Encoding, HasBOM);
SetLength(Result, Lines.Count);
for I := 0 to Pred(Lines.Count) do
Result[I] := Lines[I];
finally
Lines.Free;
end;
end;
class function TFileIO.ReadAllText(const FileName: string;
const Encoding: TEncoding; const HasBOM: Boolean): string;
var
Content: TBytes;
SizeOfBOM: Integer;
begin
Assert(Assigned(Encoding), 'TFileIO.ReadAllBytes: Encoding is nil');
Content := ReadAllBytes(FileName);
if HasBOM then
begin
SizeOfBOM := Length(Encoding.GetPreamble);
if (SizeOfBOM > 0) and not CheckBOM(Content, Encoding) then
raise EIOUtils.CreateFmt(sBadBOM, [FileName]);
end
else
SizeOfBOM := 0;
Result := Encoding.GetString(Content, SizeOfBOM, Length(Content) - SizeOfBOM);
end;
class function TFileIO.StreamToBytes(const Stream: TStream): TBytes;
begin
Stream.Position := 0;
SetLength(Result, Stream.Size);
if Stream.Size > 0 then
Stream.ReadBuffer(Pointer(Result)^, Length(Result));
end;
class procedure TFileIO.WriteAllBytes(const FileName: string;
const Bytes: TBytes);
var
FS: TFileStream;
begin
FS := TFileStream.Create(FileName, fmCreate);
try
BytesToStream(Bytes, FS);
finally
FS.Free;
end;
end;
class procedure TFileIO.WriteAllLines(const FileName: string;
const Lines: array of string; const Encoding: TEncoding;
const UseBOM: Boolean);
var
Line: string;
SB: TStringBuilder;
begin
SB := TStringBuilder.Create;
try
for Line in Lines do
SB.AppendLine(Line);
WriteAllText(FileName, SB.ToString, Encoding, UseBOM);
finally
SB.Free;
end;
end;
class procedure TFileIO.WriteAllText(const FileName, Content: string;
const Encoding: TEncoding; const UseBOM: Boolean);
var
FS: TFileStream;
begin
FS := TFileStream.Create(FileName, fmCreate);
try
if UseBOM then
BytesToStream(Encoding.GetPreamble, FS);
BytesToStream(Encoding.GetBytes(Content), FS);
finally
FS.Free;
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.