| 1 |
(* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* Version: MPL 1.1 |
| 3 |
* |
| 4 |
* The contents of this file are subject to the Mozilla Public License Version |
| 5 |
* 1.1 (the "License"); you may not use this file except in compliance with |
| 6 |
* the License. You may obtain a copy of the License at |
| 7 |
* http://www.mozilla.org/MPL/ |
| 8 |
* |
| 9 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 10 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 11 |
* for the specific language governing rights and limitations under the |
| 12 |
* License. |
| 13 |
* |
| 14 |
* The Original Code is TurboPower Abbrevia |
| 15 |
* |
| 16 |
* The Initial Developer of the Original Code is |
| 17 |
* TurboPower Software |
| 18 |
* |
| 19 |
* Portions created by the Initial Developer are Copyright (C) 1997-2002 |
| 20 |
* the Initial Developer. All Rights Reserved. |
| 21 |
* |
| 22 |
* Contributor(s): |
| 23 |
* Craig Peterson <[email protected]> |
| 24 |
* |
| 25 |
* ***** END LICENSE BLOCK ***** *) |
| 26 |
|
| 27 |
{*********************************************************} |
| 28 |
{* ABBREVIA: AbSpanSt.pas *} |
| 29 |
{*********************************************************} |
| 30 |
{* ABBREVIA: TAbSpan*Stream Classes *} |
| 31 |
{*********************************************************} |
| 32 |
{* Streams to handle splitting ZIP files or spanning *} |
| 33 |
{* them to diskettes *} |
| 34 |
{*********************************************************} |
| 35 |
|
| 36 |
unit AbSpanSt; |
| 37 |
|
| 38 |
{$I AbDefine.inc} |
| 39 |
|
| 40 |
interface |
| 41 |
|
| 42 |
uses |
| 43 |
Classes, |
| 44 |
AbArcTyp; |
| 45 |
|
| 46 |
type |
| 47 |
{ TAbSpanBaseStream interface ============================================== } |
| 48 |
TAbSpanBaseStream = class(TStream) |
| 49 |
protected {private} |
| 50 |
FArchiveName: string; |
| 51 |
|
| 52 |
FOnRequestImage: TAbRequestImageEvent; |
| 53 |
|
| 54 |
protected {methods} |
| 55 |
function GetImageName( ImageNumber: Integer ): string; |
| 56 |
|
| 57 |
public {methods} |
| 58 |
constructor Create( const ArchiveName: string ); |
| 59 |
|
| 60 |
public {events} |
| 61 |
property OnRequestImage : TAbRequestImageEvent |
| 62 |
read FOnRequestImage |
| 63 |
write FOnRequestImage; |
| 64 |
end; |
| 65 |
|
| 66 |
{ TAbSpanReadStream interface ============================================== } |
| 67 |
TAbSpanReadStream = class(TAbSpanBaseStream) |
| 68 |
protected {private} |
| 69 |
FCurrentImage: LongWord; |
| 70 |
FIsSplit: Boolean; |
| 71 |
FLastImage: LongWord; |
| 72 |
FStream: TStream; |
| 73 |
|
| 74 |
FOnRequestNthDisk : TAbRequestNthDiskEvent; |
| 75 |
|
| 76 |
protected {methods} |
| 77 |
procedure GotoImage( ImageNumber: Integer ); |
| 78 |
procedure SetOnRequestImage(Value: TAbRequestImageEvent); |
| 79 |
|
| 80 |
public {methods} |
| 81 |
constructor Create( const ArchiveName: string; CurrentImage: LongWord; |
| 82 |
Stream: TStream ); |
| 83 |
destructor Destroy; |
| 84 |
override; |
| 85 |
function Read(var Buffer; Count: Longint): Longint; |
| 86 |
override; |
| 87 |
function Write(const Buffer; Count: Longint): Longint; |
| 88 |
override; |
| 89 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 90 |
override; |
| 91 |
procedure SeekImage( Image: LongWord; const Offset: Int64); |
| 92 |
|
| 93 |
public {events} |
| 94 |
property OnRequestImage |
| 95 |
write SetOnRequestImage; |
| 96 |
property OnRequestNthDisk : TAbRequestNthDiskEvent |
| 97 |
read FOnRequestNthDisk |
| 98 |
write FOnRequestNthDisk; |
| 99 |
end; |
| 100 |
|
| 101 |
{ TAbSpanWriteStream interface ============================================= } |
| 102 |
TAbSpanWriteStream = class(TAbSpanBaseStream) |
| 103 |
protected {private} |
| 104 |
FCurrentImage: LongWord; |
| 105 |
FImageSize: Int64; |
| 106 |
FStream: TStream; |
| 107 |
FThreshold: Int64; |
| 108 |
|
| 109 |
FOnRequestBlankDisk : TAbRequestDiskEvent; |
| 110 |
|
| 111 |
protected {methods} |
| 112 |
procedure NewImage; |
| 113 |
|
| 114 |
public {methods} |
| 115 |
constructor Create( const ArchiveName: string; Stream: TStream; |
| 116 |
Threshold: Int64 ); |
| 117 |
destructor Destroy; |
| 118 |
override; |
| 119 |
function Read(var Buffer; Count: Longint): Longint; |
| 120 |
override; |
| 121 |
function Write(const Buffer; Count: Longint): Longint; |
| 122 |
override; |
| 123 |
function WriteUnspanned(const Buffer; Count: Longint; |
| 124 |
FailOnSpan: Boolean = False): Boolean; |
| 125 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 126 |
override; |
| 127 |
function ReleaseStream: TStream; |
| 128 |
|
| 129 |
public {properties} |
| 130 |
property CurrentImage : LongWord |
| 131 |
read FCurrentImage; |
| 132 |
|
| 133 |
public {events} |
| 134 |
property OnRequestBlankDisk : TAbRequestDiskEvent |
| 135 |
read FOnRequestBlankDisk |
| 136 |
write FOnRequestBlankDisk; |
| 137 |
end; |
| 138 |
|
| 139 |
implementation |
| 140 |
|
| 141 |
uses |
| 142 |
{$IFDEF MSWINDOWS} |
| 143 |
Windows, |
| 144 |
{$ENDIF} |
| 145 |
Math, RTLConsts, SysUtils, AbUtils, AbExcept, DCOSUtils, DCClassesUtf8; |
| 146 |
|
| 147 |
|
| 148 |
{============================================================================} |
| 149 |
{ TAbSpanBaseStream implementation ========================================= } |
| 150 |
constructor TAbSpanBaseStream.Create( const ArchiveName: string ); |
| 151 |
begin |
| 152 |
inherited Create; |
| 153 |
FArchiveName := ArchiveName; |
| 154 |
end; |
| 155 |
{------------------------------------------------------------------------------} |
| 156 |
function TAbSpanBaseStream.GetImageName( ImageNumber: Integer ): string; |
| 157 |
var |
| 158 |
Abort : Boolean; |
| 159 |
Ext : string; |
| 160 |
begin |
| 161 |
{generate default name} |
| 162 |
Ext := ExtractFileExt(FArchiveName); |
| 163 |
if (Length(Ext) < 2) then |
| 164 |
Ext := '.' + Format('%.2d', [ImageNumber]) |
| 165 |
else |
| 166 |
Ext := Ext[1] + Ext[2] + Format('%.2d', [ImageNumber]); |
| 167 |
Result := ChangeFileExt(FArchiveName, Ext); |
| 168 |
{call event} |
| 169 |
if Assigned(FOnRequestImage) then begin |
| 170 |
Abort := False; |
| 171 |
FOnRequestImage(Self, ImageNumber, Result, Abort); |
| 172 |
if Abort then |
| 173 |
raise EAbUserAbort.Create; |
| 174 |
end; |
| 175 |
end; |
| 176 |
|
| 177 |
{============================================================================} |
| 178 |
{ TAbSpanReadStream implementation ========================================= } |
| 179 |
constructor TAbSpanReadStream.Create( const ArchiveName: string; |
| 180 |
CurrentImage: LongWord; Stream: TStream ); |
| 181 |
begin |
| 182 |
inherited Create(ArchiveName); |
| 183 |
FCurrentImage := CurrentImage; |
| 184 |
FIsSplit := mbFileExists(GetImageName(1)) or not AbDriveIsRemovable(ArchiveName); |
| 185 |
FLastImage := CurrentImage; |
| 186 |
FStream := Stream; |
| 187 |
end; |
| 188 |
{------------------------------------------------------------------------------} |
| 189 |
destructor TAbSpanReadStream.Destroy; |
| 190 |
begin |
| 191 |
FreeAndNil(FStream); |
| 192 |
inherited; |
| 193 |
end; |
| 194 |
{------------------------------------------------------------------------------} |
| 195 |
procedure TAbSpanReadStream.GotoImage( ImageNumber: Integer ); |
| 196 |
var |
| 197 |
Abort: Boolean; |
| 198 |
ImageName: string; |
| 199 |
begin |
| 200 |
{ switch to the requested image. ImageNumber is passed in as 0-based to |
| 201 |
match the zip spec, but all of the callbacks receive 1-based values. } |
| 202 |
FreeAndNil(FStream); |
| 203 |
FCurrentImage := ImageNumber; |
| 204 |
Inc(ImageNumber); |
| 205 |
ImageName := FArchiveName; |
| 206 |
if FIsSplit then begin |
| 207 |
{ the last image uses the original filename } |
| 208 |
if FCurrentImage <> FLastImage then |
| 209 |
ImageName := GetImageName(ImageNumber) |
| 210 |
end |
| 211 |
else if Assigned(FOnRequestNthDisk) then begin |
| 212 |
Abort := False; |
| 213 |
repeat |
| 214 |
FOnRequestNthDisk(Self, ImageNumber, Abort); |
| 215 |
if Abort then |
| 216 |
raise EAbUserAbort.Create; |
| 217 |
until AbGetDriveFreeSpace(ImageName) <> -1; |
| 218 |
end |
| 219 |
else |
| 220 |
raise EAbUserAbort.Create; |
| 221 |
FStream := TFileStreamEx.Create(ImageName, fmOpenRead or fmShareDenyWrite); |
| 222 |
end; |
| 223 |
{------------------------------------------------------------------------------} |
| 224 |
function TAbSpanReadStream.Read(var Buffer; Count: Longint): Longint; |
| 225 |
var |
| 226 |
BytesRead, BytesLeft: LongInt; |
| 227 |
PBuf: PByte; |
| 228 |
begin |
| 229 |
{ read until the buffer's full, switching images if necessary } |
| 230 |
Result := 0; |
| 231 |
if FStream = nil then |
| 232 |
Exit; |
| 233 |
PBuf := @Buffer; |
| 234 |
BytesLeft := Count; |
| 235 |
while Result < Count do begin |
| 236 |
BytesRead := FStream.Read(PBuf^, BytesLeft); |
| 237 |
Inc(Result, BytesRead); |
| 238 |
Inc(PBuf, BytesRead); |
| 239 |
Dec(BytesLeft, BytesRead); |
| 240 |
if BytesRead < BytesLeft then begin |
| 241 |
if FCurrentImage <> FLastImage then |
| 242 |
GotoImage(FCurrentImage + 1) |
| 243 |
else |
| 244 |
Break; |
| 245 |
end; |
| 246 |
end; |
| 247 |
end; |
| 248 |
{------------------------------------------------------------------------------} |
| 249 |
function TAbSpanReadStream.Write(const Buffer; Count: Longint): Longint; |
| 250 |
begin |
| 251 |
raise EAbException.Create('TAbSpanReadStream.Write unsupported'); |
| 252 |
end; |
| 253 |
{------------------------------------------------------------------------------} |
| 254 |
function TAbSpanReadStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 255 |
begin |
| 256 |
if FStream = nil then |
| 257 |
Result := 0 |
| 258 |
else if (Offset = 0) and (Origin = soCurrent) then |
| 259 |
Result := FStream.Position |
| 260 |
else |
| 261 |
raise EAbException.Create('TAbSpanReadStream.Seek unsupported'); |
| 262 |
end; |
| 263 |
{------------------------------------------------------------------------------} |
| 264 |
procedure TAbSpanReadStream.SeekImage( Image: LongWord; const Offset: Int64); |
| 265 |
begin |
| 266 |
if FStream = nil then |
| 267 |
Exit; |
| 268 |
if FCurrentImage <> Image then |
| 269 |
GotoImage(Image); |
| 270 |
FStream.Position := Offset; |
| 271 |
end; |
| 272 |
{------------------------------------------------------------------------------} |
| 273 |
procedure TAbSpanReadStream.SetOnRequestImage(Value: TAbRequestImageEvent); |
| 274 |
begin |
| 275 |
FOnRequestImage := Value; |
| 276 |
FIsSplit := mbFileExists(GetImageName(1)) or not AbDriveIsRemovable(FArchiveName); |
| 277 |
end; |
| 278 |
|
| 279 |
{============================================================================} |
| 280 |
{ TAbSpanWriteStream implementation ======================================== } |
| 281 |
constructor TAbSpanWriteStream.Create( const ArchiveName: string; |
| 282 |
Stream: TStream; Threshold: Int64 ); |
| 283 |
begin |
| 284 |
inherited Create(ArchiveName); |
| 285 |
FCurrentImage := 0; |
| 286 |
FStream := Stream; |
| 287 |
FThreshold := Threshold; |
| 288 |
end; |
| 289 |
{------------------------------------------------------------------------------} |
| 290 |
destructor TAbSpanWriteStream.Destroy; |
| 291 |
begin |
| 292 |
FStream.Free; |
| 293 |
inherited; |
| 294 |
end; |
| 295 |
{------------------------------------------------------------------------------} |
| 296 |
procedure TAbSpanWriteStream.NewImage; |
| 297 |
var |
| 298 |
Abort: Boolean; |
| 299 |
begin |
| 300 |
{ start a new span or blank disk. FCurrentImage is 0-based to match the zip |
| 301 |
spec, but all of the callbacks receive 1-based values. } |
| 302 |
FreeAndNil(FStream); |
| 303 |
Inc(FCurrentImage); |
| 304 |
if FThreshold > 0 then |
| 305 |
mbRenameFile(FArchiveName, GetImageName(FCurrentImage)) |
| 306 |
else begin |
| 307 |
if Assigned(FOnRequestBlankDisk) then begin |
| 308 |
Abort := False; |
| 309 |
repeat |
| 310 |
FOnRequestBlankDisk(Self, Abort); |
| 311 |
if Abort then |
| 312 |
raise EAbUserAbort.Create; |
| 313 |
until AbGetDriveFreeSpace(FArchiveName) <> -1; |
| 314 |
end |
| 315 |
else |
| 316 |
raise EAbUserAbort.Create; |
| 317 |
AbSetSpanVolumeLabel(AbDrive(FArchiveName), FCurrentImage); |
| 318 |
end; |
| 319 |
FStream := TFileStreamEx.Create(FArchiveName, fmCreate or fmShareDenyWrite); |
| 320 |
FImageSize := 0; |
| 321 |
end; |
| 322 |
{------------------------------------------------------------------------------} |
| 323 |
function TAbSpanWriteStream.Read(var Buffer; Count: Longint): Longint; |
| 324 |
begin |
| 325 |
raise EAbException.Create('TAbSpanWriteStream.Read unsupported'); |
| 326 |
end; |
| 327 |
{------------------------------------------------------------------------------} |
| 328 |
function TAbSpanWriteStream.Write(const Buffer; Count: Longint): Longint; |
| 329 |
var |
| 330 |
BytesWritten, BytesLeft: LongInt; |
| 331 |
PBuf: PByte; |
| 332 |
begin |
| 333 |
{ write until the buffer is done, starting new spans if necessary } |
| 334 |
Result := 0; |
| 335 |
if FStream = nil then |
| 336 |
Exit; |
| 337 |
PBuf := @Buffer; |
| 338 |
BytesLeft := Count; |
| 339 |
while Result < Count do begin |
| 340 |
if FThreshold > 0 then |
| 341 |
BytesWritten := FStream.Write(PBuf^, Min(BytesLeft, FThreshold - FImageSize)) |
| 342 |
else |
| 343 |
BytesWritten := FStream.Write(PBuf^, BytesLeft); |
| 344 |
Inc(FImageSize, BytesWritten); |
| 345 |
Inc(Result, BytesWritten); |
| 346 |
Inc(PBuf, BytesWritten); |
| 347 |
Dec(BytesLeft, BytesWritten); |
| 348 |
if BytesWritten < BytesLeft then |
| 349 |
NewImage; |
| 350 |
end; |
| 351 |
end; |
| 352 |
{------------------------------------------------------------------------------} |
| 353 |
function TAbSpanWriteStream.WriteUnspanned(const Buffer; Count: Longint; |
| 354 |
FailOnSpan: Boolean = False): Boolean; |
| 355 |
var |
| 356 |
BytesWritten: LongInt; |
| 357 |
begin |
| 358 |
{ write as a contiguous block, starting a new span if there isn't room. |
| 359 |
FailOnSpan (and result = false) can be used to update data before it's |
| 360 |
written again } |
| 361 |
if FStream = nil then |
| 362 |
raise EWriteError.Create(SWriteError); |
| 363 |
if (FThreshold > 0) and (FThreshold - FImageSize < Count) then |
| 364 |
BytesWritten := 0 |
| 365 |
else |
| 366 |
BytesWritten := FStream.Write(Buffer, Count); |
| 367 |
if BytesWritten < Count then begin |
| 368 |
if BytesWritten > 0 then |
| 369 |
FStream.Size := FStream.Size - BytesWritten; |
| 370 |
NewImage; |
| 371 |
if FailOnSpan then |
| 372 |
BytesWritten := 0 |
| 373 |
else begin |
| 374 |
BytesWritten := Count; |
| 375 |
FStream.WriteBuffer(Buffer, Count); |
| 376 |
end; |
| 377 |
end; |
| 378 |
Inc(FImageSize, BytesWritten); |
| 379 |
Result := (BytesWritten = Count); |
| 380 |
end; |
| 381 |
{------------------------------------------------------------------------------} |
| 382 |
function TAbSpanWriteStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 383 |
begin |
| 384 |
if FStream = nil then |
| 385 |
Result := 0 |
| 386 |
else if (Offset = 0) and (Origin = soCurrent) then |
| 387 |
Result := FStream.Position |
| 388 |
else |
| 389 |
raise EAbException.Create('TAbSpanWriteStream.Seek unsupported'); |
| 390 |
end; |
| 391 |
{------------------------------------------------------------------------------} |
| 392 |
function TAbSpanWriteStream.ReleaseStream: TStream; |
| 393 |
begin |
| 394 |
Result := FStream; |
| 395 |
FStream := nil; |
| 396 |
end; |
| 397 |
{------------------------------------------------------------------------------} |
| 398 |
end. |