-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutextfile.pas
248 lines (188 loc) · 5.54 KB
/
utextfile.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
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
{******************************************************************************
Unit UTextFile
Usage
var
tf: CTextFile;
tfr: CTextFile;
// Writing
tf := CTextFile.Create('testcreate.txt');
tf.OpenFileWrite();
Writeln('The status of ' + tf.GetPath + ' is ' + BoolToStr(tf.GetStatus, 'OPEN', 'CLOSED'));
tf.WriteToFile('test text');
tf.WriteToFile('test text2');
tf.CloseFile();
// Reading
tfr := CTextFile.Create('testread2.txt');
tfr.OpenFileRead();
Writeln('The status of ' + tfr.GetPath + ' is ' + BoolToStr(tfr.GetStatus, 'OPEN', 'CLOSED'));
repeat
WriteLn(IntToStr(tfr.GetLineNumber()) + ': ' + tfr.ReadFromFile());
until tfr.GetEof();
tfr.CloseFile();
02 2014-11-24
01 2014-11-18
NOTE: Use AnsiStrings to support long strings (len>255)
******************************************************************************}
unit UTextFile;
{$MODE OBJFPC} // Do not forget this ever
{$M+}
{$H+}
interface
uses
SysUtils;
//USupportLibrary;
type
CTextFile = Class
private
path : string;
isOpen : boolean;
textFile : Text;
lineCount : integer;
alreadyExists: boolean;
public
//function ReadFromFile() : string; // v01
constructor CreateTheFile(pathNew : string);
function AppendingToFile(): boolean;
function GetLineNumber() : integer;
function GetEof() : boolean;
function GetPath() : string;
function GetStatus() : boolean;
function ReadFromFile() : AnsiString; // v02
procedure CloseTheFile();
procedure DeleteTheFile();
procedure RenameTheFile(newName: AnsiString);
procedure OpenFileForRead();
procedure OpenFileForWrite();
procedure WriteToFile(line : AnsiString);
function DoesFileExists(): boolean;
end;
Implementation
constructor CTextFile.CreateTheFile(pathNew : string);
begin
path := pathNew;
lineCount := 0;
alreadyExists := false;
end; // of constructor CTextFile.Create
procedure CTextFile.OpenFileForWrite();
var
dirs: string;
begin
//WriteLn('CTextFile.OpenFileWrite(): ' + path);
// Get the folder of the pat
dirs := ExtractFilePath(path);
//WriteLn(dirs);
if Length(dirs) > 0 then
begin
// Create a folder tree when needed.
// Windows: MakeFolderTree(path);
if ForceDirectories(dirs) = false then
WriteLn('UTEXTFILE: OpenFileRead(): Could not create the directory structure for ', path);
end;
{$I+}
Assign(textFile, path);
if FileExists(path) = false then
begin
// file doesn't exist, create it
Rewrite(textFile);
alreadyExists := false; // Read value with AppendingToFile()
isOpen := True;
end
else
begin
// file does exist, open it.
Append(textFile);
alreadyExists := true; // Read value with AppendingToFile()
isOpen := True;
end;
end; // of procedure CTextFile.OpenFileWrite()
procedure CTextFile.OpenFileForRead();
begin
{$I+}
Assign(textFile, path);
try
Reset(textFile);
lineCount := 0;
except
on E: EInOutError do
begin
WriteLn('UTEXTFILE: OpenFileRead(): File handling occurred. Details: ' + E.ClassName + '/' + E.Message);
end;
end;
isOpen := True;
end; // of procedure CTextFile.OpenFileRead()
procedure CTextFile.CloseTheFile();
begin
isOpen := False;
Close(textFile);
end; // procedure CTextFile.CloseFile
function CTextFile.AppendingToFile(): boolean;
//
// Returns
// True when the file already exists
// False when the file is new
begin
AppendingToFile := alreadyExists;
end; // of function CTextFile.AppendingToFile
function CTextFile.GetPath() : string;
begin
GetPath := path;
end; // function CTextFile.GetPath
procedure CTextFile.DeleteTheFile();
begin
if isOpen = True then
begin
// Close the file when it still open.
Close(textFile);
end;
sysutils.DeleteFile(path);
end; // procedure CTextFile.DeleteFile
procedure CTextFile.RenameTheFile(newName: AnsiString);
{
RenameTheFile()
Rename the current file to a new name.
Obtain current path from "path" and prefix this to newName
/temp/oldname.log > /temp/ > /temp/newname.log
newName: The new name of the file.
Named as RenameTheFile to avoid conflict with RenameFile (sysutils)
}
var
dir: AnsiString;
newPath: AnsiString;
begin
dir := ExtractFilePath(path);
WriteLn('CTextFile.RenameFile(): dir=', dir);
newPath := dir + newName;
WriteLn('CTextFile.RenameFile(): newPath=', newPath);
if RenameFile(path, newPath) = false then { RenameFile from unit SysUtils }
WriteLn('CTextFile.RenameFile() ERROR: renaming file failed: ', path, ' > ', newPath);
end; { of procedure CTextFile.RenameTheFile() }
function CTextFile.GetStatus() : boolean;
begin
GetStatus := isOpen;
end; // of function CTextFile.GetStatus
function CTextFile.DoesFileExists(): boolean;
begin
DoesFileExists := alreadyExists;
end; // of function CTextFile.DoesFileExists()
procedure CTextFile.WriteToFile(line : Ansistring);
begin
//WriteLn('CTextFile.WriteToFile():', line);
WriteLn(textFile, line);
end; // procedure CTextFile.WriteToFile
function CTextFile.ReadFromFile() : AnsiString;
var
buffBig: AnsiString; // v02: old: Array[0..4096] of Char;
begin
Inc(lineCount);
Readln(textFile, buffBig);
ReadFromFile := buffBig;
end; // of function CTextFile.ReadFromFile
function CTextFile.GetLineNumber() : integer;
begin
GetLineNumber := lineCount;
end; // function CTextFile.GetLineNumber
function CTextFile.GetEof() : boolean;
begin
GetEof := Eof(textFile);
end; // of function CTextFile.GetEof()
end. // of unit UTextFile