'================
class OutputsData
'================
'
string s 'display format
sys i,le 'index and length of display format
method deci(string*t) as string
'==============================
sys a,b,j,la
string fr,dt
dt="."
a=instr t,dt
if a then
fr=mid t,a+1 'fractional part
t=left t,a-1 'main part
else
dt=" " 'no decimal point
end if
j=i
'
def anumber %1 >= 48 and %1 <= 57
'--------------------------------
'
do
j++
if j>le then exit do
a=asc s,j
if not anumber a then exit do 'no numbers specifying layout
la=1 'layout is specified
a-=48
j++
b=asc s,j
if anumber b then a=a*10+b-48 : j++
a-=len t
if a>0 then t=space(a)+t 'left padding
'
if 46=asc s,j then
j++
a=-48+asc s,j
j++
b=asc s,j
if anumber b then a=a*10+b-48 : j++
fr=left fr,a
a-=len fr
if a>0 then fr+=space(a) 'right padding
else
dt="" 'no decimal point/space
end if
i=j
exit do
end do
if la then t+=dt+fr 'fr could be all padding
return t
end method
method fmt(string format,...) as string
'====================================
'
sys p=@format+sizeof sys
'
'VARIABLES REPRESENTING DIFFERENT POSSIBLE TYPES
'
sys nv at p 'integers
bstring sv at p 'string
bstring2 wv at p 'wide string
single fv at p 'single
double dv at p 'double
i=1
le=len format
string t
'
'TRAVERSING THE PARAMETERS IN SYNC WITH FORMAT SPEC
'
s=format
do
if i>le then exit do
select asc s,i
case "s" : t+=deci(sv) : p+=sizeof sys 'string
case "w" : t+=deci(wv) : p+=sizeof sys 'wide string (unicode)
case "i" : t+=deci(nv) : p+=sizeof sys 'integer
case "n" : t+=deci(nv) : p+=sizeof sys 'integer
case "f" : t+=deci(fv) : p+=sizeof single 'single float
case "d" : t+=deci(dv) : p+=sizeof double 'double float
case " " : t+=" "
case "," : t+=","
case ":" : t+=":"
case "t" : t+=chr(9) 'tab
case "r" : t+=chr(13) chr(10) 'new line
end select
i++
end do
return t
end method
end class
'TEST
'====
OutputsData d
'literal floating points are Double
print d.fmt "srrs tn,td2.2,td2.5",
"Testing Formatter","Result:", 42, 1.25, pi
'#recordof thing