home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_std / std_file_write.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  3.9 KB  |  199 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class STD_FILE_WRITE
  5. --
  6. -- Basic output facilities to write a named file on the disk.
  7. --
  8. -- Note : most features are common with STD_OUTPUT so you can 
  9. --        test your program first on the screen and then, changing 
  10. --        of instance (STD_OUTPUT/STD_FILE_WRITE), doing the same
  11. --        on a file.
  12. --
  13.    
  14. inherit STD_FILE
  15.    
  16. creation {ANY}
  17.    connect_to
  18.    
  19. feature {ANY}
  20.  
  21.    connect_to(new_path: STRING) is
  22.       do
  23.      mode := "w";
  24.      output_stream := fopen(new_path,mode);
  25.      if output_stream /= Void then
  26.         path := new_path;
  27.      end;
  28.       end;
  29.    
  30.    disconnect is
  31.       local
  32.      err: INTEGER;
  33.       do
  34.      err := fclose(output_stream); 
  35.      path := Void;
  36.       end;
  37.    
  38. feature {ANY}   
  39.    
  40.    put_character(c: CHARACTER) is
  41.       local
  42.      err: CHARACTER;
  43.       do
  44.      err := fputc(c,output_stream);
  45.      if err /= c then
  46.         std_error.put_string("Error while writing character."); 
  47.         crash;
  48.      end;
  49.       end;
  50.  
  51.    put_string(s: STRING) is
  52.      -- Output `s' to current output device.
  53.       require
  54.      s /= Void;
  55.       local
  56.      i: INTEGER;
  57.       do
  58.      from  
  59.         i := 1;
  60.      until
  61.         i > s.count
  62.      loop
  63.         put_character(s @ i);
  64.         i := i + 1;
  65.      end;
  66.       end;
  67.    
  68.    put_integer (i: INTEGER) is
  69.      -- Output `i' to current output device.
  70.       do
  71.      tmp_string.clear;
  72.      i.append_in(tmp_string);
  73.      put_string(tmp_string);
  74.       end;
  75.    
  76.    put_integer_format(i, s: INTEGER) is
  77.      -- Output `i' to current output device using at most
  78.      -- `s' character.
  79.       do
  80.      tmp_string.clear;
  81.      i.append_in_format(tmp_string,s);
  82.      put_string(tmp_string);
  83.       end;
  84.    
  85.    put_real(r: REAL) is
  86.      -- Output `r' to current output device.
  87.       do
  88.      tmp_string.clear;
  89.      r.append_in(tmp_string);
  90.      put_string(tmp_string);
  91.       end;
  92.    
  93.    put_real_format(r: REAL; f: INTEGER) is
  94.      -- Output `r' with only `f' digit for the fractionnal part.
  95.      -- Examples: 
  96.      --    put_real(3.519,2) print "3.51". 
  97.       require
  98.      f >= 0;
  99.       do
  100.      tmp_string.clear;
  101.      r.append_in_format(tmp_string,f);
  102.      put_string(tmp_string);
  103.       end;
  104.    
  105.    put_double(d: DOUBLE) is
  106.      -- Output `d' to current output device.
  107.       do
  108.      tmp_string.clear;
  109.      d.append_in(tmp_string);
  110.      put_string(tmp_string);
  111.       end;
  112.    
  113.    put_double_format(d: DOUBLE; f: INTEGER) is
  114.      -- Output `d' with only `f' digit for the fractionnal part.
  115.      -- Examples: 
  116.      --    put_double(3.519,2) print "3.51". 
  117.       require
  118.      f >= 0;
  119.       do
  120.      tmp_string.clear;
  121.      r.append_in_format(tmp_string,f);
  122.      put_string(tmp_string);
  123.       end;
  124.    
  125.    put_boolean(b: BOOLEAN) is
  126.      -- Output `b' to current output device according
  127.      -- to the Eiffel format.
  128.       do
  129.      if b then
  130.         put_string("true");
  131.      else
  132.         put_string("false");
  133.      end;
  134.       end;
  135.    
  136.    put_new_line is
  137.      -- Output a newline character.
  138.       do
  139.      put_character('%N');
  140.       end;
  141.    
  142.    put_spaces(nb: INTEGER) is
  143.       -- Output `nb' spaces character.
  144.       require
  145.      nb >= 0;
  146.       local
  147.      count : INTEGER;
  148.       do
  149.      from  
  150.      until
  151.         count >= nb
  152.      loop
  153.         put_character(' ');
  154.         count := count + 1;
  155.      end;
  156.       end; 
  157.    
  158.    append_file(file_name: STRING) is
  159.       require
  160.      file_exists(file_name);
  161.       local
  162.      c: CHARACTER;
  163.       do
  164.      tmp_file_read.connect_to(file_name);
  165.      from  
  166.         tmp_file_read.read_character;
  167.      until
  168.         tmp_file_read.end_of_input
  169.      loop
  170.         c := tmp_file_read.last_character;
  171.         put_character(c);
  172.         tmp_file_read.read_character;
  173.      end;
  174.      tmp_file_read.disconnect;
  175.       end;
  176.    
  177. feature {NONE}
  178.    
  179.    tmp_file_read: STD_FILE_READ is
  180.       once
  181.      !!Result.make;
  182.       end;
  183.    
  184. feature {NONE}
  185.    --
  186.    -- NOTE: use only a few basic ANSI C functions.
  187.    -- Try to use as few external C calls as possible.
  188.    --
  189.    
  190.    output_stream: POINTER;
  191.    
  192.    fputc(c: CHARACTER; stream_pointer: POINTER): CHARACTER is
  193.       external "CSE"
  194.       end;
  195.    
  196.    tmp_string: STRING is "0123456789001234567890";   
  197.    
  198. end -- STD_FILE_WRITE
  199.