HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_NTStreamUtil.h
Go to the documentation of this file.
1 
2 #ifndef __UT_NTStreamUtil_H__
3 #define __UT_NTStreamUtil_H__
4 
5 #include "UT_API.h"
6 #include "UT_TmpDir.h"
7 #include "UT_Assert.h"
8 #include "UT_StringHolder.h"
9 #include <SYS/SYS_Deprecated.h>
10 #include <SYS/SYS_Types.h>
11 #include <SYS/SYS_Math.h>
12 #include <stdio.h>
13 #include <fstream>
14 #include <iosfwd>
15 
16 class UT_OStream;
17 class UT_OStringStream;
18 class UT_WorkBuffer;
19 class UT_String;
20 class UT_IStream;
21 
22 
23 #ifdef WIN32
24  #define NTBinaryMode std::ios::binary
25 #else
26  #define NTBinaryMode ((std::ios::openmode)0)
27 #endif // WIN32
28 
29 #define UT_NoCreate ((std::ios::openmode)0)
30 
31 
32 typedef enum {
38 
39 typedef int UT_EnumType;
40 
41 
42 #ifdef WIN32
43 
44  void *getNewNamedPipe( char *pipeName );
45 
46  UT_API FILE *UnixPWrite( const char *cmd, const char *file );
47  UT_API FILE *UnixPOpen( const char *file, const char *mode );
48  UT_API void *UnixPOpen( const char *file, void **in, void **out,
49  void **err );
50  UT_API int UnixPClose( FILE *file, int block );
51  inline FILE *popen( const char *name, const char *mode )
52  { return UnixPOpen( name, mode ); }
53  inline int pclose( FILE *f, int block = 1 )
54  { return UnixPClose( f, block ); }
55 
56  UT_API const char *UTgetSocketErrorString(int err);
57 
58 #endif // WIN32
59 
60 
61 ///
62 /// We build our own stream buffer to work around a shortcoming
63 /// in the C++ standard. pbump() and pcount() are defined to use
64 /// int, not streamsize, so much sadness occurs as you cross 2gb
65 /// boundaries. See Bug: 45419
66 ///
67 /// Note this isn't a buffered implementation - we don't invoke setpbuf
68 /// to update pbase, etc. The logic is that may bring us back to the
69 /// broken pbump land. Hopefully implementations use xsputn anyways.
70 ///
71 /// exints are chosen rather than streamsize or int_type or pos_type
72 /// because I have lost all faith in how those are defined.
73 ///
74 class UT_API UT_OStrStreamBuf : public std::streambuf
75 {
76 public:
78  explicit UT_OStrStreamBuf(exint initialsize);
79  UT_OStrStreamBuf(char *pch, exint len);
80  virtual ~UT_OStrStreamBuf();
81 
82 protected:
83  // write one character
84  virtual int_type overflow(int_type c);
85 
86  // In order to allow tellg to work, we need to provide seekoff.
87  virtual pos_type seekoff(off_type _Off,
88  std::ios_base::seekdir _Way,
89  std::ios_base::openmode /*_Mode*/ = std::ios::out);
90  virtual pos_type seekpos(pos_type _Off,
91  std::ios_base::openmode _Mode = std::ios::out);
92 
93  // write multiple characters
94  virtual std::streamsize xsputn (const char* s, std::streamsize num);
95 
96 public:
97  // Crappy strstreambuf equivalent functions
98  char *str()
99  {
100  freeze(true);
101  return myBuf;
102  }
103 
104  void freeze(bool isfrozen)
105  {
106  myOwnBuf = !isfrozen;
107  }
108 
109  // Note this is the current put position, not the entries!
110  exint pcount() const
111  {
112  return myOffset;
113  }
114 
115 protected:
116  bool resizeBuf(exint minnewsize);
117 
118  exint mySize, myEntries, myOffset;
119  bool myOwnBuf;
120  char *myBuf;
121 };
122 
123 /// Our portable and much improved strstream
124 /// Uses an exint buffer to allow greater than 2gb streams.
125 class UT_API UT_OStrStream : public std::ostream
126 {
127 public:
129  UT_OStrStream();
130 
132  explicit UT_OStrStream(exint initialsize);
133 
134  /// The stream does *not* gain ownership of this buffer!
135  /// This means it will not resize if it overflows this buffer.
136 #if !defined(WIN32)
138  UT_OStrStream(char *pch, exint nLength,
139  std::ios::openmode = std::ios::out);
140 #else
142  UT_OStrStream(char *pch, exint nLength, int nMode = std::ios::out);
143 #endif
144 
145  /// Note: This is old-school streams!
146  /// You own the result of str() and must either free() it or invoke
147  /// freeze(false)!
148  char *str() { return myBuf.str(); }
149  void freeze(bool isfrozen) { myBuf.freeze(isfrozen); }
150 
151  exint pcount() const
152  {
153  return myBuf.pcount();
154  }
155 
156  // Return this properly cast so people can get at our freeze, etc.
157  UT_OStrStreamBuf *rdbuf() { return &myBuf; }
158 
159  virtual ~UT_OStrStream() {}
160 
161 private:
162  UT_OStrStreamBuf myBuf;
163 };
164 
165 // A temporary stream that uses UT_OStrStream by default unless the buffer
166 // size is so big that it should be saved on disk. Tries to balance
167 // performance with the practicality of having large buffers stored
168 // in memory.
169 //
170 // NOTE: this class is not thread safe
171 //
173 {
174 public:
175  // construct a temporary stream
176  // size_estimate - apporximate anticipated size of the buffer. This
177  // value is used to determine whether the buffer shaould be
178  // kept in memory or saved to hard drive.
179  UT_TempStream(exint size_estimate);
180  ~UT_TempStream();
181 
182  // accesses the output stream for writing to the stream
183  std::ostream &getStream();
184 
185  // writes out the temp stream to an out stream
186  void copyStream( std::ostream &out );
187 
188 private:
189  // TODO: implement a default constructor that uses UT_OStrStream
190  // by default and automatically switches to file streams
191  UT_TempStream();
192 
193 private:
194 
195  UT_OStringStream *myStrStream; // memory buffer delegate
196  std::fstream *myFileStream; // file buffer delegate
197  UT_String *myFileName; // name of the temp file
198 };
199 
200 #define DECLARE_PORTABLE_RW(type) \
201  UT_API UT_OStream &UTwrite( \
202  UT_OStream &os, const type *src, int64 numelem = 1); \
203  UT_API std::ostream &UTwrite( \
204  std::ostream &os, const type *src, int64 numelem = 1); \
205  UT_API int64 UTfread( FILE *f, type *dest, int64 numelem = 1 ); \
206  UT_API int64 UTfwrite( FILE *f, const type *src, int64 numelem = 1 ); \
207 /**/
209 DECLARE_PORTABLE_RW(unsigned char)
210 DECLARE_PORTABLE_RW(signed char)
211 DECLARE_PORTABLE_RW(short)
212 DECLARE_PORTABLE_RW(unsigned short)
214 DECLARE_PORTABLE_RW(unsigned int)
217 #undef DECLARE_PORTABLE_RW
218 
219 /// Write a floating point value to given destination type, performing a
220 /// conversion as necessary.
221 // @{
222 #define DECLARE_CONVERTIBLE_RW(T) \
223  template <typename DEST_TYPE> UT_API \
224  UT_OStream & UTwrite(UT_OStream &os, const T *src, int64 cnt = 1); \
225  template <typename DEST_TYPE> UT_API \
226  std::ostream & UTwrite(std::ostream &os, const T *src, int64 cnt = 1); \
227  template <typename DEST_TYPE> UT_API \
228  int64 UTfread(FILE *f, T *dest, int64 cnt = 1); \
229  template <typename DEST_TYPE> UT_API \
230  int64 UTfwrite(FILE *f, const T *src, int64 cnt = 1); \
231 /**/
237 // @}
238 
239 // Save a chunk of binary data that may contain null characters. Use
240 // UT_IStream::readBinaryData or UT_IStream::read(std::string &) to load
241 // it back in.
243  UT_OStream &os, const char *data, int64 size,
246  std::ostream &os, const char *data, int64 size,
247  UT_STRING_BINARY_IO minbits=UT_STRING_16BIT_IO);
248 static inline void UTsaveDataBinary(
249  std::ostream &os, const std::string &data,
250  UT_STRING_BINARY_IO minbits=UT_STRING_16BIT_IO)
251 { UTsaveDataBinary(os, data.c_str(), data.size(), minbits); }
252 static inline void UTsaveDataBinary(
253  std::ostream &os, const UT_StringHolder &data,
255 { UTsaveDataBinary(os, data.c_str(), data.length(), minbits); }
256 
257 // Save a string in a binary format.
259  std::ostream &os, const char *str, UT_STRING_BINARY_IO minbits);
261  UT_OStream &os, const char *str, UT_STRING_BINARY_IO minbits);
262 
263 // Return the number of bytes required by UTsaveStringBinary() to save a string
264 // of the specified length.
266  uint64 stringlength, UT_STRING_BINARY_IO minbits);
267 
268 UT_API void UTbuildDOSCommandLine(char *dest, const char * const *args);
269 UT_API void UTbuildDOSCommandLine(UT_WorkBuffer &buf, const char * const *args);
270 
271 // Abstract base class for filtering the data before it is sent to the output
272 // stream.
274 {
275 public:
276  // virtual destructor for derived classes
278  {};
279 
280  // Often the filtering can efficently be performed only in multiple-byte
281  // chunks, or the data may grow as a result of the filtering requiring that
282  // the data is passed in smaller chunks than the actual buffer allocation
283  // size.
284  // This method returns the the maximum input size (no larger than the
285  // specified buffer size) with which the filter can work, given the buffer
286  // size (sizes in bytes).
287  virtual int getDataChunkLength( int buffer_size )
288  { return buffer_size; }
289 
290  // Before the the data is sent to a filter, the filter may need to
291  // initialize itself and/or write out some preamble.
292  // This method lets the filter write the preamble data to the
293  // buffer (up to a buffer_size), and return the data size actually written.
294  // The stream_size is the total size of the stream that is going to be
295  // filtered chunk by chunk.
296  virtual int beginFilter(char * /*buffer*/, int /*buffer_size*/,
297  int /*stream_size*/)
298  { return 0; }
299 
300  // Performs the filtering of the data in the buffer. The data size is
301  // 'data_size' which may grow, shrink, or stay the same after filtering.
302  // The filtered data length is returned by the method.
303  virtual int doFilterChunk( char *buffer, int data_size,
304  int buffer_size ) = 0;
305 
306  // After all the data has been sent to a filter, the filter may need
307  // to return some remaining filtered data, which it did not have a chance
308  // to process.
309  // This method lets the filter write the filtered data to the
310  // buffer (up to a buffer_size), and return the data size actually written.
311  virtual int endFilter( char * /*buffer*/, int /*buffer_size*/ )
312  { return 0; }
313 };
314 
315 // Writes out the buffer to the output stream, but before it is written out
316 // the buffer is filtered using the given filter. The 'size' specifies the
317 // length of the data to be filtered and written out.
318 UT_API bool UTwriteFiltered( std::ostream &os, const char *data, int size,
320 
321 // Copies the entire contents of the input stream into the output stream.
322 // Up to 'maxlen' bytes are read from the input stream. If the filter is
323 // specified, then the bytes read from the input stream are filtered before
324 // writing them to the output stream. If the filter is applied then the number
325 // of written bytes may be different than the number of bytes read.
326 UT_API bool UTcopyStreamToStream(std::istream &is, std::ostream &os,
327  size_t maxlen = ((size_t)-1),
328  UT_StreamBufferFilter * filter = NULL);
329 UT_API bool UTcopyStreamToStream(UT_IStream &is, std::ostream &os,
330  size_t maxlen = ((size_t)-1),
331  UT_StreamBufferFilter * filter = NULL);
332 
333 // Creates a unique temp file name. This name may conflict with an existing
334 // file, but the name will be unique among all _running_ Houdinis. In other
335 // words, it is safe to mindlessly overwrite the file with the given name.
336 UT_API void UTcreateTempFileName(UT_String &tempfile, const char *matchExt = 0);
337 
338 // Tries to create the directory that will be required to hold the specified
339 // file. Returns true if the dirctory exists of was created.
340 UT_API bool UTcreateDirectoryForFile(const char *filename);
341 
342 // This function tells you whether or not you would block if you tried to
343 // read data from a file descriptor/the standard input.
344 UT_API bool UTisDataAvailableOnFD(int fd);
346 UT_API bool UTisStdinValid();
347 
348 // This function will wait until either data is available on a file descriptor
349 // /stdin or until the specified timeout has passed. The type of argument to
350 // the timeout is the same as the type of argument to sginap (10ms per clock
351 // tick).
352 UT_API void UTwaitForTimeoutOrDataOnFD(long clock_ticks, int fd);
353 UT_API void UTwaitForTimeoutOrDataOnStdin(long clock_ticks);
354 
355 #endif // __UT_NTStreamUtil_H__
UT_API void UTbuildDOSCommandLine(char *dest, const char *const *args)
UT_API void UTcreateTempFileName(UT_String &tempfile, const char *matchExt=0)
void freeze(bool isfrozen)
UT_OStrStreamBuf * rdbuf()
UT_API bool UTisDataAvailableOnStdin()
virtual int getDataChunkLength(int buffer_size)
exint pcount() const
virtual int endFilter(char *, int)
void freeze(bool isfrozen)
An output stream object that owns its own string buffer storage.
#define UT_API
Definition: UT_API.h:12
virtual ~UT_StreamBufferFilter()
UT_API bool UTisStdinValid()
GLuint buffer
Definition: glcorearb.h:659
virtual int beginFilter(char *, int, int)
GLsizeiptr size
Definition: glcorearb.h:663
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
long long int64
Definition: SYS_Types.h:106
UT_API bool UTcopyStreamToStream(std::istream &is, std::ostream &os, size_t maxlen=((size_t)-1), UT_StreamBufferFilter *filter=NULL)
GLfloat f
Definition: glcorearb.h:1925
UT_API bool UTisDataAvailableOnFD(int fd)
virtual ~UT_OStrStream()
int UT_EnumType
unsigned long long uint64
Definition: SYS_Types.h:107
#define DECLARE_CONVERTIBLE_RW(T)
exint length() const
int64 exint
Definition: SYS_Types.h:115
UT_API void UTsaveStringBinary(std::ostream &os, const char *str, UT_STRING_BINARY_IO minbits)
double fpreal64
Definition: SYS_Types.h:191
UT_API void UTwaitForTimeoutOrDataOnFD(long clock_ticks, int fd)
SYS_FORCE_INLINE const char * c_str() const
GLboolean * data
Definition: glcorearb.h:130
GLuint const GLchar * name
Definition: glcorearb.h:785
int int32
Definition: SYS_Types.h:34
GLenum mode
Definition: glcorearb.h:98
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
UT_STRING_BINARY_IO
UT_API void UTsaveDataBinary(UT_OStream &os, const char *data, int64 size, UT_STRING_BINARY_IO minbits=UT_STRING_16BIT_IO)
png_infop png_bytep png_size_t buffer_size
Definition: png.h:2124
#define DECLARE_PORTABLE_RW(type)
png_infop png_uint_32 int num
Definition: png.h:2158
UT_API bool UTcreateDirectoryForFile(const char *filename)
#define const
Definition: zconf.h:214
exint pcount() const
UT_API uint64 UTgetNBytesToSaveStringBinary(uint64 stringlength, UT_STRING_BINARY_IO minbits)
float fpreal32
Definition: SYS_Types.h:190
UT_API void UTwaitForTimeoutOrDataOnStdin(long clock_ticks)
UT_API bool UTwriteFiltered(std::ostream &os, const char *data, int size, UT_StreamBufferFilter *filter)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1296