From ff09d3171e40eb409bdf6ef5948e2c1383448bb0 Mon Sep 17 00:00:00 2001 From: Gerd von Egidy Date: Wed, 10 Mar 2010 19:22:46 +0100 Subject: [PATCH] basic framework for tmpfcopystream, close() is still to do --- src/tmpfstream.cpp | 6 +++ src/tmpfstream.hpp | 86 +++++++++++++++++++++++++++++++++++++++++++++- src/tmpfstream_impl.hpp | 9 ++++- 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/src/tmpfstream.cpp b/src/tmpfstream.cpp index 9e2a464..d574bff 100644 --- a/src/tmpfstream.cpp +++ b/src/tmpfstream.cpp @@ -23,11 +23,17 @@ using namespace std; namespace I2n { +template< typename Device, typename Tr, typename Alloc > +const std::string tmpfcopystreamTempl + ::default_template_suffix=".tmp.XXXXXX"; + // explicitly instantiate the most used versions. // means they get compiled into the lib and you don't need tmpfstream_impl.hpp // if you want to use anything else, include tmpfstream_impl.hpp. template class tmpfstreamTempl; template class tmpfstreamTempl; +template class tmpfcopystreamTempl; +template class tmpfcopystreamTempl; } diff --git a/src/tmpfstream.hpp b/src/tmpfstream.hpp index 17f9736..e270ad4 100644 --- a/src/tmpfstream.hpp +++ b/src/tmpfstream.hpp @@ -38,7 +38,7 @@ template< typename Device, class tmpfstreamTempl : public bio::stream { private: - std::string filename; + std::string tmpfilename; bool unlinked; public: @@ -59,7 +59,7 @@ public: int buffer_size = -1 , int pback_size = -1); std::string get_tmp_filename() - { return filename; } + { return tmpfilename; } bool set_file_mode(int mode); @@ -71,9 +71,91 @@ public: bool move(const std::string& targetpath, bool overwrite=false); }; +template< typename Device, + typename Tr = + BOOST_IOSTREAMS_CHAR_TRAITS( + BOOST_DEDUCED_TYPENAME bio::char_type_of::type + ), + typename Alloc = + std::allocator< + BOOST_DEDUCED_TYPENAME bio::char_type_of::type + > > +class tmpfcopystreamTempl : public tmpfstreamTempl +{ +private: + std::string originalfilename; + bool full_sync; + +public: + static const std::string default_template_suffix; + + tmpfcopystreamTempl() + : tmpfstreamTempl() + { } + + tmpfcopystreamTempl(const std::string& filename, + std::ios_base::open_mode mode = std::ios_base::out, + int buffer_size = -1 , int pback_size = -1) + : originalfilename(filename), + tmpfstreamTempl + (filename+default_template_suffix,mode,buffer_size,pback_size) + { } + + tmpfcopystreamTempl(const std::string& filename, + const std::string& tmpnametemplate, + std::ios_base::open_mode mode = std::ios_base::out, + int buffer_size = -1 , int pback_size = -1) + : originalfilename(filename), + tmpfstreamTempl + (tmpnametemplate,mode,buffer_size,pback_size) + { } + + bool open(const std::string& filename, + std::ios_base::open_mode mode = std::ios_base::out, + int buffer_size = -1 , int pback_size = -1) + { + originalfilename=filename; + return tmpfstreamTempl + ::open(filename+default_template_suffix,mode,buffer_size,pback_size); + } + + bool open(const std::string& filename, + const std::string& tmpnametemplate, + std::ios_base::open_mode mode = std::ios_base::out, + int buffer_size = -1 , int pback_size = -1) + { + originalfilename=filename; + return tmpfstreamTempl + ::open(tmpnametemplate,mode,buffer_size,pback_size); + } + + std::string get_original_filename() + { return originalfilename; } + + void set_full_sync(bool do_full_sync=true) + { full_sync=do_full_sync; } + + bool get_full_sync() + { return full_sync; } + + void close(); + +private: + + // forbid users to call these functions, no need to disturbe the internals here + bool unlink() + { return tmpfcopystreamTempl::unlink(); } + + bool move(const std::string& targetpath, bool overwrite=false) + { return tmpfcopystreamTempl::move(targetpath,overwrite); } +}; + typedef tmpfstreamTempl tmpofstream; typedef tmpfstreamTempl tmpfstream; +typedef tmpfcopystreamTempl tmpofcopystream; +typedef tmpfcopystreamTempl tmpfcopystream; + } #endif diff --git a/src/tmpfstream_impl.hpp b/src/tmpfstream_impl.hpp index f00bb3f..bdc0997 100644 --- a/src/tmpfstream_impl.hpp +++ b/src/tmpfstream_impl.hpp @@ -57,7 +57,7 @@ bool tmpfstreamTempl::open(const std::string& tmpnametemplate, flags |= O_APPEND; int fd=mkostemp(chbuf,flags); - filename=chbuf; + tmpfilename=chbuf; delete[] chbuf; if (fd==-1) @@ -120,6 +120,13 @@ bool tmpfstreamTempl::move(const std::string& targetpath, } } +template< typename Device, typename Tr, typename Alloc > +void tmpfcopystreamTempl::close() +{ + // TODO + // TODO full sync +} + } #endif -- 1.7.1