-
Notifications
You must be signed in to change notification settings - Fork 2
/
FVIEWER.PAS
1793 lines (1671 loc) · 55.2 KB
/
FVIEWER.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{/////////////////////////////////////////////////////////////////////////
//
// Dos Navigator Version 1.51 Copyright (C) 1991-99 RIT Research Labs
//
// This programs is free for commercial and non-commercial use as long as
// the following conditions are aheared to.
//
// Copyright remains RIT Research Labs, and as such any Copyright notices
// in the code are not to be removed. If this package is used in a
// product, RIT Research Labs should be given attribution as the RIT Research
// Labs of the parts of the library used. This can be in the form of a textual
// message at program startup or in documentation (online or textual)
// provided with the package.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// "Based on Dos Navigator by RIT Research Labs."
//
// THIS SOFTWARE IS PROVIDED BY RIT RESEARCH LABS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The licence and distribution terms for any publically available
// version or derivative of this code cannot be changed. i.e. this code
// cannot simply be copied and put under another distribution licence
// (including the GNU Public Licence).
//
//////////////////////////////////////////////////////////////////////////}
unit FViewer;
interface
uses Objects, Views, Dos, Drivers, HideView, Commands, DNHelp, ObjType;
type
{ TViewScroll }
PViewScroll = ^TViewScroll;
TViewScroll = object(TView)
MaxV, Value: LongInt;
function GetPalette: PPalette; virtual;
procedure HandleEvent(var Event: TEvent); virtual;
function GetPartCode: LongInt;
procedure Draw; virtual;
function GetSize: Integer;
procedure DrawPos(Pos: Integer);
end;
{ TFileViewer }
PFileViewer = ^TFileViewer;
TFileViewer = object(THideView)
RdOnly: Boolean;
NoEdit: Boolean;
FileName: PathStr;
Buf: PByteArray;
Fl: PStream;
XDelta, ViewMode, HexPos: Integer;
SearchActive: Boolean;
SearchX: LongInt;
SB: PView;
Wrap: Boolean;
Lines: Array[0..200] of record Pos: LongInt; Len: Byte; end;
FilePos, FileSize, NumLines: LongInt;
Cur: TPoint;
Info: PView;
BufPos: Word;
BufSize: LongInt;
BufLines, MaxLines: Integer;
KillAfterUse, IsValid, QuickView, Loaded, HexEdit, BufModified: Boolean;
Filter: Byte;
XLAT: Array [Char] of Char;
UseXLAT: Boolean;
XLatFile: String[8];
constructor Init(var Bounds: TRect; AStream: PStream;
AFileName: PathStr; ASB: PView; Quick, Hex: Boolean);
constructor Load(var S: TStream);
procedure Store(var S: TStream);
destructor Done; virtual;
procedure ShowView; virtual;
procedure HideView; virtual;
procedure Draw; virtual;
procedure SetXlatFile(const aFName: String);
procedure ReadFile(FName: PathStr; NewStream: Boolean);
procedure SetState(AState: Word; Enable: Boolean); virtual;
procedure HandleEvent(var Event: TEvent); virtual;
function WriteModify: Boolean;
procedure CountDown(ANumber: Integer); virtual;
procedure CountUp(ANumber: Integer); virtual;
procedure Seek(APos: LongInt);
procedure MakeLines; virtual;
function Valid(Command: Word): Boolean; virtual;
procedure ChangeBounds(var Bounds: TRect); virtual;
function GetPalette: PPalette; virtual;
private
procedure AdjustBufSize;
end;
PHFileViewer = ^THFileViewer;
THFileViewer = object(TFileViewer)
procedure ChangeBounds(var Bounds: TRect); virtual;
function GetPalette: PPalette; virtual;
end;
PNFileViewer = ^TNFileViewer;
TNFileViewer = object(TFileViewer)
function GetPalette: PPalette; virtual;
end;
PFileWindow = ^TFileWindow;
TFileWindow = object(TStdWindow)
constructor Init(const FileName: PathStr; Hex: Boolean);
function GetPalette: PPalette; virtual;
function ReactOnCmd: Boolean; virtual;
procedure ChangeBounds(var Bounds: TRect); virtual;
end;
PViewInfo = ^TViewInfo;
TViewInfo = object(TView)
Viewer: PFileViewer;
constructor Init(var R: TRect; AViewer: PFileViewer);
constructor Load(var S: TStream);
procedure Store(var S: TStream);
procedure Draw; virtual;
procedure HandleEvent(var Event: TEvent); virtual;
end;
const
RFileViewer: TStreamRec = (
ObjType: otFileViewer;
VmtLink: Ofs(TypeOf(TFileViewer)^);
Load: @TFileViewer.Load;
Store: @TFileViewer.Store
);
RFileWindow: TStreamRec = (
ObjType: otFileWindow;
VmtLink: Ofs(TypeOf(TFileWindow)^);
Load: @TFileWindow.Load;
Store: @TFileWindow.Store
);
RViewScroll: TStreamRec = (
ObjType: otViewScroll;
VmtLink: Ofs(TypeOf(TViewScroll)^);
Load: @TViewScroll.Load;
Store: @TViewScroll.Store
);
RHFileViewer: TStreamRec = (
ObjType: otHFileViewer;
VmtLink: Ofs(TypeOf(THFileViewer)^);
Load: @THFileViewer.Load;
Store: @THFileViewer.Store
);
RViewInfo : TStreamRec = (
ObjType: otViewInfo;
VmtLink: Ofs(TypeOf(TViewInfo)^);
Load: @TViewInfo.Load;
Store: @TViewInfo.Store
);
CHViewer = #13#14;
CViewer = #6#7;
CViewWindow = #112#113#114#115#116#117#118;
type
TViewSearch = record What: String[250]; Opts: Word; Dir: Word end;
const
cmChangeValue = 9990;
SearchString: TViewSearch = (What:''; Opts: 0; Dir: 0);
var
LastViewerBounds: TRect;
const
LastViewerDeskSize: TPoint = (X:0;Y:0);
LastEditDeskSize: TPoint = (X:0;Y:0);
function SearchFileStr(F: PStream; What: String; Pos: LongInt;
CaseSensitive, Display, WholeWords, Back: Boolean): LongInt;
implementation
uses Memory, Messages, DNApp, Advance, Startup, Dialogs, RStrings,
Gauge, Microed, DNStdDlg, Histries, xTime, Drives;
function MaxAvail: LongInt;
begin
MaxAvail := MemAdjust(System.MaxAvail);
end;
const
ViewerBufSize = $8000{8192};
function SearchFileStr;
label 1, LExit;
var Buf: PByteArray;
Count: LongInt;
BufLen, BufPos, BPos, T, StPos, StDelta: Word;
I, L, J, OldPos, NextPos: LongInt;
CancelSearch: Boolean;
Info: PWhileView;
R: Trect;
Tmr: TEventTimer;
Inserted: Boolean;
begin
Info := nil;
SearchFileStr := -1; Count := 0; ClrIO;
OldPos := F^.GetPos; L := F^.GetSize; if L = 0 then goto LExit;
F^.Seek (Pos);
BufLen := $7000;
if LongInt(BufLen) > L then BufLen := L;
if BufLen > MaxAvail then BufLen := MaxAvail;
if BufLen < Length(What)*2 then begin F^.Seek(OldPos); goto LExit; end;
StPos := 0;
if Display then
begin
R.Assign(1,1,30,9);
New(Info, Init(R));
Info^.Top := GetString(dlSearchProgress);
NewTimer(Tmr, 2); Inserted := False;
end;
GetMem(Buf, BufLen); I := Pos; BPos := 0; CancelSearch := Off;
repeat
if Back then
begin
NextPos := I - BufLen + BPos;
if NextPos < 0 then begin NextPos := 0; BPos := 0; end;
J := I - NextPos;
if J <= 0 then Break;
end else
begin
NextPos := F^.GetPos;
J := L - I + BPos; if J > BufLen then J := BufLen; ClrIO;
end;
if Display then
begin
Info^.Write(1, StrGrd(L, I, 30));
Info^.Write(2, ItoS(Percent(L, I))+'%');
if (not Inserted) and TimerExpired(Tmr) then
begin
Desktop^.Insert(Info);
Inserted := True;
end;
if Inserted then DispatchEvents(Info, CancelSearch);
end;
F^.Seek(NextPos);
F^.Read(Buf^[BPos], J-BPos); T := J-BPos;
1:
if J <= 0 then BufPos := 0 else
if Back then BufPos := BackSearchFor(What, Buf^[StPos], J-StPos, CaseSensitive)
else BufPos := SearchFor(What, Buf^[StPos], J-StPos, CaseSensitive);
if BufPos > 0 then
begin
if WholeWords and not (((BufPos = 1) or (Char(Buf^[BufPos-2+StPos]) in BreakChars)) and
(Char(Buf^[BufPos+Length(What)-1+StPos]) in BreakChars)) then
begin
if Back then Dec(J, StPos-BufPos)
else Inc(StPos, BufPos);
Goto 1;
end;
FreeMem(Buf, BufLen); F^.Seek(OldPos);
SearchFileStr := NextPos - LongInt(BPos) + LongInt(BufPos) - 1 + StPos;
goto LExit;
end;
if Back then
begin
Dec(I,T);
if I < -T then Break;
if I < 0 then I := 0;
end else Inc(I,T);
StPos := 1;
BPos := Length(What)+1;
if I < L then Move(Buf^[J-BPos], Buf^[0], BPos);
until Back and (I < 0) or
not Back and ((I >= L) or (T <= 0) or CancelSearch or (F^.GetPos >= F^.GetSize));
if CancelSearch then SearchFileStr := -2;
FreeMem(Buf, BufLen); F^.Seek(OldPos);
LExit: FreeObject(Info);
end;
constructor TViewInfo.Init(var R: TRect; AViewer: PFileViewer);
begin
inherited Init(R);
Viewer := AViewer;
GrowMode := gfGrowHiY + gfGrowLoY;
EventMask := evMouse;
end;
constructor TViewInfo.Load(var S: TStream);
begin
inherited Load(S);
GetPeerViewPtr(S, Viewer);
end;
procedure TViewInfo.Store(var S: TStream);
begin
inherited Store(S);
PutPeerViewPtr(S, Viewer);
end;
procedure TViewInfo.Draw;
var B: TDrawBuffer;
S: String;
I,J: Integer;
begin
if Viewer = nil then begin inherited Draw; Exit; end;
With Viewer^ do
begin
if ViewMode <> 1 then
begin
if Wrap then S := '>=<][' else S := '<=>][';
if (Lines[Size.Y].Pos < 0) and (FilePos+BufSize >= FileSize)
or (FileSize = 0) then S := S + '100'
else S := S + ItoS(Percent(FileSize, FilePos+BufPos));
S := S + '% of ' + FStr(Viewer^.FileSize) + ' Bytes';
end else
begin
S := Hex8(Cur.X div (1+Byte(HexEdit))+Cur.Y*HexPos+BufPos+FilePos)+' ';
J := (FilePos+BufPos) mod max(1,HexPos);
for I := 0 to HexPos - 1 do
S := S+Hex2(I+J)+'Ä';
Dec(S[0]);
end;
Insert('[', S, 1);
case Filter of
0: AddStr(S, ']');
1: Insert(']{ASCII}', S, Length(S)+1);
else Insert(']{32-255}', S, Length(S)+1);
end;
if UseXlat then Insert('(X)', S, Length(S)+1);
end;
if Byte(S[0]) <> Size.X then GrowTo(Byte(S[0]), 1);
MoveStr(B, S, Owner^.GetColor(2));
WriteLine(0, 0, Size.X, Size.Y, B);
end;
procedure TViewInfo.HandleEvent;
var P: TPoint;
begin
if (Event.What = evMouseDown) and Event.Double and (Viewer^.ViewMode = 0) then
begin
MakeLocal(Event.Where, P);
if P.X < 5 then Message(Viewer, evCommand, cmUnwrap, nil);
ClearEvent(Event);
end;
end;
function TFileViewer.WriteModify;
var B: Boolean;
I: LongInt;
S: TDOSStream;
begin
B := BufModified; WriteModify := Off;
if (Buf = nil) or (Fl = nil) or (Fl^.Status <> stOK) or not B then Exit;
I := MessageBox(GetString(dlViewQuery), nil, mfWarning+mfYesNoCancel);
WriteModify := I = cmCancel;
if I <> cmCancel then BufModified := Off;
if I = cmNo then begin Seek(FilePos); DrawView; end;
if I <> cmYes then Exit;
if (TypeOf(Fl^)=TypeOf(TDOSStream)) or (TypeOf(Fl^)=TypeOf(TBufStream)) then
begin
Dispose(Fl, Done);
Fl := New(PDosStream, Init(FileName, stOpen));
if (Fl^.Status <> stOk) or (Abort) then
begin
Dispose(Fl, Done); Fl := New(PDosStream, Init(FileName, stOpenRead));
MessageBox(GetString(dlFBBNoWrite)+FileName, nil, mfError + mfOKButton);
Exit;
end;
end;
Fl^.Seek(FilePos); Fl^.Write(Buf^, BufSize);
RereadDirectory(GetPath(FileName));
end;
const SCViewer: String[Length(CViewer)] = CViewer;
SCHViewer: String[Length(CHViewer)] = CHViewer;
SCNViewer: String[Length(CInputLine)] = CInputLine;
SCViewWindow: String[Length(CViewWindow)] = CViewWindow;
function TFileViewer.GetPalette;
begin
GetPalette := @SCViewer;
end;
function TNFileViewer.GetPalette;
begin
GetPalette := @SCNViewer;
end;
function THFileViewer.GetPalette;
begin
GetPalette := @SCHViewer;
end;
function TFileWindow.GetPalette;
begin
GetPalette := @SCViewWindow;
end;
{TViewScroll}
function TViewScroll.GetSize: Integer;
var
S: Integer;
begin
if Size.X = 1 then S := Size.Y else S := Size.X;
if S < 3 then GetSize := 3 else GetSize := S;
end;
procedure TViewScroll.DrawPos(Pos: Integer);
var
S: Integer;
B: TDrawBuffer;
Const Chars : TScrollChars = (#30, #31, #177, #254, #178);
begin
S := GetSize - 1;
MoveChar(B[0], Chars[0], GetColor(2), 1);
{ if Max = Min then
MoveChar(B[1], Chars[4], GetColor(1), S - 1)
else }
begin
MoveChar(B[1], Chars[2], GetColor(1), S - 1);
MoveChar(B[Pos], Chars[3], GetColor(3), 1);
end;
MoveChar(B[S], Chars[1], GetColor(2), 1);
WriteBuf(0, 0, Size.X, Size.Y, B);
end;
procedure TViewScroll.HandleEvent;
var B : Boolean;
P: TPoint;
RD,SP: LongInt;
Extent: TRect;
Tracking : Boolean;
begin
TView.HandleEvent(Event);
case Event.What of
evCommand: case Event.Command of
cmChangeValue: begin Value := Event.InfoLong;
DrawView; ClearEvent(Event)
end;
{ cmSetMargins: Message(Owner, evCommand, cmGotoLineNumber, Pointer(Value));}
end;
evMouseDown: if MaxV>0 then begin
MakeLocal(Event.Where, P);
GetExtent(Extent);
{if LongDiv(LongMul(Value,Size.Y-2),MaxV)+1 = P.Y then}
if Longint( Longint(Longint(Value) * Longint(Size.Y-2)) div MaxV ) + 1 = P.Y then
begin
RD := P.Y;
repeat
MakeLocal(Event.Where, P);
Tracking := Extent.Contains(P);
if Tracking then
begin
{ if Size.X = 1 then} SP := P.Y {else I := P.X};
if SP <= 0 then SP := 1;
if SP >= Size.Y - 1 then SP := Size.Y - 2;
end;{ else I := GetPos;}
if SP <> RD then
begin
DrawPos(SP);
RD := SP;
end;
until not MouseEvent(Event, evMouseMove+evMouseAuto);
if SP = Size.Y - 2 then
begin
Value := MaxV - (Size.Y-10)*10;
if Value > MaxV then Value := MaxV;
end
else if SP=1 then Value := 0
{ else Value := LongMul( LongDiv(MaxV,Size.Y-2),SP );}
else Value := Longint( Longint( MaxV div Longint(Size.Y-2)) * SP );
if Size.Y*4 > MaxV then Value:=0;
DrawView;
Message(Owner, evCommand, cmScrollBarChanged, Pointer(Value));
Exit;
end;
MakeLocal(Event.Where,P); RD := RepeatDelay; RepeatDelay := 0;
if Value >= MaxV then SP := Size.Y - 2
else SP := GetPartCode;
if P.Y = 0 then
begin
repeat
MakeLocal(Event.Where,P);
if (P.X>=-1) and (P.X<=1) and (P.Y>=-1) and (P.Y<=1) then
Message(Owner, evKeyDown, kbUp, nil);
until not MouseEvent(Event, evMouseMove+evMouseAuto);
end else
if P.Y = Size.Y-1 then
begin
repeat
MakeLocal(Event.Where,P);
if (P.X>=-1) and (P.X<=1) and (P.Y>=Size.Y-2) and (P.Y<=Size.Y) then
Message(Owner, evKeyDown, kbDown, nil);
until not MouseEvent(Event, evMouseMove+evMouseAuto);
end else
if P.Y < SP then
begin
RepeatDelay := RD;
repeat
if Value >= MaxV then SP := Size.Y - 2
else SP := GetPartCode;
MakeLocal(Event.Where,P);
if (P.X>=-1) and (P.X<=1) and (P.Y<SP) then
Message(Owner, evKeyDown, kbPgUp, nil);
until not MouseEvent(Event, evMouseMove+evMouseAuto);
end else
if P.Y > SP then
begin
RepeatDelay := RD;
repeat
if Value >= MaxV then SP := Size.Y - 2
else SP := GetPartCode;
MakeLocal(Event.Where, P);
if (P.X>=-1) and (P.X<=1) and (P.Y>SP) then
Message(Owner, evKeyDown, kbPgDn, nil);
until not MouseEvent(Event, evMouseMove+evMouseAuto);
end;
RepeatDelay := RD;
end;
end;
end;
function TViewScroll.GetPartCode: LongInt;
begin
GetPartCode := 1+((Size.Y-2)*Value) div MaxV;
end;
function TViewScroll.GetPalette;
const S: String[Length(CScrollBar)] = CScrollBar;
begin
GetPalette := @S;
end;
procedure TViewScroll.Draw;
var B: Array[0..128] of record C: Char; B: Byte; end;
B1: TDrawBuffer Absolute B;
C1, C2: Byte;
begin
C1 := GetColor(1); C2 := GetColor(2);
MoveChar(B, '±', C1, Size.Y);
if Value >= MaxV then B1[Size.Y-2] := 254+LongInt(C2*256)
else B1[GetPartCode] := 254+LongInt(C2*256);
B1[0] := 30+LongInt(C2*256);
B1[Size.Y-1] := 31+LongInt(C2*256);
WriteBuf(0,0,1,Size.Y,B);
end;
{ THFileViewer }
procedure THFileViewer.ChangeBounds;
var R: TRect;
begin
SetBounds(Bounds);
if Wrap then MakeLines;
if SB <> nil then
begin R :=Bounds; R.A.X := R.B.X; Inc(R.B.X); SB^.SetBounds(R) end
end;
{ TFileViewer }
procedure TFileViewer.ChangeBounds;
var R: TRect;
begin
SetBounds(Bounds);
if Wrap or (ViewMode = 2) then MakeLines;
DrawView;
end;
procedure TFileViewer.AdjustBufSize;
begin
BufSize := ViewerBufSize;
If BufSize > FileSize then BufSize := FileSize;
If MaxAvail < BufSize then BufSize := MaxAvail;
end;
constructor TFileViewer.Init;
begin
inherited Init(Bounds); ViewMode := 0;
FillChar(Xlat, SizeOf(XLat), 0); UseXLat := Off;
HelpCtx := hcView;
GrowMode := gfGrowHiX + gfGrowHiY;
Options := Options or ofSelectable or (LongInt(Quick)*ofTopSelect);
EventMask := $FFFF; SB := ASB; QuickView := Quick;
Buf := nil; IsValid := True; Loaded := Off;
KillAfterUse := TempFile <> '';
TempFile := '';
Wrap := EditorDefaults.ViOpt and 2 <> 0;
ViewMode := {EditorDefaults.ViOpt and}Byte(Hex);
Fl := AStream;
if (AFileName = '') and (Fl = nil) then Exit;
if Fl = nil then ReadFile(AFileName, On) else
if (Fl <> nil) and (Fl^.Status = stOK) then ReadFile(' ', Off)
else IsValid := Off;
end;
destructor TFileViewer.Done;
var F: File;
begin
EnableCommands([cmGotoCell,cmUnwrap]);
if Buf <> nil then FreeMem(Buf, BufSize);
if Fl <> nil then Dispose(Fl, Done);
inherited Done;
end;
constructor TFileViewer.Load;
var I: LongInt;
begin
inherited Load(S);
NoEdit := Off;
BufModified := Off;
GetPeerViewPtr(S, SB);
GetPeerViewPtr(S, Info);
S.Read(FileName[0], 1);
S.Read(FileName[1], Length(FileName));
S.Read(I, Sizeof(LongInt));
S.Read(QuickView, 1);
S.Read(Wrap, 1);
S.Read(KillAfterUse, 1);
S.Read(Filter, 1);
S.Read(ViewMode, 2);
S.Read(XlatFile, 9);
S.Read(XLAT, SizeOf(XLAT));
S.Read(UseXLAT, SizeOf(UseXLAT));
Fl := nil; Buf := nil; IsValid := True; Loaded := On;
if FileName = '' then Exit;
ReadFile(FileName, On); Seek(I);
end;
procedure TFileViewer.Store;
var I: LongInt;
begin
inherited Store(S);
PutPeerViewPtr(S, SB);
PutPeerViewPtr(S, Info);
S.Write(FileName[0], 1 + Length(FileName));
I := FilePos + LongInt(BufPos);
S.Write(I, Sizeof(LongInt));
S.Write(QuickView, 1);
S.Write(Wrap, 1);
S.Write(KillAfterUse, 1);
S.Write(Filter, 1);
S.Write(ViewMode, 2);
S.Write(XlatFile, 9);
S.Write(XLAT, SizeOf(XLAT));
S.Write(UseXLAT, SizeOf(UseXLAT));
end;
procedure XDumpStr(var S: String; var B; Addr: LongInt; Count: Integer; Filter: Byte);
begin
S := Hex8(Addr)+' ';
asm
les si, B
mov cx, Count
push ds
lds di, S
mov bx, 10
mov ah, Filter
jcxz @@3
@@1:
mov al, es:[si]
or ah, ah
jz @@2
cmp al, 32
jnc @@4
@@5:
mov al, 'ú'
jmp @@2
@@4:
cmp ah, 1
jnz @@2
cmp al, 128
jnc @@5
@@2:
mov ds:[di+bx], al
inc byte ptr ds:[di]
inc si
inc bx
loop @@1
@@3:
pop ds
end;
end;
procedure TFileViewer.Draw;
var
B: Array[0..256*9] of Word;
C: Byte;
I, J, K, M, Src: Integer;
L: LongInt;
S: String;
procedure MoveStr(var B, S; C, Flt: Byte); assembler;
asm
push ds
lds si,B
les di,S
mov cl, es:[di]
xor ch,ch
jcxz @@Exit
mov bx, 0
mov ah, C
@@1:
mov al, es:[di+1]
cmp al, 9
jne @@3
mov al, ' '
@@2:
mov ds:[si], ax
add si,2
inC bx
mov dx, bx
and dx, 7
jnz @@2
jmp @@4
@@3:
cmp Flt, 0
jz @@@2
cmp al, 32
jnc @@@4
@@@5:
mov al, 'ú'
jmp @@@2
@@@4:
cmp Flt, 1
jnz @@@2
cmp al, 128
jnc @@@5
@@@2:
mov ds:[si], ax
add si,2
inc bx
@@4:
inc di
loop @@1
@@Exit:
pop ds
end;
procedure XlatBuf(var B; Len: Integer; var XTable); assembler;
asm
cmp Len, 0
jle @@1
push ds
mov cx, Len
lds si, B
les bx, XTable
xor ah, ah
cld
@@Loop:
lodsb
mov di, ax
mov al, es:[bx+di]
mov ds:[si-1], al
loop @@Loop
pop ds
@@1:
end;
var W: Integer;
begin
if Buf = nil then
begin
ReadFile(FileName, On);
BufPos := 0;
MakeLines;
end
else
if (BufPos < 0) or (BufPos > BufSize + 20000) or
(BufPos > 65000) then
begin
BufPos := 0;
MakeLines;
end;
C := GetColor(1);
if (FileName = '') or (Buf = nil) then
begin HideCursor; inherited Draw; Exit end;
if ViewMode = 1 then
begin
EnableCommands([cmGotoCell]);
DisableCommands([cmUnwrap]);
HexPos := (Size.X - 12) div 4; if HexPos <= 0 then HexPos := 1;
XDelta := 0;
while (LongInt(Cur.Y * HexPos) + BufPos + FilePos >= FileSize) do Dec(Cur.Y);
while (LongInt(Cur.Y * HexPos) + BufPos + FilePos + Cur.X div (Byte(HexEdit)+1) >= FileSize) do Dec(Cur.X);
if Cur.Y < 0 then
begin
W := -Cur.Y;
Cur.Y := 0;
CountUp(W);
Exit;
end;
if Cur.Y >= Size.Y then
begin
W := Cur.Y - Size.Y + 1;
Dec(Cur.Y, W);
CountDown(W);
Exit;
end;
W := BufPos;
L := FilePos + LongInt(BufPos);
if HexEdit then SetCursor(10 + (Cur.X div 2) * 3 + Cur.X and 1, Cur.Y)
else SetCursor(12 + HexPos * 3 + Cur.X, Cur.Y);
ShowCursor;
for I := 0 to Size.Y - 1 do
begin
MoveChar(B[XDelta], ' ', C, Size.X);
if L + LongInt(HexPos) > FileSize then J := FileSize - L else J := HexPos;
if J > 0 then
begin
if J = HexPos then Drivers.MoveStr(B, DumpStr(Buf^[W], L, J, Filter), C)
else
begin
S := DumpStr(Buf^[W], L, J, Filter);
Drivers.MoveStr(B, Copy(S, 1, J*3+10), C);
Drivers.MoveStr(B[10+HexPos*3], Copy(S, J*3+11, 255), C);
end;
if SearchActive then
begin
if (L <= SearchX) and (L+J > SearchX) then
begin
Cur.Y := I; Cur.X := (SearchX-L)*2;
if HexEdit then SetCursor(10 + (Cur.X div 2) * 3 + Cur.X and 1, Cur.Y)
else SetCursor(12 + HexPos * 3 + Cur.X, Cur.Y);
ShowCursor;
Src := Min(L+J-SearchX, Length(SearchString.What));
MoveColor(B[10+(SearchX-L)*3], Src*3-1, GetColor(2));
MoveColor(B[12+HexPos*3+SearchX-L], Src, GetColor(2));
Src := Length(SearchString.What) - Src;
end else
if Src > 0 then
begin
K := Min(Src, J);
MoveColor(B[10], K*3-1, GetColor(2));
MoveColor(B[12+HexPos*3], K, GetColor(2));
Dec(Src, J);
end;
end;
Inc(W, J);
Inc(L, LongInt(J));
end;
WriteLine(0, I, Size.X, 1, B[XDelta]);
end;
end
else
if ViewMode = 2 then
begin
EnableCommands([cmGotoCell]);
DisableCommands([cmUnwrap]);
HideCursor;
HexPos := ((Size.X - 9) div 16)*16; if HexPos < 16 then HexPos := 16;
W := BufPos; L := FilePos + LongInt(BufPos); Src := 0;
for I := 0 to Size.Y - 1 do
begin
MoveChar(B[XDelta], ' ', C, Size.X);
if L + LongInt(HexPos) > FileSize then J := FileSize - L else J := HexPos;
if J > 0 then
begin
XDumpStr(S, Buf^[W], L, J, Filter);
if UseXlat then XLatBuf(S[10], Length(S)-9, XLat);
Drivers.MoveStr(B, S, C);
if SearchActive then
begin
if (L <= SearchX) and (L+J > SearchX) then
begin
Src := Min(L+J-SearchX, Length(SearchString.What));
MoveColor(B[9+SearchX-L], Src, GetColor(2));
Src := Length(SearchString.What) - Src;
end else
if Src > 0 then
begin
MoveColor(B[9], Min(Src, J), GetColor(2));
Dec(Src, J);
end;
end;
Inc(W, J);
Inc(L, LongInt(J));
end;
WriteLine(0, I, Size.X, 1, B[XDelta]);
end;
end
else
begin
DisableCommands([cmGotoCell]);
EnableCommands([cmUnwrap]);
for I := 0 to Size.Y - 1 do
begin
HideCursor;
J := SearchX - FilePos - Lines[I].Pos;
if SearchActive and (Lines[I].Pos <= SearchX - FilePos) and
(Lines[I+1].Pos >= SearchX - FilePos) then
begin
K := 0; M := Lines[I].Pos;
While M < J + Lines[I].Pos do
begin repeat Inc(K) until (Buf^[M] <> 9) or (K and 7 = 0); Inc(M); end;
if (XDelta + Size.X < K) or (XDelta > K ) then XDelta := K - Size.X div 2;
if XDelta < 0 then XDelta := 0;
end;
MoveChar(B[XDelta], ' ', C, Size.X);
if (Lines[I].Pos >=0) then
begin
Move(Buf^[Lines[I].Pos], S[1], Lines[I].Len); S[0] := Char(Lines[I].Len);
if UseXlat then XLatBuf(S[1], Length(S), XLat);
MoveStr(B, S, C, Filter);
end;
if SearchActive and (Lines[I].Pos <= SearchX - FilePos) and
(Lines[I+1].Pos >= SearchX - FilePos) then
begin
K := 0; M := Lines[I].Pos;
While M < J + Lines[I].Pos do
begin repeat Inc(K) until (Buf^[M] <> 9) or (K and 7 = 0); Inc(M); end;
MoveColor(B[K], Length(SearchString.What), GetColor(2));
end;
WriteLine(0, I, Size.X, 1, B[XDelta]);
end;
end;
if Info <> nil then Info^.DrawView;
end;
procedure TFileViewer.ReadFile;
var I, J: Integer;
P: Pointer;
begin
WriteModify;
if Buf <> nil then FreeMem(Buf, BufSize); Buf := nil;
BufModified := Off;
IsValid := True;
FileName := FName;
if NewStream then
begin
if Fl <> nil then Dispose(Fl, Done); Fl := nil;
if FName = '' then Exit;
Fl := New(PDosStream, Init(FName, stOpenRead));
RdOnly := Off;
end;
if Fl^.Status <> stOK then begin IsValid := False; Exit end;
FileSize := FL^.GetSize; FilePos := 0; I := 1; NumLines := 1;
if FileSize < 0 then FileSize := 0;
AdjustBufSize;
Buf := MemAlloc(BufSize);
if SB <> nil then PViewScroll(SB)^.MaxV := FileSize;
Seek(0); XDelta := 0;
end;
procedure TFileViewer.SetState;
begin
inherited SetState(AState, Enable);
if (AState and (sfActive + sfSelected) <> 0) then
if GetState(sfSelected) and (Owner^.GetState(sfActive)) then
begin if SB <> nil then begin SB^.Show; SB^.EventMask := $FFFF end; DrawView end
else if SB <> nil then begin SB^.Hide; SB^.EventMask := 0; end;
end;
procedure TFileViewer.HideView;
var E: TEvent;
begin
if SB <> nil then begin SB^.Hide; SB^.EventMask := 0; end;
Message(Owner, evCommand, cmDisableView, nil);
inherited HideView;
end;
procedure TFileViewer.ShowView;
var E: TEvent;
begin
if SB <> nil then begin SB^.Show; SB^.EventMask := 0; end;