home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / LIBRARY / IN.SA < prev    next >
Text File  |  1995-02-05  |  3KB  |  94 lines

  1. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  2. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  3. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  4. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  5. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  6. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  7.  
  8. -- in.sa: Input from stdin.
  9. -------------------------------------------------------------------
  10. class IN is
  11.    -- Direct access to stdin.
  12.  
  13.    create:SAME is return self end;   
  14.    
  15.    get_char:INT is
  16.       -- Retrieve a single character as an INT from stdin, negative for
  17.       -- end of file.
  18.       return C_IN::c_in_getchar
  19.    end;
  20.  
  21.    get_str:STR is
  22.       -- A string containing the characters on stdin up to the next
  23.       -- newline.
  24.       return get_line.str;
  25.    end;
  26.    
  27.    get_line:FSTR is
  28.       -- A string buffer containing the characters on stdin up to the 
  29.       -- next newline.
  30.       return append_line(void);
  31.    end;
  32.    
  33.    private const size : INT := 256;
  34.    append_line(s:FSTR):FSTR is
  35.       -- Retrieve a string from stdin up to the next newline and append 
  36.       -- it to `s'.
  37.       
  38.       -- Implementation contributed by hikichi@srarc2.sra.co.jp
  39.       -- (Nobuyuki Hikichi).
  40.  
  41.        bsize ::= size;
  42.        loop
  43.            buf ::= #FSTR(bsize); -- buffer to hold C string
  44.            C_IN::c_in_get_str(buf, 0, bsize);
  45.            buf.loc := bsize - 1;
  46.  
  47.            m: INT := buf.index_of('\n');
  48.            if m >= 0 then buf.loc := m; return s + buf; end; 
  49.  
  50.            n: INT := buf.index_of('\0');
  51.            if n = -1 then s := s + buf; bsize := bsize * 2;
  52.            elsif n = 0 then return s;
  53.        elsif n <= (bsize - 1) then buf.loc := n; return s + buf;
  54.        else raise "IN::append_line, out of range [-1, bsize-1]";
  55.            end;  -- if
  56.        end; -- loop
  57.        return s;                                                                -- NLP
  58.    end; -- fstr (s:FSTR)
  59.  
  60.    error:BOOL is
  61.        -- true if an error has been encountered.  Cleared by "clear".
  62.        return C_FILE::ferror(FILE::stdin_macro);
  63.    end;
  64.  
  65.    eof:BOOL is
  66.        -- true if EOF has been encountered.  Cleared by "clear".
  67.        return C_FILE::feof(FILE::stdin_macro);
  68.    end;
  69.  
  70.    clear is
  71.        -- resets end_seen and error status
  72.        C_FILE::clearerr(FILE::stdin_macro);
  73.    end;
  74.    
  75. end; -- class IN
  76.    
  77. -------------------------------------------------------------------
  78. external class C_IN is   
  79.    -- Interface to C functions supporting IN.
  80.  
  81.    c_in_getchar:INT;        -- Retrieve a single character from 
  82.       -- stdin as an INT, negative for end of file.
  83.    
  84.    c_in_get_str(s:FSTR,st,sz:INT); -- Insert characters from 
  85.       -- stdin into `s' starting at character `st', for at most `sz-1'
  86.       -- characters or until EOF or newline is read. Puts in
  87.       -- '\0' after these characters. Returns the index of the last 
  88.       -- non-zero character. If EOF is read before any chars then returns
  89.       -- 0 and doesn't change anything. 
  90.    
  91. end; -- external class C_IN
  92.    
  93. -------------------------------------------------------------------
  94.