home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / compiler / Readword.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  1.1 KB  |  46 lines  |  [TEXT/R*ch]

  1. (* To read a file word per word, and return the list of the strings read *)
  2.  
  3. local
  4.  
  5.   open BasicIO Nonstdio;
  6.  
  7. fun from_stream is =
  8.   let val buff = CharArray.array(1024, #" ")
  9.       fun readchars i =
  10.         case input_char is of
  11.             #" " => i
  12.           | #"\n" => i
  13.           | #"\r" => i (* was #"\^M" *)
  14.           | #"\t" => i
  15.           | c =>
  16.              (if i < CharArray.length buff then CharArray.update(buff, i, c)
  17.               else ();
  18.               readchars (i+1))
  19.       fun readword() =
  20.         case input_char is of
  21.             #" "    => readword()
  22.           | #"\n"   => readword()
  23.           | #"\r"   => readword() (* was #"\^M" *)
  24.           | #"\t"   => readword()
  25.           | c =>
  26.               (CharArray.update(buff, 0, c);
  27.                CharArray.extract(buff, 0, SOME (readchars 1)))
  28.       fun readwords l =
  29.         (readwords(readword() :: l))
  30.            handle Size => List.rev l
  31.   in
  32.     readwords []
  33.   end;
  34.  
  35. in
  36.  
  37. fun from_file filename =
  38.   let val is = open_in filename
  39.       val res = from_stream is
  40.   in
  41.     close_in is;
  42.     res
  43.   end;
  44.  
  45. end;
  46.