home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Caml Light 0.61 / Source / src / linker / readword.ml < prev    next >
Encoding:
Text File  |  1993-09-24  |  754 b   |  33 lines  |  [TEXT/MPS ]

  1. (* To read a file word per word, and return the list of the strings read *)
  2.  
  3. let from_chan ic =
  4.   let buff = create_string 1024 in
  5.   let rec readchars i =
  6.     match input_char ic with
  7.       ` ` | `\n` | `\r` | `\t` ->
  8.         i
  9.     | c ->
  10.         if i < string_length buff then set_nth_char buff i c;
  11.         readchars (succ i) in
  12.   let rec readword () =
  13.     match input_char ic with
  14.       ` ` | `\n` | `\r` | `\t` ->
  15.         readword()
  16.     | c ->
  17.         set_nth_char buff 0 c;
  18.         sub_string buff 0 (readchars 1) in
  19.   let rec readwords l =
  20.     try
  21.       readwords(readword() :: l)
  22.     with End_of_file ->
  23.       rev l in
  24.   readwords []
  25. ;;
  26.  
  27. let from_file filename = 
  28.   let ic = open_in filename in
  29.   let res = from_chan ic in
  30.   close_in ic;
  31.   res
  32. ;;
  33.