Automatic generation produced by ISE Eiffel
indexing description: "Sequential files, viewed as persistent sequences of characters" status: "See notice at end of class" date: "$Date: 2001/11/16 20:34:11 $" revision: "$Revision: 1.1.1.1 $" deferred class FILE inherit UNBOUNDED [CHARACTER] SEQUENCE [CHARACTER] undefine prune redefine off, append end IO_MEDIUM rename handle as descriptor, handle_available as descriptor_available end feature -- Initialization make (fn: STRING) is -- Create file object with fn as file name. require string_exists: fn /= void string_not_empty: not fn.is_empty do name := fn mode := closed_file create last_string.make (256) ensure file_named: name.is_equal (fn) file_closed: is_closed end make_open_read (fn: STRING) is -- Create file object with fn as file name -- and open file in read mode. require string_exists: fn /= void string_not_empty: not fn.is_empty do make (fn) open_read ensure exists: exists open_read: is_open_read end make_open_write (fn: STRING) is -- Create file object with fn as file name -- and open file for writing; -- create it if it does not exist. require string_exists: fn /= void string_not_empty: not fn.is_empty do make (fn) open_write ensure exists: exists open_write: is_open_write end make_open_append (fn: STRING) is -- Create file object with fn as file name -- and open file in append-only mode. require string_exists: fn /= void string_not_empty: not fn.is_empty do make (fn) open_append ensure exists: exists open_append: is_open_append end make_open_read_write (fn: STRING) is -- Create file object with fn as file name -- and open file for both reading and writing. require string_exists: fn /= void string_not_empty: not fn.is_empty do make (fn) open_read_write ensure exists: exists open_read: is_open_read open_write: is_open_write end make_create_read_write (fn: STRING) is -- Create file object with fn as file name -- and open file for both reading and writing; -- create it if it does not exist. require string_exists: fn /= void string_not_empty: not fn.is_empty do make (fn) create_read_write ensure exists: exists open_read: is_open_read open_write: is_open_write end make_open_read_append (fn: STRING) is -- Create file object with fn as file name -- and open file for reading anywhere -- but writing at the end only. -- Create file if it does not exist. require string_exists: fn /= void string_not_empty: not fn.is_empty do make (fn) open_read_append ensure exists: exists open_read: is_open_read open_append: is_open_append end feature -- Access name: STRING -- File name item: CHARACTER is -- Current item do read_character Result := last_character back end position: INTEGER is -- Current cursor position. do if not is_closed then Result := file_tell (file_pointer) end end descriptor: INTEGER is -- File descriptor as used by the operating system. require else file_opened: not is_closed do Result := file_fd (file_pointer) descriptor_available := True end descriptor_available: BOOLEAN separator: CHARACTER -- ASCII code of character following last word read file_pointer: POINTER -- File pointer as required in C file_info: UNIX_FILE_INFO is -- Collected information about the file. do set_buffer Result := clone (buffered_file_info) end inode: INTEGER is -- I-node number require file_exists: exists do set_buffer Result := buffered_file_info.inode end links: INTEGER is -- Number of links on file require file_exists: exists do set_buffer Result := buffered_file_info.links end user_id: INTEGER is -- User identification of owner require file_exists: exists do set_buffer Result := buffered_file_info.user_id end group_id: INTEGER is -- Group identification of owner require file_exists: exists do set_buffer Result := buffered_file_info.group_id end protection: INTEGER is -- Protection mode, in decimal value require file_exists: exists do set_buffer Result := buffered_file_info.protection end owner_name: STRING is -- Name of owner require file_exists: exists do set_buffer Result := buffered_file_info.owner_name end date: INTEGER is -- Time stamp (time of last modification) require file_exists: exists do set_buffer Result := buffered_file_info.date end access_date: INTEGER is -- Time stamp of last access made to the inode. require file_exists: exists do set_buffer Result := buffered_file_info.access_date end retrieved: ANY is -- Retrieved object structure -- To access resulting object under correct type, -- use assignment attempt. -- Will raise an exception (code Retrieve_exception) -- if content is not a stored Eiffel structure. do Result := c_retrieved (descriptor) end feature -- Measurement count: INTEGER is -- Size in bytes (0 if no associated physical file) do if exists then if not is_open_write then set_buffer Result := buffered_file_info.size else Result := file_size (file_pointer) end end end feature -- Status report after: BOOLEAN is -- Is there no valid cursor position to the right of cursor position? do Result := not is_closed and then (end_of_file or count = 0) end before: BOOLEAN is -- Is there no valid cursor position to the left of cursor position? do Result := is_closed end off: BOOLEAN is -- Is there no item? do Result := (count = 0) or else is_closed or else end_of_file end end_of_file: BOOLEAN is -- Has an EOF been detected? require opened: not is_closed do Result := file_feof (file_pointer) end exists: BOOLEAN is -- Does physical file exist? -- (Uses effective UID.) local external_name: ANY do external_name := name.to_c Result := file_exists ($external_name) ensure then unchanged_mode: mode = old mode end access_exists: BOOLEAN is -- Does physical file exist? -- (Uses real UID.) local external_name: ANY do external_name := name.to_c Result := file_access ($external_name, 0) end is_readable: BOOLEAN is -- Is file readable? -- (Checks permission for effective UID.) do set_buffer Result := buffered_file_info.is_readable end is_writable: BOOLEAN is -- Is file writable? -- (Checks write permission for effective UID.) do set_buffer Result := buffered_file_info.is_writable end is_executable: BOOLEAN is -- Is file executable? -- (Checks execute permission for effective UID.) do set_buffer Result := buffered_file_info.is_executable end is_creatable: BOOLEAN is -- Is file creatable in parent directory? -- (Uses effective UID to check that parent is writable -- and file does not exist.) local external_name: ANY do external_name := name.to_c Result := file_creatable ($external_name) end is_plain: BOOLEAN is -- Is file a plain file? require file_exists: exists do set_buffer Result := buffered_file_info.is_plain end is_device: BOOLEAN is -- Is file a device? require file_exists: exists do set_buffer Result := buffered_file_info.is_device end is_directory: BOOLEAN is -- Is file a directory? require file_exists: exists do set_buffer Result := buffered_file_info.is_directory end is_symlink: BOOLEAN is -- Is file a symbolic link? require file_exists: exists do set_buffer Result := buffered_file_info.is_symlink end is_fifo: BOOLEAN is -- Is file a named pipe? require file_exists: exists do set_buffer Result := buffered_file_info.is_fifo end is_socket: BOOLEAN is -- Is file a named socket? require file_exists: exists do set_buffer Result := buffered_file_info.is_socket end is_block: BOOLEAN is -- Is file a block special file? require file_exists: exists do set_buffer Result := buffered_file_info.is_block end is_character: BOOLEAN is -- Is file a character special file? require file_exists: exists do set_buffer Result := buffered_file_info.is_character end is_setuid: BOOLEAN is -- Is file setuid? require file_exists: exists do set_buffer Result := buffered_file_info.is_setuid end is_setgid: BOOLEAN is -- Is file setgid? require file_exists: exists do set_buffer Result := buffered_file_info.is_setgid end is_sticky: BOOLEAN is -- Is file sticky (for memory swaps)? require file_exists: exists do set_buffer Result := buffered_file_info.is_sticky end is_owner: BOOLEAN is -- Is file owned by effective UID? require file_exists: exists do set_buffer Result := buffered_file_info.is_owner end is_access_readable: BOOLEAN is -- Is file readable by real UID? require file_exists: exists do set_buffer Result := buffered_file_info.is_access_readable end is_access_writable: BOOLEAN is -- Is file writable by real UID? require file_exists: exists do set_buffer Result := buffered_file_info.is_access_writable end is_access_executable: BOOLEAN is -- Is file executable by real UID? require file_exists: exists do set_buffer Result := buffered_file_info.is_access_executable end is_access_owner: BOOLEAN is -- Is file owned by real UID? require file_exists: exists do set_buffer Result := buffered_file_info.is_access_owner end file_readable: BOOLEAN is -- Is there a current item that may be read? do Result := (mode >= read_write_file or mode = read_file) and readable end is_closed: BOOLEAN is -- Is file closed? do Result := mode = closed_file end is_open_read: BOOLEAN is -- Is file open for reading? do Result := mode = read_file or else mode = read_write_file or else mode = append_read_file end is_open_write: BOOLEAN is -- Is file open for writing? do Result := mode = write_file or else mode = read_write_file or else mode = append_read_file or else mode = append_file end is_open_append: BOOLEAN is -- Is file open for appending? do Result := mode = append_file or else mode = append_read_file end file_writable: BOOLEAN is -- Is there a current item that may be modified? do Result := mode >= write_file end extendible: BOOLEAN is -- May new items be added? do Result := mode >= write_file end file_prunable: BOOLEAN is -- May items be removed? do Result := mode >= write_file and prunable end Full: BOOLEAN is False -- Is structure filled to capacity? feature -- Status setting open_read is -- Open file in read-only mode. require is_closed: is_closed local external_name: ANY do external_name := name.to_c file_pointer := file_open ($external_name, 0) mode := read_file ensure exists: exists open_read: is_open_read end open_write is -- Open file in write-only mode; -- create it if it does not exist. local external_name: ANY do external_name := name.to_c file_pointer := file_open ($external_name, 1) mode := write_file ensure exists: exists open_write: is_open_write end open_append is -- Open file in append-only mode; -- create it if it does not exist. require is_closed: is_closed local external_name: ANY do external_name := name.to_c file_pointer := file_open ($external_name, 2) mode := append_file ensure exists: exists open_append: is_open_append end open_read_write is -- Open file in read and write mode. require is_closed: is_closed local external_name: ANY do external_name := name.to_c file_pointer := file_open ($external_name, 3) mode := read_write_file ensure exists: exists open_read: is_open_read open_write: is_open_write end create_read_write is -- Open file in read and write mode; -- create it if it does not exist. require is_closed: is_closed local external_name: ANY do external_name := name.to_c file_pointer := file_open ($external_name, 4) mode := read_write_file ensure exists: exists open_read: is_open_read open_write: is_open_write end open_read_append is -- Open file in read and write-at-end mode; -- create it if it does not exist. require is_closed: is_closed local external_name: ANY do external_name := name.to_c file_pointer := file_open ($external_name, 5) mode := append_read_file ensure exists: exists open_read: is_open_read open_append: is_open_append end fd_open_read (fd: INTEGER) is -- Open file of descriptor fd in read-only mode. do file_pointer := file_dopen (fd, 0) mode := read_file ensure exists: exists open_read: is_open_read end fd_open_write (fd: INTEGER) is -- Open file of descriptor fd in write mode. do file_pointer := file_dopen (fd, 1) mode := write_file ensure exists: exists open_write: is_open_write end fd_open_append (fd: INTEGER) is -- Open file of descriptor fd in append mode. do file_pointer := file_dopen (fd, 2) mode := append_file ensure exists: exists open_append: is_open_append end fd_open_read_write (fd: INTEGER) is -- Open file of descriptor fd in read-write mode. do file_pointer := file_dopen (fd, 3) mode := read_write_file ensure exists: exists open_read: is_open_read open_write: is_open_write end fd_open_read_append (fd: INTEGER) is -- Open file of descriptor fd -- in read and write-at-end mode. local external_name: ANY do external_name := name.to_c file_pointer := file_dopen (fd, 5) mode := append_read_file ensure exists: exists open_read: is_open_read open_append: is_open_append end reopen_read (fname: STRING) is -- Reopen in read-only mode with file of name fname; -- create file if it does not exist. require is_open: not is_closed valid_name: fname /= void local external_name: ANY do external_name := fname.to_c file_pointer := file_reopen ($external_name, 0, file_pointer) name := fname mode := read_file ensure exists: exists open_read: is_open_read end reopen_write (fname: STRING) is -- Reopen in write-only mode with file of name fname; -- create file if it does not exist. require is_open: not is_closed valid_name: fname /= void local external_name: ANY do external_name := name.to_c file_pointer := file_reopen ($external_name, 1, file_pointer) name := fname mode := write_file ensure exists: exists open_write: is_open_write end reopen_append (fname: STRING) is -- Reopen in append mode with file of name fname; -- create file if it does not exist. require is_open: not is_closed valid_name: fname /= void local external_name: ANY do external_name := name.to_c file_pointer := file_reopen ($external_name, 2, file_pointer) name := fname mode := append_file ensure exists: exists open_append: is_open_append end reopen_read_write (fname: STRING) is -- Reopen in read-write mode with file of name fname. require is_open: not is_closed valid_name: fname /= void local external_name: ANY do external_name := name.to_c file_pointer := file_reopen ($external_name, 3, file_pointer) name := fname mode := read_write_file ensure exists: exists open_read: is_open_read open_write: is_open_write end recreate_read_write (fname: STRING) is -- Reopen in read-write mode with file of name fname; -- create file if it does not exist. require is_open: not is_closed valid_name: fname /= void local external_name: ANY do external_name := name.to_c file_pointer := file_reopen ($external_name, 4, file_pointer) name := fname mode := read_write_file ensure exists: exists open_read: is_open_read open_write: is_open_write end reopen_read_append (fname: STRING) is -- Reopen in read and write-at-end mode with file -- of name fname; create file if it does not exist. require is_open: not is_closed valid_name: fname /= void local external_name: ANY do external_name := name.to_c file_pointer := file_reopen ($external_name, 5, file_pointer) name := fname mode := append_read_file ensure exists: exists open_read: is_open_read open_append: is_open_append end close is -- Close file. do file_close (file_pointer) mode := closed_file descriptor_available := False ensure then is_closed: is_closed end feature -- Cursor movement start is -- Go to first position. require else file_opened: not is_closed do file_go (file_pointer, 0) end finish is -- Go to last position. require else file_opened: not is_closed do file_recede (file_pointer, 0) end forth is -- Go to next position. require else file_opened: not is_closed do file_move (file_pointer, 1) end back is -- Go back one position. do file_move (file_pointer, - 1) end move (offset: INTEGER) is -- Advance by offset from current location. require file_opened: not is_closed do file_move (file_pointer, offset) end go (abs_position: INTEGER) is -- Go to the absolute position. -- (New position may be beyond physical length.) require file_opened: not is_closed non_negative_argument: abs_position >= 0 do file_go (file_pointer, abs_position) end recede (abs_position: INTEGER) is -- Go to the absolute position backwards, -- starting from end of file. require file_opened: not is_closed non_negative_argument: abs_position >= 0 do file_recede (file_pointer, abs_position) end next_line is -- Move to next input line. require is_readable: file_readable do file_tnil (file_pointer) end feature -- Element change extend (v: CHARACTER) is -- Include v at end. do put_character (v) end flush is -- Flush buffered data to disk. -- Note that there is no guarantee that the operating -- system will physically write the data to the disk. -- At least it will end up in the buffer cache, -- making the data visible to other processes. require is_open: not is_closed do file_flush (file_pointer) end link (fn: STRING) is -- Link current file to fn. -- fn must not already exist. require file_exists: exists local external_name: ANY fn_name: ANY do external_name := name.to_c fn_name := fn.to_c file_link ($external_name, $fn_name) end append (f: like Current) is -- Append a copy of the contents of f. require else target_is_closed: is_closed source_is_closed: f.is_closed do open_append f.open_read file_append (file_pointer, f.file_pointer, f.count) close f.close ensure then new_count: count = old count + f.count files_closed: f.is_closed and is_closed end put_integer (i: INTEGER) is -- Write i at current position. -- Was declared in FILE as synonym of putint. deferred end putint (i: INTEGER) is -- Write i at current position. -- Was declared in FILE as synonym of put_integer. deferred end put_boolean (b: BOOLEAN) is -- Write b at current position. -- Was declared in FILE as synonym of putbool. deferred end putbool (b: BOOLEAN) is -- Write b at current position. -- Was declared in FILE as synonym of put_boolean. deferred end put_real (r: REAL) is -- Write r at current position. -- Was declared in FILE as synonym of putreal. deferred end putreal (r: REAL) is -- Write r at current position. -- Was declared in FILE as synonym of put_real. deferred end put_double (d: DOUBLE) is -- Write d at current position. -- Was declared in FILE as synonym of putdouble. deferred end putdouble (d: DOUBLE) is -- Write d at current position. -- Was declared in FILE as synonym of put_double. deferred end put_string (s: STRING) is -- Write s at current position. -- Was declared in FILE as synony