blocxx
File.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Quest Software, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Quest Software, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
38
39#ifndef BLOCXX_FILE_HPP_INCLUDE_GUARD_
40#define BLOCXX_FILE_HPP_INCLUDE_GUARD_
41#include "blocxx/BLOCXX_config.h"
42#include "blocxx/Types.hpp"
43#include "blocxx/FileSystem.hpp"
44#include "blocxx/SafeBool.hpp"
45#include <algorithm> // for std::swap
46
47namespace BLOCXX_NAMESPACE
48{
49
54class BLOCXX_COMMON_API File
55{
56public:
72
79
83 File(const File& x);
87 ~File();
93 File& operator= (const File& x)
94 {
95 File(x).swap(*this);
96 return *this;
97 }
98 void swap(File& x)
99 {
100 std::swap(m_hdl, x.m_hdl);
101 }
102
113 size_t read(void* bfr, size_t numberOfBytes, Int64 offset=-1L) const
114 {
115 return FileSystem::read(m_hdl, bfr, numberOfBytes, offset);
116 }
117
127 size_t write(const void* bfr, size_t numberOfBytes, Int64 offset=-1L)
128 {
129 return FileSystem::write(m_hdl, bfr, numberOfBytes, offset);
130 }
131
141 Int64 seek(Int64 offset, int whence) const
142 {
143 return FileSystem::seek(m_hdl, offset, whence);
144 }
145
149 Int64 tell() const
150 {
151 return FileSystem::tell(m_hdl);
152 }
153
156 void rewind() const
157 {
159 }
160
163 UInt64 size() const
164 {
166 }
167
171 int close()
172 {
174 {
175 int rv = FileSystem::close(m_hdl);
177 return rv;
178 }
179 return 0;
180 }
181
185 int flush()
186 {
187 return FileSystem::flush(m_hdl);
188 }
189
204 int getLock(ELockType type = E_WRITE_LOCK);
220 int tryLock(ELockType type = E_WRITE_LOCK);
227 int unlock();
228
233
234
239 bool operator==(const File& rhs)
240 {
241 return m_hdl == rhs.m_hdl;
242 }
243
245 {
246 }
247
248private:
249
251};
252
253} // end namespace BLOCXX_NAMESPACE
254
255#endif
256
#define BLOCXX_SAFE_BOOL_IMPL(classname, type, variable, test)
Definition SafeBool.hpp:58
#define BLOCXX_INVALID_FILEHANDLE
Definition Types.hpp:137
The purpose of the File class is to provide an abstraction layer over the platform dependant function...
Definition File.hpp:55
File(FileHandle hdl)
Definition File.hpp:244
int close()
Close the underlying file object.
Definition File.hpp:171
Int64 seek(Int64 offset, int whence) const
Seek to a given offset within the file.
Definition File.hpp:141
int flush()
Flush any buffered data to the file.
Definition File.hpp:185
size_t read(void *bfr, size_t numberOfBytes, Int64 offset=-1L) const
Read from the underlying file.
Definition File.hpp:113
Int64 tell() const
Definition File.hpp:149
size_t write(const void *bfr, size_t numberOfBytes, Int64 offset=-1L)
Write to the underlying file.
Definition File.hpp:127
@ E_WRITE_LOCK
Flag to place an write / exclusive lock.
Definition File.hpp:70
@ E_READ_LOCK
Flag to place a read / shared lock.
Definition File.hpp:64
File()
Create a NULL File object.
Definition File.hpp:76
void rewind() const
Position the file pointer to the beginning of the file.
Definition File.hpp:156
void swap(File &x)
Definition File.hpp:98
UInt64 size() const
Current size of file.
Definition File.hpp:163
BLOCXX_COMMON_API void rewind(const FileHandle &hdl)
Position the file pointer associated with the given file handle to the beginning of the file.
BLOCXX_COMMON_API size_t read(const FileHandle &hdl, void *bfr, size_t numberOfBytes, Int64 offset=-1L)
Read data from file.
BLOCXX_COMMON_API Int64 seek(const FileHandle &hdl, Int64 offset, int whence)
Seek to a given offset within the file.
BLOCXX_COMMON_API int close(const FileHandle &hdl)
Close file handle.
BLOCXX_COMMON_API size_t write(FileHandle hdl, const void *bfr, size_t numberOfBytes, Int64 offset=-1L)
Write data to a file.
BLOCXX_COMMON_API Int64 tell(const FileHandle &hdl)
BLOCXX_COMMON_API UInt64 fileSize(FileHandle fh)
Get the size of a file from the file handle.
BLOCXX_COMMON_API int flush(FileHandle &hdl)
Flush any buffered data to the file if buffering supported.
Taken from RFC 1321.