Previous Up Next

Open files module
(openfiles)

Introduction

This module contains procedures for opening input and output text files. The module can be used by programs that request a file description (consisting of a directory, a filename, an extension and/or a version number) from the user, and to open files whose names are derived from this description. The procedure ask for input file can be used to open a file specified by the user. The description of this file is stored in global variables. The function new filename can be used to construct file descriptions derived from the filename given by the user. This description can be used to open input and output files, using the procedure open input file and open output file, respectively.

This module also contains a function which ask for a yes/no reply on the terminal.

1 Asking for a correct input file description

The procedure ask for input file prompts on the terminal for a text file description of a file to be opened as an input file. The text file to be opened is the file referred to by the first parameter inp file. The second parameter ext contains the default extension to be used when no extension is supplied. The function keeps requesting filenames from the terminal until a file is opened correctly for input.

The local procedure splits name is used to split a given file description into its components, namely: directory path, filename, extension and version. These are stored in the global variables filedirectory, filename, fileextension and fileversion, respectively.

The function open input file is called to open the text file inp file for input as the file with the given file description. This function only returns TRUE when the file could be opened correctly for input. The next subsection describes this function.

1.1 Opening a file as an input file

The function open input file returns TRUE when the text file referred to by the first parameter inp file could be opened and reset as the external file described by the second parameter fn. Otherwise it returns FALSE, and displays an error message on the terminal. There are three reasons why a file cannot be opened for input: the file does not exist, the file is not of the correct type (e.g. not a text file), or the file is empty.

2 Opening a file as an output file

The function open output file returns TRUE when the text file referred to by the first parameter outp file could be opened for output as the external file described by the second parameter fn. Otherwise it returns FALSE and displays an error message on the terminal. There are two reasons why a file cannot be opened for output rewritten: the file cannot be opened, or the file is not of the correct type.

2.1 Constructing new file descriptions

The function new filename returns a new file description, which is equal to the filename of the input file that was opened at the last call to the procedure ask for input file, together with the value of the parameter ext. If no previous call to the procedure ask for input file has been made, then the result of the function is undefined.

3 Yes/no-question function

The function yes echoes the question represented by the parameter question on the terminal, and expects a yes or no answer from the terminal. It checks the first typed character on each successive line until one of the characters "j", "J", "n", "N", "y" or "Y" is typed. The function returns TRUE if the character represents a positive answer, otherwise FALSE.

4 Interface

This module does not use declarations from other modules.

4.1 Exported types

The following types are exported by this module:

4.2 Exported variables

The following variables are exported by this module:

4.3 Exported functions and procedures

The following functions and procedures are exported by this module:

This function returns the result of a yes/no reply on the question represented by question. See section 3

This function returns a filename which equals last given input filename, with the extension equal to ext. See subsection 2.1

This function returns TRUE when the file inp file could be opened and reset as the file with filename fn, otherwise FALSE. See section 1.1

This function returns TRUE when the file outp file could be opened and rewritten as the file with filename fn, otherwise FALSE. See section 2

This procedure asks for the name of the input file and opens the input file. If the input file cannot be opened (i.e., it does not exist or is empty) a new name is repeatedly asked for. See section 1

5 Listing

[ENVIRONMENT ('openfiles.pen')]

MODULE openfiles(input, output);

(*  OPENFILES.PAS contains three procedures that open files and initialize    *)
(*  them, and a function that asks a yes/no question.                         *)

[HIDDEN] CONST
  txt_inputfile = '-inputfile   : ';
  txt_question  = '-question    : ';
  yes_or_no     = '(y/n)';

TYPE
  t_complete_filename  = VARYING[70] OF char;
  t_filename           = VARYING[30] OF char;
  t_fileextension      = VARYING[5]  OF char;
  t_question           = VARYING[30] OF char;

VAR
  input_filename   : t_complete_filename;
  filedirectory    : VARYING[40] OF char;
  filename         : t_filename;
  fileextension    : t_fileextension;
  fileversion      : VARYING[5] OF char;

5.1 Yes/no-question function

  FUNCTION yes (question: t_question) : boolean;
  (* This procedure asks the question and returns TRUE if a Y is given      *)
  (* and FALSE if a N is given and repeats its question in every other case *)
  VAR answer : char;
      ok     : boolean;
  BEGIN
     REPEAT
       write (txt_question, question, ' ', yes_or_no, ' ');
       readln(answer);
       ok := answer IN ['Y','y','J','j','N','n'];
       IF NOT ok THEN writeln ('answer "y" or "n" ');
     UNTIL ok;
     yes := answer IN ['Y','y','J','j']
  END;

5.2 Open functions

  FUNCTION new_filename (ext : t_fileextension) : t_filename;
  (* This function returns a filename equal to filename with      *)
  (* the extension ext.                                           *)
  BEGIN
    new_filename := filename + '.' + ext
  END;

  FUNCTION open_input_file (VAR inp_file : TEXT; fn : t_complete_filename)
                           : boolean;
  (* this function opens and resets the file inp_file with filename fn.      *)
  VAR ok : boolean;
  BEGIN (* open_input_file *)
       ok := FALSE;
       open(inp_file, fn, old , error:=continue);
       IF   status(inp_file) <> 0
       THEN writeln('unsuccessful opening of file : ', fn)
       ELSE BEGIN
              reset(inp_file, error:=continue);
              IF   status(inp_file) > 0
              THEN writeln('wrong kind of inputfile : ', fn)
              ELSE IF   status(inp_file) < 0
                   THEN writeln('empty inputfile : ', fn)
                   ELSE ok := TRUE
            END;
       IF NOT ok THEN close(inp_file, error:=continue);
       open_input_file := ok
  END;

  FUNCTION open_output_file (VAR outp_file : TEXT; fn : t_filename) : boolean;
  (* this function opens and rewrites the file with filename fn *)
  VAR ok : boolean;
  BEGIN (* open_output_file *)
    ok := FALSE;
    open(outp_file, fn, error:=continue);
    IF   status(outp_file) <> 0
    THEN writeln ('unsuccessful opening of file : ', fn)
    ELSE BEGIN rewrite(outp_file, error:=continue);
               IF status(outp_file) > 0
               THEN writeln('wrong kind of output file : ', fn)
               ELSE ok := TRUE
         END;
    IF NOT ok THEN close(outp_file, error:=continue);
    open_output_file := ok
  END;

  PROCEDURE ask_for_input_file (VAR inp_file : TEXT; ext : t_fileextension);
  (* This procedure asks for the name of the input file and opens  *)
  (* the input file. As long as the input file is not satisfactory *)
  (* (that is: not existing or empty) a new name is asked for.     *)

    PROCEDURE splits_name;
    (* This procedure splits the input_filename in the filedirectory,     *)
    (* the filename, the fileextension and the fileversion.               *)
    VAR
      c    : integer;
      more : boolean;

      FUNCTION uppercase (ch : char) : char;
      BEGIN
        IF ch IN ['a'..'z']
        THEN uppercase := chr (ord(ch) + ord('A') - ord('a'))
        ELSE uppercase := ch
      END;

    BEGIN  (* splits_name *)
      filedirectory := ''; filename := ''; fileextension := '';
      fileversion := '';
      c    := 0;
      IF input_filename[1] = '['
      THEN BEGIN
             filedirectory := '[';
             c    := 1;
             more := TRUE;
             WHILE more AND (c < length(input_filename))
                        AND (length(filedirectory) < 40)
             DO BEGIN
                  c := succ(c);
                  IF input_filename[c] IN ['A'..'Z','a'..'z','0'..'9','.']
                  THEN BEGIN
                         input_filename[c] := uppercase(input_filename[c]);
                         filedirectory := filedirectory + input_filename[c]
                       END
                  ELSE more := FALSE
                END;
             filedirectory := filedirectory + ']'
           END;
      more := TRUE;
      WHILE more AND (c < length(input_filename)) AND (length(filename) < 30)
      DO BEGIN
           c := succ(c);
           IF input_filename[c] IN ['A'..'Z','a'..'z','0'..'9','_']
           THEN BEGIN
                  input_filename[c] := uppercase(input_filename[c]);
                  filename := filename + input_filename[c]
                END
           ELSE more := FALSE
         END;
      IF input_filename[c] = '.'
      THEN BEGIN
             more := TRUE;
             WHILE more AND (c < length(input_filename))
                        AND (length(fileextension) < 5)
             DO BEGIN
                  c := succ(c);
                  IF input_filename[c] IN ['A'..'Z','a'..'z','0'..'9']
                  THEN BEGIN
                         input_filename[c] := uppercase(input_filename[c]);
                         fileextension := fileextension + input_filename[c]
                       END
                  ELSE more := FALSE
                END
           END;
      IF input_filename[c] = ';'
      THEN BEGIN
              fileversion := ';';
              more := TRUE;
              WHILE more AND (c < length(input_filename))
                         AND (length(fileversion) < 5)
              DO BEGIN
                   c := succ(c);
                   IF input_filename[c] IN ['0'..'9']
                   THEN fileversion := fileversion + input_filename[c]
                   ELSE more := FALSE
                 END
           END;
    END;  (* splits_name *)


  BEGIN (* ask_for_input_file *)
    REPEAT
       write (txt_inputfile);
       readln (input_filename);
       splits_name;
       IF (length(fileextension) = 0) AND (length(ext) <> 0)
       THEN BEGIN fileextension := ext;
                  input_filename := filedirectory + new_filename(ext) +
                                                           fileversion
            END;
    UNTIL open_input_file(inp_file, input_filename);
  END;  (* open_input_file *)

END.


My life as a hacker | My home page