%begin
%include "inc:util.imp"
%include "inc:region.imp"

%constinteger fmax=28
%conststring (31) %array name(1:fmax) = %c
"header_length", "image_type", "height", "width", "signed",
"fov_height", "fov_width", "stereo", "baseline", "vergence",
"gaze", "source_id", "processed", "date", "time",
"stop", "focus", "magic", "title",
"subheader","appno", "images", "subs", "x offset", "y offset",
"aspect", "mapwidth", "maplen"

%constintegerarray type(1:fmax) = %c
0,0,0,0,1, 0,0,1,0,2, 2,0,1,3,3, 0,0,0,4, 1,0,0,0,0,0, 0,0,0
!0=unsigned short 1=boolean 2=signed short 3=string(8),4=string(n)

%constintegerarray offset(1:fmax) = %c
0,1,2,3,4, 5,6,7,8,9, 10,11,12,13,17, 21,22,23,24, 247,248,249,250,251,252,
253,254,255

!!%constintegerarray minval(1:fmax) = %c
!!8,0,1,1,0, 0,0,0,0,16_FFFF, 0,0,0,0,0, 0,0,16_???,0, 0,0,0,0,0,0, 0,0,0
!!%constintegerarray maxval(1:fmax) = %c
!!2048,255,4096,4096,1, 16_7FFF,16_7FFF,15,16_7FFF,16_7FFF,
!!16_7FFF,16_7FFF,1,0,0, 16_7FFF,16_7FFF,16_????,0,
!!255,255,15,15,4095,4095, 16_7FFF,64,16_7FFF

%string (255) file, s
%integer i, f, fredno, len, m, n, c
%shortarray buff(0:511)

%routine dump field(%integer f)
   %integer c,i
   printstring(name(f)); spaces(12 - length(name(f)))
   printstring("(")
   %if type(f)=0 %start
      phex4(buff(offset(f)))
   %elseif type(f)=1
      %if buff(offset(f)) = 1 %start
         printstring("True") 
      %elseif buff(offset(f)) = 0
         printstring("False")
      %else
         printstring("Indeterminate ("); phex4(buff(offset(f))); printstring(")")
      %finish
   %elseif type(f)=2
      phex4(buff(offset(f)))
   %elseif type(f)=3
      %for i=0,1,3 %cycle
         c=buff(offset(f)+i); printsymbol(c>>8); printsymbol(c&255)
      %repeat
   %elseif type(f)=4
      %for i=0,1,127 %cycle
         c=buff(offset(f)+i)
         %exit %if c>>8&255=0
         printsymbol(c>>8)
         %exit %if c&255=0
         printsymbol(c&255)
      %repeat
   %finish
%end

%on 0 %start; ->abend; %finish

file = cli param
file = file.".iff" %if exists(file.".iff")
printline("File ".file." inaccessible") %and %stop %unless exists(file)

access file(file, 1, fredno, len)

printline("Reading bytes 0 to 1023")
read region(fredno, 0, 1024, byteinteger(addr(buff(0))))

printline("** Warning: ".file." is not a valid IFF file") %if buff(23)#16_8516
%cycle
   prompt("Field:")
   skipsymbol %while nextsymbol<'0'
   %if '0'<=nextsymbol<='9' %start
      read(f)
   %else
      readline(s)
      %for f=1, 1, fmax %cycle
         -> got it %if name(f) = s
      %repeat
      %exit
   %finish

got it:
   %if 1<=f<=fmax %start
      dump field(f)
      printstring("): ")

      prompt("Alter?")
      %cycle; readsymbol(c); %repeatuntil c&16_5F='Y' %or c&16_5F='N'
      %if c&16_5F='Y' %start
         prompt("New Value:  ")

         %if type(f)=0 %or type(f)=2 %start ;!0=unsigned short 2=signed short
            read(buff(offset(f)))

         %elseif type(f)=1 ;!1=boolean
            %cycle
               readsymbol(c); c=c&16_5F %if 'a' <=c <= 'z'
            %repeatuntil c='T' %or c='F' %or '0'<=c<='9'
            buff(offset(f)) = 0 %if c='F'
            buff(offset(f)) = 1 %if c='T'
            buff(offset(f)) = c-'0' %if '0'<=c<='9'

         %elseif type(f)=3 ;!3=string(8)
            printline("Not implemented yet")
 
         %elseif type(f)=4 ;!4=string(n)
            skipsymbol %while nextsymbol<=' '
            readline(s)
            s=s.tostring(0)
            string(addr(buff(offset(f)))) = s
         %finish
      %finish
   %elseif f=0
      %for f=1, 1, fmax %cycle
         dump field(f); newline
      %repeat
   %else
      printstring("Invalid field number ".itos(f,-1))
   %finish
   newline
%repeat

prompt("Write patches?:")
%cycle; readsymbol(c); %repeatuntil c&16_5F='Y' %or c&16_5F='N'
%if c&16_5F='Y' %start
   printline("Writing bytes 0 to 1023..")
   write region(fredno, 0, 1024, byteinteger(addr(buff(0))))
%finish

abend:
deaccess file(fredno)
%endofprogram
