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