' O2 source
extern
function reverse(char*s)
'=======================
addr ecx,s
mov edx,0
.rlen
mov al,[ecx]
cmp al,0
jz xlen
inc edx
inc ecx
jmp rlen
.xlen
;
addr ecx,s
add edx,ecx
dec ecx
;
.rswap
inc ecx
dec edx
cmp edx,ecx
jle xswap
mov al,[ecx]
mov ah,[edx]
mov [ecx],ah
mov [edx],al
jmp rswap
.xswap
end function
function getword(char*ss,sys*b) as char*
'=======================================
if b=0 then b=1
byte s at @ss
byte c,d
sys bb,bc
static char z[128]
a=0
bb=b
'SKIP LEADING SPACES
do
c=s[b]
select c
case 33 to 255,0 : exit do 'SKIP SPACE
end select
b++
end do
bc=b
'
'QUOTES
select c
case 34,39
do
b+=1
d=s[b]
if d=0 or d=c then b+=1 : jmp fwd done
end do
end select
'WORDS AND SYMBOLS
do
c=s[b]
select c
case 0 to 32 : exit do
case 35 : jmp fwd more
case 33 to 47 : 'symbols
case 48 to 57 : jmp fwd more 'numbers
case 58 to 64 : 'symbols
case 65 to 90 : jmp fwd more 'capitals
case 95 : jmp fwd more 'underscore
case 91 to 96 : 'symbols
case 97 to 122 : jmp fwd more 'lower case
case 123 to 127 : 'symbols
case 128 to 255 : jmp fwd more 'higher ascii
end select
if b=bc then b++
exit do
more:
b++
end do
done:
if b > bb then
z=mid ss,bc,b-bc
else
z = ""
end if
return z
end function
=================
Class AlignedText
=================
indexbase 1
string buf, bufo, pr, cr, tab, jus, dlm
sys Cols, Rows, ColWidth[0x100], TotWidth, ColPad, ld
method SetText(char*s)
======================
if not len cr then cr=chr(13,10)
tab=chr(9)
if not len jus then jus=string 200,"L"
buf=s
measure
end method
method measure()
================
sys a, b, wa, wb, cm, c, cw
a=1 : b=1
Cols=0 : Rows=0 : ColPad=3
ld=len dlm
if not ld then dlm="," : ld=1 'default to comma
do
wb=b
a=instr b,buf,cr
if a=0 then exit do
cm=0
c++
do
wa=instr wb,buf,dlm
if wa=0 or wa>a then exit do
cm++
if cm>cols then cols=cm
cw=wa-wb
if cw > ColWidth[cm] then ColWidth[cm]=cw
wb=wa+ld
end do
b=a+len cr
end do
rows=c
'
c=0
for i=1 to cols
ColWidth[ i ]+=ColPad
c+=ColWidth[ i ]
next
TotWidth=c+len cr
'print ShowMetrics
end method
method ShowMetrics() as char*
=============================
pr="METRICS:" cr cr
pr+=rows tab cols tab totwidth cr cr
pr+="column" tab "spacing" cr
for i=1 to cols
pr+=i tab ColWidth[ i ] cr
next
return pr
end method
method justify(char*j)
======================
jus=j
end method
method delimiter(char*j)
========================
dlm=j
end method
method endofline(char*j)
========================
cr=j
end method
method layout() as char*
========================
sys a, b, wa, wb, wl, cm, lpos, cpos
bufo=space Rows*TotWidth
a=1 : b=1
do
wb=b
a=instr(b,buf,cr)
if a=0 then exit do
cm=0
cpos=1
do
wa=instr(wb,buf,dlm)
if wa=0 or wa>a then exit do
'
cm++
'
'JUSTIFICATION
'
wl=wa-wb
p=lpos+cpos 'default "L" LEFT ALIGN
'
select case asc(jus,cm)
case "R" : p=lpos+cpos+ColWidth[cm]-wl-Colpad
case "C" : p=lpos+cpos+( ColWidth[cm]-wl-Colpad )*.5
end select
'
mid bufo,p, mid buf,wb,wl
cpos+=colwidth[cm]
wb=wa+ld
end do
b=a+len cr
lpos+=TotWidth
if lpos<len(bufo) then mid bufo,lpos-1,cr
end do
return bufo
end method
end class
'#recordof AlignedText
AlignedText atxt
function AlignText(char *in,*ju,*dl,*cr) as char*
=================================================
atxt.justify ju
atxt.delimiter dl
atxt.endofline cr
atxt.SetText in
return atxt.layout
end function
function GetTextWord(char*s, sys *i) as char*
=============================================
static string w
sys a,j
byte b at @s
j=i
i--
@b--
do
i++
@b++
select b
case 0 : exit do 'end of string
case 33 to 255 : exit do 'non white space
end select
end do
'
j=i
'
do
select b
case 0 to 32 : exit do
end select
i++
@b++
end do
'
if i>j
w=mid s,j,i-j
return w
else
return ""
end if
'
end function
sub finish()
'===========
terminate
end sub
function link(sys n) as sys
'==========================
select n
case 0 : return @finish
case 1 : return @reverse
case 2 : return @getword
case 3 : return @AlignText
case 4 : return @GetTextWord
end select
end function
end extern
addr link