include	clib.inc
include	io.inc
include	iost.inc
include	dir.inc
include	stdio.inc
include	string.inc

ifndef	__f__
	ax?	textequ <ax>
	bx?	textequ <bx>
	si?	textequ <si>
	di?	textequ <di>
	ssdi	textequ <ss::di>
	dxax	textequ <dx::ax>
	ebx?	textequ <es:[bx]>
	mov?	textequ <mov>
	cld?	textequ <cld>
	les?	textequ <les>
else
	ax?	textequ <eax>
	bx?	textequ <ebx>
	si?	textequ <esi>
	di?	textequ <edi>
	ssdi	textequ <edi>
	dxax	textequ <eax>
	ebx?	textequ <[ebx]>
	les?	textequ <mov>
	mov?	macro v:VARARG
	endm
	cld?	macro v:VARARG
	endm
endif

extrn	_argv:dword

_DATA	SEGMENT

extini	db	'.ini',0

_DATA	ENDS

_TEXT	SEGMENT

next_word:
	inc	di?
first_word:
	mov	al,[di?]
	cmp	al,' '
	je	next_word
	cmp	al,9
	je	next_word
	ret

inientry proc _CType public uses si? di? bx? section:DWORD, entry:DWORD, inifile:DWORD
local	quote:BYTE
local	lbuf[WMAXPATH]:byte
      ifndef __f__
	.if word ptr inifile
      else
	.if inifile
      endif
	    invoke strcpy,addr lbuf,inifile
	.else
	    les? bx?,_argv ; assume <program>.ini
	    invoke strcpy,addr lbuf,ebx?
	    invoke setfext,dxax,addr extini
	.endif
	lea di?,lbuf
	invoke strlen,section
	mov si?,ax?
	.if func(ogetl,ssdi,ssdi,WMAXPATH-1)
	    .repeat
		call ogets
		.break .if ZERO?
		mov bx?,si?
		inc di?
		.if lbuf == '[' && byte ptr [di?+bx?] == ']'
		    mov byte ptr [di?+bx?],0
		    .if !func(stricmp,section,ssdi)
			inc ax?
			.break
		    .endif
		.endif
		dec di?
	    .until 0
	    mov si?,ax?
	    .if ax?
		sub si?,si?	; Result
		.repeat		; Section found, read entries
		    call ogets
		    .break .if !ax
		    lea di?,lbuf
		    call first_word
		    .break .if al == '['
		    .continue .if !al || al == ';'
		    invoke strchr,ssdi,'='
		    .continue .if !ax
		    mov bx?,ax?
		    mov byte ptr [bx?],0
		    invoke strtrim,ssdi
		    .continue .if !ax
		    invoke stricmp,ssdi,entry
		    .continue .if ax
		    mov di?,bx?
		    inc di?
		    call first_word
		    invoke strtrim,ssdi
		    .break .if !ax
		    mov quote,0
		    mov si?,di?
		    cld?
		    .repeat
			lodsb
			.break .if !al
			.if al == '"'
			    xor quote,1
			.endif
			.break .if al == ';' && !quote
		    .until 0
		    sub ax?,ax?
		    mov [si?-1],al
		    .if [di?] == al
			sub si?,si?
		    .endif
		    .break
		.until 0
	    .endif
	    invoke oclose,addr STDI
	    .if si?
		invoke strcpy,addr _bufin,ssdi
	    .else
		sub ax?,ax?
		mov? dx,ax
	    .endif
	.endif
	ret
inientry endp

_TEXT	ENDS

	END
