(*--------------------------------------------------------------------*) procedure open_file(file_mode : f_modes; fileref : string15); (* This procedure attempts to open a file for writing or reading using standard Turbo Pascal procedures. If the file is opened successfully, open_ok is returned, and file_open is returned true *) var count, space_pos : integer; drive : string1; temp_fn : string[20]; filename : string[15]; filetype : string[3]; procedure open_for_write(fileref : string15; var open_ok : boolean); (* On an open for write, if reset is successful, the file already exists. In this version, we never want to destroy an existing file, so the file is never rewritten *) begin assign(outfile, fileref); (* assign filvar *) {$I-} (* turn off io checking *) reset(outfile); (* try to open it *) {$I+} (* allow error checking again *) if ioresult <> 0 then (* 0 for open, not 0 for not found *) begin (* filename is new file, open it *) rewrite(outfile); file_records := filesize(outfile); (* get the size of the file *) open_ok := true; (* flags for calling procedure *) file_open := true; buffer_num := 0; (* we are at the first buffer of data *) end else open_ok := false; (* The file already existed *) end; begin (* open_file *) case file_mode of read_open : begin assign(outfile, fileref); (* try to open file *) {$I-} reset(outfile); {$I+} if ioresult = 0 then (* yes, it exists *) begin open_ok := true; file_open := true; file_records := filesize(outfile); gotoxy(62,7); (* display filesize for progress report *) write((file_records * 128):6); buffer_num := 0; end else (* couldn't open file *) begin open_ok := false; gotoxy(1,8); write('File ', fileref, ' does not exist.'); end; end; write_open : begin open_for_write(fileref, open_ok); (* try on entry *) if not open_ok then (* File already existed, so we'll try to build a unique filename for the file and open that. For reasons I don't remember, it will only try to insert '&' signs until all the unfilled character positions in the filename are used up. The original filename will always be present. *) begin temp_fn := fileref; repeat adjust_fn(temp_fn, drive, filename, filetype); temp_fn := filename + '.' + filetype; if drive <> '!' then temp_fn := drive + ':' + temp_fn; space_pos := pos(' ',temp_fn); if space_pos <> 0 then begin delete(temp_fn,space_pos,1); insert('&',temp_fn,space_pos); while pos(' ',temp_fn) <> 0 do delete(temp_fn,pos(' ',temp_fn),1); open_for_write(temp_fn, open_ok); end; until (open_ok) or (space_pos = 0); gotoxy(1,9); if open_ok then (* print the new filename *) if (temp_fn <> fileref) then write('Filename ',fileref, ' changed to: ', temp_fn) else write('Filename: ',temp_fn) else write('Filename ', fileref, ' could not be opened.'); end; end; end; (* case *) end; (* open_file *)