
 Include "equ.h"
 Include "impequ.h"


 SECTION rediger,CODE,PUBLIC
***********************************************************
***							***
***********************************************************
dumbentry:
	clr.l	d0
	rts

	dc.l	"IMPT"
	dc.w	200
	dc.l	0

table:	dc.l	0
	dc.l	name,special,check,Rediger


* Vrification du type de document
* Le fichier doit commencer par les 3 caractres LIB 
*  partir du second octet.
check:	moveq	#0,D0		; never par dfaut
	move.l	table,A0
	move.l	buff1(A0),A1
	move.l	(A1),A1
	cmp.b	#'L',1(A1)
	bne.s	never
	cmp.b	#'I',2(A1)
	bne.s	never
	cmp.b	#'B',3(A1)
	bne.s	never
	moveq	#2,d0		; C'est bien un fichier LIB !!!
never:	rts


; Importation d'un document de type Le rdacteur
;
Rediger:
	move.l	table,a0
	move.l	flen(A0),A1
	move.l	(A1),DocLen

	move.l	f_openr(a0),a0		; Ouvre le fichier
	jsr	(a0)
	beq	Erreur			; Erreur, on sort

	move.l	table,a0
	move.l	t_open(a0),a0		; Ouvre le fichier
	jsr	(a0)
	beq	Erreur			; Erreur, on sort

; On commence par saisir l'entete du document
; Saute les 8 premiers caractres
	moveq	#8,D0
	bsr	SkipByte		; saute les 8 premiers octets

; et les diffrentes chaines de configuration
	moveq	#7-1,D7			; On saute 7 chaines
skp0:	bsr	SkipString		; La rgle de tabulation
	dbra	D7,skp0			; les 6 chaines de mise en page

; On prend l'interlignage qui est sur 2 octets selon un
; code trs bizzare
	bsr	GetWord
	sub.w	#$2020,D0
	lsl.b	#4,D0
	lsr.w	#4,D0
	move.w	D0,Interligne

	bsr	SkipString		; Saute la chaine dfinissant le
					; nombre de signes dans le texte

; Cherche la chaine $1B-$1B-$1B-$1B-$0 dfinissant la fin de l'entete
WaitFin:
	bsr	GetByte
	cmp.b	#$1B,D0
	bne.s	WaitFin
	bsr	GetByte
	cmp.b	#$1B,D0
	bne.s	WaitFin
	bsr	GetByte
	cmp.b	#$1B,D0
	bne.s	WaitFin
	bsr	GetByte
	cmp.b	#$1B,D0
	bne.s	WaitFin
	bsr	GetByte
	tst.b	D0
	bne.s	WaitFin

; On configure PPM avec le bon interlignage
;	move.w	#CMD_LSP,D0
;	bsr	PutWord
;	move.w	Interligne,D0
;	bsr	PutWord

; On est sur le texte proprement dit, au boulot ...	
; C'est la boucle de conversion.
; On peut, selon le code reu, avoir plusieurs possibilits
; 00 : on saute
; 08 : on saute
; 0A : Fin de ligne, on envoie un espace
; 0B : on saute
; 0C : Saut de page forc, on envoie un CR
; 0D : Fin de paragraphe, on envoie CR
; 1A : on saute
; 1B : Gre les attributs
; autre, on envoie tel-quel
Redac1:	tst.l	DocLen
	bls	FinDoc		; Fin du document, on sort

	bsr	GetByte
	tst.b	D0
	beq	Redac1

	cmp.b	#8,D0		; rserv
	beq	Redac1

	cmp.b	#$0B,D0		; rserv
	beq	Redac1

	cmp.b	#$1A,D0		; rserv
	beq	Redac1

	cmp.b	#$0C,D0		; Saute de page forc, on fait rien
	beq	Redac1

	cmp.b	#$0A,D0		; Fin de ligne, on en tient pas compte
	beq	SendSpace

	cmp.b	#$0D,D0		; Fin de paragraphe, on envoie CR-LF 
	beq	SendCR

	cmp.b	#$1B,D0		; Gestion d'attribut
	beq	Attribute

	lea	ktoi,a0
	and.w	#$ff,d0
	move.b	0(a0,d0.w),d1
	beq.s	Send
	move.b	d1,d0
Send:	bsr	PutByte		; Sinon, on envoie tel quel vers PPM
	bra	Redac1	

; Fin du document, on ferme tout
FinDoc:
Erreur:	move.l	table,a0
	move.l	f_closer(a0),a0
	jsr	(a0)

	move.l	table,a0
	move.l	t_close(a0),a0
	jmp	(a0)

; Envoie un espace vers PPM
SendSpace:
	moveq	#' ',D0
	bra	Send

; Envoie un retour chariot vers PPM
SendCR:	moveq	#0,D0
	bsr	PutByte
	moveq	#1,D0
	bra	Send

; Gestion des attributs de police, de style et de centrage
Attribute:
	bsr	GetByte
	cmp.b	#$01,D0
	beq	Indent
	cmp.b	#$20,D0
	beq	LeftJust
	cmp.b	#$21,D0
	beq	LeftRightJust
	cmp.b	#$22,D0
	beq	Centrage
	cmp.b	#$23,D0
	beq	RightJust
	cmp.b	#$40,D0
	bmi	None
	cmp.b	#$60,D0
	bmi	SetFont
	btst	#7,D0
	bne	SetAtt
None:	bra	Redac1

Indent:	
	bra	Redac1

LeftJust:
	move.w	#CMD_JST,D0
	bsr	PutWord
	moveq	#0,D0			; Justification  gauche
	bra	Send

RightJust:
	move.w	#CMD_JST,D0
	bsr	PutWord
	moveq	#2,D0			; Justification  droite
	bra	Send

LeftRightJust:
	move.w	#CMD_JST,D0
	bsr	PutWord
	moveq	#5,D0			; Justification automatique
	bra	Send

Centrage:
	move.w	#CMD_JST,D0
	bsr	PutWord
	moveq	#1,D0			; Centrage du texte
	bra	Send

SetFont:
	bra	Redac1

; Gestion des attributs, on met les bits dans l'ordre PPM
; et on envoie la commande.
SetAtt:	moveq	#0,D1
	btst	#0,D0
	beq.s	Bold
	bset	#0,D1
Bold:	btst	#1,D0
	beq.s	Light
	bset	#5,D1
Light:	btst	#2,D0
	beq.s	Italic
	bset	#1,D1
Italic:	btst	#3,D0
	beq.s	Under
	bset	#9,D1
Under:
	move.w	D1,-(SP)
	move.w	#CMD_AT,D0
	bsr	PutWord
	moveq	#0,d0
	move.w	(SP)+,D0
	bsr	PutLong
	bra	Redac1

; Envoie un mot vers PP
PutLong:
	move.l	table,a0
	move.l	t_putl(a0),a0
	jmp	(a0)

; Envoie un mot vers PP
PutWord:
	move.l	table,a0
	move.l	t_putw(a0),a0
	jmp	(a0)

; Envoie un octet vers PP
PutByte:
	move.l	table,a0
	move.l	t_put(a0),a0
	jmp	(a0)

; Lit un octet dans le fichier
GetByte:
	move.l	table,a0
	move.l	f_get(a0),a0
	jsr	(a0)
	subq.l	#1,DocLen
	rts

; Lit un Word dans le fichier
GetWord:
	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)
	subq.l	#2,DocLen
	rts

; Saute une chaine de caractres dans le fichier
SkipString:
	bsr	GetByte
	tst.b	D0
	beq.s	SkipString
	rts

; Saute un certain nombre d'octets
SkipByte:
	move.w	D0,-(SP)
	bsr	GetByte
	move.w	(SP)+,D0
	dbra	D0,SkipByte
	rts


***********************************************************
***							***
***********************************************************
 SECTION rediger,DATA,PUBLIC

special:dc.w	1
	dc.l	spcl1

name:	dc.b	"Redacteur v2.0.0",0

spcl1:	dc.b	"Formated Text",0


* ATARIST ktoi
ktoi:	dc.b	$00,$00,$00,$00,$00,$00,$00,$00		;$00-$07
	dc.b	$00,$00,$00,$00,$00,$00,$00,$00		;$08-$0f
	dc.b	$00,$00,$00,$00,$00,$00,$00,$00		;$10-$17
	dc.b	$00,$00,$00,$00,$00,$00,$00,$00		;$18-$1f
	dc.b	$20,$21,$22,$23,$24,$25,$26,$27
	dc.b	$28,$29,$2a,$2b,$2c,$2d,$2e,$2f
	dc.b	$30,$31,$32,$33,$34,$35,$36,$37
	dc.b	$38,$39,$3a,$3b,$3c,$3d,$3e,$3f
	dc.b	$40,$41,$42,$43,$44,$45,$46,$47
	dc.b	$48,$49,$4a,$4b,$4c,$4d,$4e,$4f
	dc.b	$50,$51,$52,$53,$54,$55,$56,$57
	dc.b	$58,$59,$5a,$5b,$5c,$5d,$5e,$5f
	dc.b	$60,$61,$62,$63,$64,$65,$66,$67
	dc.b	$68,$69,$6a,$6b,$6c,$6d,$6e,$6f
	dc.b	$70,$71,$72,$73,$74,$75,$76,$77
	dc.b	$78,$79,$7a,$7b,$7c,$7d,$7e,$00
	dc.b	199,252,233,226,228,224,229,231
	dc.b	234,235,232,239,238,236,196,197
	dc.b	201,230,198,244,246,242,251,249
	dc.b	254,214,220,162,163,161,187,164
	dc.b	225,237,243,250,241,209,189,156
	dc.b	185,000,141,145,144,186,136,137
	dc.b	227,245,159,191,247,215,192,195
	dc.b	213,000,000,180,181,183,184,182
	dc.b	000,000,138,000,000,000,000,000
	dc.b	000,000,000,000,000,000,000,000
	dc.b	000,000,000,000,000,000,000,000
	dc.b	000,000,000,000,000,165,000,151
	dc.b	000,000,000,150,000,000,153,000
	dc.b	000,000,000,000,000,160,000,000
	dc.b	131,140,135,134,000,000,139,130
	dc.b	155,172,171,128,147,148,149,173

 SECTION rediger,BSS,PUBLIC

Interligne:	ds.w	1			; Interligne
DocLen:		ds.l	1			; Longueur du fichier
