00001
00002 #ifndef __UT_NTStreamUtil_H__
00003 #define __UT_NTStreamUtil_H__
00004
00005 #include "UT_API.h"
00006 #include <stdio.h>
00007 #include <fstream.h>
00008 #include <SYS/SYS_Types.h>
00009 #include "UT_TmpDir.h"
00010
00011 #include <iostream.h>
00012 #include <fstream.h>
00013 typedef int UT_EnumType;
00014 class UT_WorkBuffer;
00015 class UT_String;
00016 class UT_IStream;
00017
00018 typedef enum {
00019 UT_STRING_8BIT_IO = 8,
00020 UT_STRING_16BIT_IO = 16,
00021 UT_STRING_32BIT_IO = 32,
00022 UT_STRING_64BIT_IO = 64,
00023 } UT_STRING_BINARY_IO;
00024
00025 #ifdef WIN32
00026 #include <strstrea.h>
00027 #define NTBinaryMode ios::binary
00028
00029 void *getNewNamedPipe( char *pipeName );
00030
00031 UT_API FILE *UnixPWrite( const char *cmd, const char *file );
00032 UT_API FILE *UnixPOpen( const char *file, const char *mode );
00033 UT_API void *UnixPOpen( const char *file, void **in, void **out,
00034 void **err );
00035 UT_API int UnixPClose( FILE *file, int block );
00036 inline FILE *popen( const char *name, const char *mode )
00037 { return UnixPOpen( name, mode ); }
00038 inline int pclose( FILE *f, int block = 1 )
00039 { return UnixPClose( f, block ); }
00040
00041 UT_API const char *UTgetSocketErrorString(int err);
00042 #else
00043 #include <strstream.h>
00044 #define NTBinaryMode (std::ios::openmode)0
00045 #endif // WIN32
00046
00047 #define UT_NoCreate (std::ios::openmode)0
00048
00049
00050
00051 class UT_API UT_OStrStream : public ostrstream
00052 {
00053 public:
00054 UT_OStrStream();
00055 explicit UT_OStrStream(int reallocsize);
00056 #if !defined(WIN32)
00057 UT_OStrStream(char *pch, int nLength, std::ios::openmode = std::ios::out)
00058 : ostrstream(pch, nLength) {}
00059 #else
00060 UT_OStrStream(char *pch, int nLength, int nMode = ios::out)
00061 : ostrstream(pch, nLength, nMode) {}
00062 #endif
00063
00064 virtual ~UT_OStrStream() {}
00065
00066 private:
00067 };
00068
00069
00070
00071
00072
00073
00074
00075
00076 class UT_API UT_TempStream
00077 {
00078 public:
00079
00080
00081
00082
00083 UT_TempStream( int size_estimate );
00084 ~UT_TempStream();
00085
00086
00087 ostream &getStream();
00088
00089
00090 void copyStream( ostream &out );
00091
00092 private:
00093
00094
00095 UT_TempStream();
00096
00097 private:
00098 UT_OStrStream *myStrStream;
00099 fstream *myFileStream;
00100 UT_String *myFileName;
00101 };
00102
00103 #define DECLARE_PORTABLE_RW(type) \
00104 UT_API ostream &UTwrite( ostream &os, const type *src, int numelem = 1); \
00105 UT_API int UTfread( FILE *f, type *dest, int numelem = 1 ); \
00106 UT_API int UTfwrite( FILE *f, const type *src, int numelem = 1 );
00107
00108 DECLARE_PORTABLE_RW(char)
00109 DECLARE_PORTABLE_RW(unsigned char)
00110 DECLARE_PORTABLE_RW(signed char)
00111 DECLARE_PORTABLE_RW(short)
00112 DECLARE_PORTABLE_RW(unsigned short)
00113 DECLARE_PORTABLE_RW(int)
00114 DECLARE_PORTABLE_RW(unsigned int)
00115 DECLARE_PORTABLE_RW(int64)
00116 DECLARE_PORTABLE_RW(uint64)
00117 DECLARE_PORTABLE_RW(fpreal32)
00118 DECLARE_PORTABLE_RW(fpreal64)
00119
00120
00121
00122
00123
00124 UT_API void UTsaveDataBinary(
00125 ostream &os, const char *data, int64 size,
00126 UT_STRING_BINARY_IO minbits=UT_STRING_16BIT_IO);
00127 static inline void UTsaveDataBinary(
00128 ostream &os, const std::string &data,
00129 UT_STRING_BINARY_IO minbits=UT_STRING_16BIT_IO)
00130 { UTsaveDataBinary(os, data.c_str(), data.size(), minbits); }
00131
00132
00133 UT_API void UTsaveStringBinary(
00134 ostream &os, const char *str, UT_STRING_BINARY_IO minbits);
00135
00136
00137
00138 UT_API uint64 UTgetNBytesToSaveStringBinary(
00139 uint64 stringlength, UT_STRING_BINARY_IO minbits);
00140
00141 UT_API void UTbuildDOSCommandLine(char *dest, const char * const *args);
00142 UT_API void UTbuildDOSCommandLine(UT_WorkBuffer &buf, const char * const *args);
00143
00144
00145
00146 class UT_API UT_StreamBufferFilter
00147 {
00148 public:
00149
00150 virtual ~UT_StreamBufferFilter()
00151 {};
00152
00153
00154
00155
00156
00157
00158
00159
00160 virtual int getDataChunkLength( int buffer_size )
00161 { return buffer_size; }
00162
00163
00164
00165
00166
00167
00168
00169 virtual int beginFilter(char * , int ,
00170 int )
00171 { return 0; }
00172
00173
00174
00175
00176 virtual int doFilterChunk( char *buffer, int data_size,
00177 int buffer_size ) = 0;
00178
00179
00180
00181
00182
00183
00184 virtual int endFilter( char * , int )
00185 { return 0; }
00186 };
00187
00188
00189
00190
00191 UT_API bool UTwriteFiltered( ostream &os, const char *data, int size,
00192 UT_StreamBufferFilter * filter );
00193
00194
00195
00196
00197
00198
00199 UT_API bool UTcopyStreamToStream(istream &is, ostream &os,
00200 size_t maxlen = ((size_t)-1),
00201 UT_StreamBufferFilter * filter = NULL);
00202 UT_API bool UTcopyStreamToStream(UT_IStream &is, ostream &os,
00203 size_t maxlen = ((size_t)-1),
00204 UT_StreamBufferFilter * filter = NULL);
00205
00206
00207
00208
00209 UT_API void UTcreateTempFileName(UT_String &tempfile, const char *matchExt = 0);
00210
00211
00212
00213 UT_API bool UTcreateDirectoryForFile(const char *filename);
00214
00215
00216
00217 UT_API bool UTisDataAvailableOnFD(int fd);
00218 UT_API bool UTisDataAvailableOnStdin();
00219 UT_API bool UTisStdinValid();
00220
00221
00222
00223
00224
00225 UT_API void UTwaitForTimeoutOrDataOnFD(long clock_ticks, int fd);
00226 UT_API void UTwaitForTimeoutOrDataOnStdin(long clock_ticks);
00227
00228 #endif // __UT_NTStreamUtil_H__