HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_OFStream.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_OFStream.h (UT Library, C++)
7  *
8  * COMMENTS: Portable replacement for std::ofstream
9  */
10 
11 #ifndef __UT_OFSTREAM_H_INCLUDED__
12 #define __UT_OFSTREAM_H_INCLUDED__
13 
14 #include "UT_API.h"
15 #include "UT_NonCopyable.h"
16 #include "UT_FileBuf.h"
17 
18 #include <ostream>
19 #include <string>
20 
21 
22 /// @brief Portable replacement for std::ofstream
23 ///
24 /// @notes Files will always be opened in binary mode to avoid platform
25 /// differences for line endings.
26 class UT_API UT_OFStream : public std::ostream, UT_NonCopyable
27 {
28 public:
29  typedef std::ostream SUPER_CLASS;
30  typedef std::ios_base IOS_BASE;
31  typedef std::ios_base::openmode OPEN_MODE;
32 
33  /// Construct with no opened file
35  : SUPER_CLASS(&myFileBuf)
36  {
37  }
38 
39  /// Construct with file path
40  /// @{
41  explicit UT_OFStream(
42  const char *filename,
43  OPEN_MODE mode = IOS_BASE::out,
44  UT_IOS_TYPE mode_type = UT_IOS_BINARY)
45  : SUPER_CLASS(&myFileBuf)
46  {
47  initOpen(filename, mode, mode_type);
48  }
49  explicit UT_OFStream(
50  const std::string &filename,
51  OPEN_MODE mode = IOS_BASE::out,
52  UT_IOS_TYPE mode_type = UT_IOS_BINARY)
53  : SUPER_CLASS(&myFileBuf)
54  {
55  initOpen(filename.c_str(), mode, mode_type);
56  }
57  /// @}
58 
59  /// Destructor
60  virtual ~UT_OFStream()
61  {
62  }
63 
64  /// Open with file path
65  /// @{
66  void open(const char *filename, OPEN_MODE mode = IOS_BASE::out,
67  UT_IOS_TYPE mode_type = UT_IOS_BINARY)
68  {
69  initOpen(filename, mode, mode_type);
70  }
71  void open(const std::string &filename, OPEN_MODE mode = IOS_BASE::out,
72  UT_IOS_TYPE mode_type = UT_IOS_BINARY)
73  {
74  initOpen(filename.c_str(), mode, mode_type);
75  }
76  /// @}
77 
78  /// Close the stream
79  void close()
80  {
81  if (!myFileBuf.close())
82  SUPER_CLASS::setstate(IOS_BASE::failbit);
83  }
84 
85  /// Return pointer to internal filebuf object
86  UT_FileBuf *rdbuf() const
87  {
88  // std::ofstream interface requires const_cast
89  return const_cast<UT_FileBuf *>(&myFileBuf);
90  }
91 
92  /// Returns whether stream is currently associated to a file
93  bool is_open() const
94  {
95  return myFileBuf.is_open();
96  }
97 
98 private:
99  void initOpen(const char *filename, OPEN_MODE mode, UT_IOS_TYPE mode_type)
100  {
101  mode |= IOS_BASE::out;
102  if (myFileBuf.open(filename, mode, mode_type) == 0)
103  setstate(IOS_BASE::failbit);
104  }
105 
106 private:
107  UT_FileBuf myFileBuf;
108 };
109 
110 #endif // __UT_OFSTREAM_H_INCLUDED__
virtual ~UT_OFStream()
Destructor.
Definition: UT_OFStream.h:60
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
void open(const std::string &filename, OPEN_MODE mode=IOS_BASE::out, UT_IOS_TYPE mode_type=UT_IOS_BINARY)
Definition: UT_OFStream.h:71
#define UT_API
Definition: UT_API.h:12
UT_IOS_TYPE
Definition: UT_IOS.h:15
void close()
Close the stream.
Definition: UT_OFStream.h:79
UT_FileBuf * rdbuf() const
Return pointer to internal filebuf object.
Definition: UT_OFStream.h:86
std::ostream SUPER_CLASS
Definition: UT_OFStream.h:29
std::ios_base::openmode OPEN_MODE
Definition: UT_OFStream.h:31
UT_OFStream(const char *filename, OPEN_MODE mode=IOS_BASE::out, UT_IOS_TYPE mode_type=UT_IOS_BINARY)
Definition: UT_OFStream.h:41
void open(const char *filename, OPEN_MODE mode=IOS_BASE::out, UT_IOS_TYPE mode_type=UT_IOS_BINARY)
Definition: UT_OFStream.h:66
Portable replacement for std::ofstream.
Definition: UT_OFStream.h:26
GLenum mode
Definition: glcorearb.h:98
UT_OFStream(const std::string &filename, OPEN_MODE mode=IOS_BASE::out, UT_IOS_TYPE mode_type=UT_IOS_BINARY)
Definition: UT_OFStream.h:49
bool is_open() const
Returns whether stream is currently associated to a file.
Definition: UT_OFStream.h:93
std::ios_base IOS_BASE
Definition: UT_OFStream.h:30
UT_OFStream()
Construct with no opened file.
Definition: UT_OFStream.h:34