HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_FileBuf.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_FileBuf.h (UT Library, C++)
7  *
8  * COMMENTS: Portable replacement for std::filebuf
9  */
10 
11 #ifndef __UT_FILEBUF_H_INCLUDED__
12 #define __UT_FILEBUF_H_INCLUDED__
13 
14 #include "UT_API.h"
15 #include "UT_Assert.h"
16 #include "UT_IOS.h"
17 #include "UT_NonCopyable.h"
18 #include "UT_NTStreamUtil.h"
19 
20 #include <SYS/SYS_Stdio.h>
21 
22 #include <stdio.h>
23 #include <streambuf>
24 #include <string>
25 
26 
27 class UT_API UT_FileBuf : public std::streambuf
28 {
29 public:
30  typedef std::streambuf SUPER_CLASS;
31  typedef std::ios_base IOS_BASE;
32  typedef std::ios_base::openmode OPEN_MODE;
33  typedef traits_type::int_type int_type;
34  typedef traits_type::pos_type pos_type;
35  typedef traits_type::off_type off_type;
36 
37 
39  : myFile(NULL)
40  {
41  }
42 
43  ~UT_FileBuf() override
44  {
45  if (myFile)
46  close();
47  }
48 
50 
51  /// Test if there exists an opened file
52  bool is_open() const
53  {
54  return (myFile != NULL);
55  }
56 
57  /// Open the given file with SYSfopen and return a pointer to result. The
58  /// returned pointer is valid until the next open operation, the next close
59  /// operation, or this object destructs.
60  /// @{
61  FILE* open(const char *filename, OPEN_MODE mode,
62  UT_IOS_TYPE mode_type = UT_IOS_BINARY);
63 
65  UT_IOS_TYPE mode_type = UT_IOS_BINARY)
66  { return open(filename.c_str(), mode, mode_type); }
67  /// @}
68 
69  /// @brief Create filebuf from UTgetTmpFile() for read/write.
70  /// @note This is a SESI-specific extension.
71  FILE* openTmpFile();
72 
73  /// Close the currently opened file (if any)
74  UT_FileBuf* close();
75 
76  /// @brief Attempt to set the opened (writable) file as sparse.
77  /// The exact behaviour is platform-specific depending on the underlying
78  /// file system. So this function provides a hint at best.
79  /// @note This is a SESI-specific extension. This works on most filesystems
80  /// on Linux, Windows and OSX post-Sierra.
81  bool setFileAsSparse();
82 
83 protected:
84 
85  // [BUFFER] Set the buffer for the stream
86  SUPER_CLASS*
87  setbuf(char* buf, std::streamsize n) override
88  {
89  if (!myFile)
90  return NULL;
91  int mode = (buf == NULL && n == 0) ? _IONBF : _IOFBF;
92  return (::setvbuf(myFile, buf, mode, n) == 0) ? this : NULL;
93  }
94 
95  // [BUFFER] Set internal position to relative position
96  pos_type
97  seekoff(off_type off, IOS_BASE::seekdir way,
98  OPEN_MODE /*which*/ = IOS_BASE::in | IOS_BASE::out) override
99  {
100  int whence = SEEK_CUR;
101  UT_ASSERT(myFile);
102  switch (way)
103  {
104  case IOS_BASE::beg:
105  whence = SEEK_SET;
106  break;
107  case IOS_BASE::end:
108  whence = SEEK_END;
109  break;
110  case IOS_BASE::cur:
111  default:
112  whence = SEEK_CUR;
113  break;
114  }
115  return (SYSfseek(myFile, off, whence) == 0)
116  ? pos_type(SYSftell(myFile))
117  : pos_type(off_type(-1));
118  }
119 
120  // [BUFFER] Set internal position to absolute position
121  pos_type
123  OPEN_MODE /*which*/ = IOS_BASE::in | IOS_BASE::out) override
124  {
125  UT_ASSERT(myFile);
126  return (SYSfseek(myFile, off, SEEK_SET) == 0)
127  ? pos_type(SYSftell(myFile))
128  : pos_type(off_type(-1));
129  }
130 
131  // [BUFFER] Flush buffer to disk
132  int
133  sync() override
134  {
135  // ostream::flush calls this indiscriminately regardless of the
136  // stream's error state.
137  if (!myFile)
138  return -1;
139 
140  return (::fflush(myFile) == 0) ? 0 : -1;
141  }
142 
143  // [OUTPUT] Write single character
144  int_type
145  overflow(int_type ch = traits_type::eof()) override
146  {
147  if (::putc(ch, myFile) == ch)
148  return ch;
149  return traits_type::eof();
150 
151  }
152  // [OUTPUT] Write multiple cahracters
153  std::streamsize
154  xsputn(const char* buf, std::streamsize n) override
155  {
156  UT_ASSERT(myFile && n >= 0);
157  return ::fwrite(buf, sizeof(char), n, myFile);
158  }
159 
160  // [INPUT] Read multiple characters at once
161  std::streamsize
162  xsgetn(char *buf, std::streamsize n) override
163  {
164  UT_ASSERT(myFile && n >= 0);
165  return ::fread(buf, sizeof(char), n, myFile);
166  }
167  // [INPUT] Read a character, without advancing the position
168  int_type
169  underflow() override
170  {
171  int_type ch = ::getc(myFile);
172  if (ch != EOF)
173  {
174  if (::ungetc(ch, myFile) == ch)
175  return ch;
176  }
177  return traits_type::eof();
178  }
179  // [INPUT] Read a character and advance the position
180  int_type
181  uflow() override
182  {
183  int_type ch = ::getc(myFile);
184  if (ch != EOF)
185  return ch;
186  return traits_type::eof();
187  }
188  // [INPUT] Put back a character
189  int_type
190  pbackfail(int_type ch = traits_type::eof()) override
191  {
192  UT_ASSERT(myFile);
193  if (::ungetc(ch, myFile) == ch)
194  return ch;
195  return traits_type::eof();
196  }
197 
198 private:
199  FILE * myFile;
200 
201  static const char *const theBinaryCharModes[];
202  static const char *const theAsciiCharModes[];
203  static const OPEN_MODE theOpenModes[];
204 };
205 
206 #endif // __UT_FILEBUF_H_INCLUDED__
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
GT_API const UT_StringHolder filename
#define SEEK_CUR
Definition: zconf.h:181
int_type overflow(int_type ch=traits_type::eof()) override
Definition: UT_FileBuf.h:145
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
#define SEEK_END
Definition: zconf.h:182
#define UT_API
Definition: UT_API.h:14
UT_IOS_TYPE
Definition: UT_IOS.h:15
SUPER_CLASS * setbuf(char *buf, std::streamsize n) override
Definition: UT_FileBuf.h:87
int_type uflow() override
Definition: UT_FileBuf.h:181
void close() override
pos_type seekoff(off_type off, IOS_BASE::seekdir way, OPEN_MODE=IOS_BASE::in|IOS_BASE::out) override
Definition: UT_FileBuf.h:97
GLdouble n
Definition: glcorearb.h:2008
int open(float queuesize) override
GLuint GLuint end
Definition: glcorearb.h:475
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
pos_type seekpos(pos_type off, OPEN_MODE=IOS_BASE::in|IOS_BASE::out) override
Definition: UT_FileBuf.h:122
int sync() override
Definition: UT_FileBuf.h:133
std::streamsize xsputn(const char *buf, std::streamsize n) override
Definition: UT_FileBuf.h:154
FILE * open(const std::string &filename, OPEN_MODE mode, UT_IOS_TYPE mode_type=UT_IOS_BINARY)
Definition: UT_FileBuf.h:64
GLenum mode
Definition: glcorearb.h:99
std::ios_base::openmode OPEN_MODE
Definition: UT_FileBuf.h:32
#define SEEK_SET
Definition: zconf.h:180
traits_type::off_type off_type
Definition: UT_FileBuf.h:35
int_type underflow() override
Definition: UT_FileBuf.h:169
std::streambuf SUPER_CLASS
Definition: UT_FileBuf.h:30
traits_type::pos_type pos_type
Definition: UT_FileBuf.h:34
traits_type::int_type int_type
Definition: UT_FileBuf.h:33
std::streamsize xsgetn(char *buf, std::streamsize n) override
Definition: UT_FileBuf.h:162
~UT_FileBuf() override
Definition: UT_FileBuf.h:43
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
int_type pbackfail(int_type ch=traits_type::eof()) override
Definition: UT_FileBuf.h:190
std::ios_base IOS_BASE
Definition: UT_FileBuf.h:31