! Pagecvt - converts a "straight" Teletext page (of the sort handled by Andie
! Ness's utilities) to a new form one.  It does this by:
! - prepending a 64-byte header [filled, by default, with blanks];
! - converting any control characters in the original file to ASCII spaces.
%include "inc:region.imp"
%include "inc:util.imp"
%begin
   %byte %array file image(0:1023)
   %string(255) infile, outfile
   %string(64) header
   %integer i, insize, fileref

   ! Get infile and outfile names (use PAM)
   define param("Input file", infile, pam infile+pam nodefault)
   define param("Output file",outfile, pam outfile+pam nodefault)
   header = "                                                                "
   define param("Header to prepend",header, pam keepcase)
   process parameters(cliparam)

   access file(infile,0,fileref,insize)
   %if insize#960 %then %start
      print string(infile." is ")
      write(insize,4)
      print string(" bytes long")
      new line
      %if insize<960 %then %c
         print string("It will be padded with blanks") %c
      %else %start
         print string("It will be truncated")
         insize=960
      %finish
      new line
   %finish

   read region(fileref, 0, insize, file image(64))

   deaccess file(fileref)

   ! Pad the beginning with the appropriate header
   file image(i)=charno(header,i+1) %for i=0,1,63

   ! Pad the end with blanks, if necessary
   %if insize<960 %then %start
      file image(i)=' ' %for i=insize+63,1,1023
   %finish

   ! Write out file, substituting spaces for control characters
   open output(2,outfile)
   select output(2)
   %for i=0,1,1023 %cycle
      %if file image(i)<32 %then print symbol(' ') %c
      %else print symbol(file image(i))
   %repeat
   close output
   select output(0)

%endofprogram
