Author Topic: Data Formatting  (Read 2603 times)

0 Members and 2 Guests are viewing this topic.

Charles Pegge

  • Guest
Data Formatting
« on: July 11, 2011, 11:34:59 PM »
Second round of developing a data formatter. This will support tabs, commas, new lines, ascii strings, unicode strings, integers and floats, left / right padding and floating point number  truncation.

Without ellipsis and the new type conversion abilities this code would be significantly more complex.


Code: OxygenBasic
  1.  
  2.   '================
  3.  class OutputsData
  4.   '================
  5.  '
  6.  string  s    'display format
  7.  sys     i,le 'index and length of display format
  8.  
  9.   method deci(string*t) as string
  10.   '==============================
  11.    sys a,b,j,la
  12.     string fr,dt
  13.     dt="."
  14.     a=instr t,dt
  15.     if a then
  16.       fr=mid t,a+1 'fractional part
  17.      t=left t,a-1 'main part
  18.    else
  19.       dt=" " 'no decimal point
  20.    end if
  21.     j=i
  22.     '
  23.    def anumber %1 >= 48 and %1 <= 57
  24.     '--------------------------------
  25.    '
  26.    do
  27.       j++
  28.       if j>le then exit do
  29.       a=asc s,j
  30.       if not anumber a then exit do 'no numbers specifying layout
  31.      la=1 'layout is specified
  32.      a-=48
  33.       j++
  34.       b=asc s,j
  35.       if anumber b then a=a*10+b-48 : j++
  36.       a-=len t
  37.       if a>0 then t=space(a)+t 'left padding
  38.      '
  39.      if 46=asc s,j then
  40.         j++
  41.         a=-48+asc s,j
  42.         j++
  43.         b=asc s,j
  44.         if anumber b then a=a*10+b-48 : j++
  45.         fr=left fr,a
  46.         a-=len fr
  47.         if a>0 then fr+=space(a) 'right padding
  48.      else
  49.         dt="" 'no decimal point/space
  50.      end if
  51.       i=j
  52.       exit do
  53.     end do
  54.     if la then t+=dt+fr 'fr could be all padding
  55.    return t
  56.   end method
  57.  
  58.   method fmt(string format,...) as string
  59.   '====================================
  60.  '
  61.  sys p=@format+sizeof sys
  62.   '
  63.  'VARIABLES REPRESENTING DIFFERENT POSSIBLE TYPES
  64.  '
  65.  sys      nv at p 'integers
  66.  bstring  sv at p 'string
  67.  bstring2 wv at p 'wide string
  68.  single   fv at p 'single
  69.  double   dv at p 'double
  70.  i=1
  71.   le=len format
  72.   string t
  73.   '
  74.  'TRAVERSING THE PARAMETERS IN SYNC WITH FORMAT SPEC
  75.  '
  76.  s=format
  77.   do
  78.     if i>le then exit do
  79.     select asc s,i
  80.     case "s" : t+=deci(sv) : p+=sizeof sys    'string
  81.    case "w" : t+=deci(wv) : p+=sizeof sys    'wide string (unicode)
  82.    case "i" : t+=deci(nv) : p+=sizeof sys    'integer
  83.    case "n" : t+=deci(nv) : p+=sizeof sys    'integer
  84.    case "f" : t+=deci(fv) : p+=sizeof single 'single float
  85.    case "d" : t+=deci(dv) : p+=sizeof double 'double float
  86.    case " " : t+=" "
  87.     case "," : t+=","
  88.     case ":" : t+=":"
  89.     case "t" : t+=chr(9)          'tab
  90.    case "r" : t+=chr(13) chr(10) 'new line
  91.    end select
  92.     i++
  93.   end do
  94.   return t
  95.   end method
  96.  
  97.   end class
  98.  
  99.  
  100.   'TEST
  101.  '====
  102.  
  103.   OutputsData d
  104.  
  105.   'literal floating points are Double
  106.  
  107.   print d.fmt "srrs tn,td2.2,td2.5",
  108.         "Testing Formatter","Result:", 42, 1.25, pi
  109.  
  110.   '#recordof thing
  111.  

Charles

kryton9

  • Guest
Re: Data Formatting
« Reply #1 on: July 12, 2011, 10:29:02 AM »
That's cool Charles and good code to study, thanks.