HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 /// @note 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
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  ~UT_OFStream() override
61  {
62  }
63 
65 
66  /// Open with file path
67  /// @{
69  UT_IOS_TYPE mode_type = UT_IOS_BINARY)
70  {
71  initOpen(filename, mode, mode_type);
72  }
73  void open(const std::string &filename, OPEN_MODE mode = IOS_BASE::out,
74  UT_IOS_TYPE mode_type = UT_IOS_BINARY)
75  {
76  initOpen(filename.c_str(), mode, mode_type);
77  }
78  /// @}
79 
80  /// Close the stream
81  void close()
82  {
83  if (!myFileBuf.close())
84  SUPER_CLASS::setstate(IOS_BASE::failbit);
85  }
86 
87  /// Return pointer to internal filebuf object
88  UT_FileBuf *rdbuf() const
89  {
90  // std::ofstream interface requires const_cast
91  return const_cast<UT_FileBuf *>(&myFileBuf);
92  }
93 
94  /// Returns whether stream is currently associated to a file
95  bool is_open() const
96  {
97  return myFileBuf.is_open();
98  }
99 
100 private:
101  void initOpen(const char *filename, OPEN_MODE mode, UT_IOS_TYPE mode_type)
102  {
103  mode |= IOS_BASE::out;
104  if (myFileBuf.open(filename, mode, mode_type) == 0)
105  setstate(IOS_BASE::failbit);
106  }
107 
108 private:
109  UT_FileBuf myFileBuf;
110 };
111 
112 #endif // __UT_OFSTREAM_H_INCLUDED__
GT_API const UT_StringHolder filename
void
Definition: png.h:1083
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
void open(const std::string &filename, OPEN_MODE mode=IOS_BASE::out, UT_IOS_TYPE mode_type=UT_IOS_BINARY)
Definition: UT_OFStream.h:73
#define UT_API
Definition: UT_API.h:14
UT_IOS_TYPE
Definition: UT_IOS.h:15
void close()
Close the stream.
Definition: UT_OFStream.h:81
UT_FileBuf * rdbuf() const
Return pointer to internal filebuf object.
Definition: UT_OFStream.h:88
int open(float queuesize) override
std::ostream SUPER_CLASS
Definition: UT_OFStream.h:29
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
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
Portable replacement for std::ofstream.
Definition: UT_OFStream.h:26
GLenum mode
Definition: glcorearb.h:99
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:95
std::ios_base IOS_BASE
Definition: UT_OFStream.h:30
~UT_OFStream() override
Destructor.
Definition: UT_OFStream.h:60
#define const
Definition: zconf.h:214
UT_OFStream()
Construct with no opened file.
Definition: UT_OFStream.h:34