kviewshell
ByteStream.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifndef _BYTESTREAM_H
00058 #define _BYTESTREAM_H
00059 #ifdef HAVE_CONFIG_H
00060 #include "config.h"
00061 #endif
00062 #if NEED_GNUG_PRAGMAS
00063 # pragma interface
00064 #endif
00065
00102
00103
00104 #include "Arrays.h"
00105 #include <stdio.h>
00106
00107 #ifdef HAVE_NAMESPACES
00108 namespace DJVU {
00109 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00110 }
00111 #endif
00112 #endif
00113
00114 class GURL;
00115 class GUTF8String;
00116 class GNativeString;
00117
00129 class ByteStream : public GPEnabled
00130 {
00131 public:
00132 class Stdio;
00133 class Static;
00134 class Memory;
00135 class Wrapper;
00136 enum codepage_type {RAW,AUTO,NATIVE,UTF8} cp;
00137
00142 public:
00144 virtual ~ByteStream();
00156 virtual size_t read(void *buffer, size_t size);
00166 virtual size_t write(const void *buffer, size_t size);
00169 virtual long tell(void) const = 0;
00192 virtual int seek(long offset, int whence = SEEK_SET, bool nothrow=false);
00197 virtual void flush(void);
00199
00204 public:
00212 size_t readall(void *buffer, size_t size);
00219 size_t writall(const void *buffer, size_t size);
00228 size_t copy(ByteStream &bsfrom, size_t size=0);
00232 GP<ByteStream> duplicate(const size_t size=0) const;
00234 size_t format(const char *fmt, ... );
00236 int scanf(const char *fmt, ... );
00238 size_t writestring(const GUTF8String &s);
00240 size_t writestring(const GNativeString &s);
00243 void formatmessage( const char *fmt, ... );
00245 void writemessage( const char *message );
00247 void write8 (unsigned int card8);
00251 void write16(unsigned int card16);
00255 void write24(unsigned int card24);
00259 void write32(unsigned int card32);
00261 unsigned int read8 ();
00265 unsigned int read16();
00269 unsigned int read24();
00273 unsigned int read32();
00277 virtual int size(void) const;
00279 TArray<char> get_data(void);
00283 virtual size_t readat(void *buffer, size_t sz, int pos);
00285 virtual bool is_static(void) const { return false; }
00287 protected:
00288 ByteStream(void) : cp(AUTO) {};
00289 private:
00290
00291 ByteStream(const ByteStream &);
00292 ByteStream & operator=(const ByteStream &);
00293 public:
00299 static GP<ByteStream> create(void);
00303 static GP<ByteStream> create(void const * const buffer, const size_t size);
00313 static GP<ByteStream> create(
00314 const GURL &url, char const * const mode);
00316 static GP<ByteStream> create( char const * const mode);
00317
00322 static GP<ByteStream> create(
00323 const int fd, char const * const mode, const bool closeme);
00324
00329 static GP<ByteStream> create(
00330 FILE * const f, char const * const mode, const bool closeme);
00337 static GP<ByteStream> create_static(
00338 void const * const buffer, const size_t size);
00339
00341 static GP<ByteStream> get_stdin(char const * const mode=0);
00342 static GP<ByteStream> get_stdout(char const * const mode=0);
00343 static GP<ByteStream> get_stderr(char const * const mode=0);
00344
00346 static const char *EndOfFile;
00348 GNativeString getAsNative(void);
00350 GUTF8String getAsUTF8(void);
00351 };
00352
00353 inline size_t
00354 ByteStream::readat(void *buffer, size_t sz, int pos)
00355 {
00356 size_t retval;
00357 long tpos=tell();
00358 seek(pos, SEEK_SET, true);
00359 retval=readall(buffer,sz);
00360 seek(tpos, SEEK_SET, true);
00361 return retval;
00362 }
00363
00364 inline int
00365 ByteStream::size(void) const
00366 {
00367 ByteStream *bs=const_cast<ByteStream *>(this);
00368 int bsize=(-1);
00369 long pos=tell();
00370 if(bs->seek(0,SEEK_END,true))
00371 {
00372 bsize=(int)tell();
00373 (void)(bs->seek(pos,SEEK_SET,false));
00374 }
00375 return bsize;
00376 }
00377
00381 class ByteStream::Wrapper : public ByteStream
00382 {
00383 protected:
00384 GP<ByteStream> gbs;
00385 ByteStream *bs;
00386 Wrapper(void) : bs(0) {}
00387 Wrapper(const GP<ByteStream> &xbs) : gbs(xbs), bs(xbs) {}
00388 public:
00389 ~Wrapper();
00390 ByteStream * operator & () const {return bs;}
00391 ByteStream * operator & () {return bs;}
00392 virtual size_t read(void *buffer, size_t size)
00393 { return bs->read(buffer,size); }
00394 virtual size_t write(const void *buffer, size_t size)
00395 { return bs->write(buffer,size); }
00396 virtual long tell(void) const
00397 { return bs->tell(); }
00398 virtual int seek(long offset, int whence = SEEK_SET, bool nothrow=false)
00399 { return bs->seek(offset,whence,nothrow); }
00400 virtual void flush(void)
00401 { bs->flush(); }
00402 };
00403
00404
00406
00407
00408
00409 #ifdef HAVE_NAMESPACES
00410 }
00411 # ifndef NOT_USING_DJVU_NAMESPACE
00412 using namespace DJVU;
00413 # endif
00414 #endif
00415 #endif
00416