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 
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 
47 namespace BLOCXX_NAMESPACE
48 {
49 
54 class BLOCXX_COMMON_API File
55 {
56 public:
57  enum ELockType
58  {
64  E_READ_LOCK,
70  E_WRITE_LOCK
71  };
72 
76  File() : m_hdl(BLOCXX_INVALID_FILEHANDLE)
77  {
78  }
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  }
113  size_t read(void* bfr, size_t numberOfBytes, Int64 offset=-1L) const
114  {
115  return FileSystem::read(m_hdl, bfr, numberOfBytes, offset);
116  }
127  size_t write(const void* bfr, size_t numberOfBytes, Int64 offset=-1L)
128  {
129  return FileSystem::write(m_hdl, bfr, numberOfBytes, offset);
130  }
141  Int64 seek(Int64 offset, int whence) const
142  {
143  return FileSystem::seek(m_hdl, offset, whence);
144  }
149  Int64 tell() const
150  {
151  return FileSystem::tell(m_hdl);
152  }
156  void rewind() const
157  {
159  }
163  UInt64 size() const
164  {
165  return FileSystem::fileSize(m_hdl);
166  }
171  int close()
172  {
174  {
175  int rv = FileSystem::close(m_hdl);
177  return rv;
178  }
179  return 0;
180  }
185  int flush()
186  {
187  return FileSystem::flush(m_hdl);
188  }
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 
244  File(FileHandle hdl) : m_hdl(hdl)
245  {
246  }
247 
248 private:
249 
250  FileHandle m_hdl;
251 };
252 
253 } // end namespace BLOCXX_NAMESPACE
254 
255 #endif
256 
BLOCXX_NAMESPACE::FileSystem::seek
BLOCXX_COMMON_API Int64 seek(const FileHandle &hdl, Int64 offset, int whence)
Seek to a given offset within the file.
Definition: PosixFileSystem.cpp:878
BLOCXX_NAMESPACE::FileHandle
int FileHandle
Definition: Types.hpp:164
BLOCXX_NAMESPACE::FileSystem::write
BLOCXX_COMMON_API size_t write(FileHandle hdl, const void *bfr, size_t numberOfBytes, Int64 offset=-1L)
Write data to a file.
Definition: PosixFileSystem.cpp:832
BLOCXX_SAFE_BOOL_IMPL
#define BLOCXX_SAFE_BOOL_IMPL(classname, type, variable, test)
Definition: SafeBool.hpp:88
SafeBool.hpp
BLOCXX_NAMESPACE
Taken from RFC 1321.
Definition: AppenderLogger.cpp:48
BLOCXX_NAMESPACE::BinarySerialization::write
void write(std::streambuf &ostrm, void const *dataOut, size_t dataOutLen)
Definition: BinarySerialization.cpp:194
BLOCXX_NAMESPACE::BinarySerialization::read
void read(std::streambuf &istrm, void *dataIn, size_t dataInLen)
Definition: BinarySerialization.cpp:231
BLOCXX_NAMESPACE::swap
void swap(Array< T > &x, Array< T > &y)
Definition: ArrayImpl.hpp:474
FileSystem.hpp
BLOCXX_NAMESPACE::FileSystem::rewind
BLOCXX_COMMON_API void rewind(const FileHandle &hdl)
Position the file pointer associated with the given file handle to the beginning of the file.
Definition: PosixFileSystem.cpp:969
BLOCXX_NAMESPACE::File::m_hdl
FileHandle m_hdl
Definition: File.hpp:310
BLOCXX_NAMESPACE::FileSystem::close
BLOCXX_COMMON_API int close(const FileHandle &hdl)
Close file handle.
Definition: PosixFileSystem.cpp:983
BLOCXX_NAMESPACE::FileSystem::flush
BLOCXX_COMMON_API int flush(FileHandle &hdl)
Flush any buffered data to the file if buffering supported.
Definition: PosixFileSystem.cpp:997
BLOCXX_NAMESPACE::operator==
bool operator==(const Array< T > &x, const Array< T > &y)
Definition: ArrayImpl.hpp:464
BLOCXX_NAMESPACE::File
The purpose of the File class is to provide an abstraction layer over the platform dependant function...
Definition: File.hpp:85
BLOCXX_INVALID_FILEHANDLE
#define BLOCXX_INVALID_FILEHANDLE
Definition: Types.hpp:167
BLOCXX_NAMESPACE::FileSystem::tell
BLOCXX_COMMON_API Int64 tell(const FileHandle &hdl)
Definition: PosixFileSystem.cpp:915
Types.hpp
BLOCXX_NAMESPACE::FileSystem::read
BLOCXX_COMMON_API size_t read(const FileHandle &hdl, void *bfr, size_t numberOfBytes, Int64 offset=-1L)
Read data from file.
Definition: PosixFileSystem.cpp:786
BLOCXX_NAMESPACE::FileSystem::fileSize
BLOCXX_COMMON_API UInt64 fileSize(FileHandle fh)
Get the size of a file from the file handle.
Definition: PosixFileSystem.cpp:937