added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / file.hpp
blobc1f70b50c5d5c3f480629607bc3e826bef7d8353
1 /*
3 Copyright (c) 2003, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_FILE_HPP_INCLUDED
34 #define TORRENT_FILE_HPP_INCLUDED
36 #include <memory>
37 #include <string>
38 #include <vector>
40 #ifdef _MSC_VER
41 #pragma warning(push, 1)
42 #endif
44 #include <boost/noncopyable.hpp>
45 #include <boost/filesystem/path.hpp>
47 #ifdef _MSC_VER
48 #pragma warning(pop)
49 #endif
51 #include "libtorrent/error_code.hpp"
52 #include "libtorrent/size_type.hpp"
53 #include "libtorrent/config.hpp"
55 namespace libtorrent
57 namespace fs = boost::filesystem;
59 class TORRENT_EXPORT file: public boost::noncopyable
61 public:
63 class seek_mode
65 friend class file;
66 private:
67 seek_mode(int v): m_val(v) {}
68 int m_val;
71 static const seek_mode begin;
72 static const seek_mode end;
74 class open_mode
76 friend class file;
77 public:
79 open_mode(): m_mask(0) {}
80 open_mode operator|(open_mode m) const
81 { return open_mode(m.m_mask | m_mask); }
83 open_mode operator&(open_mode m) const
84 { return open_mode(m.m_mask & m_mask); }
86 open_mode operator|=(open_mode m)
88 m_mask |= m.m_mask;
89 return *this;
92 bool operator==(open_mode m) const { return m_mask == m.m_mask; }
93 bool operator!=(open_mode m) const { return m_mask != m.m_mask; }
94 operator bool() const { return m_mask != 0; }
96 private:
98 open_mode(int val): m_mask(val) {}
99 int m_mask;
102 static const open_mode in;
103 static const open_mode out;
105 file();
106 file(fs::path const& p, open_mode m, error_code& ec);
107 ~file();
109 bool open(fs::path const& p, open_mode m, error_code& ec);
110 bool is_open() const;
111 void close();
112 bool set_size(size_type size, error_code& ec);
114 size_type write(const char*, size_type num_bytes, error_code& ec);
115 size_type read(char*, size_type num_bytes, error_code& ec);
117 size_type seek(size_type pos, seek_mode m, error_code& ec);
118 size_type tell(error_code& ec);
120 private:
122 #ifdef TORRENT_WINDOWS
123 HANDLE m_file_handle;
124 #else
125 int m_fd;
126 #endif
127 #ifndef NDEBUG
128 open_mode m_open_mode;
129 #endif
135 #endif // TORRENT_FILE_HPP_INCLUDED