home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_std / std_file_read.e < prev    next >
Text File  |  1999-06-05  |  4KB  |  177 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. class STD_FILE_READ 
  13. --
  14. -- Basic input facilities to read a named file on the disc.
  15. --
  16. -- Note : most features are common with STD_INPUT so you can 
  17. -- test your program on the screen first and then, just changing
  18. -- of instance (STD_INPUT/STD_FILE_READ), doing the same in a file.
  19. --
  20.    
  21. inherit INPUT_STREAM;
  22.    
  23. creation 
  24.    connect_to, make
  25.  
  26. feature
  27.    
  28.    path: STRING;
  29.          -- Not Void when connected to the corresponding file 
  30.          -- on the disk.
  31.    
  32. feature {INPUT_STREAM}
  33.    
  34.    input_stream: POINTER;
  35.  
  36. feature {NONE}
  37.  
  38.    memory: INTEGER;
  39.          -- Memory of the last available user's value.
  40.  
  41. feature 
  42.    
  43.    make is
  44.       do
  45.       end;
  46.    
  47.    connect_to(new_path: STRING) is
  48.       require
  49.          not is_connected;
  50.          not new_path.empty
  51.       local
  52.          p: POINTER;
  53.       do
  54.          p := new_path.to_external;
  55.          input_stream := sfr_open(p);
  56.          if input_stream.is_not_null then
  57.             push_back_flag := false;
  58.             memory := (' ').code;
  59.             path := new_path;
  60.          end;
  61.       end;
  62.  
  63. feature    
  64.  
  65.    disconnect is
  66.       require
  67.          is_connected
  68.       do
  69.          fclose(input_stream); 
  70.          path := Void;
  71.       end;
  72.    
  73.    is_connected: BOOLEAN is
  74.       do
  75.          Result := path /= Void;
  76.       end;
  77.    
  78.    read_character is
  79.       do
  80.          if push_back_flag then
  81.             push_back_flag := false;
  82.          else
  83.             memory := read_byte(input_stream);
  84.          end;
  85.       end;
  86.  
  87.    last_character: CHARACTER is
  88.       do
  89.          Result := memory.to_character;
  90.       end;
  91.  
  92.    unread_character is
  93.       do
  94.          push_back_flag := true;
  95.       end;
  96.  
  97.    end_of_input: BOOLEAN is
  98.       do
  99.          if not push_back_flag then
  100.             Result := memory = eof_code;
  101.          end;
  102.       end;
  103.  
  104. feature
  105.  
  106.    read_line_in(buffer: STRING) is
  107.       local
  108.          mem: INTEGER;
  109.          stream: POINTER;
  110.       do
  111.          stream := input_stream;
  112.          from  
  113.             if push_back_flag then
  114.                push_back_flag := false;
  115.                mem := memory;
  116.             else
  117.                mem := read_byte(stream);
  118.             end;
  119.          until
  120.             mem = eof_code 
  121.                or else 
  122.             mem = ('%N').code
  123.                or else
  124.             mem = ('%R').code
  125.          loop
  126.             buffer.extend(mem.to_character);
  127.             mem := read_byte(stream);
  128.          end;
  129.          if mem = ('%R').code then
  130.             mem := read_byte(stream);
  131.             if mem /= ('%N').code then
  132.                push_back_flag := true;
  133.             end;
  134.          end;
  135.          memory := mem;
  136.       end;
  137.  
  138. feature {FILE_TOOLS}
  139.  
  140.    same_as(other: like Current): BOOLEAN is
  141.       require
  142.          is_connected;
  143.          other.is_connected
  144.       local
  145.          is1, is2: like input_stream;
  146.          c1, c2: INTEGER;
  147.       do
  148.          from
  149.             is1 := input_stream;
  150.             is2 := other.input_stream;
  151.          until
  152.             c1 /= c2 or else c1 = eof_code
  153.          loop
  154.             c1 := read_byte(is1);
  155.             c2 := read_byte(is2);
  156.          end
  157.          Result :=  c1 = c2;
  158.          disconnect;
  159.          other.disconnect;
  160.       ensure
  161.          not is_connected;
  162.          not other.is_connected
  163.       end;
  164.  
  165. feature {NONE}
  166.  
  167.    sfr_open(path_pointer: POINTER): POINTER is
  168.       external "SmallEiffel"
  169.       end;
  170.  
  171.    fclose(stream_pointer : POINTER) is
  172.       external "C_InlineWithoutCurrent"
  173.       end;
  174.  
  175. end -- STD_FILE_READ 
  176.  
  177.