Menu

[r3166]: / trunk / Src / USourceFileInfo.pas  Maximize  Restore  History

Download this file

227 lines (197 with data), 7.3 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
{
* 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) 2006-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements class that provides information about types of source code output
* that are supported.
}
unit USourceFileInfo;
interface
uses
// Project
UEncodings;
type
/// <summary>
/// Enumeration of file types that can be used for source code output.
/// </summary>
TSourceFileType = (
sfText, // plain text files
sfPascal, // pascal files (either .pas for units or .inc for include files
sfHTML, // HTML files
sfRTF // rich text files
);
type
/// <summary>
/// Record that stores information about an encoding for use by save source
/// dialog boxes.
/// </summary>
TSourceFileEncoding = record
strict private
fEncodingType: TEncodingType; // Value of EncodingType property
fDisplayName: string; // Value of DisplayName property
public
/// <summary>Sets values of properties.</summary>
constructor Create(const AEncodingType: TEncodingType;
const ADisplayName: string);
/// <summary>Type of this encoding.</summary>
property EncodingType: TEncodingType read fEncodingType;
/// <summary>Description of encoding for display in dialog box.</summary>
property DisplayName: string read fDisplayName;
end;
type
/// <summary>Array of source file encoding records.</summary>
TSourceFileEncodings = array of TSourceFileEncoding;
type
/// <summary>
/// Record that stores information about a source file type required by save
/// source dialog boxes.
/// </summary>
TSourceFileTypeInfo = record
strict private
fExtension: string; // Value of Extension property
fDisplayName: string; // Value of DisplayName property
fEncodings: TSourceFileEncodings; // Value of Encodings property
public
/// <summary>Sets values of properties.</summary>
constructor Create(const AExtension, ADisplayName: string;
const AEncodings: array of TSourceFileEncoding);
/// <summary>File extension associated with this file type.</summary>
property Extension: string read fExtension;
/// <summary>Name of file extension to display in save dialog box.
/// </summary>
property DisplayName: string read fDisplayName;
/// <summary>Encodings supported by this file type.</summary>
property Encodings: TSourceFileEncodings read fEncodings;
end;
type
/// <summary>
/// Class that provides information about types of source code output that
/// are supported.
/// </summary>
TSourceFileInfo = class(TObject)
strict private
var
/// <summary>Stores information about the different source code output
// types required by save source dialog boxes.</summary>
fFileTypeInfo: array[TSourceFileType] of TSourceFileTypeInfo;
// <summary>Value of DefaultFileName property.</summary>
fDefaultFileName: string;
/// <summary>Read accessor for FileTypeInfo property.</summary>
function GetFileTypeInfo(const FileType: TSourceFileType):
TSourceFileTypeInfo;
/// <summary>Write accessor for FileTypeInfo property.</summary>
procedure SetFileTypeInfo(const FileType: TSourceFileType;
const Info: TSourceFileTypeInfo);
/// <summary>Write access for DefaultFileName property.</summary>
/// <remarks>Converts new property value into a valid Pascal identifier if
/// necessary.</remarks>
procedure SetDefaultFileName(const Value: string);
public
/// <summary>Builds filter string for use in open / save dialog boxes from
/// descriptions and file extensions of each supported file type.</summary>
function FilterString: string;
/// <summary>Finds source file type associated with a file extension.
/// </summary>
function FileTypeFromExt(const Ext: string): TSourceFileType;
/// <summary>Array of information about each supported file type that is
/// of use to save source dialog boxes.</summary>
property FileTypeInfo[const FileType: TSourceFileType]: TSourceFileTypeInfo
read GetFileTypeInfo write SetFileTypeInfo;
/// <summary>Default source code file name.</summary>
/// <remarks>Must be a valid Pascal identifier. Invalid characters are
/// replaced by underscores.</remarks>
property DefaultFileName: string
read fDefaultFileName write SetDefaultFileName;
end;
implementation
uses
// Delphi
SysUtils, Windows {for inlining}, Character,
// Project
UStrUtils;
{ TSourceFileInfo }
function TSourceFileInfo.FileTypeFromExt(const Ext: string): TSourceFileType;
var
FT: TSourceFileType; // loops thru all source file types
begin
// Assume text file type if extension not recognised
Result := sfText;
for FT := Low(TSourceFileType) to High(TSourceFileType) do
begin
if StrSameText(Ext, fFileTypeInfo[FT].Extension) then
begin
Result := FT;
Break;
end;
end;
end;
function TSourceFileInfo.FilterString: string;
const
cFilterFmt = '%0:s (*%1:s)|*%1:s'; // format string for creating file filter
var
FT: TSourceFileType; // loops thru all source file types
begin
Result := '';
for FT := Low(TSourceFileType) to High(TSourceFileType) do
begin
if Result <> '' then
Result := Result + '|';
Result := Result + Format(
cFilterFmt, [fFileTypeInfo[FT].DisplayName, fFileTypeInfo[FT].Extension]
);
end;
end;
function TSourceFileInfo.GetFileTypeInfo(
const FileType: TSourceFileType): TSourceFileTypeInfo;
begin
Result := fFileTypeInfo[FileType];
end;
procedure TSourceFileInfo.SetDefaultFileName(const Value: string);
var
Idx: Integer; // loops through characters of filename
begin
// convert to "camel" case
fDefaultFileName := StrStripWhiteSpace(StrCapitaliseWords(Value));
// replaces invalid Pascal identifier characters with underscore
if (fDefaultFileName <> '')
and not TCharacter.IsLetter(fDefaultFileName[1])
and (fDefaultFileName[1] <> '_') then
fDefaultFileName[1] := '_';
for Idx := 2 to Length(fDefaultFileName) do
if not TCharacter.IsLetterOrDigit(fDefaultFileName[Idx])
and (fDefaultFileName[Idx] <> '_') then
fDefaultFileName[Idx] := '_';
Assert((fDefaultFileName <> '') and IsValidIdent(fDefaultFileName),
ClassName + '.SetFileName: Not a valid identifier');
end;
procedure TSourceFileInfo.SetFileTypeInfo(const FileType: TSourceFileType;
const Info: TSourceFileTypeInfo);
begin
fFileTypeInfo[FileType] := Info;
end;
{ TSourceFileTypeInfo }
constructor TSourceFileTypeInfo.Create(const AExtension, ADisplayName: string;
const AEncodings: array of TSourceFileEncoding);
var
I: Integer;
begin
fExtension := AExtension;
fDisplayName := ADisplayName;
SetLength(fEncodings, Length(AEncodings));
for I := 0 to Pred(Length(AEncodings)) do
fEncodings[I] := AEncodings[I];
end;
{ TSourceFileEncoding }
constructor TSourceFileEncoding.Create(const AEncodingType: TEncodingType;
const ADisplayName: string);
begin
fEncodingType := AEncodingType;
fDisplayName := ADisplayName;
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.