src/Pure/General/file.ML
author berghofe
Sat, 10 May 2003 20:53:02 +0200
changeset 13998 75a399c2781f
parent 12894 61f485eb0dc2
child 14981 e73f8140af78
permissions -rw-r--r--
Added new function eta_long.
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
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
8806
wenzelm
parents: 8163
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
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
File system operations.
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     7
*)
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     8
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     9
signature FILE =
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    10
sig
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    11
  val sys_pack_fn: (Path.T -> string) ref
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    12
  val sys_unpack_fn: (string -> Path.T) ref
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    13
  val sysify_path: Path.T -> string
9784
d8f8dc335c8c export quote_sysify_path;
wenzelm
parents: 9046
diff changeset
    14
  val quote_sysify_path: Path.T -> string
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    15
  val rm: Path.T -> unit
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    16
  val cd: Path.T -> unit
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    17
  val pwd: unit -> Path.T
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    18
  val full_path: Path.T -> Path.T
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    19
  val tmp_path: Path.T -> Path.T
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    20
  val read: Path.T -> string
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    21
  val write: Path.T -> string -> unit
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    22
  val append: Path.T -> string -> unit
9046
17c5edf1706b Exported system_command.
berghofe
parents: 8806
diff changeset
    23
  val system_command: string -> unit
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    24
  val copy: Path.T -> Path.T -> unit
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    25
  eqtype info
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    26
  val info: Path.T -> info option
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    27
  val exists: Path.T -> bool
6318
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
    28
  val mkdir: Path.T -> unit
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 9784
diff changeset
    29
  val use: Path.T -> unit
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    30
end;
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    31
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    32
structure File: FILE =
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    33
struct
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    34
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    35
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    36
(* system path representations (default: Unix) *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    37
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    38
val sys_pack_fn = ref Path.pack;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    39
val sys_unpack_fn = ref Path.unpack;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    40
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    41
fun sysify_path path = ! sys_pack_fn (Path.expand path);
9784
d8f8dc335c8c export quote_sysify_path;
wenzelm
parents: 9046
diff changeset
    42
val quote_sysify_path = enclose "'" "'" o sysify_path;
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    43
fun unsysify_path s = ! sys_unpack_fn s;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    44
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    45
val rm = OS.FileSys.remove o sysify_path;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    46
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    47
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    48
(* current path *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    49
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    50
val cd = Library.cd o sysify_path;
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    51
val pwd = unsysify_path o Library.pwd;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    52
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    53
fun full_path path =
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    54
  if Path.is_absolute path then path
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    55
  else Path.append (pwd ()) path;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    56
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    57
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    58
(* tmp_path *)
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    59
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    60
fun tmp_path path =
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    61
  Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    62
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    63
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    64
(* read / write files *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    65
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    66
fun fail_safe f g x =
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    67
  let val y = f x handle exn => (g x; raise exn)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    68
  in g x; y end;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    69
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    70
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    71
fun output txt stream = TextIO.output (stream, txt);
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    72
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    73
fun read path = fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (sysify_path path));
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    74
fun write path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openOut (sysify_path path));
7716
b0cb304517d4 added copy_all;
wenzelm
parents: 7129
diff changeset
    75
fun append path txt =
b0cb304517d4 added copy_all;
wenzelm
parents: 7129
diff changeset
    76
  fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (sysify_path path));
6218
3e9d6edc99a8 added join_info;
wenzelm
parents: 6182
diff changeset
    77
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    78
fun copy inpath outpath = write outpath (read inpath);
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    79
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    80
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    81
(* file info *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    82
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    83
datatype info = Info of string;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    84
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    85
fun info path =
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    86
  let val name = sysify_path path in
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    87
    (case file_info name of
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    88
      "" => None
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    89
    | s => Some (Info s))
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    90
  end;
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    91
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    92
val exists = is_some o info;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    93
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    94
7716
b0cb304517d4 added copy_all;
wenzelm
parents: 7129
diff changeset
    95
(* directories *)
6318
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
    96
7850
3689adcf9b8b mkdir, copy_all: system_command;
wenzelm
parents: 7716
diff changeset
    97
fun system_command cmd =
3689adcf9b8b mkdir, copy_all: system_command;
wenzelm
parents: 7716
diff changeset
    98
  if system cmd <> 0 then error ("System command failed: " ^ cmd)
3689adcf9b8b mkdir, copy_all: system_command;
wenzelm
parents: 7716
diff changeset
    99
  else ();
12894
61f485eb0dc2 moved copy_all to Thy/present.ML;
wenzelm
parents: 12116
diff changeset
   100
9784
d8f8dc335c8c export quote_sysify_path;
wenzelm
parents: 9046
diff changeset
   101
fun mkdir path = system_command ("mkdir -p " ^ quote_sysify_path path);
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   102
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   103
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 9784
diff changeset
   104
(* use ML files *)
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   105
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 9784
diff changeset
   106
val use = use o sysify_path;
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   107
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   108
end;