src/Pure/General/file.ML
changeset 75616 986506233812
parent 75615 4494cd69f97f
child 76884 a004c5322ea4
equal deleted inserted replaced
75615:4494cd69f97f 75616:986506233812
    20   val is_file: Path.T -> bool
    20   val is_file: Path.T -> bool
    21   val check_dir: Path.T -> Path.T
    21   val check_dir: Path.T -> Path.T
    22   val check_file: Path.T -> Path.T
    22   val check_file: Path.T -> Path.T
    23   val fold_dir: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a
    23   val fold_dir: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a
    24   val read_dir: Path.T -> string list
    24   val read_dir: Path.T -> string list
    25   val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a
    25   val read: Path.T -> string
    26   val read_lines: Path.T -> string list
    26   val read_lines: Path.T -> string list
    27   val read: Path.T -> string
       
    28   val write: Path.T -> string -> unit
    27   val write: Path.T -> string -> unit
    29   val append: Path.T -> string -> unit
    28   val append: Path.T -> string -> unit
    30   val write_list: Path.T -> string list -> unit
    29   val write_list: Path.T -> string list -> unit
    31   val append_list: Path.T -> string list -> unit
    30   val append_list: Path.T -> string list -> unit
    32   val eq: Path.T * Path.T -> bool
    31   val eq: Path.T * Path.T -> bool
    96     in read a end);
    95     in read a end);
    97 
    96 
    98 fun read_dir path = sort_strings (fold_dir cons path []);
    97 fun read_dir path = sort_strings (fold_dir cons path []);
    99 
    98 
   100 
    99 
   101 (*
   100 (* read *)
   102   scalable iterator:
       
   103   . avoid size limit of TextIO.inputAll and overhead of many TextIO.inputLine
       
   104   . optional terminator at end-of-input
       
   105 *)
       
   106 fun fold_lines f path a = File_Stream.open_input (fn file =>
       
   107   let
       
   108     fun read buf x =
       
   109       (case File_Stream.input file of
       
   110         "" => (case Buffer.content buf of "" => x | line => f line x)
       
   111       | input =>
       
   112           (case String.fields (fn c => c = #"\n") input of
       
   113             [rest] => read (Buffer.add rest buf) x
       
   114           | line :: more => read_more more (f (Buffer.content (Buffer.add line buf)) x)))
       
   115     and read_more [rest] x = read (Buffer.add rest Buffer.empty) x
       
   116       | read_more (line :: more) x = read_more more (f line x);
       
   117   in read Buffer.empty a end) path;
       
   118 
       
   119 fun read_lines path = rev (fold_lines cons path []);
       
   120 
   101 
   121 val read = File_Stream.open_input File_Stream.input_all;
   102 val read = File_Stream.open_input File_Stream.input_all;
       
   103 
       
   104 val read_lines = Bytes.read #> Bytes.trim_split_lines;
   122 
   105 
   123 
   106 
   124 (* write *)
   107 (* write *)
   125 
   108 
   126 fun write_list path ss = File_Stream.open_output (fn stream => File_Stream.outputs stream ss) path;
   109 fun write_list path ss = File_Stream.open_output (fn stream => File_Stream.outputs stream ss) path;