Menu

[r1173]: / branches / v4-dev / Src / UIOUtils.pas  Maximize  Restore  History

Download this file

194 lines (168 with data), 5.8 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
{
* UIOUtils.pas
*
* Provides a container for assisting with common file operations.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UIOUtils.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
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;
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>
/// 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;
end;
type
/// <summary>Class of exception raised by UIOUtils code.</summary>
EIOUtils = class(Exception);
implementation
uses
// Delphi
IOUtils;
resourcestring
// Error messages
sBadBOM = 'Preamble of file %s does not match expected encoding';
{ TFileIO }
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 function TFileIO.ReadAllBytes(const FileName: string): TBytes;
begin
// using TFile is OK here: no encodings involved
Result := TFile.ReadAllBytes(FileName);
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 procedure TFileIO.WriteAllBytes(const FileName: string;
const Bytes: TBytes);
begin
// using TFile is OK here: no encodings involved
TFile.WriteAllBytes(FileName, Bytes);
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.