src/Pure/General/file.ML
changeset 6182 4a07dfe3583f
parent 6118 caa439435666
child 6218 3e9d6edc99a8
equal deleted inserted replaced
6181:128646d4a975 6182:4a07dfe3583f
     1 (*  Title:      Pure/General/file.ML
     1 (*  Title:      Pure/General/file.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     4 
     5 File system operations.
     5 File system operations.
       
     6 
       
     7 TODO:
       
     8   - close in case of error (!?);
     6 *)
     9 *)
     7 
    10 
     8 signature FILE =
    11 signature FILE =
     9 sig
    12 sig
    10   val tmp_name: string -> string
    13   val sys_path_fn: (Path.T -> string) ref
    11   val exists: string -> bool
    14   val tmp_path: Path.T -> Path.T
    12   val info: string -> string
    15   eqtype info
    13   val read: string -> string
    16   val info: Path.T -> info option
    14   val write: string -> string -> unit
    17   val exists: Path.T -> bool
    15   val append: string -> string -> unit
    18   val read: Path.T -> string
    16   val copy: string -> string -> unit
    19   val write: Path.T -> string -> unit
       
    20   val append: Path.T -> string -> unit
       
    21   val copy: Path.T -> Path.T -> unit
       
    22   val use: Path.T -> unit
       
    23   val cd: Path.T -> unit
       
    24   val rm: Path.T -> unit
    17 end;
    25 end;
    18 
    26 
    19 structure File: FILE =
    27 structure File: FILE =
    20 struct
    28 struct
    21 
    29 
    22 
    30 
    23 (* tmp_name *)
    31 (* sys_path (default for Unix) *)
    24 
    32 
    25 fun tmp_name name =
    33 val sys_path_fn = ref Path.pack;
    26   Path.pack (Path.evaluate (Path.unpack o getenv)
    34 fun sysify path = ! sys_path_fn (Path.expand path);
    27     (Path.append (Path.variable "ISABELLE_TMP") (Path.unpack name)));
       
    28 
    35 
    29 
    36 
    30 (* exists / info *)
    37 (* tmp_path *)
    31 
    38 
    32 fun exists name = (file_info name <> "");
    39 fun tmp_path path =
    33 val info = file_info;
    40   Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
       
    41 
       
    42 
       
    43 (* info / exists *)
       
    44 
       
    45 datatype info = Info of string;
       
    46 
       
    47 fun info path =
       
    48   let val name = sysify path in
       
    49     (case file_info name of
       
    50       "" => None
       
    51     | s => Some (Info (name ^ ": " ^ s)))	(* FIXME include full path (!?) *)
       
    52   end;
       
    53 
       
    54 val exists = is_some o info;
    34 
    55 
    35 
    56 
    36 (* read / write files *)
    57 (* read / write files *)
    37 
    58 
    38 fun read name =
    59 fun read path =
    39   let
    60   let
    40     val instream  = TextIO.openIn name;
    61     val instream  = TextIO.openIn (sysify path);
    41     val intext = TextIO.inputAll instream;
    62     val intext = TextIO.inputAll instream;
    42   in
    63   in
    43     TextIO.closeIn instream;
    64     TextIO.closeIn instream;
    44     intext
    65     intext
    45   end;
    66   end;
    46 
    67 
    47 fun write name txt =
    68 fun write path txt =
    48   let val outstream = TextIO.openOut name in
    69   let val outstream = TextIO.openOut (sysify path) in
    49     TextIO.output (outstream, txt);
    70     TextIO.output (outstream, txt);
    50     TextIO.closeOut outstream
    71     TextIO.closeOut outstream
    51   end;
    72   end;
    52 
    73 
    53 fun append name txt =
    74 fun append path txt =
    54   let val outstream = TextIO.openAppend name in
    75   let val outstream = TextIO.openAppend (sysify path) in
    55     TextIO.output (outstream, txt);
    76     TextIO.output (outstream, txt);
    56     TextIO.closeOut outstream
    77     TextIO.closeOut outstream
    57   end;
    78   end;
    58 
    79 
    59 fun copy infile outfile =
    80 fun copy inpath outpath = write outpath (read inpath);
    60   if not (exists infile) then error ("File not found: " ^ quote infile)
    81 
    61   else write outfile (read infile);
    82 
       
    83 (* misc operations *)
       
    84 
       
    85 val use = use o sysify;
       
    86 val cd = Library.cd o sysify;
       
    87 val rm = OS.FileSys.remove o sysify;
    62 
    88 
    63 
    89 
    64 end;
    90 end;