~ubuntu-branches/ubuntu/vivid/ddrescueview/vivid-backports

« back to all changes in this revision

Viewing changes to source/Shared.pas

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2015-05-11 11:31:36 UTC
  • Revision ID: package-import@ubuntu.com-20150511113136-7zdpjspc6jfd2nxz
Tags: upstream-0.4~alpha
ImportĀ upstreamĀ versionĀ 0.4~alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(*
 
2
   Shared.pas - Shared functionality
 
3
 
 
4
   Copyright (C) 2013 - 2015 Martin Bittermann (martinbittermann@gmx.de)
 
5
 
 
6
   This file is part of ddrescueview.
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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/>.
 
20
*)
 
21
 
 
22
unit Shared;
 
23
 
 
24
(* This unit contains shared functionality, especially constants,
 
25
   types and essential functions for multiple parts of the program *)
 
26
 
 
27
interface
 
28
 
 
29
uses Classes, Graphics;
 
30
 
 
31
type
 
32
  PTextFile = ^TextFile;
 
33
  TLogEntry = record
 
34
    offset : int64;
 
35
    length : int64;
 
36
    status : char;
 
37
  end;
 
38
  TLog = Array of TLogEntry;
 
39
  TRescueStatus = record
 
40
    devicesize : int64;
 
41
    suggestedBlockSize : integer;
 
42
    pos : int64;
 
43
    rescued : int64;
 
44
    nontried : int64;
 
45
    bad : int64;
 
46
    nonscraped : int64;
 
47
    nontrimmed : int64;
 
48
    errors : int64;
 
49
    curOperation : Char;
 
50
    strCurOperation : String;
 
51
  end;
 
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);
 
58
  end;
 
59
 
 
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 *)
 
64
  public
 
65
    procedure AttachObserver(AObserver : TObject);
 
66
    procedure DetachObserver(AObserver : TObject);
 
67
  end;
 
68
 
 
69
 
 
70
const
 
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);
 
85
 
 
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;
 
94
 
 
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;
 
101
 
 
102
  // color primary masks
 
103
  MASK_B = $ff0000;
 
104
  MASK_G = $00ff00;
 
105
  MASK_R = $0000ff;
 
106
 
 
107
  // default device block size
 
108
  DEF_BSIZE = 512;
 
109
 
 
110
  PROGRAM_TITLE = 'ddrescue log viewer';
 
111
  VERSION_MAJOR = '0';
 
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 : '');
 
116
 
 
117
var
 
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);
 
138
 
 
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;
 
143
 
 
144
 
 
145
implementation
 
146
uses SysUtils, Math;
 
147
 
 
148
 
 
149
function BlockStatusToString(status: char) : String;
 
150
begin
 
151
  case status of
 
152
    '?': result := 'Non-tried';
 
153
    '*': result := 'Non-trimmed';
 
154
    '/': result := 'Non-scraped';
 
155
    '-': result := 'Bad sector(s)';
 
156
    '+': result := 'Rescued';
 
157
  else
 
158
    result := 'Unknown status';
 
159
  end;
 
160
end;
 
161
 
 
162
// status strings for the status line
 
163
function OperationToText(status: char) : string;
 
164
begin
 
165
  case status of
 
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';
 
173
  else
 
174
    OperationToText := 'Unknown operation';
 
175
  end;
 
176
end;
 
177
 
 
178
function SizeStr(sizeInBytes : int64): String;
 
179
begin
 
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';
 
188
  end else begin
 
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';
 
196
  end;
 
197
end;
 
198
 
 
199
function BlockOverlap(b1Start, b1End, b2Start, b2End: Int64): Int64;
 
200
begin
 
201
  BlockOverlap:=Max(0, Min(Min(b1End-b1Start, b2End-b2Start),
 
202
                           Min(b2End-b1Start, b1End-b2Start)));
 
203
end;
 
204
 
 
205
procedure TObservablePersistent.AttachObserver(AObserver : TObject);
 
206
begin
 
207
  FPOAttachObserver(AObserver);
 
208
end;
 
209
 
 
210
procedure TObservablePersistent.DetachObserver(AObserver : TObject);
 
211
begin
 
212
  FPODetachObserver(AObserver);
 
213
end;
 
214
 
 
215
end.