src/Pure/General/file.ML
author wenzelm
Fri, 30 Jul 1999 13:34:27 +0200
changeset 7129 7e0ec1b293c3
parent 6640 d2e8342bf5c3
child 7716 b0cb304517d4
permissions -rw-r--r--
export sysify_path;
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
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     4
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     5
File system operations.
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
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     8
signature FILE =
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     9
sig
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    10
  val sys_pack_fn: (Path.T -> string) ref
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    11
  val sys_unpack_fn: (string -> Path.T) ref
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    12
  val sysify_path: Path.T -> string
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    13
  val rm: Path.T -> unit
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    14
  val cd: Path.T -> unit
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    15
  val pwd: unit -> Path.T
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    16
  val full_path: Path.T -> Path.T
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    17
  val tmp_path: Path.T -> Path.T
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    18
  val read: Path.T -> string
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    19
  val write: Path.T -> string -> unit
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    20
  val append: Path.T -> string -> unit
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    21
  val copy: Path.T -> Path.T -> unit
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    22
  eqtype info
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    23
  val info: Path.T -> info option
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    24
  val exists: Path.T -> bool
6318
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
    25
  val mkdir: Path.T -> unit
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
    26
  val source: Path.T -> (string, string list) Source.source * Position.T
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
    27
  val plain_use: Path.T -> unit
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
    28
  val symbol_use: Path.T -> unit
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    29
end;
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    30
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    31
structure File: FILE =
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    32
struct
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    33
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    34
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    35
(* system path representations (default: Unix) *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    36
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    37
val sys_pack_fn = ref Path.pack;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    38
val sys_unpack_fn = ref Path.unpack;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    39
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    40
fun sysify_path path = ! sys_pack_fn (Path.expand path);
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    41
fun unsysify_path s = ! sys_unpack_fn s;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    42
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    43
val rm = OS.FileSys.remove o sysify_path;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    44
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    45
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    46
(* current path *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    47
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    48
val cd = Library.cd o sysify_path;
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    49
val pwd = unsysify_path o Library.pwd;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    50
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    51
fun full_path path =
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    52
  if Path.is_absolute path then path
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    53
  else Path.append (pwd ()) path;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    54
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    55
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    56
(* tmp_path *)
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    57
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    58
fun tmp_path path =
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    59
  Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    60
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    61
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    62
(* read / write files *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    63
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    64
fun fail_safe f g x =
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    65
  let val y = f x handle exn => (g x; raise exn)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    66
  in g x; y end;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    67
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    68
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    69
fun output txt stream = TextIO.output (stream, txt);
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    70
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    71
fun read path = fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (sysify_path path));
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    72
fun write path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openOut (sysify_path path));
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    73
fun append path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (sysify_path path));
6218
3e9d6edc99a8 added join_info;
wenzelm
parents: 6182
diff changeset
    74
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    75
fun copy inpath outpath = write outpath (read inpath);
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    76
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    77
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    78
(* file info *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    79
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    80
datatype info = Info of string;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    81
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    82
fun info path =
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    83
  let val name = sysify_path path in
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    84
    (case file_info name of
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    85
      "" => None
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    86
    | s => Some (Info s))
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    87
  end;
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    88
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    89
val exists = is_some o info;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    90
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    91
6318
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
    92
(* mkdir *)
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
    93
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    94
fun mkdir path = (execute ("mkdir -p " ^ enclose "'" "'" (sysify_path path)); ());
6318
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
    95
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
    96
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
    97
(* source *)
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
    98
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
    99
fun source raw_path =
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   100
  let val path = Path.expand raw_path
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   101
  in (Source.of_string (read path), Position.line_name 1 (quote (Path.pack path))) end;
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   102
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   103
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   104
(* symbol_use *)
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   105
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
   106
val plain_use = use o sysify_path;
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   107
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   108
(* version of 'use' with input filtering *)
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   109
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   110
val symbol_use =
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   111
  if not needs_filtered_use then plain_use	(*ML system (wrongly!) accepts non-ASCII*)
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   112
  else
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   113
    fn path =>
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   114
      let
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   115
        val txt = read path;
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   116
        val txt_out = Symbol.input txt;
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   117
      in
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   118
        if txt = txt_out then plain_use path
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   119
        else
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   120
          let val tmp_path = tmp_path path in
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   121
            write tmp_path txt_out;
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   122
            plain_use tmp_path handle exn => (rm tmp_path; raise exn);
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   123
            rm tmp_path
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   124
          end
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   125
      end;
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   126
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   127
end;