home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TUR6_102.ZIP / DISKARC.INC < prev    next >
Encoding:
Text File  |  1986-01-01  |  7.0 KB  |  281 lines

  1. {  This module has used routines that were  writen by Michael Quinlin }
  2. {  for used in creating a turbo pascal arc program.  I have used his coding }
  3. {  for reading the arc file and creating the directory information from it. }
  4. {  My thanks and gratitude go to him for this coding. }
  5.  
  6. type long    = record           { used to simulate long (4 byte) integers }
  7.                  l, h : integer
  8.                end;
  9.  
  10. const BLOCKSIZE = 128;
  11.       NUMFILES  = 1;
  12.  
  13. var
  14.                fil     : file;
  15.                rw      : char;
  16.                buf     : array [1..BLOCKSIZE] of char;
  17.                next    : integer;
  18.                endfile : boolean;
  19.                lastout : char;
  20.  
  21. const arcmarc = 26;              { special archive marker }
  22.       arcver  = 6;               { archive header version code }
  23.       strlen  = 100;             { standard string length }
  24.       fnlen   = 12;              { file name length - 1 }
  25.  
  26. type strtype = string[strlen];
  27.      fntype  = array[0..fnlen] of char;
  28.  
  29. type heads   = record
  30.                  name   : fntype;
  31.                  size   : long;
  32.                  date   : integer;
  33.                  time   : integer;
  34.                  crc    : integer;
  35.                  length : long
  36.                end;
  37.  
  38. var hdrver              : byte;
  39.     arcname             : strtype;
  40.     arcdate             : integer;
  41.     arctime             : integer;
  42.     arcopen             : boolean;
  43.     FilePosition        : real;
  44.  
  45. procedure abort(s : strtype);
  46.   begin
  47.     writeln('ABORT: ', s);
  48.     halt(1)
  49.   end;
  50.  
  51. function fn_to_str(var fn : fntype) : strtype;
  52. { convert strings from C format (trailing 0) to Turbo Pascal format (leading
  53.     length byte). }
  54.   var s : strtype;
  55.       i : integer;
  56.   begin
  57.     s := '';
  58.     i := 0;
  59.     while fn[i] <> #0 do begin
  60.       s := s + fn[i];
  61.       i := i + 1
  62.     end;
  63.     fn_to_str := s
  64.   end;
  65.  
  66. function unsigned_to_real(u : integer) : real;
  67. { convert unsigned integer to real }
  68. { note: INT is a function that returns a REAL!!!}
  69.   begin
  70.     if u >= 0 then unsigned_to_real := Int(u)
  71.     else if u = $8000 then unsigned_to_real := 32768.0
  72.     else unsigned_to_real := 65536.0 + u
  73.   end;
  74.  
  75. function long_to_real(l : long) : real;
  76. { convert long integer to a real }
  77. { note: INT is a function that returns a REAL!!! }
  78.   var r : real;
  79.       s : (POS, NEG);
  80.   const rcon = 65536.0;
  81.   begin
  82.     if l.h >= 0 then begin
  83.       r := Int(l.h) * rcon;
  84.       s := POS
  85.     end else begin
  86.       s := NEG;
  87.       if l.h = $8000 then r := rcon * rcon
  88.       else r := Int(-l.h) * rcon
  89.     end;
  90.     r := r + unsigned_to_real(l.l);
  91.     if s = NEG then long_to_real := -r
  92.     else long_to_real := r
  93.   end;
  94.  
  95. procedure Read_Block;
  96.   begin
  97.       if EOF(fil) then endfile := TRUE
  98.       else BlockRead(fil, buf, 1);
  99.       next := 1;
  100.   end;
  101.  
  102.  
  103. function fopen(var name : strtype) : boolean;
  104. { only binary I/O supported; }
  105.   begin
  106.     {$I-}
  107.     assign(fil, name);
  108.     {$I+}
  109.     if ioresult <> 0 then begin
  110.       fopen := FALSE;
  111.       exit;
  112.     end;
  113.  
  114.     {$I-}
  115.     reset(fil);
  116.     {$I+}
  117.     if ioresult <> 0 then begin
  118.       fopen := FALSE;
  119.     exit
  120.     end;
  121.     endfile  := FALSE;
  122.     rw := 'R';
  123.     Read_Block;
  124.     FilePosition := 0.0;
  125.     fopen := TRUE
  126.   end;
  127.  
  128. function fgetc : char;
  129.   begin
  130.       if endfile then fgetc := chr(0)
  131.       else begin
  132.         fgetc := buf[next];
  133.  
  134.         if next = BLOCKSIZE then Read_Block
  135.         else next := next + 1
  136.       end;
  137.   end;
  138.  
  139. function fgetb : byte;
  140.   begin
  141.       if endfile then fgetb := 0
  142.       else begin
  143.         fgetb := ord(buf[next]);
  144.         if next = BLOCKSIZE then Read_Block
  145.         else next := next + 1
  146.       end;
  147.   end;
  148.  
  149. function feof : boolean;
  150.   begin
  151.     feof := endfile
  152.   end;
  153.  
  154. procedure fclose;
  155.   begin
  156.       close(fil);
  157.   end;
  158.  
  159. procedure do_seek(offset : real);
  160.   var i, ofs, rec : integer;
  161.       c           : char;
  162.   begin
  163.     rec := Trunc(Offset / BLOCKSIZE);
  164.     ofs := Trunc(Offset - (Int(rec) * BLOCKSIZE));  { Int converts to Real }
  165.     seek(fil, rec);
  166.     Read_Block;
  167.     for i := 1 to ofs do c := fgetc;
  168.   end;
  169.  
  170. procedure fseek(offset : real);
  171. { only works with input files }
  172.   var b,c : real;
  173.   begin
  174.       FilePosition := FilePosition + offset;
  175.       do_seek(FilePosition)
  176.   end;
  177.  
  178. procedure fread(var buf; reclen, numrec : integer);
  179.   var i, n : integer;
  180.       b : array [1..MaxInt] of char absolute buf;
  181.   begin
  182.     n := reclen * numrec;
  183.     for i := 1 to n do b[i] := fgetc;
  184.     FilePosition := FilePosition + n + 2;
  185.   end;
  186.  
  187. function readhdr(var hdr : heads) : boolean;
  188. { FALSE = eof found; TRUE = header found }
  189.   var name : fntype;
  190.       try  : integer;
  191.   begin
  192.     try := 20;
  193.  
  194.     if feof then begin
  195.       readhdr := FALSE;
  196.       exit
  197.     end;
  198.  
  199.     while fgetb <> arcmarc do begin
  200.       if try = 0 then abort(arcname + ' is not an archive');
  201.       try := try - 1;
  202.       if feof then abort('Archive length error')
  203.     end;
  204.  
  205.     hdrver := fgetb;
  206.     if hdrver < 0 then abort('Invalid header in archive ' + arcname);
  207.     if hdrver = 0 then begin   { special end of file marker }
  208.       readhdr := FALSE;
  209.       exit
  210.     end;
  211.     if hdrver > arcver then begin
  212.       fread(name, sizeof(char), fnlen{, f});
  213.       writeln('I dont know how to handle file ', fn_to_str(name),
  214.         ' in archive ', arcname);
  215.       writeln('I think you need a newer version of DISKOVER.');
  216.       halt(1)
  217.     end;
  218.  
  219.     if hdrver = 1 then begin
  220.       fread(hdr, sizeof(heads) - sizeof(long), 1);
  221.       hdrver := 2;
  222.       hdr.length := hdr.size
  223.     end else
  224.       fread(hdr, sizeof(heads), 1);
  225.     readhdr := TRUE
  226.   end;
  227.  
  228. procedure openarc;
  229.   begin
  230.     if not fopen(arcname) then
  231.       arcopen := FALSE
  232.     else
  233.       arcopen := TRUE;
  234.   end;
  235.  
  236. procedure lstfile(var hdr : heads);{ ArcList : SortArray; i : integer);}
  237.   var yr, mo, dy : integer;
  238.       hh, mm, ss : integer;
  239.       b          : integer;
  240.       fn         : strtype;
  241.       rlen, rsiz : real;
  242.       aorp       : char;
  243.       sthold     : string[7];
  244.       s          : string[2];
  245.  
  246.   procedure twodig(n : integer);
  247.     begin
  248.       str(n:2, s);
  249.       if s[1] = ' ' then s[1] := '0';
  250.       if s[2] = ' ' then s[2] := '0';
  251.     end;
  252.  
  253.  
  254.   begin
  255.     fn := fn_to_str(hdr.name);
  256.     rlen := long_to_real(hdr.length);
  257.     rsiz := long_to_real(hdr.size);
  258.  
  259.     yr := (hdr.date shr 9) and $7F;
  260.     mo := (hdr.date shr 5) and $0F;
  261.     dy := hdr.date and $1F;
  262.  
  263.     hh := (hdr.time shr 11) and $1F;
  264.     mm := (hdr.time shr 5) and $3F;
  265.     ss := (hdr.time and $1F) * 2;
  266.  
  267.     ArcList[ArcSrn] := ReadDrive + fn;
  268.     for b := length(fn) to 12 do ArcList[ArcSrn] := ArcList[ArcSrn] + ' ';
  269.     twodig(mo);
  270.     ArcList[ArcSrn] := ArcList[ArcSrn] + s + '-';
  271.     twodig(dy);
  272.     ArcList[ArcSrn] := ArcList[ArcSrn] + s + '-';
  273.     twodig((yr + 80) mod 100);
  274.     ArcList[ArcSrn] := ArcList[ArcSrn] + s + '  ';
  275.     twodig(hh);
  276.     ArcList[ArcSrn] := ArcList[ArcSrn] + s + ':';
  277.     twodig(mm);
  278.     Str(rlen:7:0,sthold);
  279.     ArcList[ArcSrn] := ArcList[ArcSrn] + s + sthold + '     ';
  280.   end;
  281.