!****************************************************************
!*                                                              *
!*      SORTNAMES:  Produce a sorted list of names and PIN      *
!*                  nos from the Canon copy controller RAM      *
!*                                                              *
!*                  Version 1.1    6 Mar 1989                   *
!*                                                              *
!****************************************************************

%begin
%include "inc:util.imp"
%string (255) infile,namefile,outfile

%integer n
%string (63) %array info(0:511)
%integer c,p,ad,len,entries,acct,pinhi,pinlo,group,i,acopies,glimit,gcopies
%recordformat itemf(%integer pin,acct)
%record (itemf) %array item(0:1023)

%integerfn bcd(%integer ad)
   %integer a0,a1,a2,a3,a4,a5
   a0=byte(ad)>>4;   a1=byte(ad)&15
   a2=byte(ad+1)>>4; a3=byte(ad+1)&15
   a4=byte(ad+2)>>4; a5=byte(ad+2)&15
   %result=((((a0*10+a1)*10+a2)*10+a3)*10+a4)*10+a5
%end

%routine p2(%integer ad)
   %if byteinteger(ad)=0 %then spaces(2) %else phex2(byteinteger(ad))
%end

%routine get names
   %integer n
   %on 3,9 %start; ->eof; %finish
   %for n=0,1,511 %cycle; info(n)=""; %repeat
   openinput(1,namefile); selectinput(1)
   %cycle
     read(n)
     %exit %if n<0
     readline(info(n))
   %repeat
   eof:
   close input
%end

   %routine PINsort(%record (itemf) %array %name s(0:1023))
      !Sort integer array data throughout its length
      %integer i,j,k,l,n1
      %record (itemf) temp

      n1 = 1
      l = entries - 2
      n1 = 2*n1 %while n1<l
      l = 0
       %while n1 > 1 %cycle
         n1 = n1 // 2
         i = l
          %cycle
            i = i+1
            j = i+n1
            %exitif j > entries
            k = i
             %cycle
               %exitif s(j-1)_pin > s(k-1)_pin
               temp = s(j-1); s(j-1) = s(k-1); s(k-1) = temp
               j = k
               k = k - n1
             %repeatuntil k < 1
          %repeat
       %repeat
    %end

   %routine dump item(%integer i)
      write(i, 4); printstring(": ")
      phex(item(i)_pin); space; printstring(info(item(i)_acct))
      newline
   %end

namefile="names"
get names
infile = cli param
outfile="" %unless infile -> infile.("/").outfile
printline("Can't access RAM file") %and %stop %unless exists(infile)
ad = heapget(8192)
connectfile(infile,0,ad,len)
printline("***** RAM file wrong length (".itos(len,-1).") *****") %unless len=8192
open output(2, outfile) %and selectoutput(2) %unless outfile=""

printline("Acct     PIN   Gp  Limit Copies ")
newline

entries=0
%for p={0} 16_1DF9, -8, 16_1701 %cycle
   acct = ad+p
   item(entries)_acct=entries
   item(entries)_pin = (byte(acct)<<8+byte(acct+1))<<8+byte(acct+2)
   entries=entries+1
%repeat

close input
PINsort(item)

%for i=0,1,entries-2 %cycle
  %if item(i)_pin = item(i+1)_pin %start
     dump item(i)
     dump item(i+1)
     newline
  %finish
%repeat
    
%for i=0,1,entries-1 %cycle
   dump item(i)
%repeat

closeoutput
selectoutput(0)
%endofprogram
