Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fstream.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_FSTREAM_H_)
21 #define __MITSUBA_CORE_FSTREAM_H_
22 
23 #include <mitsuba/mitsuba.h>
24 #include <mitsuba/core/stream.h>
25 #include <boost/filesystem.hpp>
26 #include <boost/scoped_ptr.hpp>
27 
29 
30 /** \brief Simple \ref Stream implementation for accessing files.
31  *
32  * This class uses POSIX streams on Linux and OSX and the native
33  * WIN32 API when used on Windows.
34  *
35  * \ingroup libcore
36  * \ingroup libpython
37  */
39 public:
40  /// Supported file opening modes
41  enum EFileMode {
42  EReadOnly = 0, ///< rb
43  EReadWrite, ///< rb+
44  ETruncWrite, ///< wb
45  ETruncReadWrite, ///< wb+
46  EAppendWrite, ///< ab
47  EAppendReadWrite ///< ab+
48  };
49 
50  // =============================================================
51  //! @{ \name Constructors
52  // =============================================================
53 
54  /// Create a file stream class with no file open
55  FileStream();
56 
57  /// Create a file stream class and open a file with a given EFileMode
58  explicit FileStream(const fs::path &path, EFileMode mode = EReadOnly);
59 
60  //! @}
61  // =============================================================
62 
63  // =============================================================
64  //! @{ \name File-specific features
65  // =============================================================
66 
67  /// Return the file path
68  const fs::path &getPath() const;
69 
70  /// Open a file with a given open mode
71  void open(const fs::path &filename, EFileMode mode = EReadOnly);
72 
73  /// Close the current file
74  void close();
75 
76  /// Remove the current file
77  void remove();
78 
79  /// Return a string representation
80  std::string toString() const;
81 
82  //! @}
83  // =============================================================
84 
85  // =============================================================
86  //! @{ \name Stream interface
87  // =============================================================
88 
89  void read(void *ptr, size_t size);
90  void write(const void *ptr, size_t size);
91  void seek(size_t pos);
92  size_t getPos() const;
93  size_t getSize() const;
94  void truncate(size_t size);
95  void flush();
96  bool canWrite() const;
97  bool canRead() const;
98 
99  //! @}
100  // =============================================================
101 
102  // =============================================================
103  //! @{ \name Miscellaneous
104  // =============================================================
105 
106  /**
107  * \brief Create a temporary file and return an associated FileStream
108  *
109  * \remark When closing the file stream, the file is automatically
110  * deleted.
111  */
112  static ref<FileStream> createTemporary();
113 
114  /// Initialize the file I/O layer (unicode conversions etc.)
115  static void staticInitialization();
116 
117  /// Release resources taken up by staticInitialization()
118  static void staticShutdown();
119 
120  //! @}
121  // =============================================================
122 
124 protected:
125  /** \brief Virtual destructor
126  *
127  * The destructor frees all resources and closes
128  * the file if it is still open
129  */
130  virtual ~FileStream();
131 private:
132  struct FileStreamPrivate;
133  boost::scoped_ptr<FileStreamPrivate> d;
134 };
135 
137 
138 #endif /* __MITSUBA_CORE_FSTREAM_H_ */
virtual void flush()=0
Flush the stream&#39;s buffers.
static void staticInitialization()
Initializes the built-in reference count debugger (if enabled)
virtual bool canRead() const =0
Can we read from the stream?
ab
Definition: fstream.h:46
EFileMode
Supported file opening modes.
Definition: fstream.h:41
#define MTS_EXPORT_CORE
Definition: getopt.h:29
virtual bool canWrite() const =0
Can we write to the stream?
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
wb+
Definition: fstream.h:45
virtual void read(void *ptr, size_t size)=0
Read a specified amount of data from the stream.
wb
Definition: fstream.h:44
rb+
Definition: fstream.h:43
Simple Stream implementation for accessing files.
Definition: fstream.h:38
Abstract seekable stream class.
Definition: stream.h:58
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Reference counting helper.
Definition: ref.h:40
virtual void write(const void *ptr, size_t size)=0
Write a specified amount of data into the stream.
virtual size_t getPos() const =0
Get the current position inside the stream.
static void staticShutdown()
Free the memory taken by staticInitialization()
#define MTS_NAMESPACE_END
Definition: platform.h:138
virtual std::string toString() const
Return a string representation.
virtual void seek(size_t pos)=0
Seek to a position inside the stream.
virtual size_t getSize() const =0
Return the size of the stream.
virtual void truncate(size_t size)=0
Truncate the stream to a given size.