basic framework for tmpfcopystream, close() is still to do
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Wed, 10 Mar 2010 18:22:46 +0000 (19:22 +0100)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Wed, 10 Mar 2010 18:23:29 +0000 (19:23 +0100)
src/tmpfstream.cpp
src/tmpfstream.hpp
src/tmpfstream_impl.hpp

index 9e2a464..d574bff 100644 (file)
@@ -23,11 +23,17 @@ using namespace std;
 namespace I2n
 {
 
+template< typename Device, typename Tr, typename Alloc >
+const std::string tmpfcopystreamTempl<Device, Tr, Alloc>
+    ::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<bio::file_descriptor_sink>;
 template class tmpfstreamTempl<bio::file_descriptor>;
 
+template class tmpfcopystreamTempl<bio::file_descriptor_sink>;
+template class tmpfcopystreamTempl<bio::file_descriptor>;
 
 }
index 17f9736..e270ad4 100644 (file)
@@ -38,7 +38,7 @@ template< typename Device,
 class tmpfstreamTempl : public bio::stream<Device, Tr, Alloc>
 {
 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<Device>::type
+              ),
+          typename Alloc =
+              std::allocator<
+                  BOOST_DEDUCED_TYPENAME  bio::char_type_of<Device>::type
+              > >
+class tmpfcopystreamTempl : public tmpfstreamTempl<Device, Tr, Alloc>
+{
+private:
+    std::string originalfilename;
+    bool full_sync;
+
+public:
+    static const std::string default_template_suffix;
+
+    tmpfcopystreamTempl()
+        : tmpfstreamTempl<Device, Tr, Alloc>()
+        { }
+
+    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<Device, Tr, Alloc>
+            (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<Device, Tr, Alloc>
+            (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<Device, Tr, Alloc>
+            ::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<Device, Tr, Alloc>
+            ::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<Device, Tr, Alloc>::unlink(); }
+
+    bool move(const std::string& targetpath, bool overwrite=false)
+        { return tmpfcopystreamTempl<Device, Tr, Alloc>::move(targetpath,overwrite); }
+};
+
 typedef tmpfstreamTempl<bio::file_descriptor_sink> tmpofstream;
 typedef tmpfstreamTempl<bio::file_descriptor> tmpfstream;
 
+typedef tmpfcopystreamTempl<bio::file_descriptor_sink> tmpofcopystream;
+typedef tmpfcopystreamTempl<bio::file_descriptor> tmpfcopystream;
+
 }
 
 #endif
index f00bb3f..bdc0997 100644 (file)
@@ -57,7 +57,7 @@ bool tmpfstreamTempl<Device,Tr,Alloc>::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<Device,Tr,Alloc>::move(const std::string& targetpath,
     }
 }
 
+template< typename Device, typename Tr, typename Alloc >
+void tmpfcopystreamTempl<Device,Tr,Alloc>::close()
+{
+    // TODO
+    // TODO full sync
+}
+
 }
 
 #endif