next up previous index
Next: Miscellaneous Up: Input/output Previous: Random numbers   Index

Unix files

In file.cecil:

A unix_file object acts like a mutable, extensible, positionable stream of characters, as well as supporting lots of standard file I/O operations.

    template object unix_file
                isa m_positionable_stream[char], extensible_stream[char];

method stdin():unix_file;
method stdout():unix_file;
method stderr():unix_file;
abstract object open_mode;
concrete representation open_for_reading isa open_mode;
concrete representation create_for_writing isa open_mode;
concrete representation open_for_append isa open_mode;
concrete representation open_for_update isa open_mode;
concrete representation create_for_update isa open_mode;
concrete representation open_for_appending_update isa open_mode;
concrete representation open_binary_for_reading isa open_mode;
concrete representation create_binary_for_writing isa open_mode;
concrete representation open_binary_for_append isa open_mode;
concrete representation open_binary_for_update isa open_mode;
concrete representation create_binary_for_update isa open_mode;
concrete representation open_binary_for_appending_update isa open_mode;
method open_file(n@:string, m@:open_mode):unix_file;
method open_file(n@:string, m@:open_mode, if_error:&(string):unix_file
                 ):unix_file;
method name(f@:unix_file):string;
method is_readable(f@:unix_file):bool;
method is_unreadable(f@:unix_file):bool;
method is_writable(f@:unix_file):bool;
method is_unwritable(f@:unix_file):bool;
method is_read_write(f@:unix_file):bool;
method is_append(f@:unix_file):bool;
method read(f@:unix_file, buffer:m_indexed[char], size:int):void;
method read(f@:unix_file, buffer:m_indexed[char], size:int,
            if_error:&(string):void):void;
method read(f@:unix_file, buffer@:m_vstring, size:int,
            if_error:&(string):void):void;
method read_whole_text_file(f@:unix_file):string;
method read_whole_text_file(f@:unix_file, if_error:&(string):string):string;
method read_partial(f@:unix_file, buffer:m_indexed[char], from:int, size:int
                    ):int;
method read_partial(f@:unix_file, buffer:m_indexed[char], from:int, size:int,
                    if_error:&(string):int):int;
method read_partial(f@:unix_file, buffer@:m_vstring, from@:int, size@:int,
                     if_error:&(string):int):int;
method read_line(f@:unix_file, if_eof@:&():string):string;  - returns the line's contents with no trailing
n; ``'' if the line is empty

method read_line(f@:unix_file,
                 if_eof@:&():string,
                 if_error@:&(string):string):string;
method read_line(f@:unix_file, buffer@:m_indexed[char], size:int):int;
method read_line(f@:unix_file, buffer@:m_indexed[char], size:int,
                 if_error:&(string):int):int;
method read_line(f@:unix_file, buffer@:m_vstring, size:int,
                 if_error:&(string):int):int;
method write_char(f@:unix_file, x:char):void;
method write_line(f@:unix_file):void;
method write_line(f@:unix_file, buffer@:indexed[char]):void;
method write(f@:unix_file, buffer@:indexed[char]):void;
method write(f@:unix_file, buffer@:indexed[char], size:int):void;
method write(f@:unix_file, buffer@:indexed[char], size:int,
             if_error:&(string):void):void;
method write(f@:unix_file, buffer@:vstring, size@:int,
             if_error:&(string):void):void;
method print(s@:indexed[char], f:unix_file):void;
method write_to_file(o:any, fname:string):void;
method write_to_file(o:any, fname:string, if_error:&(string):none):void;
method position(f@:unix_file):int;
method position(f@:unix_file, if_error:&(string):int):int;
abstract object position_mode;
concrete representation from_start isa position_mode;
concrete representation from_current_position isa position_mode;
concrete representation from_end isa position_mode;
method set_position(f@:unix_file, offset:int, if_error:&(string):void):void;
method set_position_relative(f@:unix_file, offset:int,
                             from@:position_mode):void;
method set_position_relative(f@:unix_file, offset:int, from@:position_mode,
                             if_error:&(string):void):void;
method detected_eof(f@:unix_file):bool;
method length(f@:unix_file):int;
method flush(f@:unix_file):void;
method flush(f@:unix_file, if_error:&(string):void):void;
method close(f@:unix_file):void;
method close(f@:unix_file, if_error:&(string):void):void;
method get_mod_time(f_name@:string):integer;
method get_mod_time(f_name@:string, if_error:&(string):integer):integer;
method get_mod_time_internal(f_name@:string, if_error:&(string):int):int;
method mod_time(f@:unix_file):integer;
method mod_time(f@:unix_file, if_error:&(string):integer):integer;
method mod_time_internal(f@:unix_file, if_error:&(string):int):int;
method error_string(i:int):string;
method unix_error(err:string, s:string):none;
method nonfatal_unix_error(err:string, s:string):void;
extend unix_file isa m_positionable_stream[char], extensible_stream[char];
method next(f@:unreadable_unix_file, at_end:&():char):char;
method next(f@:readable_unix_file, at_end:&():char):char;
method peek_next(f@:unix_file, at_end:&():char):char;
method peek_prev(f@:unix_file, at_start:&():char):char;
method prev(f@:unix_file, at_start:&():char):char;
method set_next(f@:unwritable_unix_file, x:char):void;
method set_next(f@:writable_unix_file, x:char):void;
method set_peek_next(f@:unix_file, x:char):void;
method set_peek_prev(f@:unix_file, x:char):void;
method set_prev(f@:unix_file, x:char):void;
method add_last(f@:unwritable_unix_file, x:char):void;
method add_last(f@:writable_unix_file, x:char):void;

Unix files can be opened, given the name of the file and an open mode, using open_file. The optional if_error closure taken by open_file and many other file operations is invoked if there was a standard Unix error during the operation, passing a string describing the kind of error. The unix_error function invokes error with an appropriate error message derived from the error string and a user-supplied message. The nonfatal_unix_error also prints the error message, but then successfully returns to the caller.

The three standard files can be returned by the stdin, stdout, and stderr functions. Six functions are available to query properties of the file: whether or not the file is readable and/or writable and whether or not writes always append. The read_partial functions read up to size characters into a buffer, starting at index from; they return how many characters actually were read. The read functions repeatedly call read_partial until all requested characters are read. The read_line functions work like read, except that they stop reading after they've seen (and copied to the buffer) a newline character. The read_whole_text_file functions create and return a string containing the whole file's contents; it correctly handles text files with varying CR/LF conventions on different platforms. The write functions write their argument character buffer (optionally copying only size characters) to the file. Collectors can be written to a file directly, more efficiently than first flattening the collector into a string. Individual characters can also be written to a file.

The mod_time operations return the modification timestamp of the file; get_mod_time is a convenience in case the file hasn't been opened yet. (The time data structure supports parsing the timestamp integer.)

In addition to the other stream-style operations, unix_files support testing whether they have detected the end of the file (subtly different than actually being at the end of the file) and performing lseek-style repositioning relative either to the start of the file, the current position, or the end of the file. Note that file positions and file lengths are in terms of raw characters in the underlying file, ignoring any CR/LF conversion that might be performed by read operations.

method file_exists(s:string):bool;
method parse_path(path_string@:string, path_separator:char
                  ):extensible_sequence[string];
method parse_path(path_string@:string):extensible_sequence[string];
method path_name(t@:string):string;
method strip_leading_path(t@:string):string;
method dirname(t@:string):string;
method basename(t@:string):string;
method expand_filename(s@:string):string;
method expand_filename(s@:string, if_error:&(string):string):string;
method shrink_filename(s@:string):string;
method is_abs_filename(s@:string):bool;
method find_file(base_name@:string, dirs@:ordered_collection[string],
                 if_fail:&(string):string):string;
method find_file(base_name@:string, dirs@:sequence[string]):string;
method write_object_to_file_name(obj:any, f_name:string):void;
method write_object_to_file_name(obj:any, f_name:string,
                                 if_error:&(string):void):void;
method write_object_to_file(x:any, f@:unix_file, use_bs@:bool):void;
method write_object_to_file(x:any, f@:unix_file,
                            if_error@:&(string):void):void;
method write_object_to_file(x:any, f@:unix_file, use_bs@:bool,
                            if_error:&(string):void):void;
method read_object_from_file_name(f_name@:string):dynamic;
method read_object_from_file_name(f_name@:string, use_bs:bool):dynamic;
method read_object_from_file_name(f_name@:string, use_bs:bool,
                                  if_error:&(string):dynamic):dynamic;
method read_object_from_file(f@:unix_file, use_bs@:bool):dynamic;
method read_object_from_file(f@:unix_file,
                             if_error@:&(string):dynamic):dynamic;
method read_object_from_file(f@:unix_file, use_bs:bool,
                             if_error:&(string):dynamic):dynamic;
A number of file-related operations have been defined. The find_file operation takes a file name and a directory search path and returns an absolute path name for the first file that matches the name in the search path. To do this work find_file invokes file_exists to test whether a given file name is defined and expand to expand away user file-name prefixes. The parse_path helper function converts a Unix search path string (directory names separated by colons) into a sequence of directory names.


next up previous index
Next: Miscellaneous Up: Input/output Previous: Random numbers   Index

Cecil/Vortex Project