	; each v0.01 (12 May 1998)
	; by Paul Clifford (paul@plasma.demon.co.uk)

	AREA	|C$$code|, CODE, READONLY

	EXPORT	dirscan_do
	EXPORT	substitute_args
	EXPORT	convert_filetype
	EXPORT	read_datestamp
	EXPORT	compare_datestamp
	EXPORT	extract_day
	EXPORT	get_previous_day
	EXPORT	read_character

; int dirscan_do(dirscan_ctx *)
dirscan_do
	stmfd	r13!, {r4-r6, r14}
	mov	r12, r0						; dirscan_ctx *ctx
	mov	r0, #11
	ldr	r1, [r12, #0]					; directory name
	add	r2, r12, #12					; buffer
	mov	r3, #1024					; number of entries to read
	ldr	r4, [r12, #8]					; offset
	mov	r5, #1024					; size of buffer
	adr	r6, star					; wildcarded name to match
	swi	0x2000c						; XOS_GBPB
	movvc	r0, #0
	strvc	r3, [r12, #4]
	strvc	r4, [r12, #8]
	movvs	r0, #1
	ldmfd	r13!, {r4-r6, pc}^
star
	=	"*", 0, 0, 0

; int substitute_args(const char *args, char *buffer, const int buffer_size, const char *cmnd, const int cmnd_size)
substitute_args
	stmfd	r13!, {r4, r14}
	ldr	r4, [r13, #8]					; length of command template
	orr	r0, r0, #1 << 31				; prevent args from being automatically added to end
	swi	0x20043						; XOS_SubstituteArgs
	mov	r0, r2						; characters in result string (including terminator)
	ldmfd	r13!, {r4, pc}^

; int convert_filetype(const char *name, int *type)
convert_filetype
	stmfd	r13!, {r14}
	mov	r12, r1						; int *type
	mov	r1, r0						; filetype string
	mov	r0, #31						; convert filetype string to number
	swi	0x20029						; XOS_FSControl
	movvc	r0, #0
	strvc	r2, [r12, #0]
	movvs	r0, #1
	ldmfd	r13!, {pc}^

; int read_datestamp(int *date_low, int *date_high, const char *filename)
read_datestamp
	stmfd	r13!, {r4-r5, r11, r14}
	mov	r11, r0						; int *date_low
	mov	r12, r1						; int *date_high
	mov	r1, r2						; const char *filename
	mov	r0, #17
	swi	0x20008						; XOS_File
	movvs	r0, #1
	ldmvsfd	r13!, {r4-r5, r11, pc}^
	str	r3, [r11, #0]
	and	r2, r2, #0xff
	str	r2, [r12, #0]
	cmp	r0, #0
	moveq	r0, #1
	movne	r0, #0
	ldmfd	r13!, {r4-r5, r11, pc}^

; int compare_datestamp(const int d1_low, const int d1_high, const int d2_low, const int d2_high)
compare_datestamp
	stmfd	r13!, {r14}
	and	r1, r1, #0xff
	and	r3, r3, #0xff
	subs	r0, r0, r2
	sbcs	r1, r1, r3
	cmpeq	r0, #0
	movgt	r0, #1
	movlt	r0, #-1
	ldmfd	r13!, {pc}^

; int extract_day(const int d1_low, const int d1_high, int *d2_low, int *d2_high)
extract_day
	stmfd	r13!, {r11, r14}
	mov	r11, r2						; int *d2_low
	mov	r12, r3						; int *d2_high
extract_day_x
	mov	r14, #0x83000000
	orr	r14, r14, #0x00d60000
	mov	r2, r0
	mov	r3, r1
extract_day_loop
	subs	r2, r2, r14, lsl #8
	sbcs	r3, r3, r14, lsr #24
	bge	extract_day_loop
	adds	r2, r2, r14, lsl #8
	adcs	r3, r3, r14, lsr #24
	cmpeq	r2, #0
	tstne	r14, #0xf0000
	movne	r14, r14, lsr #4
	bne	extract_day_loop
	subs	r2, r0, r2
	sbc	r3, r1, r3
	swi	0x63048						; XTerritory_ReadCurrentTimeZone
	movvs	r1, #0
	subs	r0, r2, r1
	sbc	r1, r3, #0
	str	r0, [r11, #0]
	str	r1, [r12, #0]
	ldmfd	r13!, {r11, pc}^

; int get_previous_day(const int offset, int *date_low, int *date_high)
get_previous_day
	stmfd	r13!, {r11, r14}
	mov	r11, r1						; int *date_low
	mov	r12, r2						; int *date_high
	mov	r3, r0						; const int offset
	ldr	r1, =time_buffer
	mov	r0, #3
	str	r0, [r1, #0]
	mov	r0, #14
	swi	0x20007						; XOS_Word
	ldmia	r1, {r0-r1}
	and	r1, r1, #0xff
	mov	r2, #0x00008300
	orr	r2, r2, #0x000000d6
	mul	r3, r2, r3
	subs	r0, r0, r3, lsl #8
	sbc	r1, r1, r3, lsr #24
	b	extract_day_x

; char read_character(void)
read_character
	stmfd	r13!, {r14}
	swi	0x20004
	ldmfd	r13!, {pc}^


	AREA	|C$$zidata|, NOINIT

time_buffer
	%	8

	END
