2
Shared.pas - Shared functionality
4
Copyright (C) 2013 - 2015 Martin Bittermann (martinbittermann@gmx.de)
6
This file is part of ddrescueview.
8
ddrescueview is free software: you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation, either version 3 of the License, or
11
(at your option) any later version.
13
ddrescueview is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with ddrescueview. If not, see <http://www.gnu.org/licenses/>.
24
(* This unit contains shared functionality, especially constants,
25
types and essential functions for multiple parts of the program *)
29
uses Classes, Graphics;
32
PTextFile = ^TextFile;
38
TLog = Array of TLogEntry;
39
TRescueStatus = record
41
suggestedBlockSize : integer;
50
strCurOperation : String;
52
IAttachableObserved = Interface(IFPObserved)
53
(* Extend the IFPObserved intf to have Attach/detach wrappers
54
that can be implemented as public methods by implementing classes.
55
This would make these classes actually 'attachable' from the outside. *)
56
procedure AttachObserver(AObserver : TObject);
57
procedure DetachObserver(AObserver : TObject);
60
TObservablePersistent = class(TPersistent, IAttachableObserved)
61
(* Implement the IAttachableObserved interface for TPersistent.
62
Predefined TPersistent descendants that need to be attachable should
63
implement it like here *)
65
procedure AttachObserver(AObserver : TObject);
66
procedure DetachObserver(AObserver : TObject);
71
// block type masks for each block flag.
72
// The upper 8 bits are reserved for these flags,
73
// the lower 24 bits contain the color used for display.
74
// The actual display color is blended together from the flags
75
// in the upper 8 bits by ColorizeBlockMask
76
MASK_NON_TRIED = $01000000;
77
MASK_NON_TRIMMED = $02000000;
78
MASK_NON_SCRAPED = $04000000;
79
MASK_BAD_SECT = $08000000;
80
MASK_FINISHED = $10000000;
81
MASK_ALL_STATUSES = $1F000000;
82
MASK_ACTIVE = $20000000;
83
MASKS : array[0..4] of Longint =
84
(MASK_NON_TRIED, MASK_FINISHED, MASK_NON_TRIMMED, MASK_NON_SCRAPED, MASK_BAD_SECT);
86
// color defaults for the block statuses
87
DEF_COLOR_NON_TRIED = $909090;
88
DEF_COLOR_NON_TRIMMED = $00e0ff;
89
DEF_COLOR_NON_SCRAPED = $ff2020;
90
DEF_COLOR_BAD_SECT = $0000ff;
91
DEF_COLOR_FINISHED = $20e020;
92
DEF_COLOR_UNDEFINED = $606060;
93
DEF_COLOR_ACTIVE = $ffff00;
95
// color weight defaults to be used by ColorizeBlockMask for blending
96
DEF_WEIGHT_NON_TRIED = 1;
97
DEF_WEIGHT_FINISHED = 2;
98
DEF_WEIGHT_NON_TRIMMED = 4;
99
DEF_WEIGHT_NON_SCRAPED = 10;
100
DEF_WEIGHT_BAD_SECT = 40;
102
// color primary masks
107
// default device block size
110
PROGRAM_TITLE = 'ddrescue log viewer';
112
VERSION_MINOR = '4 alpha';
113
emptyRescueStatus : TRescueStatus =
114
(devicesize : 0; suggestedBlockSize : DEF_BSIZE; pos : 0; rescued : 0; nontried : 0; bad : 0;
115
nonscraped : 0; nontrimmed : 0; errors : 0; curOperation : #0; strCurOperation : '');
118
useDecimalUnits : boolean = true;
119
// colors for the block statuses
120
COLOR_NON_TRIED : Longint = $909090;
121
COLOR_NON_TRIMMED : Longint = $00e0ff;
122
COLOR_NON_SCRAPED : Longint = $ff2020;
123
COLOR_BAD_SECT : Longint = $0000ff;
124
COLOR_FINISHED : Longint = $20e020;
125
COLOR_UNDEFINED : Longint = $606060;
126
COLOR_ACTIVE : Longint = $ffff00;
127
// color weights to be used by ColorizeBlockMask for blending
128
WEIGHT_NON_TRIED : Longint = 1;
129
WEIGHT_FINISHED : Longint = 2;
130
WEIGHT_NON_TRIMMED : Longint = 4;
131
WEIGHT_NON_SCRAPED : Longint = 10;
132
WEIGHT_BAD_SECT : Longint = 40;
133
// arrays containing the above constants for easier looping
134
COLORS : array[0..4] of ^Longint =
135
(@COLOR_NON_TRIED, @COLOR_FINISHED, @COLOR_NON_TRIMMED, @COLOR_NON_SCRAPED, @COLOR_BAD_SECT);
136
WEIGHTS : array[0..4] of ^Longint =
137
(@WEIGHT_NON_TRIED, @WEIGHT_FINISHED, @WEIGHT_NON_TRIMMED, @WEIGHT_NON_SCRAPED, @WEIGHT_BAD_SECT);
139
function BlockStatusToString(status: char) : String;
140
function OperationToText(status: char) : string;
141
function SizeStr(sizeInBytes : int64): String;
142
function BlockOverlap(b1Start, b1End, b2Start, b2End : Int64): Int64;
149
function BlockStatusToString(status: char) : String;
152
'?': result := 'Non-tried';
153
'*': result := 'Non-trimmed';
154
'/': result := 'Non-scraped';
155
'-': result := 'Bad sector(s)';
156
'+': result := 'Rescued';
158
result := 'Unknown status';
162
// status strings for the status line
163
function OperationToText(status: char) : string;
166
'?': OperationToText := 'Copying non-tried';
167
'*': OperationToText := 'Trimming non-trimmed blocks';
168
'/': OperationToText := 'Scraping non-scraped blocks';
169
'-': OperationToText := 'Retrying bad sectors';
170
'F': OperationToText := 'Filling specified blocks';
171
'G': OperationToText := 'Generating approximate logfile';
172
'+': OperationToText := 'Finished';
174
OperationToText := 'Unknown operation';
178
function SizeStr(sizeInBytes : int64): String;
180
if useDecimalUnits then begin
181
if sizeInBytes < 0 then SizeStr := 'invalid size?'
182
else if sizeInBytes < 100000 then SizeStr := IntToStr(sizeInBytes)+ ' Byte'
183
else if sizeInBytes < 100000000 then SizeStr := IntToStr(sizeInBytes div 1000)+ ' KB'
184
else if sizeInBytes < 100000000000 then SizeStr := IntToStr(sizeInBytes div 1000000)+ ' MB'
185
else if sizeInBytes < 100000000000000 then SizeStr := IntToStr(sizeInBytes div 1000000000)+ ' GB'
186
else if sizeInBytes < 100000000000000000 then SizeStr := IntToStr(sizeInBytes div 1000000000000)+ ' TB'
187
else SizeStr := IntToStr(sizeInBytes div 1000000000000000)+ ' PB';
189
if sizeInBytes < 0 then SizeStr := 'invalid size?'
190
else if sizeInBytes < 102400 then SizeStr := IntToStr(sizeInBytes)+ ' Byte'
191
else if sizeInBytes < 104857600 then SizeStr := IntToStr(sizeInBytes div 1024)+ ' KiB'
192
else if sizeInBytes < 107374182400 then SizeStr := IntToStr(sizeInBytes div 1048576)+ ' MiB'
193
else if sizeInBytes < 109951162777600 then SizeStr := IntToStr(sizeInBytes div 1073741824)+ ' GiB'
194
else if sizeInBytes < 112589990684262400 then SizeStr := IntToStr(sizeInBytes div 1099511627776)+ ' TiB'
195
else SizeStr := IntToStr(sizeInBytes div 1125899906842624)+ ' PiB';
199
function BlockOverlap(b1Start, b1End, b2Start, b2End: Int64): Int64;
201
BlockOverlap:=Max(0, Min(Min(b1End-b1Start, b2End-b2Start),
202
Min(b2End-b1Start, b1End-b2Start)));
205
procedure TObservablePersistent.AttachObserver(AObserver : TObject);
207
FPOAttachObserver(AObserver);
210
procedure TObservablePersistent.DetachObserver(AObserver : TObject);
212
FPODetachObserver(AObserver);