src/Pure/General/file.ML
author wenzelm
Tue, 21 Jun 2022 14:51:17 +0200
changeset 75571 ac5e633ad9b3
parent 75569 f848f66a8cb7
child 75593 3e09396db078
permissions -rw-r--r--
tuned signature: more operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6118
caa439435666 fixed titles;
wenzelm
parents: 5009
diff changeset
     1
(*  Title:      Pure/General/file.ML
64698
e022a69db531 tuned comments;
wenzelm
parents: 64304
diff changeset
     2
    Author:     Makarius
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     3
64698
e022a69db531 tuned comments;
wenzelm
parents: 64304
diff changeset
     4
File-system operations.
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     5
*)
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     6
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     7
signature FILE =
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     8
sig
60970
e08d868ceca9 clarified File.standard_path vs. File.platform_path (like Isabelle/Scala operations);
wenzelm
parents: 59058
diff changeset
     9
  val standard_path: Path.T -> string
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    10
  val platform_path: Path.T -> string
62549
9498623b27f0 File.bash_string operations in ML as in Scala -- exclusively for GNU bash, not perl and not user output;
wenzelm
parents: 62468
diff changeset
    11
  val bash_path: Path.T -> string
69223
44d68a00917c more operations;
wenzelm
parents: 64698
diff changeset
    12
  val bash_paths: Path.T list -> string
72278
199dc903131b clarified signature;
wenzelm
parents: 70998
diff changeset
    13
  val bash_platform_path: Path.T -> string
73314
87403fde8cc3 more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents: 73264
diff changeset
    14
  val absolute_path: Path.T -> Path.T
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    15
  val full_path: Path.T -> Path.T -> Path.T
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    16
  val tmp_path: Path.T -> Path.T
17826
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    17
  val exists: Path.T -> bool
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    18
  val rm: Path.T -> unit
40785
c755df0f7062 more permissive Isabelle_System.mkdir;
wenzelm
parents: 40746
diff changeset
    19
  val is_dir: Path.T -> bool
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
    20
  val is_file: Path.T -> bool
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    21
  val check_dir: Path.T -> Path.T
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    22
  val check_file: Path.T -> Path.T
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
    23
  val open_dir: (OS.FileSys.dirstream -> 'a) -> Path.T -> 'a
60982
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
    24
  val open_input: (BinIO.instream -> 'a) -> Path.T -> 'a
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
    25
  val open_output: (BinIO.outstream -> 'a) -> Path.T -> 'a
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
    26
  val open_append: (BinIO.outstream -> 'a) -> Path.T -> 'a
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
    27
  val fold_dir: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
    28
  val read_dir: Path.T -> string list
75569
f848f66a8cb7 tuned signature;
wenzelm
parents: 75566
diff changeset
    29
  val input: BinIO.instream -> string
f848f66a8cb7 tuned signature;
wenzelm
parents: 75566
diff changeset
    30
  val input_size: int -> BinIO.instream -> string
f848f66a8cb7 tuned signature;
wenzelm
parents: 75566
diff changeset
    31
  val input_all: BinIO.instream -> string
28028
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
    32
  val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a
43845
d89353d17f54 added File.fold_pages for streaming of large files;
wenzelm
parents: 43616
diff changeset
    33
  val fold_pages: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
    34
  val read_lines: Path.T -> string list
44879
3b6613366dd7 more orthogonal signature;
wenzelm
parents: 43848
diff changeset
    35
  val read_pages: Path.T -> string list
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    36
  val read: Path.T -> string
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    37
  val write: Path.T -> string -> unit
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    38
  val append: Path.T -> string -> unit
69223
44d68a00917c more operations;
wenzelm
parents: 64698
diff changeset
    39
  val generate: Path.T -> string -> unit
60982
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
    40
  val output: BinIO.outstream -> string -> unit
75571
ac5e633ad9b3 tuned signature: more operations;
wenzelm
parents: 75569
diff changeset
    41
  val outputs: BinIO.outstream -> string list -> unit
16713
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
    42
  val write_list: Path.T -> string list -> unit
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
    43
  val append_list: Path.T -> string list -> unit
28028
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
    44
  val write_buffer: Path.T -> Buffer.T -> unit
16603
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
    45
  val eq: Path.T * Path.T -> bool
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    46
end;
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    47
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    48
structure File: FILE =
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    49
struct
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    50
26503
4dec4460244f discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents: 26220
diff changeset
    51
(* system path representations *)
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    52
60970
e08d868ceca9 clarified File.standard_path vs. File.platform_path (like Isabelle/Scala operations);
wenzelm
parents: 59058
diff changeset
    53
val standard_path = Path.implode o Path.expand;
62468
d97e13e5ea5b clarified modules;
wenzelm
parents: 60982
diff changeset
    54
val platform_path = ML_System.platform_path o standard_path;
26980
f7f48bb9a025 ident: naive caching prevents potentially slow external invocations;
wenzelm
parents: 26946
diff changeset
    55
73264
440546ea20e6 clarified modules;
wenzelm
parents: 72511
diff changeset
    56
val bash_path = Bash.string o standard_path;
440546ea20e6 clarified modules;
wenzelm
parents: 72511
diff changeset
    57
val bash_paths = Bash.strings o map standard_path;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    58
73264
440546ea20e6 clarified modules;
wenzelm
parents: 72511
diff changeset
    59
val bash_platform_path = Bash.string o platform_path;
72278
199dc903131b clarified signature;
wenzelm
parents: 70998
diff changeset
    60
70292
bc9d02f916c4 tuned whitespace;
wenzelm
parents: 69483
diff changeset
    61
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    62
(* full_path *)
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    63
73314
87403fde8cc3 more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents: 73264
diff changeset
    64
val absolute_path =
87403fde8cc3 more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents: 73264
diff changeset
    65
  Path.expand #> (fn path =>
87403fde8cc3 more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents: 73264
diff changeset
    66
    if Path.is_absolute path then path
87403fde8cc3 more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents: 73264
diff changeset
    67
    else Path.explode (ML_System.standard_path (OS.FileSys.getDir ())) + path);
87403fde8cc3 more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents: 73264
diff changeset
    68
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    69
fun full_path dir path =
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    70
  let
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    71
    val path' = Path.expand path;
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    72
    val _ = Path.is_current path' andalso error "Bad file specification";
73314
87403fde8cc3 more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents: 73264
diff changeset
    73
  in absolute_path (dir + path') end;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    74
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    75
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    76
(* tmp_path *)
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    77
72511
460d743010bc clarified signature: overloaded "+" for Path.append;
wenzelm
parents: 72278
diff changeset
    78
fun tmp_path path = Path.variable "ISABELLE_TMP" + Path.base path;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    79
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    80
26980
f7f48bb9a025 ident: naive caching prevents potentially slow external invocations;
wenzelm
parents: 26946
diff changeset
    81
(* directory entries *)
f7f48bb9a025 ident: naive caching prevents potentially slow external invocations;
wenzelm
parents: 26946
diff changeset
    82
23861
72bb3494746f replaced info by ident (for full identification, potentially content-based);
wenzelm
parents: 23826
diff changeset
    83
val exists = can OS.FileSys.modTime o platform_path;
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    84
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    85
val rm = OS.FileSys.remove o platform_path;
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    86
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
    87
fun test_dir path = the_default false (try OS.FileSys.isDir (platform_path path));
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
    88
fun is_dir path = exists path andalso test_dir path;
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
    89
fun is_file path = exists path andalso not (test_dir path);
40785
c755df0f7062 more permissive Isabelle_System.mkdir;
wenzelm
parents: 40746
diff changeset
    90
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    91
fun check_dir path =
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
    92
  if is_dir path then path
63668
5efaa884ac6c tuned error;
wenzelm
parents: 62854
diff changeset
    93
  else error ("No such directory: " ^ Path.print (Path.expand path));
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    94
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    95
fun check_file path =
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
    96
  if is_file path then path
63668
5efaa884ac6c tuned error;
wenzelm
parents: 62854
diff changeset
    97
  else error ("No such file: " ^ Path.print (Path.expand path));
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41944
diff changeset
    98
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    99
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
   100
(* open streams *)
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   101
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   102
local
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   103
69483
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69427
diff changeset
   104
fun with_file open_file close_file f =
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69427
diff changeset
   105
  Thread_Attributes.uninterruptible (fn restore_attributes => fn path =>
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69427
diff changeset
   106
    let
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69427
diff changeset
   107
      val file = open_file path;
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69427
diff changeset
   108
      val result = Exn.capture (restore_attributes f) file;
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69427
diff changeset
   109
    in close_file file; Exn.release result end);
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   110
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   111
in
6218
3e9d6edc99a8 added join_info;
wenzelm
parents: 6182
diff changeset
   112
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
   113
fun open_dir f = with_file OS.FileSys.openDir OS.FileSys.closeDir f o platform_path;
60982
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
   114
fun open_input f = with_file BinIO.openIn BinIO.closeIn f o platform_path;
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
   115
fun open_output f = with_file BinIO.openOut BinIO.closeOut f o platform_path;
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
   116
fun open_append f = with_file BinIO.openAppend BinIO.closeOut f o platform_path;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   117
28028
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   118
end;
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   119
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   120
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
   121
(* directory content *)
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
   122
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
   123
fun fold_dir f path a =
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
   124
  check_dir path |> open_dir (fn stream =>
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
   125
    let
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
   126
      fun read x =
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
   127
        (case OS.FileSys.readDir stream of
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
   128
          NONE => x
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
   129
        | SOME entry => read (f entry x));
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 69223
diff changeset
   130
    in read a end);
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
   131
69427
ff2f39a221d4 clarified operations: uniform sorting of results;
wenzelm
parents: 69300
diff changeset
   132
fun read_dir path = sort_strings (fold_dir cons path []);
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
   133
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
   134
28028
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   135
(* input *)
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   136
75569
f848f66a8cb7 tuned signature;
wenzelm
parents: 75566
diff changeset
   137
val input = Byte.bytesToString o BinIO.input;
f848f66a8cb7 tuned signature;
wenzelm
parents: 75566
diff changeset
   138
fun input_size n stream = Byte.bytesToString (BinIO.inputN (stream, n));
f848f66a8cb7 tuned signature;
wenzelm
parents: 75566
diff changeset
   139
val input_all = Byte.bytesToString o BinIO.inputAll;
f848f66a8cb7 tuned signature;
wenzelm
parents: 75566
diff changeset
   140
44879
3b6613366dd7 more orthogonal signature;
wenzelm
parents: 43848
diff changeset
   141
(*
3b6613366dd7 more orthogonal signature;
wenzelm
parents: 43848
diff changeset
   142
  scalable iterator:
3b6613366dd7 more orthogonal signature;
wenzelm
parents: 43848
diff changeset
   143
  . avoid size limit of TextIO.inputAll and overhead of many TextIO.inputLine
48911
5debc3e4fa81 tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents: 48446
diff changeset
   144
  . optional terminator at end-of-input
44879
3b6613366dd7 more orthogonal signature;
wenzelm
parents: 43848
diff changeset
   145
*)
43845
d89353d17f54 added File.fold_pages for streaming of large files;
wenzelm
parents: 43616
diff changeset
   146
fun fold_chunks terminator f path a = open_input (fn file =>
28028
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   147
  let
28510
66b95e857bde fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents: 28500
diff changeset
   148
    fun read buf x =
75569
f848f66a8cb7 tuned signature;
wenzelm
parents: 75566
diff changeset
   149
      (case input file of
28510
66b95e857bde fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents: 28500
diff changeset
   150
        "" => (case Buffer.content buf of "" => x | line => f line x)
66b95e857bde fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents: 28500
diff changeset
   151
      | input =>
43845
d89353d17f54 added File.fold_pages for streaming of large files;
wenzelm
parents: 43616
diff changeset
   152
          (case String.fields (fn c => c = terminator) input of
28510
66b95e857bde fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents: 28500
diff changeset
   153
            [rest] => read (Buffer.add rest buf) x
43616
9e237a9dc1fd reverted 782991e4180d: fold_fields was never used
noschinl
parents: 42329
diff changeset
   154
          | line :: more => read_lines more (f (Buffer.content (Buffer.add line buf)) x)))
9e237a9dc1fd reverted 782991e4180d: fold_fields was never used
noschinl
parents: 42329
diff changeset
   155
    and read_lines [rest] x = read (Buffer.add rest Buffer.empty) x
9e237a9dc1fd reverted 782991e4180d: fold_fields was never used
noschinl
parents: 42329
diff changeset
   156
      | read_lines (line :: more) x = read_lines more (f line x);
28028
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   157
  in read Buffer.empty a end) path;
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   158
43845
d89353d17f54 added File.fold_pages for streaming of large files;
wenzelm
parents: 43616
diff changeset
   159
fun fold_lines f = fold_chunks #"\n" f;
d89353d17f54 added File.fold_pages for streaming of large files;
wenzelm
parents: 43616
diff changeset
   160
fun fold_pages f = fold_chunks #"\f" f;
d89353d17f54 added File.fold_pages for streaming of large files;
wenzelm
parents: 43616
diff changeset
   161
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
   162
fun read_lines path = rev (fold_lines cons path []);
44879
3b6613366dd7 more orthogonal signature;
wenzelm
parents: 43848
diff changeset
   163
fun read_pages path = rev (fold_pages cons path []);
43848
8f2bf02a0ccb some file and directory operations;
wenzelm
parents: 43845
diff changeset
   164
75569
f848f66a8cb7 tuned signature;
wenzelm
parents: 75566
diff changeset
   165
val read = open_input input_all;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   166
28028
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   167
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   168
(* output *)
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   169
60982
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
   170
fun output file txt = BinIO.output (file, Byte.stringToBytes txt);
75571
ac5e633ad9b3 tuned signature: more operations;
wenzelm
parents: 75569
diff changeset
   171
val outputs = List.app o output;
28028
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   172
60982
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
   173
fun output_list txts file = List.app (output file) txts;
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
   174
fun write_list path txts = open_output (output_list txts) path;
67e389f67073 precise BinIO, without newline conversion on Windows;
wenzelm
parents: 60970
diff changeset
   175
fun append_list path txts = open_append (output_list txts) path;
16713
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   176
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   177
fun write path txt = write_list path [txt];
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   178
fun append path txt = append_list path [txt];
69223
44d68a00917c more operations;
wenzelm
parents: 64698
diff changeset
   179
fun generate path txt = if try read path = SOME txt then () else write path txt;
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
   180
75566
389b193af8ae clarified signature;
wenzelm
parents: 73314
diff changeset
   181
fun write_buffer path buf =
389b193af8ae clarified signature;
wenzelm
parents: 73314
diff changeset
   182
  open_output (fn file => List.app (output file) (Buffer.contents buf)) path;
28028
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   183
c0f54a32491e renamed Buffer.write to File.write_buffer;
wenzelm
parents: 26980
diff changeset
   184
56533
cd8b6d849b6a explicit 'document_files' in session ROOT specifications;
wenzelm
parents: 48911
diff changeset
   185
(* eq *)
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   186
16603
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   187
fun eq paths =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 56533
diff changeset
   188
  (case try (apply2 (OS.FileSys.fileId o platform_path)) paths of
26656
62fff5feb756 Library.is_equal;
wenzelm
parents: 26503
diff changeset
   189
    SOME ids => is_equal (OS.FileSys.compare ids)
16603
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   190
  | NONE => false);
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   191
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   192
end;