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

	import	D_AlertBox

	ENTRY	X_BlurbString
	ENTRY	X_ButtonOne
	ENTRY	X_ButtonThree
	ENTRY	X_ButtonTwo
	ENTRY	X_ContinueLine
	ENTRY	X_FSCancel
	ENTRY	X_FSCancelString
	ENTRY	X_FSFields
	ENTRY	X_FSFlag
	ENTRY	FSMaxFields
	ENTRY	X_FSOK
	ENTRY	X_FSOKString
	ENTRY	X_FSOff
	ENTRY	X_FSScratch
	ENTRY	X_FSSize
	ENTRY	X_FSSrc
	ENTRY	X_FSep
	ENTRY	X_NumberLine
	ENTRY	X_RSep
	entry	X_FieldTooBig
	entry	X_AlertFlag


;**************************************************************************
;
;   FieldScanner - This scanner will scan an ascii handle and put the
;	next field into a buffer using one of three different field
;	separation schemes.
;
;**************************************************************************

;**************************************************************************
;
;   X_InitFieldScanner - This initializes the scanner so that it knows how
;	much Q_Text there is, and what the separators are.
;
;   Inputs -
;	Scrap - a handle with ascii Q_Text inside.
;	SepFlag - A value telling which separtors to use
;	  0 - Tabs between fields (columns), CRs between records (rows)
;	  1 - Spaces between field , CRs between records (rows)
;	  2 - CRs between fields, number of fields specified
;	FieldsPerLine - Only used if 2 is the value of the SepFlag
;	  The number of fields per record
;
;**************************************************************************

X_InitFieldScanner	PROC	EXPORT
	;Using X_FieldScannerData

	input Scrap:l,SepFlag:w,FieldsPerLine:w

	local Ptr:l

	begin +b

	MoveLong Scrap,X_FSSrc

	SpaceLong
	PushLong Scrap
	_GetHandleSize
	PullLong X_FSSize

	MoveWord SepFlag,X_FSFlag

	Stzl X_FSOff

	MoveWord FieldsPerLine,FSMaxFields
	stz X_FSFields

	lda SepFlag
	asl a
	tax
	jmp (SepTable,x)

SepTable
	DC.W	dotabcr,dospacecr,docrnum

dotabcr	MoveWord #Tab,X_FSep
	MoveWord #CR,X_RSep
	bra exit

dospacecr
	MoveWord #Space,X_FSep
	MoveWord #CR,X_RSep
	bra exit

docrnum	MoveWord #CR,X_FSep
	sta X_RSep

exit	return
	ENDP

;***********************************************************************
;
;   X_NextField(Result:W) - puts the next field into the X_FSScratch buffer and
;	returns with a code:
;	0 - field separator
;	1 - record separator
;	-1 - end of scrap (no more input)
;
;***********************************************************************

X_NextField	PROC	EXPORT
	;Using X_FieldScannerData

	local Hand:l,Sptr:l

	output Result:w

	begin +b

	MoveLong X_FSSrc,Hand
	AddLong [Hand],X_FSOff,Sptr

	stz X_FSScratch

	ldy #0

loop	Cmpl X_FSOff,X_FSSize
	jge endofscrap
	short
	lda [Sptr],y
	cmp X_FSep
	jeq field
	cmp X_RSep
	jeq record
	iny
	cpy #255
	jge toobig
	tyx
	sta X_FSScratch,x
	long
	IncLong X_FSOff
	brl loop

	LONGA Off
	LONGI Off

field	tya
	sta X_FSScratch
	long
	lda X_FSFlag
	cmp #X_CRNumber
	beq checkforrecord
	IncLong X_FSOff
	stz Result
	brl exit

checkforrecord
	inc X_FSFields
	Cmpw X_FSFields,FSMaxFields
	bge dorecord
	IncLong X_FSOff
	stz Result
	brl exit

	LONGA Off
	LONGI Off

record	tya
	sta X_FSScratch
	long
dorecord	stz X_FSFields
	IncLong X_FSOff
	MoveWord #1,Result
	brl exit

	LONGA Off
	LONGI Off
toobig	tya
	short
	sta	X_FSScratch
	long
	MoveWord	#-1,Result
	lda	X_AlertFlag
	bne	exit
	call	D_AlertBox,in=(#OKBox:w,#X_FieldTooBig:l),out=(a:w)
	bra	exit
	
endofscrap
	tya
	short
	sta X_FSScratch
	long
	MoveWord #-1,Result

exit	return
	ENDP

X_FieldScannerData	PROC	EXPORT
	EXPORT	FSMaxFields
	EXPORT	X_FSFields
	EXPORT	X_FSFlag
	EXPORT	X_FSSrc
	EXPORT	X_FSOff
	EXPORT	X_FSSize
	EXPORT	X_FSScratch
	EXPORT	X_FSep
	EXPORT	X_RSep
	export	X_FieldTooBig
	export	X_AlertFlag

FSMaxFields	DS.B 2
X_FSFields	DS.B 2
X_FSFlag	DS.B 2
X_FSSrc	DS.B 4
X_FSOff	DS.B 4
X_FSSize	DS.B 4
X_FSScratch	DS.B 256

X_FSep	DS.B 2
X_RSep	DS.B 2
X_FieldTooBig	STR	'There was a cell or field with more than 255',CR,'characters. The data was truncated.'
X_AlertFlag	DC.W	0

	ENDP

X_FSDialogData	PROC	EXPORT
	EXPORT	FSDialogTemplate
	EXPORT	X_FSOK
	EXPORT	X_FSOKString
	EXPORT	X_FSCancel
	EXPORT	X_FSCancelString
	EXPORT	X_BlurbString
	EXPORT	X_FSBlurbString
	EXPORT	X_Blurb1
	EXPORT	X_Blurb2
	EXPORT	X_ButtonOne
	EXPORT	X_Button1Text
	EXPORT	X_SSButton1Text
	EXPORT	X_DBButton1Text
	EXPORT	X_ButtonTwo
	EXPORT	X_Button2Text
	EXPORT	X_SSButton2Text
	EXPORT	X_DBButton2Text
	EXPORT	X_ButtonThree
	EXPORT	X_Button3Text
	EXPORT	X_SSButton3Text
	EXPORT	X_DBButton3Text
	EXPORT	X_NumberLine
	EXPORT	X_ContinueLine
	EXPORT	X_ContText
	EXPORT	X_SSContText
	EXPORT	X_DBContText

FSDialogTemplate
	DC.W	50,100,135,540
	DC.W	1
	DC.L	0
	DC.L	X_FSOK
	DC.L	X_FSCancel
	DC.L	X_BlurbString
	DC.L	X_ButtonOne
	DC.L	X_ButtonTwo
	DC.L	X_ButtonThree
	DC.L	X_NumberLine
	DC.L	X_ContinueLine
	DC.L	0

X_FSOK	DC.W	OK
	DC.W	65,225,80,325
	DC.W	ButtonItem
	DC.L	X_FSOKString
	DC.W	0
	DC.W	0
	DC.L	0
X_FSOKString	STR 'OK'

X_FSCancel	DC.W	Cancel
	DC.W	65,100,80,200
	DC.W	ButtonItem
	DC.L	X_FSCancelString
	DC.W	0
	DC.W	0
	DC.L	0
X_FSCancelString	STR 'Cancel'

X_BlurbString
	DC.W	3
	DC.W	3,20,14,420
	DC.W	statText+itemDisable
X_FSBlurbString
	DC.L	0
	DC.W	0
	DC.W	0
	DC.L	0
X_Blurb1	STR 'Define row and column separators:'
X_Blurb2	STR 'Define field and record separators:'

X_ButtonOne
	DC.W	200
	DC.W	15,30,26,420
	DC.W	radioItem
X_Button1Text	DC.L	0
	DC.W	1
	DC.W	1
	DC.L	0
X_SSButton1Text	STR 'Tabs between columns, returns between rows.'
X_DBButton1Text	STR 'Tabs between fields, returns between records.'

X_ButtonTwo
	DC.W	201
	DC.W	30,30,41,420
	DC.W	radioItem
X_Button2Text	DC.L	0
	DC.W	0
	DC.W	1
	DC.L	0
X_SSButton2Text	STR 'Spaces between columns, returns between rows.'
X_DBButton2Text	STR 'Spaces between fields, returns between records.'

X_ButtonThree
	DC.W	202
	DC.W	45,30,56,240
	DC.W	radioItem
X_Button3Text	DC.L	0
	DC.W	0
	DC.W	1
	DC.L	0
X_SSButton3Text	STR 'Returns between columns,'
X_DBButton3Text	STR 'Returns between fields,'

X_NumberLine
	DC.W	203
	DC.W	44,246,57,290
	DC.W	editLine
	DC.L	0
	DC.W	3
	DC.W	0
	DC.L	0

X_ContinueLine
	DC.W	204
	DC.W	46,296,58,420
	DC.W	statText
X_ContText	DC.L	0
	DC.W	0
	DC.W	0
	DC.L	0
X_SSContText	STR 'columns per row.'
X_DBContText	STR 'fields per record.'


	ENDP
	END

