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  int_type overflow(int_type c) override
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  pos_type seekoff(off_type _Off,
58  std::ios_base::seekdir _Way,
59  std::ios_base::openmode
60  /*_Mode*/ = std::ios::out) override
61  {
62  int whence = SEEK_CUR;
63  UT_ASSERT(myFP);
64  switch (_Way)
65  {
66  case std::ios_base::beg:
67  whence = SEEK_SET;
68  break;
69  case std::ios_base::end:
70  whence = SEEK_END;
71  break;
72  case std::ios_base::cur:
73  default:
74  whence = SEEK_CUR;
75  break;
76  }
77  if (SYSfseek(myFP, _Off, whence) < 0)
78  return -1;
79  return (pos_type)SYSftell(myFP);
80  }
81  pos_type seekpos(pos_type _Off,
82  std::ios_base::openmode
83  /*_Mode*/ = std::ios::out) override
84  {
85  if (SYSfseek(myFP, _Off, SEEK_SET) < 0)
86  return -1;
87  return (pos_type)SYSftell(myFP);
88  }
89 
90  // write multiple characters
91  std::streamsize xsputn(const char* s, std::streamsize num) override
92  {
93  UT_ASSERT(myFP);
94  return fwrite(s, 1, num, myFP);
95  }
96 
97 protected:
98  FILE *myFP; // My file pointer
99 };
100 
101 class UT_API UT_OFPStream : public std::ostream
102 {
103  protected:
105  public:
106  UT_OFPStream (FILE *fp) : std::ostream(0), buf(fp)
107  {
108  rdbuf(&buf);
109  }
110 
111  // Streams cannot be moved
112  UT_OFPStream(UT_OFPStream &&) = delete;
113 
114  void close() { buf.close(); }
115 };
116 
117 #endif
118 
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
#define SEEK_CUR
Definition: zconf.h:471
UT_FPOutBuf(FILE *_fp)
Definition: UT_FPStream.h:37
#define SEEK_END
Definition: zconf.h:472
pos_type seekoff(off_type _Off, std::ios_base::seekdir _Way, std::ios_base::openmode=std::ios::out) override
Definition: UT_FPStream.h:57
GLdouble s
Definition: glad.h:3009
#define UT_API
Definition: UT_API.h:14
FILE * myFP
Definition: UT_FPStream.h:98
UT_FPOutBuf buf
Definition: UT_FPStream.h:104
GLuint GLuint end
Definition: glcorearb.h:475
void close()
Definition: UT_FPStream.h:41
int_type overflow(int_type c) override
Definition: UT_FPStream.h:50
#define SEEK_SET
Definition: zconf.h:470
void close()
Definition: UT_FPStream.h:114
UT_OFPStream(FILE *fp)
Definition: UT_FPStream.h:106
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
std::streamsize xsputn(const char *s, std::streamsize num) override
Definition: UT_FPStream.h:91
pos_type seekpos(pos_type _Off, std::ios_base::openmode=std::ios::out) override
Definition: UT_FPStream.h:81