
!**********************************************************************
!*                                                                    *
!*    CONVERT: Routine for performing a variety of data conversions   *
!*                                                                    *
!*                   JHB:  Version 1.1 22 Apr 1988                    *
!*                                                                    *
!**********************************************************************

%include "inc:util.imp"
%begin

%string (255) infile, outfile, param, in
%constintegerarray hex1(0:15)= %c
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
%integer choice

%routine do strip controls(%string (255) file)
   %integer a
   %on 3, 9 %start; ->eof; %finish
   openinput(1, file); selectinput(1)
   %cycle
      readsymbol(a)
      printsymbol(a) %if a=10 %or a=12 %or a=13 %or a>=32
   %repeat

eof:close input
%end

%routine do crtolf(%string (255) file)
   %integer a
   %on 3, 9 %start; ->eof; %finish
   openinput(1, file); selectinput(1)
   %cycle
      readsymbol(a)
      a=10 %if a=13
      printsymbol(a)
   %repeat

eof:newline
   close input
%end

%routine do bit7(%string (255) file)
   %integer a
   %on 3, 9 %start; ->eof; %finish
   openinput(1, file); selectinput(1)
   %cycle
      readsymbol(a)
      printsymbol(a&127)
   %repeat

eof:close input
%end

%routine do bftoh(%string (255) file)
   %integer a, n
   %on 3,9 %start; ->eof; %finish
   openinput(1, file); selectinput(1)
   n=0
   %cycle
      readsymbol(a)
      printsymbol(hex1(a>>4&15)); printsymbol(hex1(a&15))
      n=n+2
      %if n=80 %then newline %and n=0
   %repeat

eof:newline
   close input
%end

%routine do hftob(%string (255) file)
   %integer a, b, c, m, n, csum
   %on 3,9 %start; -> eof; %finish
   openinput(1, file); selectinput(1)
   csum=0
   m=0; n=0
   %cycle
      skipsymbol %while nextsymbol<'0'
      readsymbol(a); a=a-'0'; a=a-7 %if a>9; m=m+1
      skipsymbol %while nextsymbol<'0'
      readsymbol(b); b=b-'0'; b=b-7 %if b>9; m=m+1
      c=a<<4+b
      printsymbol(c); n=n+1
      csum=(csum+c)&16_FFFF
   %repeat

eof:close input
   selectoutput(0)
   printline("Odd number of hex characters read - n=".itos(m,-1)) %if m&1#0
   write(n,-1); printstring(" characters generated - checksum = ")
   phex4(csum); newline
   selectoutput(2)
%end

%routine do file(%string (255) file, %integer choice)
   selectoutput(2)
   %if choice = 1 %start ;!Binary to hex-pair
      do bftoh(file)
   %elseif choice = 2 ;!Hex-pair to binary
      do hftob(file)
   %elseif choice = 3 ;!Strip out control characters
      do strip controls(file)
   %elseif choice = 4 ;!Convert carriage-returns to linefeeds
      do cr to lf(file)
   %elseif choice = 5 ;!strip top bit
      do bit7(file)
   %finish
%end

param = cli param

outfile = infile %and infile = param %unless param -> infile.("/").outfile
printline(infile." not accessible") %unless exists(infile)

printline("Choose conversion:")

printline("1:  Binary to Hex-pair")
printline("2:  Hex-pair to binary")
printline("3:  Strip control characters")
printline("4:  Convert carriage-returns to linefeeds")
printline("5:  Strip top bit if present")
%cycle
   read(choice)
   %exit %if 1<= choice <= 5
   printline("Not recognised, sorry")
%repeat

openoutput(2, outfile) %if charno(outfile,1) # ':'

infile=infile.","
do file(in,choice) %while infile -> in.(",").infile
close output %if charno(outfile, 1) # ':'

%endofprogram
