source: trunk/UProject.pas

Last change on this file was 28, checked in by chronos, 8 years ago
  • Modified: Dynamically calculate sector count from drive size and sector size.
File size: 3.8 KB
Line 
1unit UProject;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Contnrs, DOM, XMLRead, XMLWrite, UXMLUtils, UDriveScan,
9 UPhysDrive;
10
11type
12
13 { TProject }
14
15 TProject = class
16 private
17 FCurrentScan: TDriveScan;
18 function GetCurrentScan: TDriveScan;
19 function GetSectorCount: Integer;
20 public
21 Name: string;
22 FileName: string;
23 Modified: Boolean;
24 Scans: TDriveScanList;
25 DriveInfo: TDriveInfo;
26 ScanProfile: TDriveScanProfile;
27 constructor Create;
28 destructor Destroy; override;
29 procedure LoadFromFile(FileName: string);
30 procedure SaveToFile(FileName: string);
31 property SectorCount: Integer read GetSectorCount;
32 property CurrentScan: TDriveScan read FCurrentScan write FCurrentScan;
33 end;
34
35
36implementation
37
38resourcestring
39 SWrongFileFormat = 'Wrong file format';
40
41
42{ TProject }
43
44function TProject.GetCurrentScan: TDriveScan;
45begin
46 if Scans.Count > 0 then Result := TDriveScan(Scans[Scans.Count - 1])
47 else Result := nil;
48end;
49
50function TProject.GetSectorCount: Integer;
51begin
52 Result := DriveInfo.SectorCount;
53end;
54
55constructor TProject.Create;
56begin
57 Scans := TDriveScanList.Create;
58 DriveInfo := TDriveInfo.Create;
59 ScanProfile := TDriveScanProfile.Create;
60end;
61
62destructor TProject.Destroy;
63begin
64 FreeAndNil(ScanProfile);
65 FreeAndNil(DriveInfo);
66 FreeAndNil(Scans);
67 inherited Destroy;
68end;
69
70procedure TProject.SaveToFile(FileName: string);
71var
72 NewNode: TDOMNode;
73 Doc: TXMLDocument;
74 RootNode: TDOMNode;
75begin
76 Self.FileName := FileName;
77 Doc := TXMLDocument.Create;
78 with Doc do try
79 RootNode := CreateElement('CoolDiskProject');
80 AppendChild(RootNode);
81 with RootNode do begin
82 WriteInteger(RootNode, 'SectorCount', DriveInfo.SectorCount);
83 WriteInteger(RootNode, 'SectorSize', DriveInfo.SectorSize);
84 WriteString(RootNode, 'DriveName', DriveInfo.Model);
85 WriteString(RootNode, 'DrivePath', DriveInfo.Path);
86 WriteInt64(RootNode, 'DriveSize', DriveInfo.Size);
87
88 NewNode := OwnerDocument.CreateElement('ScanProfile');
89 AppendChild(NewNode);
90 WriteInteger(NewNode, 'SectorStart', ScanProfile.SectorStart);
91 WriteInteger(NewNode, 'SectorEnd', ScanProfile.SectorEnd);
92 WriteInteger(NewNode, 'WritePattern', ScanProfile.WritePattern);
93 WriteInteger(NewNode, 'Mode', Integer(ScanProfile.Mode));
94
95 NewNode := OwnerDocument.CreateElement('Scans');
96 AppendChild(NewNode);
97 Scans.SaveToNode(NewNode);
98 end;
99 ForceDirectories(ExtractFileDir(FileName));
100 WriteXMLFile(Doc, FileName);
101 finally
102 Doc.Free;
103 end;
104 Modified := False;
105end;
106
107procedure TProject.LoadFromFile(FileName: string);
108var
109 Doc: TXMLDocument;
110 RootNode: TDOMNode;
111 NewNode: TDOMNode;
112begin
113 Modified := False;
114 Self.FileName := FileName;
115 ReadXMLFile(Doc, FileName);
116 with Doc do try
117 if Doc.DocumentElement.NodeName <> 'CoolDiskProject' then
118 raise Exception.Create(SWrongFileFormat);
119 RootNode := Doc.DocumentElement;
120 with RootNode do begin
121 DriveInfo.SectorSize := ReadInteger(RootNode, 'SectorSize', 4096);
122 ScanProfile.SectorCount := DriveInfo.SectorCount;
123 DriveInfo.Model := ReadString(RootNode, 'DriveName', '');
124 DriveInfo.Path := ReadString(RootNode, 'DrivePath', '');
125 DriveInfo.Size := ReadInt64(RootNode, 'DriveSize', 0);
126
127 NewNode := FindNode('ScanProfile');
128 if Assigned(NewNode) then begin
129 ScanProfile.SectorStart := ReadInteger(NewNode, 'SectorStart', 0);
130 ScanProfile.SectorEnd := ReadInteger(NewNode, 'SectorEnd', ScanProfile.SectorCount - 1);
131 ScanProfile.WritePattern := ReadInteger(NewNode, 'WritePattern', 0);
132 ScanProfile.Mode := TRunMode(ReadInteger(NewNode, 'Mode', 0));
133 end;
134
135 NewNode := FindNode('Scans');
136 if Assigned(NewNode) then
137 Scans.LoadFromNode(NewNode);
138 end;
139 finally
140 Doc.Free;
141 end;
142end;
143
144
145end.
146
Note: See TracBrowser for help on using the repository browser.