	load	'macros.dump'
	include	'driver.equ'
	include	'scrap.equ'
	include	'dbsc.equ'


	IMPORT	D_AlertBox
	IMPORT	X_AsciiFilter
	IMPORT	D_AuxHand
	IMPORT	D_BeachBall
	IMPORT	X_Blurb2
	IMPORT	X_Button1Text
	IMPORT	X_Button2Text
	IMPORT	X_Button3Text
	IMPORT	D_CloseDialog
	IMPORT	X_ContText
	IMPORT	D_CurCursor
	IMPORT	X_DefaultDBFieldBlock
	IMPORT	D_DigitsOnly
	IMPORT	X_FSBlurbString
	IMPORT	FSDialogTemplate
	IMPORT	X_FSScratch
	IMPORT	FieldRecSize
	IMPORT	X_FieldRecord
	IMPORT	X_FormatMask
	IMPORT	D_GetNewModalDialog
	IMPORT	D_GrowHandle
	IMPORT	X_InitFieldScanner
	IMPORT	D_NeedHand
	IMPORT	X_NextField
	IMPORT	X_OldCursor
	IMPORT	X_SStoAsciiScrap
	IMPORT	X_PLtoAsciiScrap
	IMPORT	D_SetCursor
	IMPORT	X_WPtoAsciiScrap
	IMPORT	X_DBButton1Text
	IMPORT	X_DBButton2Text
	IMPORT	X_DBButton3Text
	IMPORT	X_DBContText
	import	X_AsciitoDBScrap
	import	X_ClipData
	import	X_DoAsciitoDBScrap
	import	X_NumberStr
	

X_ScDoAsciitoDBScrap	PROC	EXPORT
	;Using X_DBScrapData
	;Using X_FieldScannerData
	;Using D_CursorData
	;Using X_ClipData

	input Src:l,SepFlag:w,ScanFields:w

	local Dptr:l,Dsize:l,Doff:l,ScrapPtr:l,D_AuxHand:l
	local Aptr:l,Asize:l,Aoff:l,MaxRow:w,MaxCol:w
	local Col:w,Result:w,StrSize:w,FieldPtr:l
	local DataOff:l,RowSize:l,Row:w,Cells:l
	local ScrapSize:l,Sptr:l

	output Dest:l

	error err

	begin

	MoveWord >D_CurCursor,X_OldCursor

	SpaceLong
	PushLong Src
	_GetHandleSize
	PullLong ScrapSize

	DecLong ScrapSize

	AddLong [Src],ScrapSize,Sptr

	short
	lda [Sptr]
	cmp #CR
	bne nocr
	long

	PushLong ScrapSize
	PushLong Src
	jsl D_GrowHandle
	bra scan

nocr	long
;Initialize scanner
scan	PushLong Src
	PushWord SepFlag
	PushWord ScanFields
	jsl X_InitFieldScanner

;Allocate string block
	SpaceLong
	PushLong #1024
	jsl D_NeedHand
	sta err
	PullLong D_AuxHand
	jcs error

;Allocate handle for scrap
	SpaceLong
	_MaxBlock
	PullLong ScrapSize

	SpaceLong
	PushLong ScrapSize
	jsl D_NeedHand
	sta err
	PullLong Dest
	jcs exit

;Initialize sizes, field block to copy, etc.
	Stzl Dsize
	Stzl Doff

	PushLong #X_DefaultDBFieldBlock
	PushLong #X_FieldRecord
	PushLong #FieldRecSize
	_BlockMove

	lda #X_DBScHeaderSize
	jsr FixDptr
	jcs exit

	MoveWord #X_DBCellType,[Dptr]
	MoveWord #0,[Dptr]:#X_DBLabels
	MoveWord #0,[Dptr]:#X_DBFormHeight
	MoveWord #0,[Dptr]:#X_DBHeaderHeight
	MoveWord #0,[Dptr]:#X_DBFooterHeight

	MoveLong #X_DBScHeaderSize,Dsize
	MoveLong #X_DBScHeaderSize,Doff

	Stzl Cells

	MoveLong [D_AuxHand],Aptr
	Stzl Asize
	Stzl Aoff
	MoveWord #1,MaxRow
	sta MaxCol
	sta Col

loop	jsl D_BeachBall
	SpaceWord
	jsl X_NextField
	PullWord Result

	lda X_FSScratch
	and #$ff
	sta StrSize
	bne notempty
	brl test

notempty	clc
	adc #4
	inc a
	jsr FixAptr
	jcs listerr

	MoveWord MaxRow,[Aptr]
	MoveWord Col,[Aptr]:#2

	PushLong #X_FSScratch
	Addwls #4,Aptr
	pea 0
	inc StrSize
	pei StrSize
	_BlockMove

	Addwl #4,Aoff
	Addwl StrSize,Aoff
	IncLong Cells

test	lda Result
	beq dofield
	bpl dorecord
	bra doendofscrap

dofield	Cmpw Col,MaxCol
	bge newmax
	bra nonewmax
newmax	MoveWord Col,MaxCol
nonewmax	inc Col
	brl loop

dorecord	Cmpw Col,MaxCol
	bge blah1
	bra blah2
blah1	MoveWord Col,MaxCol
blah2	MoveWord #1,Col
	inc MaxRow
	brl loop

doendofscrap
	Cmpw Col,MaxCol
	bge blah3
	bra blah4
blah3	MoveWord Col,MaxCol
blah4	MoveLong [Dest],Dptr
	MoveLong MaxRow,[Dptr]:#X_DBRows
	MoveLong MaxCol,[Dptr]:#X_DBColumns

	SpaceLong
	PushWord #(FieldRecSize+13)
	PushWord MaxCol
	_Multiply
	pla
	sta DataOff
	plx
	stx DataOff+2
	clc
	adc #X_DBScHeaderSize
	jcs listerr
	sta DataOff

	MoveLong DataOff,[Dptr]:#X_DBDataOffset

	MoveWord #1,Col
	MoveLong #(FieldRecSize+13),X_FieldRecord

colloop	jsl D_BeachBall
	lda #(FieldRecSize+13)
	jsr FixDptr
	jcs listerr

	PushLong #X_FieldRecord
	PushLong Dptr
	PushLong #FieldRecSize
	_BlockMove

	Addwl #FieldRecSize,Dptr
	MoveLong >FieldName,[Dptr]

	Addwl #4,Dptr
	MoveByte #0,[Dptr]
	IncLong Dptr
	MoveLong >FieldName,[Dptr]
	MoveLong #0,[Dptr]:#4
	lda	>FieldBody
	inc	a
	sta	>FieldBody
	bne	@1
	lda	>FieldBody+2
	inc	a
	sta	>FieldBody+2
@1	Addwl #(FieldRecSize+13),Doff

	lda Col
	cmp MaxCol
	bge endcolloop
	inc Col
	brl colloop

endcolloop
	MoveWord #1,Row

	MoveLong [D_AuxHand],Aptr

outer	MoveWord #1,Col
	Stzl RowSize
	MoveLong Doff,FieldPtr

inner	jsl D_BeachBall
	Cmpl #0,Cells
	jeq blankcell

	Cmpw [Aptr],Row
	jne blankcell

	Cmpw [Aptr]:#2,Col
	jne blankcell

	Addwl #4,Aptr
	lda [Aptr]
	and #$ff
	inc a
	sta StrSize
	clc
	adc #2
	jsr FixDptr
	jcs exit

	lda #0
	sta [Dptr]
	Addwl #2,Dptr

	PushLong Aptr
	PushLong Dptr
	pea 0
	PushWord StrSize
	_BlockMove

	Addwl StrSize,Aptr
	Addwl StrSize,Doff
	Addwl #2,Doff
	Addwl StrSize,RowSize
	DecLong Cells
	bra endinner

blankcell
	lda #2
	jsr FixDptr
	jcs listerr

	MoveWord #X_DBCellEmpty,[Dptr]

	Addwl #2,Doff
	IncLong RowSize

endinner
	lda Col
	cmp MaxCol
	bge endouter
	inc Col
	brl inner

endouter
	MoveLong [Dest],Dptr
	AddLong Dptr,FieldPtr,Dptr
	lda Row
	cmp MaxRow
	bge done
	inc Row
	brl outer

done	PushLong D_AuxHand
	_DisposeHandle

	PushLong Src
	_HUnlock

	brl exit

listerr
	PushLong D_AuxHand
	_DisposeHandle

error
	PushLong Dest
	_DisposeHandle

exit	PushWord X_OldCursor
	jsl D_SetCursor
	return

FieldName	DC.B	3
FieldBody	DC.L	$31313131

FixDptr	clc
	adc Dsize
	sta Dsize
	bcc x2
	inc Dsize+2
x2	pei Dsize+2
	pei Dsize
	pei Dest+2
	pei Dest
	jsl D_GrowHandle
	sta err
	bcc noprob
	rts
noprob	MoveLong [Dest],Dptr
	MoveLong Dptr,ScrapPtr
	AddLong Dptr,Doff,Dptr
	clc
	rts

FixAptr	clc
	adc Asize
	sta Asize
	bcc x3
	inc Asize+2
x3	pei Asize+2
	pei Asize
	pei D_AuxHand+2
	pei D_AuxHand
	jsl D_GrowHandle
	sta err
	bcc noprob2
	rts
noprob2	AddLong [D_AuxHand],Aoff,Aptr
	clc
	rts

	ENDP
	
X_ScImportAsciitoDBScrap	PROC	EXPORT
	;Using X_FieldScannerData
	;Using X_FSDialogData
	;Using X_ClipData
	;Using D_CursorData

	input Src:l

	local Dialog:l,ButtonValue:w,Fields:w

	output Dest:l

	error err

	begin

	PushLong Src
	jsl X_AsciiFilter

	MoveLong #X_Blurb2,X_FSBlurbString
	MoveLong #X_DBButton1Text,X_Button1Text
	MoveLong #X_DBButton2Text,X_Button2Text
	MoveLong #X_DBButton3Text,X_Button3Text
	MoveLong #X_DBContText,X_ContText

	stz ButtonValue

	SpaceLong
	PushLong #FSDialogTemplate
	jsl D_GetNewModalDialog
	sta err
	PullLong Dialog
	jcs exit

	PushWord #ArrowCursor
	jsl D_SetCursor

dialogloop
	SpaceWord
	PushLong #(D_DigitsOnly+$80000000)
	_ModalDialog
	pla
	beq dialogloop
	cmp #Cancel
	jeq docancel
	cmp #OK
	jeq dook
	sec
	sbc #200
	asl a
	tax
	jmp (buttontable,x)

buttontable
	DC.W	dobuttonone,dobuttontwo,dobuttonthree,dobuttonthree
	DC.W	dobuttonthree

dobuttonone
	PushWord #1
	PushLong Dialog
	PushWord #200
	_SetDItemValue

	stz ButtonValue
	brl dialogloop

dobuttontwo
	PushWord #1
	PushLong Dialog
	PushWord #201
	_SetDItemValue

	MoveWord #1,ButtonValue
	brl dialogloop

dobuttonthree
	PushWord #1
	PushLong Dialog
	PushWord #202
	_SetDItemValue

	MoveWord #2,ButtonValue
	brl dialogloop

docancel	MoveWord #-1,err
	PushWord #WatchCursor
	jsl D_SetCursor
	brl exit

dook	lda ButtonValue
	cmp #2
	jne notcrnum

	PushLong Dialog
	PushWord #203
	PushLong #X_NumberStr
	_GetIText

	lda X_NumberStr
	and #$ff
	beq dostringerr

	SpaceWord
	PushLong #(X_NumberStr+1)
	lda X_NumberStr
	and #$ff
	pha
	pea 0
	_Dec2Int
	PullWord Fields
	bpl cont

dostringerr
	SpaceWord
	PushWord #OKBox
	PushLong #FieldErrorString
	jsl D_AlertBox
	pla
	brl dialogloop

notcrnum	stz Fields

cont	MoveWord >D_CurCursor,X_OldCursor
	PushWord #WatchCursor
	jsl D_SetCursor

	SpaceLong
	PushLong Src
	PushWord ButtonValue
	PushWord Fields
	jsl X_DoAsciitoDBScrap
	sta err
	PullLong Dest

	PushWord X_OldCursor
	jsl D_SetCursor

exit	PushLong Dialog
	jsl D_CloseDialog

	return err

FieldErrorString	STR 'Please type the number of fields.'
	ENDP

	END