home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / TURBOPAS / ENVIRON.LBR / STDIO.PZS / STDIO.PAS
Pascal/Delphi Source File  |  2000-06-30  |  9KB  |  423 lines

  1. { last mod 04-Jul-85 }
  2. {$X-}
  3. procedure ioinit(numfiles:integer);
  4.  
  5. { initialize i/o variables, character table }
  6.  
  7. var
  8.      i           :integer;
  9.      fbp       :^fbuf;
  10. begin
  11.      if numfiles + 3 > MAXOPEN then
  12.       error('Too many files requested.');
  13.      openlist[TRMIN].mode := IOREAD;
  14.      openlist[TRMOUT].mode := IOWRITE;
  15.      openlist[PRINTER].mode := IOWRITE;
  16.      for i:=PRINTER+1 to PRINTER+numfiles do
  17.       with openlist[i] do
  18.       begin
  19.            new(fbp);
  20.            fbufptr := fbp;
  21.            mode := IOAVAIL;
  22.       end;
  23.      for i:=PRINTER+numfiles+1 to MAXOPEN do
  24.       openlist[i].mode := IONAVAIL;
  25.      for i:=0 to 47 do chartbl[i] := 'X';
  26.      for i:=48 to 57 do chartbl[i] := 'D';
  27.      for i:=58 to 64 do chartbl[i] := 'X';
  28.      for i:=65 to 90 do chartbl[i] := 'U';
  29.      for i:=91 to 96 do chartbl[i] := 'X';
  30.      for i:=97 to 122 do chartbl[i] := 'L';
  31.      for i:=123 to 127 do chartbl[i] := 'X';
  32. end;
  33.  
  34. function open(var name:textline; accmode:integer):filedesc;
  35.  
  36. { open a file with the given name for access in the given mode }
  37.  
  38. var
  39.      intname   :string80;
  40.      found     :boolean;
  41.      i           :integer;
  42.  
  43. function openfile(accmode:integer; var iostuff: ioblock; var intname: string80)
  44.          :boolean;
  45.  
  46. { machine-dependent subroutine, attempts to open file with name intname
  47.   and mode accmode.  If open ok, initializes iostuff and returns 'true'.
  48.   If error, returns 'false' }
  49.  
  50. var
  51.      foundcz   :boolean;
  52.      j           :integer;
  53.      fs           :integer;
  54.      mode2     :byte;
  55. begin
  56.      {$i-}
  57.      openfile := false;
  58.      with iostuff do
  59.      begin
  60.       assign(filevar,intname);
  61.       if ioresult = 0 then
  62.       begin
  63.            mode2 := accmode and MODEMASK;
  64.            if mode2 = IOREAD then
  65.            begin
  66.             reset(filevar);
  67.             lastrec := filesize(filevar);
  68.             reccnt := 0;
  69.             bufindx := FBUFSIZE+1;
  70.            end
  71.            else if mode2 = IOWRITE then
  72.            begin
  73.             rewrite(filevar);
  74.             bufindx := 1;
  75.            end
  76.            else if mode2 = IOAPPEND then
  77.            begin
  78.             reset(filevar);
  79.             fs := filesize(filevar);
  80.             if (ioresult = 0) and (fs > 0) then
  81.             { file already exists }
  82.             begin
  83.              seek(filevar,fs-1);
  84.              blockread(filevar,fbufptr^,1);
  85.              seek(filevar,fs-1);
  86.              { to overwrite last sector of file }
  87.              j := 1; foundcz := false;
  88.              while (j<=SECTSIZE) and (not foundcz) do
  89.              begin
  90.                   foundcz := ord(fbufptr^[j]) = eofchar;
  91.                   if not foundcz then j:=j+1;
  92.              end;
  93.              bufindx := j;
  94.             end
  95.             else { file doesn't exist, create it }
  96.             begin
  97.              rewrite(filevar);
  98.              bufindx := 1;
  99.             end;
  100.            end;
  101.            if ioresult = 0 then
  102.            begin
  103.             openfile := true; mode := accmode; { flag file open }
  104.             eofflag := false;
  105.            end;
  106.       end
  107.       {$i+}
  108.      end;
  109. end; { openfile }
  110.  
  111. begin { open }
  112.      intname := makestring(name);
  113.      open := IOERROR;
  114.      found := false;
  115.      i := 1;
  116.      while (i<=MAXOPEN) and (not found) do
  117.      begin
  118.       found := (openlist[i].mode = IOAVAIL);
  119.       if found then
  120.       begin
  121.            if openfile(accmode,openlist[i],intname) then
  122.             open := i;
  123.       end
  124.       else i := i + 1;
  125.      end;
  126. end { open };
  127. {$X+}
  128.  
  129. procedure remove(var name:textline);
  130.  
  131. { removes a file }
  132.  
  133. var
  134.      filvar    :file;
  135.      intname   :string80;
  136. begin
  137.      {$i-}
  138.      intname := makestring(name);
  139.      assign(filvar,intname);
  140.      {$i+}
  141.      if ioresult = 0 then
  142.       erase(filvar);
  143. end;
  144.  
  145. procedure putc(c:character);
  146.  
  147. { puts 1 character to std. output }
  148.  
  149. begin
  150.      if c = NEWLINE then
  151.       writeln
  152.      else
  153.       write(chr(c));
  154. end;
  155.  
  156. {$b-}
  157. function keyin(var c:character):character;
  158.  
  159. { gets a char. from the keyboard, doesn't echo it}
  160.  
  161. var
  162.      ch      :char;
  163. begin
  164.      read(Kbd,ch);
  165.      c := ord(ch);
  166.      if (c = eofchar) then
  167.       c := ENDFILE
  168.      else if c = CR then
  169.       c := NEWLINE;
  170.      keyin := c;
  171. end;
  172. {$b+}
  173.  
  174. function getc(var c:character):character;
  175.  
  176. { get 1 character from keyboard, echo it to screen}
  177.  
  178. var
  179.      ch      :char;
  180.  
  181. begin
  182.      c := keyin(c);
  183.      putc(c);
  184.      getc := c;
  185. end;
  186.  
  187. function getbyte(var b:byte; fd:filedesc): boolean;
  188.  
  189. { reads a binary byte from the file, returns false if physical end of file }
  190.  
  191. begin
  192.      getbyte := true;
  193.      with openlist[fd] do
  194.      begin
  195.       if eofflag then
  196.            getbyte := false
  197.       else
  198.       begin
  199.            if (bufindx > FBUFSIZE) then
  200.            begin
  201.             if eof(filevar) then
  202.             begin
  203.             getbyte := false;
  204.             eofflag := true;
  205.             end
  206.             else
  207.             begin
  208.              {$i-}
  209.              blockread(filevar,fbufptr^[1],FBUFSIZE div SECTSIZE);
  210.              {$i+}
  211.              if not (ioresult in [$99,0]) then
  212.                   error('Disk read error');
  213.             end;
  214.             bufindx := 1;
  215.            end;
  216.            b := fbufptr^[bufindx];
  217.            if bufindx and (SECTSIZE-1) = 0 then
  218.         {don't read past last record}
  219.            begin
  220.             reccnt:=reccnt+1;
  221.             if reccnt>=lastrec then eofflag := true;
  222.            end;
  223.            bufindx := bufindx + 1;
  224.       end;
  225.      end;
  226. end; { getbyte}
  227.  
  228. function getcf(var c:character; fd: filedesc):character;
  229.  
  230. { get a character from a file }
  231.  
  232. var
  233.      junk :boolean;
  234.      b      :byte;
  235. begin
  236.      if fd  = TRMIN then
  237.       getcf := getc(c)
  238.      else with openlist[fd] do
  239.      begin
  240.       if getbyte(b,fd) then
  241.       begin
  242.            c := b and $7F;
  243.            if c = eofchar then
  244.            begin
  245.             c := ENDFILE;
  246.             eofflag := true;
  247.            end
  248.            else
  249.            begin
  250.             if (c = CR) or (c = LF) then
  251.             begin
  252.              junk := getbyte(b,fd);
  253.              c := NEWLINE;
  254.             end
  255.            end;
  256.       end
  257.       else
  258.            c:=ENDFILE;
  259.       getcf := c;
  260.      end;
  261. end { getcf };
  262.  
  263. procedure putbyte(b:byte; fd:filedesc);
  264.  
  265. { writes a binary byte to the file }
  266.  
  267. begin
  268.      with openlist[fd] do
  269.      begin
  270.       fbufptr^[bufindx] := b;
  271.       bufindx := bufindx + 1;
  272.       if bufindx > FBUFSIZE then
  273.       begin
  274.            {$i-}
  275.            blockwrite(filevar,fbufptr^[1],FBUFSIZE div SECTSIZE);
  276.            {$i+}
  277.            if ioresult<>0 then error('Disk write error');
  278.            bufindx := 1;
  279.       end
  280.      end
  281. end; { putbyte }
  282.  
  283. procedure putcf(c:character; fd: filedesc);
  284.  
  285. { put a character to a file }
  286.  
  287. begin
  288.      if fd = TRMOUT then
  289.       putc(c)
  290.      else if fd = PRINTER then
  291.      begin
  292.       if c = NEWLINE then writeln(lst) else write(lst,chr(c));
  293.      end
  294.      else
  295.      begin
  296.       if c = NEWLINE then { do cr first }
  297.       begin
  298.            putbyte(CR,fd);
  299.            c := LF;
  300.       end;
  301.       putbyte(c,fd);
  302.      end;
  303. end { putcf };
  304.  
  305. procedure pclose(fd: filedesc);
  306.  
  307. { close a file }
  308.  
  309. begin
  310.      if not (fd in [TRMIN,TRMOUT,PRINTER]) then
  311.      with openlist[fd] do
  312.      begin
  313.       if ((mode and MODEMASK) in [IOWRITE,IOAPPEND]) then
  314.       { flush last buffer }
  315.       begin
  316.            if (mode and BINMASK) = 0 then putcf(eofchar,fd);
  317.            if bufindx > 1 then
  318.             blockwrite(filevar,fbufptr^[1],
  319.                   ((bufindx-2) div SECTSIZE)+1);
  320.       end;
  321.       close(filevar);
  322.       mode := IOAVAIL;
  323.      end;
  324. end;
  325.  
  326. function getline(var s:textline; fd:filedesc; maxsize:integer):boolean;
  327.  
  328. { gets line from file, returns false if end of file }
  329.  
  330. var
  331.      i      :integer;
  332.      c      :character;
  333. begin
  334.      i := 1;
  335.      repeat
  336.       if fd = TRMIN then {handle terminal line editing }
  337.       begin
  338.           s[i] := keyin(c);
  339.           if (c=bks) then
  340.           begin
  341.           if (i>1) then
  342.           begin
  343.               i := i - 1; putc(bks); putc(space); putc(bks)
  344.           end
  345.           end
  346.           else if ((c>=32) and (c<>127)) or (c=NEWLINE) then
  347.           begin
  348.           i := i + 1;
  349.           putc(c)
  350.           end
  351.       end
  352.       else
  353.       begin
  354.           s[i] := getcf(c,fd);
  355.           i := i + 1;
  356.       end
  357.      until (c = NEWLINE) or (c = ENDFILE) or (i>=maxsize);
  358.      if c = ENDFILE then
  359.       i := i - 1;
  360.      s[i] := EOS;
  361.      getline := (c <> ENDFILE);
  362. end;
  363.  
  364. procedure putstr(var str:textline; fd:filedesc);
  365.  
  366. { put string in a file }
  367.  
  368. var
  369.      i      :integer;
  370. begin
  371.      i := 1;
  372.      while str[i] <> EOS do
  373.      begin
  374.       putcf(str[i],fd);
  375.       i := i + 1;
  376.      end;
  377. end;
  378.  
  379. function getfile(var filevar :filedesc;
  380.          var prompt:textline;
  381.          var name:textline;
  382.          mode:integer) :boolean;
  383.  
  384. { get file name from keyboard and open file, returns 'false' if
  385.   CR entered after prompt }
  386.  
  387. var
  388.      openok,nofile    :boolean;
  389.      junk      :boolean;
  390.      fd           :filedesc;
  391.      lenname   :integer;
  392.  
  393. begin
  394.      openok := false;
  395.      repeat
  396.       putstr(prompt,TRMOUT);
  397.       {$u+}
  398.       junk := getline(name,TRMIN,MAXSTR);
  399.       {$u-}
  400.       lenname := slength(name);
  401.       if name[lenname] = NEWLINE then name[lenname] := EOS;
  402.       nofile := (name[1] in [EOS,NEWLINE]);
  403.       if not nofile then
  404.       begin
  405.           fd := open(name,mode);
  406.           openok := fd <> IOERROR;
  407.           if openok then
  408.           begin
  409.            filevar := fd;
  410.            getfile := true;
  411.           end
  412.           else
  413.           begin
  414.            writeln;
  415.            write('Can''t open: '); putstr(name,TRMOUT);
  416.            writeln;
  417.           end
  418.       end
  419.       else getfile := false;
  420.       until openok or nofile;
  421. end;
  422.  
  423.