NEW
  AUTO 3,1
*/--------------------------------------
* # strtof
* Convert String to 40 bits Float
* ## C
* `float strtof (const char* str, char** endptr);`
* ## ASM
* **In:**
* `>PUSHWI EndPtr`
* `>LDYA str`
* `>SYSCALL strtof`
* ## RETURN VALUE
* On stack (float)
*\--------------------------------------
K.strtof		>STYA TXTPTR			Ptr to source string
				>PULLW ZPPtr1
				
				jsr K.AToF.I

				lda TXTPTR
				sta (ZPPtr1)
				ldy #1
				lda TXTPTR+1
				sta (ZPPtr1),y
				rts				
*/--------------------------------------
* # AToF
* Convert String to 40 bits Float
* ## C
* `float atof (const char* str);`
* ## ASM
* **In:**
* `>LDYA str`
* `>SYSCALL atof`
* ## RETURN VALUE
* On stack (float)
*\--------------------------------------
K.AToF			>STYA TXTPTR			Ptr to source string
				
K.AToF.I		jsr CHARGOT

				ldx #ROM.FIN
				jmp GP.RomCallPushFAC
*/--------------------------------------
* # StrToL/StrToUL
* Convert String to 32 bits (unsigned) int
* ## C
* `long strtol (const char* str, char** endptr, int base);`
* `unsigned long strtoul (const char* str, char** endptr, int base);`
* ## ASM
* **In:**
* `>PUSHB Base`
* `>PUSHWI EndPtr`
* `>LDYAI str`
* `>SYSCALL strtol`
* ## RETURN VALUE
* On stack (long)
*\--------------------------------------
K.StrToL		sec						Signed
				.HS 90					BCC
K.StrToUL		clc						Unsigned
				jsr MEM.SPtr2PPtr1
				>PULLA					Base

				jsr K.AToL.I
				bcs K.StrToUL.rts
*				clc	
			
				adc ZPPtr2
				sta (ZPPtr1)
				lda #0
				adc ZPPtr2+1
				ldy #1
				sta (ZPPtr1),y
						
K.StrToUL.rts	rts				
*/--------------------------------------
* # atol
* Convert String to 32 bits long
* ## C
* `long atol ( const char * str );`
* ## ASM
* **In:**
* `>LDYA str`
* `>SYSCALL atol`
* ## RETURN VALUE
* On stack (long)
*\--------------------------------------
*STDLIB.32		.BS 4					32 bits max
STDLIB.32		.EQ FAC					32 bits max
*--------------------------------------
K.AToL			>STYA ZPPtr2			C-String in Ptr2, Dst buffer in Ptr1

				lda #10					base 10
				sec						signed
				
K.AToL.I		jsr STDLIB.GetDec
				bcs .9
				phy						Save Count processed 
				
				ldy #3

.3				lda STDLIB.32,y
				>PUSHA
				dey
				bpl .3
				pla
				
*				clc				
				
.9				rts
*/--------------------------------------
* # atoi
* Convert String to 16 bits int
* ## C
* `int atoi ( const char * str );`
* ## ASM
* **In:**
* `>LDYAI str`
* `>SYSCALL atoi`
* ## RETURN VALUE
*  Y,A = int
*\--------------------------------------
K.atoi			>STYA ZPPtr2
				lda #10					base 10
				sec						signed
				
				jsr STDLIB.GetDec
				bcs .9
				
				>LDYA STDLIB.32
.9				rts
*--------------------------------------
* Convert Hex int at ZPPtr2 to STDLIB.32
*--------------------------------------
STDLIB.GetHex	lda (ZPPtr2)
				beq .9
				
				jsr MEM.IsHexDigit
				bcs .9
				
				jsr STDLIB.32.Clear
				
				sta STDLIB.32
				
				ldy #0
				
.1				iny
				lda (ZPPtr2),y
				beq .8
				jsr MEM.IsHexDigit
				bcs .8
				
				pha
				
				ldx #4
				
.2				jsr STDLIB.32.T2
				bcs .99					overflow!!!
				dex
				bne .2
				
				pla
				ora STDLIB.32
				sta STDLIB.32
				bra .1
				
.8				clc
				rts				

.99				pla				
.9				sec
				rts
*--------------------------------------
* Convert Decimal int at ZPPtr2 to STDLIB.32
*--------------------------------------
STDLIB.GetDec	jsr STDLIB.32.Clear
				
				clc
				lda (ZPPtr2)
				eor #'-'
				bne .10
				
				jsr MEM.NextCharPtr2	skip '-'
				sec
				
.10				ror .80+1				set pos/neg flag

				ldy #$ff

.1				iny
				lda (ZPPtr2),y
				beq .8
				
				jsr MEM.IsDigit
				bcs .8

				phy						Save Y, pointing to next char
				jsr STDLIB.32.T10
				ply
				
				bcs .9

				lda (ZPPtr2),y
				and #$0F
				
*				clc

				adc STDLIB.32
				sta STDLIB.32
				bcc .1
				inc STDLIB.32+1
				bne .1
				inc STDLIB.32+2
				bne .1
				inc STDLIB.32+3
				bne .1					if 0, overflow!!!
				
.9				lda #E.INUM
				sec
				rts
				
.8				tya						no digit parsed...error
				beq .9
				
.80				lda #$FF				SELF MODIFIED
				bpl .88
				
				ldx #4
				ldy #0
				
				sec
				
.81				lda STDLIB.32,y
				eor #$ff
				adc #0
				sta STDLIB.32,y
				iny
				dex
				bne .81
				
.88				clc
				rts		
*--------------------------------------
STDLIB.32.T10	ldx #3

.1				lda STDLIB.32,x			save STDLIB.32 for 4+1
				pha
				dex
				bpl .1

				jsr STDLIB.32.T2		STDLIB.32 * 2 -> STDLIB.32
				bcs STDLIB.32.9			overflow!!!
				
				jsr STDLIB.32.T2		STDLIB.32 * 4 -> STDLIB.32
				bcs STDLIB.32.9			overflow!!!
				
				ldx #0
				ldy #4
				
*				clc

.2				pla						STDLIB.32 * 4 + STDLIB.32 -> STDLIB.32
				adc STDLIB.32,x
				sta STDLIB.32,x
				inx
				dey
				bne .2
				
				bcs STDLIB.32.RTS		overflow!!!

* STDLIB.32 * 2 -> STDLIB.32

STDLIB.32.T2	asl STDLIB.32
				rol STDLIB.32+1
				rol STDLIB.32+2
				rol STDLIB.32+3
				rts						if CS, overflow!!!
				
STDLIB.32.9		pla						discard saved STDLIB.32
				pla
				pla
				pla
				
STDLIB.32.RTS	rts
*--------------------------------------
STDLIB.32.Clear	ldx #3

.1				stz STDLIB.32,x
				dex
				bpl .1
				rts
*/--------------------------------------
* # RealPath
* Return the canonicalized absolute pathname
* ## C
* `unsigned short int realpath (const char* str, char *resolvedpath);`
* ## ASM
* **In:**
* `>PUSHWI resolvedpath`
* `>LDYA str`
* `>SYSCALL realpath`
* ## RETURN VALUE
* CC : success
*  Y,A = Ptr to Full Path (C-String)
*  X = hMem of Full Path
* CS : A = Error Code
*\--------------------------------------
K.realpath		sec
				.HS 90					BCC
STDLIB.realpath.I
				clc
				ror .82+1

				ldx #SYS.ExpandStr
				jsr K.SYSCALL2.BANK
				bcs STDLIB.32.RTS
				
				>STYA ZPPtr1
				stx .99+1				save expanded buffer hMem
				
				ldx #$ff

				lda (ZPPtr1)
				beq .1
				
				cmp #'/'				full path starting with '/'?
				beq .3					yes, do not append to current prefix
				
.1				ldy #S.PS.hPREFIX
				lda (pPs),y
				jsr K.GetMemPtr
				>STYA ZPPtr2
				
				ldy #$ff
				
.2				iny
				inx
				lda (ZPPtr2),y
				sta K.Buf256,x
				bne .2
				
				dex
				
.3				ldy #$ff

.4				iny
				inx
				lda (ZPPtr1),y
				sta K.Buf256,x
				bne .4
				
				dex
				beq .81					we have '/'....nothing to do...
*--------------------------------------
* X=LEN, K.Buf256 = /dir1/./../file(/)\0
*--------------------------------------
				ldx #1					skip leading /
			
				lda K.Buf256,x
				
.5				ldy #0					dot counter=0

.6				cmp #'/'
				beq .8
				
				cmp #'.'
				bne .7
				
				iny
				.HS 2C					BIT ABS, skip "LDY #0"
.7				ldy #0					not a dot....reset dot counter

				inx
				lda K.Buf256,x
				bne .6					we have /dir\0
				
.8				tya
				beq .80					Y was 0....nothing to do...
				dey						"./" ?
				bne .9					no..
				
				lda K.Buf256-2,x
				cmp #'/'				"/./" ?
				bne .80
				
				dex
				dex
				jsr K.RealPath.RemoveAtX	we found "/./", remove,useless....
				
				bra .80
				
.9				dey						"../" ?
				bne .99					".../" ??!!...mmm...syntax error 
				
				lda K.Buf256-3,x
				cmp #'/'				"/../" ?
				bne .80

				dex
				dex
				dex
				
				txa						we found "/../"
				beq .99					at the beginning of string...cannot remove /dir/..

				jsr K.RealPath.RemoveAtX	remove "/.."

.10				dex
				
				lda K.Buf256,x			go to "/dir" 
				cmp #'/'
				bne .10
				
				jsr K.RealPath.RemoveAtX ...remove "/dir"
				
.80				inx
				lda K.Buf256,x
				bne .5
*--------------------------------------
.81				jsr .99

.82				lda #$ff				SELF MODIFIED
				bpl .88
				>LDYAI K.Buf256
				ldx #SYS.StrDup			BANK 2
				jmp K.SYSCALL2.BANK
				
.88				clc
				rts
				
.99				lda #$ff				SELF MODIFIED
				jsr K.FreeMem

				lda #E.BADPATH
				sec
				rts
*--------------------------------------
K.RealPath.RemoveAtX
				txa						X = "/something"
				tay
				
.1				iny
				lda K.Buf256,y
				beq .2					found /something\0
				cmp #'/'
				bne .1					found /something/
				
.2				phx						save X for exit
				
.3				iny						K.Buf256,y=/ or 0
				inx
				lda K.Buf256-1,y
				sta K.Buf256-1,x
				bne .3
				
				txa
				bne .8
				
				lda #'/'				Make sure we have a least '/' in the buffer
				sta K.Buf256
				stz K.Buf256+1

.8				plx						restore X
				rts
*--------------------------------------
MAN
SAVE USR/SRC/SYS/KERNEL.S.STDLIB
LOAD USR/SRC/SYS/KERNEL.S
ASM
