home *** CD-ROM | disk | FTP | other *** search
- -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C)
- -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
- --
- class STD_FILE_WRITE
- --
- -- Basic output facilities to write a named file on the disk.
- --
- -- Note : most features are common with STD_OUTPUT so you can
- -- test your program first on the screen and then, changing
- -- of instance (STD_OUTPUT/STD_FILE_WRITE), doing the same
- -- on a file.
- --
-
- inherit STD_FILE
-
- creation {ANY}
- connect_to
-
- feature {ANY}
-
- connect_to(new_path: STRING) is
- do
- mode := "w";
- output_stream := fopen(new_path,mode);
- if output_stream /= Void then
- path := new_path;
- end;
- end;
-
- disconnect is
- local
- err: INTEGER;
- do
- err := fclose(output_stream);
- path := Void;
- end;
-
- feature {ANY}
-
- put_character(c: CHARACTER) is
- local
- err: CHARACTER;
- do
- err := fputc(c,output_stream);
- if err /= c then
- std_error.put_string("Error while writing character.");
- crash;
- end;
- end;
-
- put_string(s: STRING) is
- -- Output `s' to current output device.
- require
- s /= Void;
- local
- i: INTEGER;
- do
- from
- i := 1;
- until
- i > s.count
- loop
- put_character(s @ i);
- i := i + 1;
- end;
- end;
-
- put_integer (i: INTEGER) is
- -- Output `i' to current output device.
- do
- tmp_string.clear;
- i.append_in(tmp_string);
- put_string(tmp_string);
- end;
-
- put_integer_format(i, s: INTEGER) is
- -- Output `i' to current output device using at most
- -- `s' character.
- do
- tmp_string.clear;
- i.append_in_format(tmp_string,s);
- put_string(tmp_string);
- end;
-
- put_real(r: REAL) is
- -- Output `r' to current output device.
- do
- tmp_string.clear;
- r.append_in(tmp_string);
- put_string(tmp_string);
- end;
-
- put_real_format(r: REAL; f: INTEGER) is
- -- Output `r' with only `f' digit for the fractionnal part.
- -- Examples:
- -- put_real(3.519,2) print "3.51".
- require
- f >= 0;
- do
- tmp_string.clear;
- r.append_in_format(tmp_string,f);
- put_string(tmp_string);
- end;
-
- put_double(d: DOUBLE) is
- -- Output `d' to current output device.
- do
- tmp_string.clear;
- d.append_in(tmp_string);
- put_string(tmp_string);
- end;
-
- put_double_format(d: DOUBLE; f: INTEGER) is
- -- Output `d' with only `f' digit for the fractionnal part.
- -- Examples:
- -- put_double(3.519,2) print "3.51".
- require
- f >= 0;
- do
- tmp_string.clear;
- r.append_in_format(tmp_string,f);
- put_string(tmp_string);
- end;
-
- put_boolean(b: BOOLEAN) is
- -- Output `b' to current output device according
- -- to the Eiffel format.
- do
- if b then
- put_string("true");
- else
- put_string("false");
- end;
- end;
-
- put_new_line is
- -- Output a newline character.
- do
- put_character('%N');
- end;
-
- put_spaces(nb: INTEGER) is
- -- Output `nb' spaces character.
- require
- nb >= 0;
- local
- count : INTEGER;
- do
- from
- until
- count >= nb
- loop
- put_character(' ');
- count := count + 1;
- end;
- end;
-
- append_file(file_name: STRING) is
- require
- file_exists(file_name);
- local
- c: CHARACTER;
- do
- tmp_file_read.connect_to(file_name);
- from
- tmp_file_read.read_character;
- until
- tmp_file_read.end_of_input
- loop
- c := tmp_file_read.last_character;
- put_character(c);
- tmp_file_read.read_character;
- end;
- tmp_file_read.disconnect;
- end;
-
- feature {NONE}
-
- tmp_file_read: STD_FILE_READ is
- once
- !!Result.make;
- end;
-
- feature {NONE}
- --
- -- NOTE: use only a few basic ANSI C functions.
- -- Try to use as few external C calls as possible.
- --
-
- output_stream: POINTER;
-
- fputc(c: CHARACTER; stream_pointer: POINTER): CHARACTER is
- external "CSE"
- end;
-
- tmp_string: STRING is "0123456789001234567890";
-
- end -- STD_FILE_WRITE
-