home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / mbug / mbug041.arc / FILE-IO.MOD < prev    next >
Text File  |  1979-12-31  |  6KB  |  171 lines

  1. { GENERALITY : Borland turbo pascal
  2.   APPLICATION AREA : file input / output
  3.   MODULE DESCRIPTION : prompts user to enter the name of a file to be opened
  4.   MODULE NAME : file-io.mod (file input / output module)
  5.   PROCEDURE DESCRIPTIONS :
  6.     io_error_message - displays on the output device the input / output error
  7.       message corresponding to the turbo pascal input / output error message
  8.       number given
  9.     open_input - prompt user to enter the name of a file which is then opened
  10.       for input
  11.     open_output - prompt user to enter the name of a file which is then opened
  12.       for output
  13.   NECESSARY MODULES : none
  14.   NECESSARY DECARATION : the source program must include a type declaration of
  15.      the type file_type eg. TYPE file_type = text;
  16.   SOURCE LANGUAGE : Borland turbo pascal (ver 2)
  17.   WRITTEN : 25 December 1985 - G. Irlam
  18.   LAST MODIFIED : 27 December 1985 - G. Irlam
  19.  
  20.   COMMENTS :
  21.     Reason for language implementation dependance - standard pascal does not
  22.       allow the testing of the success of an attempted file opening. However
  23.       other portions of code are also non-standard, this is because of
  24.       lazyness.
  25.     A word of warning on turbo pacal input / output. The use of a logical
  26.       device (eg. CON:, TRM:, ..) as an input file will often cause problems.
  27.       The behaviour of these logical devices differs from the standard text
  28.       input file type in that a call to eoln or eof will return its status
  29.       based on the last key pressed prior to the call (normally the status is
  30.       dependent upon the next character to be processed). I am not aware of an
  31.       easy solution to this problem. A normal pascal compiler gets around this
  32.       problem by not returning from an eoln or eof call on an interactive
  33.       device until the next character is known. It is conceivable that a
  34.       similar action could be implemented through use of the keypressed
  35.       function (and possibly buffering) prior to each such call. I have not
  36.       tried this, instead I avoid the use of the logical devices in
  37.       application programs which normally expect input from a disk file and
  38.       do more than simple readlns.}
  39.  
  40.   TYPE
  41.     fname = string [80];
  42.  
  43.   PROCEDURE io_error_message (error_number : byte; f_name : fname);
  44.  
  45.     VAR
  46.       file_name : fname;
  47.       i : integer;
  48.  
  49.     PROCEDURE name_at_end;
  50.       { Write f_name to output and then a full stop. }
  51.  
  52.       BEGIN
  53.         IF f_name <> '' THEN
  54.           write (' ', file_name);
  55.         writeln ('.')
  56.       END;
  57.  
  58.     BEGIN
  59.       write ('IO error -  ');
  60.       IF f_name = '' THEN
  61.         file_name := 'File'
  62.         { Default file name. }
  63.       ELSE
  64.         BEGIN
  65.           file_name := f_name;
  66.           FOR i := 1 TO length (file_name) DO
  67.             file_name [i] := upcase (file_name [i])
  68.         END;
  69.       { Write error message. }
  70.       CASE error_number OF
  71.         $01 :
  72.           writeln (file_name, ' does not exist.');
  73.         $02 :
  74.           writeln (file_name, ' not open for input.');
  75.         $03 :
  76.           writeln (file_name, ' not open for output.');
  77.         $04 :
  78.           writeln (file_name, ' not open.');
  79.         $10 :
  80.           writeln ('Error in numeric format.');
  81.         $20 :
  82.           writeln ('Operation not allowed on a logical device.');
  83.         $21 :
  84.           BEGIN
  85.             write ('Operation not allowed on untyped file');
  86.             name_at_end
  87.           END;
  88.         $22 :
  89.           writeln ('Reassignment of a standard file illegal.');
  90.         $90 :
  91.           BEGIN
  92.             write ('Record length does not match that of file');
  93.             name_at_end
  94.           END;
  95.         $91 :
  96.           BEGIN
  97.             write ('Seek attempted beyond end of file');
  98.             name_at_end
  99.           END;
  100.         $99 :
  101.           writeln (file_name, ' ends unexpectedly.');
  102.         $F0 :
  103.           writeln ('Disk can not be written to.');
  104.         $F1 :
  105.           writeln ('Directory full.');
  106.         $F2 :
  107.           writeln (file_name, ' over-flowed disk space.');
  108.         $FF :
  109.           writeln (file_name, ' has disappeared.')
  110.       END
  111.     END;
  112.  
  113. PROCEDURE open_input (VAR f : file_type);
  114.  
  115.   VAR
  116.     io_error : byte;
  117.     filename : string [80];
  118.  
  119.   BEGIN
  120.     REPEAT
  121.       write ('Enter input filename : ');
  122.       readln (filename);
  123.       assign (f, filename);
  124. {$I-}
  125.       reset (f);
  126. {$I+}
  127.       io_error := ioresult;
  128.       IF io_error > 0 THEN
  129.         io_error_message (io_error, filename)
  130.     UNTIL io_error = 0;
  131.   END;
  132.  
  133. PROCEDURE open_output (VAR f : file_type);
  134.  
  135.   VAR
  136.     io_error : byte;
  137.     filename : string [80];
  138.     ch : char;
  139.     i  : integer;
  140.  
  141.   BEGIN
  142.     REPEAT
  143.       REPEAT
  144.         write ('Enter output filename : ');
  145.         readln (filename);
  146.         assign (f, filename);
  147. {$I-}
  148.         rename (f, filename);
  149. {$I+}
  150.         ch := 'y';
  151.         IF ioresult = 0 THEN
  152.           BEGIN
  153.             FOR i := 1 TO length (filename) DO
  154.               filename [i] := upcase (filename [i]);
  155.             writeln (filename, ' already exists.');
  156.             write ('Overwrite (Y/N)? ');
  157.             REPEAT
  158.               read (kbd, ch)
  159.             UNTIL ch IN ['Y', 'y', 'N', 'n'];
  160.             writeln (upcase (ch))
  161.           END
  162.       UNTIL ch IN ['Y', 'y'];
  163. {$I-}
  164.       rewrite (f);
  165. {$I+}
  166.       io_error := ioresult;
  167.       IF io_error > 0 THEN
  168.         io_error_message (io_error, filename)
  169.     UNTIL io_error = 0
  170.   END;
  171.