From 13dad5b6ac7197dbcdbb9daa86937e184dc11659 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 26 Mar 2016 12:22:25 +0100 Subject: [PATCH] add `CCIO.File.with_temp` for creating temporary files --- src/core/CCIO.ml | 4 ++++ src/core/CCIO.mli | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/core/CCIO.ml b/src/core/CCIO.ml index b3c1231a..b8c12cca 100644 --- a/src/core/CCIO.ml +++ b/src/core/CCIO.ml @@ -329,4 +329,8 @@ module File = struct | `File -> "file:" | `Dir -> "dir:" ) ^ f + + let with_temp ?temp_dir ~prefix ~suffix f = + let name = Filename.temp_file ?temp_dir prefix suffix in + finally_ f name ~h:remove_noerr end diff --git a/src/core/CCIO.mli b/src/core/CCIO.mli index 92e6a119..98134e4d 100644 --- a/src/core/CCIO.mli +++ b/src/core/CCIO.mli @@ -195,4 +195,14 @@ module File : sig symlinks, etc.) *) val show_walk_item : walk_item -> string + + val with_temp : + ?temp_dir:string -> prefix:string -> suffix:string -> + (string -> 'a) -> 'a + (** [with_temp ~prefix ~suffix f] will call [f] with the name of a new + temporary file (located in [temp_dir]). + After [f] returns, the file is deleted. Best to be used in + combination with {!with_out}. + See {!Filename.temp_file} + @since NEXT_RELEASE *) end