I've created 2 functions to aid clean column alignments:
function padstr(int lmar,wid,rmar,string d) as string
=====================================================
string s=string(wid," ")
mid(s,1,d) 'd will be right clipped
function=string(lmar," ")+s+string(rmar," ")
end function
function numstr(int wid,rmar,dp, double v) as string
====================================================
string s,ng
int n,ls
if v<0
v=-v
ng="-"
endif
n=10^dp
s=str(round(v*n))
ls=len(s)
if ls<=dp
s=string(dp+1-ls,"0")+s
endif
ls=len(s)
s=ng+left(s,ls-dp)+"."+mid(s,-dp)
ls=len(s)
if ls>wid 'ensure number is never clipped
wid=ls
endif
function=string(wid+rmar," ")
mid(function,wid-ls+1,s)
end function
included in jjArray:
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$filename "t.exe"
use rtl64
use console
use corewin
% ITEMS = 10
include "getfields.o2bas"
'==============================================================================
Macro LineInput string (s,c,b=1024)
s = nuls b
s = (char*)fgets(s,b,c)
End Macro
'==============================================================================
Macro Pause
Print cr "Press any key to continue" cr
waitkey
End Macro
'==============================================================================
Type InfoType
char Country[64]
float f1990
float f2013
End Type
'==============================================================================
'32bit cdecl callback
'Function TypeCompare cdecl (void *a, void *b) As Integer, callback
'64bit callback
Function TypeCompare (void *a, void *b) As Integer, callback
InfoType Ptr ia = (InfoType Ptr) a
InfoType Ptr ib = (InfoType Ptr) b
Function = ((100 * ia.f2013) - (100 * ib.f2013))
End Function
function padstr(int lmar,wid,rmar,string d) as string
=====================================================
string s=string(wid," ")
mid(s,1,d) 'd will be right clipped
function=string(lmar," ")+s+string(rmar," ")
end function
function numstr(int wid,rmar,dp, double v) as string
====================================================
string s,ng
int n,ls
if v<0
v=-v
ng="-"
endif
n=10^dp
s=str(round(v*n))
ls=len(s)
if ls<=dp
s=string(dp+1-ls,"0")+s
endif
ls=len(s)
s=ng+left(s,ls-dp)+"."+mid(s,-dp)
ls=len(s)
if ls>wid 'ensure number is never clipped
wid=ls
endif
function=string(wid+rmar," ")
mid(function,wid-ls+1,s)
end function
Function main() As sys
string sFileIn = "UnderFiveMortalityRate.tab"
string sLine
sys fp1,fields,count
'InfoType Info[25]
Dim InfoType Info at getmemory sizeof(InfoType)*25
fp1 = fopen sfileIn,"r"
If not fp1 Then
printl "PROBLEM #1"
Pause
exit function
End If
count = 0
'sLine = LineInput(fp1)
Do
sLine = LineInput(fp1)
If Len(sLine) = 0 Then
Exit Do
End If
fields = GetFields(sLine,chr(9))
'print "fields = " fields cr
If (Len(arg(3)) = 0) OR (Len(arg(4)) = 0) Then
continue Do
End If
Info[count].Country = arg(1)
Info[count].f1990 = val(arg(3))
Info[count].f2013 = val(arg(4))
count++
loop
fclose(fp1)
'fails on qsort this call
qsort(Info,count,sizeof(Info),@TypeCompare)
int i
string s1
double f1,f2
for i=1 to count
s1=Info[i].Country
f1=Info[i].f1990
f2=Info[i].f2013
print padstr(2,12,1,s1)+numstr(8,1,2,f1)+numstr(8,1,2,f2) cr
next
freememory @Info
Print "end" cr
wait
Function = 0
End Function
return main()