premiere-libtorrent/src/file.cpp

200 lines
4.9 KiB
C++
Raw Normal View History

2004-01-16 03:57:45 +01:00
/*
Copyright (c) 2003, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
2004-02-19 17:47:12 +01:00
#include <fstream>
2004-01-16 03:57:45 +01:00
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
#include "libtorrent/file.hpp"
namespace fs = boost::filesystem;
namespace
{
2004-01-16 16:07:01 +01:00
enum { mode_in = 1, mode_out = 2 };
2004-01-16 03:57:45 +01:00
std::ios_base::openmode map_open_mode(fs::path const& p, int m)
{
std::ios_base::openmode ret(std::ios_base::binary);
2004-01-16 16:07:01 +01:00
if ((m & mode_in) || fs::exists(p)) ret |= std::ios_base::in;
if (m & mode_out) ret |= std::ios_base::out;
2004-01-16 03:57:45 +01:00
return ret;
}
}
namespace libtorrent
{
2004-01-16 16:07:01 +01:00
const file::open_mode file::in(mode_in);
const file::open_mode file::out(mode_out);
2004-01-16 03:57:45 +01:00
const file::seek_mode file::begin(1);
const file::seek_mode file::end(2);
struct file::impl
{
2004-01-16 16:07:01 +01:00
impl(): m_open_mode(0) {}
2004-01-16 03:57:45 +01:00
impl(fs::path const& path, int mode)
2004-02-19 17:47:12 +01:00
#if defined(_MSC_VER) && _MSC_VER < 1300
: m_file(path.native_file_string().c_str(), map_open_mode(path, mode))
#else
2004-01-16 03:57:45 +01:00
: m_file(path, map_open_mode(path, mode))
2004-02-19 17:47:12 +01:00
#endif
2004-01-16 16:07:01 +01:00
, m_open_mode(mode)
2004-01-16 03:57:45 +01:00
{
2004-01-16 16:07:01 +01:00
assert(mode == mode_in ||mode == mode_out);
2004-01-16 03:57:45 +01:00
if (m_file.fail())
throw file_error("open failed '" + path.native_file_string() + "'");
}
void open(fs::path const& path, int mode)
{
if (m_file.is_open()) m_file.close();
m_file.clear();
2004-02-19 12:35:08 +01:00
#if defined(_MSC_VER) && _MSC_VER < 1300
m_file.open(path.native_file_string().c_str(), map_open_mode(path, mode));
#else
2004-01-16 03:57:45 +01:00
m_file.open(path, map_open_mode(path, mode));
2004-02-19 12:35:08 +01:00
#endif
2004-01-16 16:07:01 +01:00
m_open_mode = mode;
assert(mode == mode_in ||mode == mode_out);
2004-01-16 03:57:45 +01:00
if (m_file.fail())
throw file_error("open failed '" + path.native_file_string() + "'");
}
void close()
{
m_file.close();
2004-01-16 16:07:01 +01:00
m_open_mode = 0;
2004-01-16 03:57:45 +01:00
}
size_type read(char* buf, size_type num_bytes)
{
2004-01-16 16:07:01 +01:00
assert(m_open_mode == mode_in);
assert(m_file.is_open());
2004-01-16 03:57:45 +01:00
// TODO: split the read if num_bytes > 2 gig
m_file.read(buf, num_bytes);
return m_file.gcount();
}
size_type write(const char* buf, size_type num_bytes)
{
2004-01-16 16:07:01 +01:00
assert(m_open_mode == mode_out);
assert(m_file.is_open());
2004-01-16 03:57:45 +01:00
// TODO: split the write if num_bytes > 2 gig
2004-01-16 13:43:23 +01:00
std::ostream::pos_type a = m_file.tellp();
2004-01-16 03:57:45 +01:00
m_file.write(buf, num_bytes);
2004-01-16 13:43:23 +01:00
return m_file.tellp() - a;
2004-01-16 03:57:45 +01:00
}
void seek(size_type offset, int m)
{
2004-01-16 16:07:01 +01:00
assert(m_open_mode);
assert(m_file.is_open());
2004-01-16 03:57:45 +01:00
std::ios_base::seekdir d =
(m == 1) ? std::ios_base::beg : std::ios_base::end;
2004-01-16 16:07:01 +01:00
m_file.clear();
if (m_open_mode == mode_in)
m_file.seekg(offset, d);
else
m_file.seekp(offset, d);
2004-01-16 03:57:45 +01:00
}
size_type tell()
{
2004-01-16 16:07:01 +01:00
assert(m_open_mode);
assert(m_file.is_open());
m_file.clear();
if (m_open_mode == mode_in)
2004-01-31 16:07:49 +01:00
return static_cast<std::streamoff>(m_file.tellg());
2004-01-16 16:07:01 +01:00
else
2004-01-31 16:07:49 +01:00
return static_cast<std::streamoff>(m_file.tellp());
2004-01-16 03:57:45 +01:00
}
2004-02-19 17:47:12 +01:00
#if defined(_MSC_VER) && _MSC_VER < 1300
std::fstream m_file;
#else
2004-01-16 03:57:45 +01:00
fs::fstream m_file;
2004-02-19 17:47:12 +01:00
#endif
2004-01-16 16:07:01 +01:00
int m_open_mode;
2004-01-16 03:57:45 +01:00
};
2004-01-16 13:43:23 +01:00
// pimpl forwardings
2004-01-16 03:57:45 +01:00
file::file() : m_impl(new impl()) {}
file::file(boost::filesystem::path const& p, file::open_mode m)
: m_impl(new impl(p, m.m_mask))
{}
file::~file() {}
void file::open(boost::filesystem::path const& p, file::open_mode m)
{
m_impl->open(p, m.m_mask);
}
void file::close()
{
m_impl->close();
}
size_type file::write(const char* buf, size_type num_bytes)
2004-01-16 03:57:45 +01:00
{
return m_impl->write(buf, num_bytes);
}
size_type file::read(char* buf, size_type num_bytes)
2004-01-16 03:57:45 +01:00
{
return m_impl->read(buf, num_bytes);
}
void file::seek(size_type pos, file::seek_mode m)
2004-01-16 03:57:45 +01:00
{
m_impl->seek(pos, m.m_val);
}
size_type file::tell()
2004-01-16 03:57:45 +01:00
{
return m_impl->tell();
}
}