00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __UT_IStream__
00024 #define __UT_IStream__
00025
00026 #include "UT_API.h"
00027 #include <iostream.h>
00028 #include <fstream.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <SYS/SYS_Types.h>
00032 #include "UT_WorkBuffer.h"
00033 #include "UT_IntArray.h"
00034 #include "UT_PtrArray.h"
00035
00036 class UT_String;
00037 class UT_IStreamBuf;
00038
00039 typedef enum {
00040 UT_ISTREAM_ASCII=0,
00041 UT_ISTREAM_BINARY
00042 } UT_ISTREAM_READTYPE;
00043
00044
00045
00046
00047
00048
00049
00050
00051 typedef enum {
00052 UT_ISTREAM_8BIT_IO = 8,
00053 UT_ISTREAM_16BIT_IO = 16,
00054 UT_ISTREAM_32BIT_IO = 32,
00055 UT_ISTREAM_64BIT_IO = 64
00056 } UT_ISTREAM_RLE_IO;
00057
00058 class UT_API UT_IStream {
00059 public:
00060 UT_IStream(FILE *fp,
00061 UT_ISTREAM_READTYPE binary=UT_ISTREAM_BINARY);
00062 UT_IStream(istream &is,
00063 UT_ISTREAM_READTYPE binary=UT_ISTREAM_BINARY);
00064 UT_IStream(const char *buffer, int bufsize,
00065 UT_ISTREAM_READTYPE binary);
00066
00067 UT_IStream(const UT_WorkBuffer &buffer,
00068 UT_ISTREAM_READTYPE binary=UT_ISTREAM_ASCII);
00069 virtual ~UT_IStream();
00070
00071
00072
00073
00074
00075
00076 static void setStdinCompatible(bool state);
00077
00078 enum {
00079 UT_SEEK_BEG=0,
00080 UT_SEEK_CUR=1,
00081 UT_SEEK_END=2
00082 };
00083
00084
00085
00086 int64 tellg() const;
00087 bool seekg(int64 pos, int seekdir=UT_SEEK_BEG);
00088
00089
00090
00091 bool isEof() const;
00092
00093
00094 bool isError() const { return myError; }
00095
00096 bool isAscii() const { return !myBinary; }
00097 bool isBinary() const { return myBinary; }
00098
00099
00100
00101
00102
00103 void setVersion(int version) { myVersion = version; }
00104 int getVersion() const { return myVersion; }
00105
00106
00107
00108
00109 void setLabel(const char *label);
00110 const char *getLabel() const { return myLabel ? myLabel : ""; }
00111
00112
00113
00114
00115
00116 void pushBinary(UT_ISTREAM_READTYPE binary);
00117 void popBinary();
00118
00119
00120
00121
00122
00123 void pushOperation(const char *operation);
00124 void popOperation();
00125
00126
00127
00128
00129
00130
00131 UT_IStreamBuf *getStreamBuffer() { return myStream; }
00132 UT_IStream *getSubStream(int64 start, int64 nbytes);
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 UT_IStream *getZlibStream();
00144 UT_IStream *getGzipStream();
00145
00146 bool getLine(UT_WorkBuffer &buffer, int end='\n');
00147 bool skipLine(int end='\n');
00148 bool getWord(UT_WorkBuffer &buffer);
00149 bool getString(UT_WorkBuffer &buffer);
00150 bool skipLeadingWhitespace();
00151
00152
00153
00154
00155 bool getAll(UT_WorkBuffer &buffer);
00156
00157
00158
00159 bool getNonCommentWord(UT_WorkBuffer &buffer, int comment='#');
00160
00161
00162
00163
00164
00165
00166 int getNonCommentLine(UT_WorkBuffer &buffer, int comment='#',
00167 bool strip_inline_comments = true);
00168
00169
00170
00171 bool checkNonCommentToken(const char *expected, int comment='#')
00172 {
00173 UT_WorkBuffer wbuf;
00174 if (!getNonCommentWord(wbuf, comment))
00175 return false;
00176 return !strcmp(wbuf.buffer(), expected);
00177 }
00178
00179
00180
00181 bool checkToken(const char *expected)
00182 {
00183 UT_WorkBuffer wbuf;
00184 if (!getWord(wbuf))
00185 return false;
00186 return !strcmp(wbuf.buffer(), expected);
00187 }
00188
00189
00190 int getc();
00191 int peek();
00192
00193
00194
00195
00196
00197 int ungetc() { return unwind(1); }
00198 int unwind(int amount);
00199
00200
00201
00202
00203
00204
00205
00206
00207 int read(int16 *array, int sz=1)
00208 {
00209 return myBinary ? bread(array, sz) : aread(array, sz);
00210 }
00211 int read(uint16 *array, int sz=1)
00212 {
00213 return myBinary ? bread(array, sz) : aread(array, sz);
00214 }
00215 int read(int32 *array, int sz=1)
00216 {
00217 return myBinary ? bread(array, sz) : aread(array, sz);
00218 }
00219 int read(uint32 *array, int sz=1)
00220 {
00221 return myBinary ? bread(array, sz) : aread(array, sz);
00222 }
00223 int read(int64 *array, int sz=1)
00224 {
00225 return myBinary ? bread(array, sz) : aread(array, sz);
00226 }
00227 int read(uint64 *array, int sz=1)
00228 {
00229 return myBinary ? bread(array, sz) : aread(array, sz);
00230 }
00231 int read(fpreal32 *array, int sz=1)
00232 {
00233 return myBinary ? bread(array, sz) : aread(array, sz);
00234 }
00235 int read(fpreal64 *array, int sz=1)
00236 {
00237 return myBinary ? bread(array, sz) : aread(array, sz);
00238 }
00239 bool read(UT_String &string)
00240 {
00241 return myBinary ? bread(string) : aread(string);
00242 }
00243
00244
00245 bool readBinaryString(UT_String &str,
00246 UT_ISTREAM_RLE_IO startbits);
00247
00248 bool readBool(bool &result)
00249 {
00250 int32 ival;
00251 if (read(&ival))
00252 {
00253 result = (ival != 0);
00254 return true;
00255 }
00256 return false;
00257 }
00258 bool readChar(char &result)
00259 {
00260 if (isBinary())
00261 return bread(&result) == 1;
00262 return areadChar(result);
00263 }
00264 bool areadChar(char &result)
00265 {
00266 if (!skipLeadingWhitespace())
00267 return false;
00268 return bread(&result, 1) == 1;
00269 }
00270
00271
00272
00273 int bread(char *buffer, int asize=1);
00274 int bread(unsigned char *buffer, int asize=1)
00275 { return bread((char *)buffer, asize); }
00276 int bread(int16 *buffer, int asize=1);
00277 int bread(uint16 *buffer, int asize=1)
00278 { return bread((int16 *)buffer, asize); }
00279 int bread(int32 *buffer, int asize=1);
00280 int bread(uint32 *buffer, int asize=1)
00281 { return bread((int32 *)buffer, asize); }
00282 int bread(int64 *buffer, int asize=1);
00283 int bread(uint64 *buffer, int asize=1)
00284 { return bread((int64 *)buffer, asize); }
00285 int bread(fpreal32 *buffer, int asize=1);
00286 int bread(fpreal64 *buffer, int asize=1);
00287 bool bread(UT_String &str) { return readString(str, true); }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 int aread(int16 *buffer, int asize=1);
00304 int aread(uint16 *buffer, int asize=1);
00305 int aread(int32 *buffer, int asize=1);
00306 int aread(uint32 *buffer, int asize=1);
00307 int aread(int64 *buffer, int asize=1);
00308 int aread(uint64 *buffer, int asize=1);
00309 int aread(fpreal32 *buffer, int asize=1);
00310 int aread(fpreal64 *buffer, int asize=1);
00311 bool aread(UT_String &str) { return readString(str, false); }
00312
00313 protected:
00314
00315
00316
00317 UT_IStream(const char *filename);
00318 UT_IStream(UT_IStreamBuf *sbuf, UT_ISTREAM_READTYPE binary);
00319
00320 protected:
00321 void initCommon(UT_ISTREAM_READTYPE binary);
00322 bool readString(UT_String &str, bool binary);
00323 void setError();
00324 void clearError();
00325
00326 UT_IntArray myBinaryStack;
00327 UT_PtrArray<const char *> myOperationStack;
00328 UT_IStreamBuf *myStream;
00329 char *myLabel;
00330 int myVersion;
00331 bool myBinary, myEof, myError;
00332 };
00333
00334
00335
00336
00337 class UT_API UT_IFStream : public UT_IStream {
00338 public:
00339 UT_IFStream();
00340 UT_IFStream(const char *filename,
00341 UT_ISTREAM_READTYPE binary=UT_ISTREAM_BINARY);
00342 virtual ~UT_IFStream();
00343
00344 bool open(const char *filename,
00345 UT_ISTREAM_READTYPE binary=UT_ISTREAM_BINARY);
00346 bool close();
00347 };
00348
00349
00350
00351
00352 class UT_API UT_IZStream : public UT_IStream {
00353 public:
00354 UT_IZStream(UT_IStream &src);
00355 virtual ~UT_IZStream();
00356 };
00357
00358 class UT_API UT_IGZStream : public UT_IStream {
00359 public:
00360 UT_IGZStream(UT_IStream &src);
00361 virtual ~UT_IGZStream();
00362 };
00363
00364
00365 class UT_API UT_IBlowfishStream : public UT_IStream {
00366 public:
00367 UT_IBlowfishStream(UT_IStream &src, const unsigned char * key,
00368 int key_size,
00369 UT_ISTREAM_READTYPE binary=UT_ISTREAM_BINARY);
00370 virtual ~UT_IBlowfishStream();
00371
00372
00373 static int getPlainTextLength(UT_IStream &src);
00374 };
00375
00376
00377
00378 class UT_API UT_IStreamAutoBinary {
00379 public:
00380 explicit UT_IStreamAutoBinary(UT_IStream &is, UT_ISTREAM_READTYPE binary)
00381 : myIs(is)
00382 {
00383 myIs.pushBinary(binary);
00384 }
00385 explicit UT_IStreamAutoBinary(UT_IStream &is, bool binary)
00386 : myIs(is)
00387 {
00388 myIs.pushBinary(binary ? UT_ISTREAM_BINARY : UT_ISTREAM_ASCII);
00389 }
00390 ~UT_IStreamAutoBinary()
00391 {
00392 myIs.popBinary();
00393 }
00394 private:
00395 UT_IStream &myIs;
00396 };
00397
00398
00399
00400
00401 class UT_API UT_IStreamAutoOp {
00402 public:
00403
00404
00405
00406 explicit UT_IStreamAutoOp(UT_IStream &is, const char *operation,
00407 bool harden = false)
00408 : myIs(is),
00409 myOperation(operation, harden ? 1 : 0)
00410 {
00411 myIs.pushOperation(myOperation);
00412 }
00413 ~UT_IStreamAutoOp()
00414 {
00415 myIs.popOperation();
00416 }
00417 private:
00418 UT_IStream &myIs;
00419 UT_String myOperation;
00420 };
00421
00422 #endif