-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuDatabase.pas
76 lines (61 loc) · 1.47 KB
/
uDatabase.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
unit uDatabase;
interface
uses
Classes,
SysUtils;
type
TOpenMode = (openReadOnly, openReadWrite);
TToDoDatabase = class
private
FMode: TOpenMode;
FDatabase: string;
FRows: TStringlist;
procedure CreateEmptyFile;
public
constructor Create(ADatabase: string; AMode: TOpenMode); overload;
destructor Destroy; override;
procedure Append(AItem: string);
function IDToRowIndex(AIndex: string): integer;
property Rows: TStringList read FRows;
end;
implementation
{ TToDoDatabase }
procedure TToDoDatabase.Append(AItem: string);
begin
FRows.Add(AItem);
end;
constructor TToDoDatabase.Create(ADatabase: string; AMode: TOpenMode);
begin
FDatabase := ADatabase;
FMode := AMode;
FRows := TStringlist.Create;
if not FileExists(FDatabase) then
CreateEmptyFile
else
FRows.LoadFromFile(FDatabase);
end;
procedure TToDoDatabase.CreateEmptyFile;
var
LMode: Word;
begin
LMode := fmOpenWrite;
if not FileExists(FDatabase) then
LMode := LMode or fmCreate;
var todoFS := TFileStream.Create(FDatabase, LMode);
todoFS.Free;
end;
destructor TToDoDatabase.Destroy;
begin
if FMode = openReadWrite then
FRows.SaveToFile(FDatabase);
inherited;
end;
function TToDoDatabase.IDToRowIndex(AIndex: string): integer;
begin
var LIdx := AIndex.ToInteger - 1;
if (LIdx >= 0) and (LIdx < FRows.Count) then
Result := LIdx
else
Result := -1;
end;
end.