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();
00042 #else
00043 #include <strstream.h>
00044 #if defined(INTEL_COMPILER) || defined(GCC3)
00045 #define NTBinaryMode (std::ios::openmode)0
00046 #else
00047 #define NTBinaryMode 0
00048 #endif
00049 #endif // WIN32
00050
00051 #if (defined(WIN32) && _MSC_VER >= 1300) || defined(GCC3)
00052 #define UT_NoCreate (std::ios::openmode)0
00053 #else
00054 #define UT_NoCreate ios::nocreate
00055 #endif
00056
00057
00058
00059 class UT_API UT_OStrStream : public ostrstream
00060 {
00061 public:
00062 UT_OStrStream();
00063 explicit UT_OStrStream(int reallocsize);
00064 #if defined(INTEL_COMPILER) || defined(GCC3)
00065 UT_OStrStream(char *pch, int nLength, std::ios::openmode = std::ios::out)
00066 : ostrstream(pch, nLength) {}
00067 #else
00068 #if defined(LINUX) || defined(MBSD)
00069 UT_OStrStream(char *pch, int nLength, int nMode = ios::out);
00070 #else
00071 UT_OStrStream(char *pch, int nLength, int nMode = ios::out)
00072 : ostrstream(pch, nLength, nMode) {}
00073 #endif
00074 #endif
00075
00076 virtual ~UT_OStrStream() {}
00077
00078 private:
00079 #if (defined(LINUX) || defined(MBSD)) && !defined(INTEL_COMPILER) && !defined(GCC3)
00080 void utInitStream();
00081 void utInitStream(char *cp, int n, int mode);
00082 #endif
00083 };
00084
00085
00086
00087
00088
00089
00090
00091
00092 class UT_API UT_TempStream
00093 {
00094 public:
00095
00096
00097
00098
00099 UT_TempStream( int size_estimate );
00100 ~UT_TempStream();
00101
00102
00103 ostream &getStream();
00104
00105
00106 void copyStream( ostream &out );
00107
00108 private:
00109
00110
00111 UT_TempStream();
00112
00113 private:
00114 UT_OStrStream *myStrStream;
00115 fstream *myFileStream;
00116 UT_String *myFileName;
00117 };
00118
00119 #define DECLARE_PORTABLE_RW(type) \
00120 UT_API ostream &UTwrite( ostream &os, const type *src, int numelem = 1); \
00121 UT_API int UTfread( FILE *f, type *dest, int numelem = 1 ); \
00122 UT_API int UTfwrite( FILE *f, const type *src, int numelem = 1 );
00123
00124 DECLARE_PORTABLE_RW(char)
00125 DECLARE_PORTABLE_RW(unsigned char)
00126 DECLARE_PORTABLE_RW(short)
00127 DECLARE_PORTABLE_RW(unsigned short)
00128 DECLARE_PORTABLE_RW(int)
00129 DECLARE_PORTABLE_RW(unsigned int)
00130 DECLARE_PORTABLE_RW(int64)
00131 DECLARE_PORTABLE_RW(uint64)
00132 DECLARE_PORTABLE_RW(fpreal32)
00133 DECLARE_PORTABLE_RW(fpreal64)
00134
00135
00136 UT_API void UTbuildDOSCommandLine(char *dest, const char * const *args);
00137 UT_API void UTbuildDOSCommandLine(UT_WorkBuffer &buf, const char * const *args);
00138
00139
00140
00141
00142 UT_API void UTsaveStringBinary(ostream &os, const char *str,
00143 UT_STRING_BINARY_IO minbits);
00144
00145
00146
00147 class UT_API UT_StreamBufferFilter
00148 {
00149 public:
00150
00151 virtual ~UT_StreamBufferFilter()
00152 {};
00153
00154
00155
00156
00157
00158
00159
00160
00161 virtual int getDataChunkLength( int buffer_size )
00162 { return buffer_size; }
00163
00164
00165
00166
00167
00168
00169
00170 virtual int beginFilter(char * , int ,
00171 int )
00172 { return 0; }
00173
00174
00175
00176
00177 virtual int doFilterChunk( char *buffer, int data_size,
00178 int buffer_size ) = 0;
00179
00180
00181
00182
00183
00184
00185 virtual int endFilter( char * , int )
00186 { return 0; }
00187 };
00188
00189
00190
00191
00192 UT_API bool UTwriteFiltered( ostream &os, const char *data, int size,
00193 UT_StreamBufferFilter * filter );
00194
00195
00196
00197
00198
00199
00200 UT_API bool UTcopyStreamToStream(istream &is, ostream &os,
00201 unsigned maxlen = 0xFFFFFFFF,
00202 UT_StreamBufferFilter * filter = NULL);
00203 UT_API bool UTcopyStreamToStream(UT_IStream &is, ostream &os,
00204 unsigned maxlen = 0xFFFFFFFF,
00205 UT_StreamBufferFilter * filter = NULL);
00206
00207
00208
00209
00210 UT_API void UTcreateTempFileName(UT_String &tempfile, const char *matchExt = 0);
00211
00212
00213
00214 UT_API bool UTcreateDirectoryForFile(const char *filename);
00215
00216
00217
00218 UT_API bool UTisDataAvailableOnFD(int fd);
00219 UT_API bool UTisDataAvailableOnStdin();
00220 UT_API bool UTisStdinValid();
00221
00222
00223
00224
00225
00226 UT_API void UTwaitForTimeoutOrDataOnFD(long clock_ticks, int fd);
00227 UT_API void UTwaitForTimeoutOrDataOnStdin(long clock_ticks);
00228
00229 #endif // __UT_NTStreamUtil_H__