;	*-------+---------------------------------------*
;	|Name	| Dir					|
;	|Version| 0.1	Comment:Com-piling		|
;	+-------+---------------------------------------+
;	|Shows a sorted dir, 3 columns, Name & Size	|
;	|						|
;	+-----------------------------------------------+
;	|  	  Copyright (C) 1993 MACROSOFT		|
;	*-----------------------------------------------*
LibBits=%00000000
;if OpenLib/CloseLib is used, set LibBits!
;Bits 0-7=Dos,Int,Gfx,Con,Icon,MathF,MathT,MathI
;Presume that all macros destroy D0-D1/A0-A1, and preserves all other regs.
;Macros that use supplied work-registers preserve ALL registers.
;When calling a macro, A5 MUST BE INIT-ADR+32768 AND A6 MUST BE $DFF002!
;Most Macros CONTAIN GLOBAL LABELS, so use local only in-between Macro calls.
;Labels to define if relevant macros are used:
;Params (#Max longs)
;CurrHdl (current filehandle storage long)
;Buf (blk.b 81,0) for conversion & input routines
INCLUDE "CO:Symbols.S"
INCLUDE "CO:Makaron.S"
B:
	INIT
	OPENLIBS
	
Quit:	CLOSELIBS
	RTS


********************************
NumStr:				;convert Dec/Hex d0.L->TxtBuf.d1.B:0=dec,1=hex
	MOVEM.L D0-D2/D7,-(SP)		;d7=no. of digits
	lea TxtBuf(PC),a0
	tst.b d1			;hexadecimal?
	bne.s NShex			;yes, other conv
NSdec:	move.b #"+",(a0)+
	tst.l d0
	bpl.s .NoNeg
	neg.l d0
	move.b #"-",-1(a0)
.NoNeg:	move.l #10000,d1
	divu d1,d0			;de 5 overzta ziffrorna
	swap d0
	moveq #0,d2
	move.w d0,d2			;resten (0-9999) i d2
	clr.w d0
	swap d0
	moveq #4,d7			;5 digits
.loop1:	divu d1,d0
	add.b #$30,d0			;digits ASCII $30-$3a
	move.b d0,(a0)+			;Store in TxtBuf
	clr.w d0
	swap d0				;remainder
	divu #10,d1			;next digit
	dbf d7,.loop1
	move.l #1000,d1
	moveq #3,d7			;4 digits
.loop2:	divu d1,d2
	add.b #$30,d2			;digits ASCII $30-$3a
	move.b d2,(a0)+			;Store in TxtBuf
	clr.w d2
	swap d2				;remainder
	divu #10,d1			;next digit
	dbf d7,.loop2
	clr.b (a0)
	moveq #7,d7			;at least 1 digit
	bra.s NScont
NShex:	move.b #"$",(a0)+
	addq.w #8,a0
	clr.b (a0)
	moveq #7,d7
.loop:	move.b d0,d1
	and.b #$f,d1
	add.b #$30,d1			;digits
	cmp.b #$3a,d1			;abcdef?
	blt.s .NoAdd
	addq.b #7,d1			;yes, add ASCII "A"-$3a
.NoAdd:	move.b d1,-(a0)
	lsr.l #4,d0
	dbf d7,.loop
	moveq #5,d7			;at least two hex digits
NScont:	lea TxtBuf+1(PC),a0
.loop:	cmp.b #"0",(a0)+
	bne.s NSret
	dbf d7,.loop
	addq.w #1,a0
NSret:	subq.w #1,a0
	MOVEM.L (SP)+,D0-D2/D7		;a0=1st non-0 digit.
	RTS
********************************
Copy0:		;copy (a0) to (a1) until 10 or 0, last byte-->0
	move.b (a0)+,d0
	move.b d0,(a1)+
	beq.s .End
	cmp.b #10,d0
	bne.s Copy0
.End:	clr.b -1(a1)
	RTS
********************************
LockIt:
	MOVEM.L D1-D7/A0-A6,-(SP)
	move.l #DirName,d1
	moveq #-2,d2
	jsr -84(a6)			;Lock
	move.l d0,LockSav
	bsr RefreshGGs
	MOVEM.L (SP)+,D1-D7/A0-A6
	RTS

FRdir:					;DIR OF [DirName].Uses TxtBuf-->DirData
	move.w #-1,DirOffs		;so that ALWAYS updated!
	moveq #8,d0
	moveq #22,d1
	move.w #42*8,d2
	moveq #8,d3
	moveq #1,d4
	bsr MyRect			;clr dirname area
	moveq #0,d5			;nr of files
	lea DirData(PC),a3		;file/dirnames dest
	move.l DosBase(PC),a6
	bsr LockIt
	move.l LockSav(PC),d1
	beq.w .Err
	lea FileInfo(PC),a4
	move.l a4,d2
	jsr -102(a6)			;transfer file/dir data
	tst.l d0
	beq.w .Err

	MOVEM.L D0-D7/A0-A6,-(SP)
	bsr RefreshGGs
	lea T02(PC),a0			;"reading dir..."
	bsr BarMsg
	lea 8(a4),a0
	clr.b 29(a0)
	move.w #22*bytes+1,d0		;type disk/dirname
	bsr Print
	lea T04(PC),a0			;":"
	bsr Print
	move.w d0,d7
	move.l LockSav(PC),d1
	lea DiskInfo(PC),a4
	move.l a4,d2
	jsr -114(a6)			;UNIT INFO!
	move.l 12(a4),d0		;numblocks.L
	sub.l 16(a4),d0			;-blocksused.L
	subq.l #2,d0			;=blocksfree
	bpl.s .No9
	moveq #0,d0
.No9:	move.l 20(a4),d1		;(*bytesperblock)
	mulu d1,d0			;bytes free
	moveq #0,d1			;deci
	bsr NumStr
	move.w d7,d0
	bsr Print
	lea T05(PC),a0			;" Free"
	bsr Print
	bsr ClrDirWW
	move.w #339,d0			;clr contents-bar
	moveq #45,d1
	moveq #10,d2
	moveq #70,d3
	moveq #3,d4
	bsr MyRect
	move.l a3,a0			;CLEAR FILENAME-AREA (for dir)
	move.l #$20202020,d0
	move.w #38*128/4-1,d1
.SpcLp:	move.l d0,(a0)+
	dbf d1,.SpcLp
	MOVEM.L (SP)+,D0-D7/A0-A6

.Loop:	move.w #$0100,TxtBpl
	move.l LockSav(PC),d1
	move.l a4,d2
	jsr -108(a6)			;transfer file/dir data
	tst.l d0
	beq.s .Err			;end of files/dirs?
	btst #6,$bfe001
	beq.w .DirBrk
	move.l a3,a1
	tst.l 4(a4)			;dir?
	bmi.s .NoDir
	move.w #$0001,TxtBpl		;yep,set color&skip length
	bra.s .YepDir
.NoDir:	move.l 124(a4),d0		;filelen.L
	moveq #0,d1			;deci
	bsr NumStr
	lea TxtBuf+4(PC),a0		;max 6 digits(999k)
	lea -1(a3),a1
.ZeroLp:addq.w #1,a1
	cmp.b #"0",(a0)+
	beq.s .ZeroLp
	subq.w #1,a0
	bsr Copy0			;copy deci file length
	move.b #32,-(a1)
.YepDir:lea 8(a4),a0
	clr.b 30(a0)			;max 30 chars in item name
	lea 7(a3),a1
	bsr Copy0			;copy filename
	addq.w #1,d5			;d5=file&dirnumber
	cmp.w #128,d5
	bge.s .Err
	cmp.w #12,d5			;print the filename to the screen?
	bgt.s .Dont
	move.w d5,d0
	mulu #8*bytes,d0
	add.w #24*bytes+3,d0
	move.l a3,a0
	bsr PrintLF
.Dont:	lea 38(a3),a3
	bra .Loop
.Err:	jsr -132(a6)			;dos error number-->d0
	cmp.l #$e8,d0
	bne.s DIRERR
.NoErr:	move.w d5,DirItems
	subq.w #1,DirItems
	bsr FRsortrtn
	clr.w d0
	bsr UpdateDir
	bsr BarRdy
	bra MLRET
.DirBrk:move.w d5,DirItems
	subq.w #1,DirItems
	lea T03(PC),a0			;"User Break!"
	bsr BarMsg
	clr.w d0
	bsr UpdateDir
	bra MLRET
DIRERR:	move.w #-1,DirItems
	lea T00(PC),a0			;"Dir Error!"
	bsr BarErr
	bsr RefreshGGs
	bsr UpdateDir
	bra MLRET

Print:				;a0=txt,d0=bploffs
	MOVEM.L D1-D7/A1-A6,-(SP)
	lea BplPtrs(PC),a3
	movem.l (a3)+,d4-d5
	sub.l d4,d5
	ext.l d0
	add.l d0,d4
	moveq #0,d2			;x-add
	moveq #0,d0
	moveq #0,d1
	move.b (a3)+,d0
	move.b (a3)+,d1
	lea Font(PC),a3
	lea 96*8(a3),a1			;inverted font
PRcol:	add.w d0,d0
	add.w d0,d0
	add.w d1,d1
	add.w d1,d1
	lea PrtJmpTbl(PC),a4
	move.l (a4,d0.w),a5
	move.l 4*4(a4,d1.w),a6
PRloop:	moveq #0,d3
	move.b (a0)+,d3
	bmi.w PRnext
	sub.b #32,d3
	bpl.s .Char
.Ctrl:	cmp.b #0-32,d3
	beq.w PRend
	cmp.b #10-32,d3
	bne.s .NoLF
	add.l #8*bytes,d4
	moveq #0,d2
	bra.s PRloop
.NoLF:	cmp.b #5-32,d3
	bne.s .Nolf2
	add.l #4*bytes,d4
	moveq #0,d2
	bra.s PRloop
.Nolf2:	cmp.b #9-32,d3
	bne.s .NoTAB
	addq.w #8,d2
	and.w #-8,d2
	bra.s PRloop
.NoTAB:	cmp.b #1-32,d3
	bne.s .NoCol
	move.b (a0)+,d0
	move.b (a0)+,d1
	bra.s PRcol
.NoCol:	bra.s PRloop

.Char:	lsl.w #3,d3
	move.l d4,a2
	lea (a2,d2.w),a2
	jmp (a5)
Bpl1Ret:move.b (a4)+,(a2)
	move.b (a4)+,bytes*1(a2)
	move.b (a4)+,bytes*2(a2)
	move.b (a4)+,bytes*3(a2)
	move.b (a4)+,bytes*4(a2)
	move.b (a4)+,bytes*5(a2)
	move.b (a4)+,bytes*6(a2)
	move.b (a4)+,bytes*7(a2)
	add.l d5,a2
	jmp (a6)
Bpl2Ret:move.b (a4)+,(a2)
	move.b (a4)+,bytes*1(a2)
	move.b (a4)+,bytes*2(a2)
	move.b (a4)+,bytes*3(a2)
	move.b (a4)+,bytes*4(a2)
	move.b (a4)+,bytes*5(a2)
	move.b (a4)+,bytes*6(a2)
	move.b (a4)+,bytes*7(a2)
PRnext:	addq.w #1,d2
	bra PRloop
PRend:	move.l d4,d0
	add.l d2,d0
	sub.l BplPtrs(PC),d0
	MOVEM.L (SP)+,D1-D7/A1-A6
	RTS

Prt00:	move.l a3,a4
	bra.s Bpl1Ret
Prt01:	lea (a3,d3.w),a4
	bra.s Bpl1Ret
Prt02:	lea (a1,d3.w),a4
	bra.s Bpl1Ret
Prt03:	move.l a1,a4
	bra.s Bpl1Ret
Prt10:	move.l a3,a4
	bra.s Bpl2Ret
Prt11:	lea (a3,d3.w),a4
	bra.s Bpl2Ret
Prt12:	lea (a1,d3.w),a4
	bra.s Bpl2Ret
Prt13:	move.l a1,a4
	bra.s Bpl2Ret

PrtJmpTbl:
dc.l Prt00,Prt01,Prt02,Prt03
dc.l Prt10,Prt11,Prt12,Prt13
