HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_FPStream.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_FPStream.h ( UT Library, C++)
7  *
8  * COMMENTS: Stream using FILE * objects instead of file descriptors
9  *
10  * In general, use this class instead of UT_FDStream if possible. This is
11  * because this class is buffered while the FDStreams are not (and thus
12  * have very poor performance).
13  */
14 
15 #ifndef __UT_FPStream__
16 #define __UT_FPStream__
17 
18 #include "UT_API.h"
19 #include "UT_Assert.h"
20 #include <SYS/SYS_Stdio.h>
21 
22 #include <istream>
23 #include <ostream>
24 #include <stdio.h>
25 #include <streambuf>
26 #include <string.h>
27 
28 
29 /************************************************************
30  * UT_OFPStream
31  * - a stream that writes on a file descriptor
32  ************************************************************/
33 
34 class UT_API UT_FPOutBuf : public std::streambuf
35 {
36 public:
37  UT_FPOutBuf (FILE *_fp)
38  : myFP(_fp)
39  {
40  }
41  void close()
42  {
43  UT_ASSERT(myFP);
44  ::fclose(myFP);
45  myFP = 0;
46  }
47 
48 protected:
49  // write one character
50  virtual int_type overflow(int_type c)
51  {
52  UT_ASSERT(myFP);
53  return putc(c, myFP);
54  }
55 
56  // In order to allow tellg to work, we need to provide seekoff.
57  virtual pos_type seekoff(off_type _Off,
58  std::ios_base::seekdir _Way,
59  std::ios_base::openmode /*_Mode*/ = std::ios::out)
60  {
61  int whence = SEEK_CUR;
62  UT_ASSERT(myFP);
63  switch (_Way)
64  {
65  case std::ios_base::beg:
66  whence = SEEK_SET;
67  break;
68  case std::ios_base::end:
69  whence = SEEK_END;
70  break;
71  case std::ios_base::cur:
72  default:
73  whence = SEEK_CUR;
74  break;
75  }
76  if (SYSfseek(myFP, _Off, whence) < 0)
77  return -1;
78  return (pos_type)SYSftell(myFP);
79  }
80  virtual pos_type seekpos(pos_type _Off,
81  std::ios_base::openmode /*_Mode*/ = std::ios::out)
82  {
83  if (SYSfseek(myFP, _Off, SEEK_SET) < 0)
84  return -1;
85  return (pos_type)SYSftell(myFP);
86  }
87 
88  // write multiple characters
89  virtual std::streamsize xsputn (const char* s, std::streamsize num)
90  {
91  UT_ASSERT(myFP);
92  return fwrite(s, 1, num, myFP);
93  }
94 
95 protected:
96  FILE *myFP; // My file pointer
97 };
98 
99 class UT_API UT_OFPStream : public std::ostream
100 {
101  protected:
103  public:
104  UT_OFPStream (FILE *fp) : std::ostream(0), buf(fp)
105  {
106  rdbuf(&buf);
107  }
108 
109  // Streams cannot be moved
110  UT_OFPStream(UT_OFPStream &&) = delete;
111 
112  void close() { buf.close(); }
113 };
114 
115 #endif
116 
#define SEEK_CUR
Definition: zconf.h:471
UT_FPOutBuf(FILE *_fp)
Definition: UT_FPStream.h:37
#define SEEK_END
Definition: zconf.h:472
#define UT_API
Definition: UT_API.h:13
FILE * myFP
Definition: UT_FPStream.h:96
virtual pos_type seekoff(off_type _Off, std::ios_base::seekdir _Way, std::ios_base::openmode=std::ios::out)
Definition: UT_FPStream.h:57
UT_FPOutBuf buf
Definition: UT_FPStream.h:102
png_FILE_p fp
Definition: png.h:2028
virtual std::streamsize xsputn(const char *s, std::streamsize num)
Definition: UT_FPStream.h:89
GLuint GLuint end
Definition: glcorearb.h:474
void close()
Definition: UT_FPStream.h:41
virtual int_type overflow(int_type c)
Definition: UT_FPStream.h:50
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
#define SEEK_SET
Definition: zconf.h:470
void close()
Definition: UT_FPStream.h:112
png_infop png_uint_32 int num
Definition: png.h:2158
UT_OFPStream(FILE *fp)
Definition: UT_FPStream.h:104
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
virtual pos_type seekpos(pos_type _Off, std::ios_base::openmode=std::ios::out)
Definition: UT_FPStream.h:80