aboutsummaryrefslogtreecommitdiff
path: root/cutl
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2009-09-19 08:41:33 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2009-09-19 08:41:33 +0200
commit5409489c632fcaa3868a0767cecef65f079e5a94 (patch)
tree829cdf6020eb848526778741afe995ee242e7a4d /cutl
parent6ca1c8ee64bf7268d194eb15e72d3024a335039d (diff)
Add RAII-based file auto-remover
Diffstat (limited to 'cutl')
-rw-r--r--cutl/fs/auto-remove.cxx39
-rw-r--r--cutl/fs/auto-remove.hxx81
-rw-r--r--cutl/fs/exception.cxx18
-rw-r--r--cutl/fs/exception.hxx36
-rw-r--r--cutl/makefile2
5 files changed, 175 insertions, 1 deletions
diff --git a/cutl/fs/auto-remove.cxx b/cutl/fs/auto-remove.cxx
new file mode 100644
index 0000000..08718e9
--- /dev/null
+++ b/cutl/fs/auto-remove.cxx
@@ -0,0 +1,39 @@
+// file : cutl/fs/auto-remove.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#include <cstdio> // std::remove
+#include <cerrno>
+
+#include <cutl/fs/exception.hxx>
+#include <cutl/fs/auto-remove.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ auto_remove::
+ ~auto_remove ()
+ {
+ if (!canceled_)
+ {
+ if (std::remove (path_.string ().c_str ()) == -1)
+ throw error (errno);
+ }
+ }
+
+ auto_removes::
+ ~auto_removes ()
+ {
+ if (!canceled_)
+ {
+ for (paths::iterator i (paths_.begin ()); i != paths_.end (); ++i)
+ {
+ if (std::remove (i->string ().c_str ()) == -1)
+ throw error (errno);
+ }
+ }
+ }
+ }
+}
diff --git a/cutl/fs/auto-remove.hxx b/cutl/fs/auto-remove.hxx
new file mode 100644
index 0000000..0ca7f8f
--- /dev/null
+++ b/cutl/fs/auto-remove.hxx
@@ -0,0 +1,81 @@
+// file : cutl/fs/auto-remove.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#ifndef CUTL_FS_AUTO_REMOVE_HXX
+#define CUTL_FS_AUTO_REMOVE_HXX
+
+#include <vector>
+
+#include <cutl/exception.hxx>
+#include <cutl/fs/path.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ // Remove a file or an empty directory on destruction unless canceled.
+ //
+ struct auto_remove
+ {
+ explicit
+ auto_remove (path const& p)
+ : path_ (p), canceled_ (false)
+ {
+ }
+
+ ~auto_remove ();
+
+ void
+ cancel ()
+ {
+ canceled_ = true;
+ }
+
+ private:
+ auto_remove (auto_remove const&);
+
+ auto_remove&
+ operator= (auto_remove const&);
+
+ private:
+ path path_;
+ bool canceled_;
+ };
+
+ // Remove a list of file or aempty directories on destruction unless
+ // canceled.
+ //
+ struct auto_removes
+ {
+ ~auto_removes ();
+
+ void
+ add (path const& p)
+ {
+ paths_.push_back (p);
+ }
+
+ void
+ cancel ()
+ {
+ canceled_ = true;
+ }
+
+ private:
+ auto_removes (auto_removes const&);
+
+ auto_removes&
+ operator= (auto_removes const&);
+
+ private:
+ typedef std::vector<path> paths;
+
+ paths paths_;
+ bool canceled_;
+ };
+ }
+}
+
+#endif // CUTL_FS_AUTO_REMOVE_HXX
diff --git a/cutl/fs/exception.cxx b/cutl/fs/exception.cxx
new file mode 100644
index 0000000..793902d
--- /dev/null
+++ b/cutl/fs/exception.cxx
@@ -0,0 +1,18 @@
+// file : cutl/fs/exception.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#include <cutl/fs/exception.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ char const* error::
+ what () const throw ()
+ {
+ return "filesystem error";
+ }
+ }
+}
diff --git a/cutl/fs/exception.hxx b/cutl/fs/exception.hxx
new file mode 100644
index 0000000..b412491
--- /dev/null
+++ b/cutl/fs/exception.hxx
@@ -0,0 +1,36 @@
+// file : cutl/fs/exception.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#ifndef CUTL_FS_EXCEPTION_HXX
+#define CUTL_FS_EXCEPTION_HXX
+
+#include <cutl/exception.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ struct error: exception
+ {
+ error (int code): code_ (code) {}
+
+ // Error code (errno).
+ //
+ int
+ code () const
+ {
+ return code_;
+ }
+
+ virtual char const*
+ what () const throw ();
+
+ private:
+ int code_;
+ };
+ }
+}
+
+#endif // CUTL_FS_EXCEPTION_HXX
diff --git a/cutl/makefile b/cutl/makefile
index 294bbd5..1635311 100644
--- a/cutl/makefile
+++ b/cutl/makefile
@@ -7,7 +7,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
cxx_tun := exception.cxx shared-ptr/base.cxx
-cxx_tun += fs/path.cxx
+cxx_tun += fs/exception.cxx fs/path.cxx fs/auto-remove.cxx
cxx_tun += \
compiler/context.cxx \