|
Boost-Commit : |
From: technews_at_[hidden]
Date: 2008-06-24 20:48:11
Author: turkanis
Date: 2008-06-24 20:48:10 EDT (Tue, 24 Jun 2008)
New Revision: 46668
URL: http://svn.boost.org/trac/boost/changeset/46668
Log:
added helper types and functions for Boost.Filesystem interoperability and for accessing file timestamps
Added:
trunk/boost/iostreams/detail/file_handle.hpp (contents, props changed)
trunk/boost/iostreams/detail/file_times.hpp (contents, props changed)
trunk/boost/iostreams/detail/path.hpp (contents, props changed)
trunk/libs/iostreams/src/file_times.cpp (contents, props changed)
Text files modified:
trunk/libs/iostreams/build/Jamfile.v2 | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
Added: trunk/boost/iostreams/detail/file_handle.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/iostreams/detail/file_handle.hpp 2008-06-24 20:48:10 EDT (Tue, 24 Jun 2008)
@@ -0,0 +1,32 @@
+/*
+ * Distributed under the Boost Software License, Version 1.0.(See accompanying
+ * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
+ *
+ * See http://www.boost.org/libs/iostreams for documentation.
+ *
+ * File: boost/iostreams/detail/file_handle.hpp
+ * Date: Sun Jun 22 14:23:12 MDT 2008
+ * Copyright: 2008 CodeRage, LLC
+ * Author: Jonathan Turkanis
+ * Contact: turkanis at coderage dot com
+ *
+ * Defines the type boost::iostreams::detail::file_handle, representing an
+ * operating system file handle.
+ */
+
+#ifndef BOOST_IOSTREAMS_DETAIL_FILE_HANDLE_HPP_INCLUDED
+#define BOOST_IOSTREAMS_DETAIL_FILE_HANDLE_HPP_INCLUDED
+
+#include <boost/iostreams/detail/config/windows_posix.hpp>
+
+namespace boost { namespace iostreams { namespace detail {
+
+#ifdef BOOST_IOSTREAMS_WINDOWS
+ typedef void* file_handle; // A.k.a. HANDLE
+#else
+ typedef int file_handle;
+#endif
+
+} } } // End namespaces detail, iostreams, boost.
+
+#endif // #ifndef BOOST_IOSTREAMS_DETAIL_FILE_HANDLE_HPP_INCLUDED
Added: trunk/boost/iostreams/detail/file_times.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/iostreams/detail/file_times.hpp 2008-06-24 20:48:10 EDT (Tue, 24 Jun 2008)
@@ -0,0 +1,43 @@
+/*
+ * Distributed under the Boost Software License, Version 1.0.(See accompanying
+ * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
+ *
+ * See http://www.boost.org/libs/iostreams for documentation.
+ *
+ * File: boost/iostreams/detail/file_times.hpp
+ * Date: Sun Jun 22 14:23:12 MDT 2008
+ * Copyright: Jorge Lodos and 2008 CodeRage, LLC
+ * Author: Jorge Lodos and Jonathan Turkanis
+ * Contact: turkanis at coderage dot com
+ *
+ * Declares functions for accessing and manipulating timestamps associated
+ * with a file descriptor.
+ */
+
+#ifndef BOOST_IOSTREAMS_DETAIL_FILE_TIMES_HPP_INCLUDED
+#define BOOST_IOSTREAMS_DETAIL_FILE_TIMES_HPP_INCLUDED
+
+#include <ctime>
+#include <boost/iostreams/detail/file_handle.hpp>
+
+namespace boost { namespace iostreams { namespace detail {
+
+// Returns the last access time of the file associated with the given handle.
+// Reports errors by throwing instances of std::failure.
+std::time_t last_read_time(file_handle);
+
+// Sets the last access time of the file associated with the given handle.
+// Reports errors by throwing instances of std::failure.
+void set_last_read_time(file_handle, std::time_t);
+
+// Returns the last modification time of the file associated with the given
+// handle. Reports errors by throwing instances of std::failure.
+std::time_t last_write_time(file_handle);
+
+// Sets the last modification time of the file associated with the given
+// handle. Reports errors by throwing instances of std::failure.
+void set_last_write_time(file_handle, std::time_t);
+
+} } } // End namespaces detail, iostreams, boost.
+
+#endif // #ifndef BOOST_IOSTREAMS_DETAIL_FILE_TIMES_HPP_INCLUDED
Added: trunk/boost/iostreams/detail/path.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/iostreams/detail/path.hpp 2008-06-24 20:48:10 EDT (Tue, 24 Jun 2008)
@@ -0,0 +1,134 @@
+/*
+ * Distributed under the Boost Software License, Version 1.0.(See accompanying
+ * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
+ *
+ * See http://www.boost.org/libs/iostreams for documentation.
+ *
+ * File: boost/iostreams/detail/path.hpp
+ * Date: Sat Jun 21 21:24:05 MDT 2008
+ * Copyright: 2008 CodeRage, LLC
+ * Author: Jonathan Turkanis
+ * Contact: turkanis at coderage dot com
+ *
+ * Defines the class boost::iostreams::detail::path, for storing a
+ * a std::string or std::wstring.
+ *
+ * This class allows interoperability with Boost.Filesystem without
+ * creating a dependence on Boost.Filesystem headers or implementation.
+ */
+
+#ifndef BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED
+#define BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED
+
+#include <string>
+#include <boost/static_assert.hpp>
+#include <boost/type.hpp>
+#include <boost/type_traits/is_same.hpp>
+
+
+namespace boost { namespace iostreams { namespace detail {
+
+class path {
+public:
+
+ // Default constructor
+ path() : narrow_(), wide_(), is_wide_(false) { }
+
+ // Constructor taking a std::string
+ path(const std::string& p) : narrow_(p), wide_(), is_wide_(false) { }
+
+ // Constructor taking a C-style string
+ path(const char* p) : narrow_(p), wide_(), is_wide_(false) { }
+
+ // Constructor taking a boost::filesystem::path or boost::filesystem::wpath
+ template<typename Path>
+ explicit path(const Path& p)
+ {
+ typedef typename Path::external_string_type string_type;
+ init(p, boost::type<string_type>());
+ }
+
+ // Copy constructor
+ path(const path& p)
+ : narrow_(p.narrow_), wide_(p.wide_), is_wide_(p.is_wide_)
+ { }
+
+ // Assignment operator taking another path
+ path& operator=(const path& p)
+ {
+ narrow_ = p.narrow_;
+ wide_ = p.wide_;
+ is_wide_ = p.is_wide_;
+ }
+
+ // Assignment operator taking a std::string
+ path& operator=(const std::string& p)
+ {
+ narrow_ = p;
+ wide_.clear();
+ is_wide_ = false;
+ }
+
+ // Assignment operator taking a C-style string
+ path& operator=(const char* p)
+ {
+ narrow_.assign(p);
+ wide_.clear();
+ is_wide_ = false;
+ }
+
+ // Assignment operator taking a Boost.Filesystem path
+ template<typename Path>
+ path& operator=(const Path& p)
+ {
+ typedef typename Path::external_string_type string_type;
+ init(p, boost::type<string_type>());
+ }
+
+ bool is_wide() const { return is_wide_; }
+
+ // Returns a representation of the underlying path as a std::string
+ // Requires: is_wide() returns false
+ std::string to_string() const { return narrow_; }
+
+ // Returns a representation of the underlying path as a std::wstring
+ // Requires: is_wide() returns true
+ std::wstring to_wstring() const { return wide_; }
+private:
+
+ // For wide-character paths, use a boost::filesystem::wpath instead of a
+ // std::wstring
+ path(const std::wstring&);
+ path& operator=(const std::wstring&);
+
+ template<typename Path>
+ void init(const Path& p, boost::type<std::string>)
+ {
+ narrow_ = p.external_file_string();
+ wide_.clear();
+ is_wide_ = false;
+ }
+
+ template<typename Path>
+ void init(const Path& p, boost::type<std::wstring>)
+ {
+ wide_ = p.external_file_string();
+ narrow_.clear();
+ is_wide_ = true;
+ }
+
+ std::string narrow_;
+ std::wstring wide_;
+ bool is_wide_;
+};
+
+inline bool operator==(const path& lhs, const path& rhs)
+{
+ return lhs.is_wide() ?
+ rhs.is_wide() && lhs.to_wstring() == rhs.to_wstring() :
+ !rhs.is_wide() && lhs.to_string() == rhs.to_string();
+}
+
+} } } // End namespaces detail, iostreams, boost.
+
+#endif // #ifndef BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED
Modified: trunk/libs/iostreams/build/Jamfile.v2
==============================================================================
--- trunk/libs/iostreams/build/Jamfile.v2 (original)
+++ trunk/libs/iostreams/build/Jamfile.v2 2008-06-24 20:48:10 EDT (Tue, 24 Jun 2008)
@@ -115,7 +115,7 @@
}
-local sources = file_descriptor.cpp mapped_file.cpp ;
+local sources = file_descriptor.cpp mapped_file.cpp file_times.cpp ;
local z = [ create-library zlib : zll z : adler32 compress
crc32 deflate gzio infback inffast inflate inftrees trees uncompr zutil :
<link>shared:<define>ZLIB_DLL ] ;
Added: trunk/libs/iostreams/src/file_times.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/iostreams/src/file_times.cpp 2008-06-24 20:48:10 EDT (Tue, 24 Jun 2008)
@@ -0,0 +1,153 @@
+/*
+ * Distributed under the Boost Software License, Version 1.0.(See accompanying
+ * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
+ *
+ * See http://www.boost.org/libs/iostreams for documentation.
+ *
+ * File: libs/iostreams/src/file_times.cpp
+ * Date: Sun Jun 22 14:23:12 MDT 2008
+ * Copyright: Jorge Lodos and 2008 CodeRage, LLC
+ * Author: Jorge Lodos and Jonathan Turkanis
+ * Contact: turkanis at coderage dot com
+ *
+ * Implements the functions declared in the header
+ * <boost/iostreams/detail/file_times.hpp>.
+ */
+
+// Define BOOST_IOSTREAMS_SOURCE so that <boost/iostreams/detail/config.hpp>
+// knows that we are building the library (possibly exporting code), rather
+// than using it (possibly importing code).
+#define BOOST_IOSTREAMS_SOURCE
+
+#include <ctime>
+#include <boost/iostreams/detail/config/windows_posix.hpp>
+#include <boost/iostreams/detail/file_times.hpp>
+#include <boost/iostreams/detail/system_failure.hpp>
+
+#ifdef BOOST_IOSTREAMS_WINDOWS
+# define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
+# include <windows.h>
+#else
+# include <fcntl.h>
+# include <sys/stat.h>
+# include <sys/time.h> // futimes
+# include <sys/types.h> // struct stat.
+#endif
+
+namespace boost { namespace iostreams { namespace detail {
+
+time_t last_read_time(file_handle h)
+{
+#ifdef BOOST_IOSTREAMS_WINDOWS
+ FILETIME ft_access;
+ if (!::GetFileTime(h, NULL, &ft_access, NULL))
+ throw_system_failure("failed querying last access time");
+ SYSTEMTIME st_access;
+ if (!FileTimeToSystemTime(&ft_access, &st_access))
+ throw_system_failure("failed querying last access time");
+ std::tm tm;
+ tm.tm_sec = st_access.wSecond;
+ tm.tm_min = st_access.wMinute;
+ tm.tm_hour = st_access.wHour;
+ tm.tm_mday = st_access.wDay;
+ tm.tm_mon = st_access.wMonth - 1;
+ tm.tm_year = st_access.wYear - 1900;
+ return std::mktime(&tm);
+#else
+ struct stat info;
+ if (::fstat(handle_, &info) == -1)
+ throw_system_failure("failed querying last access time");
+ return info.st_atime;
+#endif
+}
+
+void set_last_read_time(file_handle h, time_t tm)
+{
+#ifdef BOOST_IOSTREAMS_WINDOWS
+ struct std::tm* t = std::gmtime(&tm);
+ if (t == NULL)
+ throw_system_failure("failed settting last access time");
+ SYSTEMTIME st_access;
+ st_access.wSecond = t->tm_sec;
+ st_access.wMinute = t->tm_min;
+ st_access.wHour = t->tm_hour;
+ st_access.wDay = t->tm_mday;
+ st_access.wMonth = t->tm_mon + 1;
+ st_access.wYear = t->tm_year + 1900;
+ FILETIME ft_access;
+ if (!SystemTimeToFileTime(&st_access, &ft_access))
+ throw_system_failure("failed settting last access time");
+ if (!::SetFileTime(h, NULL, &ft_access, NULL))
+ throw_system_failure("failed settting last access time");
+#else
+ struct stat info;
+ if (::fstat(handle_, &info) == -1)
+ throw_system_failure("failed settting last access time");
+ struct timeval tv[2];
+ tv[0].tv_sec = tm;
+ tv[0].tv_usec = 0;
+ tv[1].tv_sec = info.st_mtime;
+ tv[1].tv_usec = 0;
+ if (futimes(handle_, tv) != 0)
+ throw_system_failure("failed settting last access time");
+#endif
+}
+
+time_t last_write_time(file_handle h)
+{
+#ifdef BOOST_IOSTREAMS_WINDOWS
+ FILETIME ft_modification;
+ if (!::GetFileTime(h, NULL, NULL, &ft_modification))
+ throw_system_failure("failed querying last modification time");
+ SYSTEMTIME st_modification;
+ if (!FileTimeToSystemTime(&ft_modification, &st_modification))
+ throw_system_failure("failed querying last modification time");
+ std::tm tm;
+ tm.tm_sec = st_modification.wSecond;
+ tm.tm_min = st_modification.wMinute;
+ tm.tm_hour = st_modification.wHour;
+ tm.tm_mday = st_modification.wDay;
+ tm.tm_mon = st_modification.wMonth - 1;
+ tm.tm_year = st_modification.wYear - 1900;
+ return std::mktime(&tm);
+#else
+ struct stat info;
+ if (::fstat(handle_, &info) == -1)
+ throw_system_failure("failed querying last modification time");
+ return info.st_mtime;
+#endif
+}
+
+void set_last_write_time(file_handle h, time_t tm)
+{
+#ifdef BOOST_IOSTREAMS_WINDOWS
+ struct std::tm* t = std::gmtime(&tm);
+ if (t == NULL)
+ throw_system_failure("failed settting last modification time");
+ SYSTEMTIME st_modification;
+ st_modification.wSecond = t->tm_sec;
+ st_modification.wMinute = t->tm_min;
+ st_modification.wHour = t->tm_hour;
+ st_modification.wDay = t->tm_mday;
+ st_modification.wMonth = t->tm_mon + 1;
+ st_modification.wYear = t->tm_year + 1900;
+ FILETIME ft_modification;
+ if (!SystemTimeToFileTime(&st_modification, &ft_modification))
+ throw_system_failure("failed settting last modification time");
+ if(!::SetFileTime(h, NULL, &ft_modification, NULL))
+ throw_system_failure("failed settting last modification time");
+#else
+ struct stat info;
+ if (::fstat(handle_, &info) == -1)
+ throw_system_failure("failed settting last modification time");
+ struct timeval tv[2];
+ tv[0].tv_sec = info.st_atime;
+ tv[0].tv_usec = 0;
+ tv[1].tv_sec = tm;
+ tv[1].tv_usec = 0;
+ if (futimes(handle_, tv) != 0)
+ throw_system_failure("failed settting last modification time");
+#endif
+}
+
+} } } // End namespaces detail, iostreams, boost.
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk