Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/cm/util/autodir.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/util/autodir.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 452 - (view) (download)

1 : blume 354 (*
2 :     * Opening files for output while automagically creating any
3 :     * necessary directories.
4 :     *
5 :     * Copyright (c) 1999 by Lucent Technologies, Bell Laboratories
6 :     *
7 :     * Author: Matthias Blume (blume@cs.princeton.edu)
8 :     *)
9 :     structure AutoDir :> sig
10 :     val openBinOut : string -> BinIO.outstream
11 :     val openTextOut : string -> TextIO.outstream
12 :     end = struct
13 :    
14 :     structure P = OS.Path
15 :     structure F = OS.FileSys
16 :    
17 :     fun fileExists n = F.access (n, []) handle _ => false
18 :    
19 :     fun openOut fileopener p = let
20 : blume 452 fun mkDir d =
21 :     F.mkDir d handle exn => (if fileExists d then () else raise exn)
22 : blume 354 fun generic (maker, pmaker, p) =
23 :     maker p
24 :     handle exn => let
25 : blume 376 val dir = P.dir p
26 : blume 354 in
27 : blume 452 (* If the parent dir exists, then we must consider
28 :     * these cases:
29 :     * - non-parallel: we should signal an error
30 :     * - parallel: somebody else may have made this dir
31 :     * in the meantime, so we should try again
32 :     * Both cases can be handled by simply calling maker
33 :     * again. (It will fail in the non-parallel case, but
34 :     * that's actually what we want.) *)
35 :     if dir = "" orelse fileExists dir then maker p
36 : blume 354 else (pmaker dir; maker p)
37 :     end
38 : blume 376 fun makedirs dir = generic (mkDir, makedirs, dir)
39 : blume 354 fun advertisemakedirs dir =
40 :     (Say.vsay ["[creating directory ", dir, " ...]\n"];
41 :     makedirs dir)
42 :     in
43 :     generic (fileopener, advertisemakedirs, p)
44 :     end
45 :    
46 :     val openTextOut = openOut TextIO.openOut
47 :     val openBinOut = openOut BinIO.openOut
48 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0