00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef basestrmH
00027 #define basestrmH
00028
00029
00030 #include <string>
00031 #include <stdio.h>
00032 #include <time.h>
00033 #include <vector>
00034 #include <list>
00035 #include <queue>
00036
00037 #ifdef _SDL_
00038 #include <SDL.h>
00039 #endif
00040
00041
00042 #include "global.h"
00043 #include "basestreaminterface.h"
00044 #include "lzw.h"
00045 #include "errors.h"
00046
00047 #include "simplestream.h"
00048
00049
00050 #ifdef USE_SYSTEM_BZ2
00051 #include <bzlib.h>
00052 #else
00053 extern "C" {
00054 #include "libs/bzlib/bzlib.h"
00055 }
00056 #endif
00057
00058 #ifdef _SDL_
00059 extern SDL_RWops *SDL_RWFromStream( pnstream stream );
00060
00061 class RWOPS_Handler {
00062 SDL_RWops *rwo;
00063 public:
00064 RWOPS_Handler( SDL_RWops *rw ) : rwo (rw) {};
00065 SDL_RWops* Get() { return rwo; };
00066 void Close() { if ( rwo ) { SDL_RWclose(rwo); rwo = NULL; } };
00067 ~RWOPS_Handler() { Close(); };
00068 };
00069
00070 #endif
00071
00072
00073
00074 const int maxFileStringSize = 10000;
00075
00076
00077 class CharBuf {
00078 public:
00079 int size;
00080 char* buf;
00081 CharBuf ( void );
00082 CharBuf ( int _size );
00083 void resize ( int newsize );
00084 ~CharBuf();
00085 };
00086
00090
00091
00092
00093
00094
00095
00096 class tcompressionerror : public ASCmsgException {
00097 public:
00098 tcompressionerror ( const ASCString& msg, int returncode ) : ASCmsgException ( msg )
00099 {
00100 message += "\n the returncode is ";
00101 message += strrr ( returncode );
00102 };
00103 };
00104
00105 class toutofmem : public ASCexception {
00106 public:
00107 int required;
00108 toutofmem ( int m ) ;
00109 };
00110
00111 class tbufferoverflow : public ASCexception {
00112 };
00113
00114
00115 class tinvalidversion : public tfileerror {
00116 public:
00117 tinvalidversion ( const ASCString& fileName, int ex, int fnd );
00118 const int expected;
00119 const int found;
00120 ASCString getMessage() const;
00121 };
00122
00123
00127
00128
00129 template<class T>
00130 class dynamic_array {
00131 protected:
00132 int blksize;
00133 int size;
00134 T* buf;
00135 virtual void resize ( int newsize );
00136 int maxaccessed;
00137
00138 public:
00139 dynamic_array ( void );
00140 dynamic_array ( int sze );
00141 virtual ~dynamic_array();
00142 T& operator[]( int a );
00143 int getlength( void );
00144 void reset ( void );
00145 };
00146
00147 template<class T>
00148 class dynamic_initialized_array : public dynamic_array<T> {
00149 T initval;
00150 protected:
00151 virtual void resize ( int newsize );
00152
00153 public:
00154 dynamic_initialized_array ( T ival );
00155 dynamic_initialized_array ( T ival, int sze );
00156 };
00157
00158
00162
00163
00164 class MemoryStreamCopy : public tnstream {
00165 void* buf;
00166 int size;
00167 int pos;
00168
00169
00170 public:
00171 MemoryStreamCopy ( pnstream stream );
00172 ~MemoryStreamCopy ( );
00173 void writedata ( const void* buf, int size );
00174 int readdata ( void* buf, int size, bool excpt = true );
00175 void seek ( int newpos );
00176 int getPosition ( void ) { return pos; };
00177 int getSize ( void ) { return size; };
00178 ASCString getLocation();
00179 };
00180
00181
00183
00184 class tmemorystreambuf {
00185 friend class tmemorystream;
00186
00187 bool initialized;
00188
00189 int used;
00190 int allocated;
00191 int dummy[10];
00192 char* buf;
00193 public:
00194 tmemorystreambuf ( void );
00195 void writetostream ( pnstream stream );
00196 void readfromstream ( pnstream stram );
00197 void clear() { used= 0; };
00198 int getMemoryFootprint() const { return allocated; };
00199
00200 const char* getBuffer() const { return buf; };
00201 int getSize() const { return used; };
00202
00203 ~tmemorystreambuf ( );
00204 };
00205
00206
00207 class tmemorystream : public tnstream {
00208 protected:
00209 int blocksize;
00210 char* zeiger;
00211 IOMode _mode;
00212 int actmempos;
00213 tmemorystreambuf* buf;
00214
00215 public:
00216 tmemorystream ( tmemorystreambuf* lbuf, IOMode mode );
00217 virtual void writedata ( const void* nbuf, int size );
00218 virtual int readdata ( void* nbuf, int size, bool excpt = true );
00219 int dataavail ( void );
00220 };
00221
00223
00224
00225
00226 class tlzwstreamcompression {
00227
00228
00229 CountType LZWTotBytesIn;
00230 CountType LZWTotBytesOut;
00231 CountType LZWCurrBytesIn;
00232 CountType LZWCurrBytesOut;
00233 unsigned LZWBestRatio;
00234 unsigned LZWLastRatio;
00235
00236
00237
00238
00239 struct Wdictionary {
00240 unsigned char c;
00241 CodeType code;
00242 CodeType parent;
00243 };
00244 struct Wdictionary *wdictionary;
00245
00246 void LZWOut ( CodeType code );
00247 IndexType LZWFind ( CodeType currcode, int in );
00248
00249
00250 int in;
00251 CountType i;
00252 CodeType currcode;
00253 IndexType idx;
00254
00255 int currcodeloaded;
00256
00257
00258
00259
00260
00261 struct Rdictionary {
00262 unsigned char c;
00263 CodeType parent;
00264 };
00265
00266
00267
00268 unsigned char *DecodeBuffer;
00269 unsigned DecodeBufferSize ;
00270
00271 unsigned int inchar;
00272 unsigned count;
00273 CodeType oldcode;
00274
00275
00276 struct Rdictionary *rdictionary;
00277
00278 CodeType incode;
00279
00280 void LZWIn ( void );
00281 unsigned LZWLoadBuffer ( unsigned count, CodeType code );
00282 int readcnt;
00283
00284
00285 char rlestartbyte;
00286 char rlenum;
00287 char rledata;
00288
00289
00290 IndexType freecode;
00291
00292 int initread;
00293 int initwrite;
00294 void initreading( void );
00295 void initwriting( void );
00296
00297 protected:
00298
00299 typedef deque<char> CDQ;
00300 queue<char,CDQ> tempbuf;
00301
00302 enum tmode { none, reading, writing, readingdirect, readingrle };
00303 tmode mode;
00304
00305 public:
00306
00307 void writedata ( const void* buf, int size );
00308 int readdata ( void* buf, int size, bool excpt = true );
00309 virtual int readlzwdata ( void* buf, int size, bool excpt = true ) = 0;
00310 virtual void writelzwdata ( const void* buf, int size ) = 0;
00311 tlzwstreamcompression ( void );
00312 virtual ~tlzwstreamcompression ( void );
00313 protected:
00314 void close( void );
00315
00316 };
00317
00318
00319 class t_compressor_stream_interface {
00320 public:
00321 virtual void writecmpdata ( const void* buf, int size ) = 0;
00322 virtual int readcmpdata ( void* buf, int size, bool excpt = true ) = 0;
00323 virtual ~t_compressor_stream_interface() {};
00324 };
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339 typedef t_compressor_stream_interface *p_compressor_stream_interface;
00340
00341 class libbzip_compression {
00342 bz_stream bzs;
00343 char* outputbuf;
00344 int outputbufsize;
00345 p_compressor_stream_interface stream;
00346 public:
00347 void close_compression ( void );
00348 void writedata ( const void* buf, int size );
00349 libbzip_compression ( p_compressor_stream_interface strm );
00350 virtual ~libbzip_compression ( );
00351
00352 };
00353
00354 class libbzip_decompression {
00355 bz_stream bzs;
00356 char* inputbuf;
00357 int inputbufsize;
00358 int inputbufused;
00359 int inputbufread;
00360 p_compressor_stream_interface stream;
00361
00362 public:
00363 int readdata ( void* buf, int size, bool excpt = true );
00364 libbzip_decompression ( p_compressor_stream_interface strm );
00365 virtual ~libbzip_decompression ( );
00366
00367 };
00368
00369
00371
00372
00373
00374 class tanycompression : public t_compressor_stream_interface, public tlzwstreamcompression {
00375
00376 typedef deque<char> CDQ;
00377 queue<char, CDQ> _queue;
00378
00379 libbzip_compression* bzip_compress;
00380 libbzip_decompression* bzip_decompress;
00381 int mmd;
00382 int status;
00383
00384 protected:
00385 virtual int readlzwdata ( void* buf, int size, bool excpt = true );
00386 virtual void writelzwdata ( const void* buf, int size );
00387 void close_compression ( void );
00388
00389 public:
00390 tanycompression ( int md );
00391 void init ( void );
00392
00393 void writedata ( const void* buf, int size );
00394 int readdata ( void* buf, int size, bool excpt = true );
00395 ~tanycompression ( );
00396 };
00397
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00412
00413 class tn_lzw_file_buf_stream : public tn_file_buf_stream, protected tanycompression {
00414 public:
00415 tn_lzw_file_buf_stream ( const char* name, IOMode mode ) : tn_file_buf_stream ( name, mode ) , tanycompression ( mode )
00416 {
00417 tanycompression :: init ( );
00418 } ;
00419
00420 void writedata ( const void* buf, int size );
00421 int readdata ( void* buf, int size, bool excpt = true );
00422 int readcmpdata ( void* buf, int size, bool excpt = true );
00423 void writecmpdata ( const void* buf, int size );
00424 ~tn_lzw_file_buf_stream();
00425 };
00426
00427
00429 typedef class tncontainerstream* pncontainerstream;
00430
00431
00432 class tn_c_lzw_filestream : public tnstream, protected tanycompression {
00433 tn_file_buf_stream *strm;
00434 pncontainerstream containerstream;
00435 int inp;
00436 ASCString fname;
00437 ASCString location;
00438 protected:
00439 int readcmpdata ( void* buf, int size, bool excpt = true );
00440 void writecmpdata ( const void* buf, int size );
00441 public:
00442 tn_c_lzw_filestream ( const ASCString& name, IOMode mode );
00443 void writedata ( const void* buf, int size );
00444 int readdata ( void* buf, int size, bool excpt = true );
00445 virtual ~tn_c_lzw_filestream ( );
00446 virtual time_t get_time ( void );
00447 virtual int getSize( void );
00448 ASCString getLocation();
00449 };
00450
00451 typedef tn_c_lzw_filestream tnfilestream ;
00452 typedef tnfilestream* pnfilestream ;
00453
00454
00455
00456
00458
00459
00460 struct tcontainerindex {
00461 char* name;
00462 int start;
00463 int end;
00464 };
00465
00466
00467 class ContainerIndexer {
00468 public:
00469 virtual void addfile ( const char* filename, const pncontainerstream stream, int directoryLevel ) = 0;
00470 virtual ~ContainerIndexer() {};
00471 };
00472
00473
00474 class tncontainerstream : public tn_file_buf_stream {
00475 tcontainerindex* index;
00476 tcontainerindex* actfile;
00477 int containerfilepos;
00478 int num;
00479
00480 int actname;
00481
00482 public:
00483 tncontainerstream ( const char* containerfilename, ContainerIndexer* indexer, int directoryLevel );
00484 void opencontainerfile ( const char* name );
00485 int getcontainerfilesize ( const char* name );
00486 int readcontainerdata ( void* buf, int size, bool excpt = true );
00487 void closecontainerfile ( void );
00488 char* getfirstname ( void );
00489 char* getnextname ( void );
00490 virtual ~tncontainerstream ();
00491 };
00492
00494
00495
00496
00497 extern ASCString listContainer();
00498
00499
00500
00501
00502
00504
00506 class tfindfile {
00507
00508
00509 public:
00510 struct FileInfo {
00511 ASCString name;
00512 int directoryLevel;
00513 bool isInContainer;
00514 ASCString location;
00515 int size;
00516 int date;
00517
00518 void read ( tnstream& stream ) {
00519 int version = stream.readInt();
00520 if ( version != 1 )
00521 throw tinvalidversion( stream.getDeviceName(), 1, version );
00522
00523 name = stream.readString();
00524 directoryLevel = stream.readInt();
00525 isInContainer = stream.readInt() != 0;
00526 location = stream.readString();
00527 size = stream.readInt();
00528 date = stream.readInt();
00529 };
00530
00531 void write ( tnstream& stream ) const {
00532 stream.writeInt(1 );
00533 stream.writeString ( name );
00534 stream.writeInt( directoryLevel );
00535 stream.writeInt ( isInContainer );
00536 stream.writeString ( location );
00537 stream.writeInt ( size );
00538 stream.writeInt ( date );
00539 };
00540
00541 };
00542
00543 private:
00544 vector<FileInfo> fileInfo;
00545 int found;
00546 int act;
00547
00548 public:
00549 enum SearchPosition { DefaultDir, CurrentDir, PrimaryDir, AllDirs };
00550 enum SearchTypes { InsideContainer, OutsideContainer, All };
00551
00558 tfindfile ( ASCString name, SearchPosition searchPosition = DefaultDir, SearchTypes searchTypes = All);
00559
00565 ASCString getnextname ( int* loc = NULL, bool* inContainer = NULL, ASCString* location = NULL );
00566 bool getnextname ( FileInfo& fi );
00567
00568 int getFoundFileNum() {
00569 return found;
00570 };
00571
00572 };
00573
00575
00576
00577 #include "basetemp.h"
00578
00579 extern const char* asc_configurationfile;
00580 extern const int maxfilenamelength;
00581
00582
00583 extern int compressrle ( const void* p, void* q);
00584
00588
00589 extern bool patimat (const ASCString& pat, const ASCString& str, bool forceCaseInsensitivity = false) ;
00590
00591 extern int checkforvaliddirectory ( char* dir );
00592
00593 extern ASCString getnextfilenumname ( const ASCString& first, const ASCString& suffix, int num = -1 );
00594
00595
00596 #define writedata2(a) writedata ( &(a), sizeof(a) )
00597 #define readdata2(a) readdata ( &(a), sizeof(a) )
00598
00600 extern bool exist ( const ASCString& s );
00601 extern time_t get_filetime ( const char* devicename );
00602
00603 extern void opencontainer ( const char* wildcard );
00604
00605 extern const char* containermagic;
00606
00607
00608 extern const char* filereadmode;
00609 extern const char* filewritemode;
00610 extern const char* filereadmodetext;
00611 extern const char* filewritemodetext;
00612
00613
00615 class FileName : public ASCString {
00616 public:
00619 ASCString suffix();
00620 FileName& operator= ( const ASCString& s ) { ASCString::operator=(s); return *this;};
00621 FileName() {};
00622 FileName( const ASCString& s ) : ASCString ( s ) {};
00623 };
00624
00625
00626 extern const char pathdelimitter;
00627 extern const char* pathdelimitterstring;
00628 extern int filesize( const char *name);
00629
00630 extern void addSearchPath ( const ASCString& path );
00631 extern void appendbackslash ( char* String );
00632 extern void appendbackslash ( ASCString& String );
00633 extern char* constructFileName( char* buf, int directoryLevel, const char* path, const char* filename );
00634 extern ASCString constructFileName( int directoryLevel, const ASCString& path, ASCString filename );
00635 extern bool directoryExist ( const ASCString& path );
00636 extern char* extractPath ( char* buf, const char* filename );
00637 extern char* extractFileName ( char* buf, const char* filename );
00638 extern ASCString extractFileName ( const ASCString& filename );
00639 extern ASCString extractFileName_withoutSuffix ( const ASCString& filename );
00640 extern int createDirectory ( const char* name );
00641 extern int getSearchPathNum();
00642 extern ASCString getSearchPath ( int i );
00643 extern bool isPathRelative( const ASCString& path );
00644
00646 extern void convertPathDelimitters ( ASCString& path );
00647
00648
00649
00650
00651 template<typename C>
00652 void writePointerContainer ( const C& c, tnstream& stream )
00653 {
00654 stream.writeInt ( 1 );
00655 stream.writeInt ( c.size() );
00656 typedef typename C::const_iterator IT;
00657 for ( IT i = c.begin(); i != c.end(); ++i )
00658 (*i)->write ( stream );
00659 }
00660
00661 template<typename BaseType>
00662 void readPointerContainer ( vector<BaseType*>& v, tnstream& stream )
00663 {
00664 stream.readInt();
00665 int num = stream.readInt();
00666 v.clear();
00667 for ( int i = 0; i < num; ++i ) {
00668 BaseType* bt = new BaseType;
00669 bt->read( stream );
00670 v.push_back( bt );
00671 }
00672 }
00673
00674 template<typename BaseType>
00675 void readPointerContainer ( list<BaseType*>& v, tnstream& stream )
00676 {
00677 stream.readInt();
00678 int num = stream.readInt();
00679 v.clear();
00680 for ( int i = 0; i < num; ++i ) {
00681 BaseType* bt = new BaseType;
00682 bt->read( stream );
00683 v.push_back( bt );
00684 }
00685 }
00686
00687
00688
00689 template<typename C>
00690 void writeClassContainer ( const C& c, tnstream& stream )
00691 {
00692 stream.writeInt ( 1 );
00693 stream.writeInt ( c.size() );
00694 typedef typename C::const_iterator IT;
00695 for ( IT i = c.begin(); i != c.end(); ++i )
00696 i->write ( stream );
00697 }
00698
00699 template<typename C>
00700 void readClassContainer ( C& c, tnstream& stream )
00701 {
00702 int version = stream.readInt();
00703 if ( version != 1 )
00704 throw tinvalidversion( stream.getLocation(), 1, version );
00705
00706 int num = stream.readInt();
00707 c.clear();
00708 for ( int i = 0; i < num; ++i ) {
00709 typedef typename C::value_type VT;
00710 VT vt;
00711 vt.read( stream );
00712 c.push_back( vt );
00713 }
00714 }
00715
00716 template<>
00717 inline void writeClassContainer<> ( const vector<ASCString>& c, tnstream& stream )
00718 {
00719 stream.writeInt ( 1 );
00720 stream.writeInt ( c.size() );
00721 typedef vector<ASCString>::const_iterator IT;
00722 for ( IT i = c.begin(); i != c.end(); ++i )
00723 stream.writeString(*i);
00724 }
00725
00726
00727 template<>
00728 inline void readClassContainer<> ( vector<ASCString>& c, tnstream& stream )
00729 {
00730 stream.readInt();
00731 int num = stream.readInt();
00732 c.clear();
00733 for ( int i = 0; i < num; ++i )
00734 c.push_back( stream.readString() );
00735 }
00736
00737 template<>
00738 inline void writeClassContainer<> ( const vector<int>& c, tnstream& stream )
00739 {
00740 stream.writeInt ( 1 );
00741 stream.writeInt ( c.size() );
00742 typedef vector<int>::const_iterator IT;
00743 for ( IT i = c.begin(); i != c.end(); ++i )
00744 stream.writeInt(*i);
00745 }
00746
00747
00748 template<>
00749 inline void readClassContainer<> ( vector<int>& c, tnstream& stream )
00750 {
00751 stream.readInt();
00752 int num = stream.readInt();
00753 c.clear();
00754 for ( int i = 0; i < num; ++i )
00755 c.push_back ( stream.readInt() );
00756 }
00757
00758 template<>
00759 inline void writeClassContainer<> ( const vector<pair<int,int> >& c, tnstream& stream )
00760 {
00761 stream.writeInt ( 1 );
00762 stream.writeInt ( c.size() );
00763 typedef vector<pair<int,int> >::const_iterator IT;
00764 for ( IT i = c.begin(); i != c.end(); ++i ) {
00765 stream.writeInt(i->first);
00766 stream.writeInt(i->second );
00767 }
00768 }
00769
00770
00771 template<>
00772 inline void readClassContainer<> ( vector<pair<int,int> >& c, tnstream& stream )
00773 {
00774 stream.readInt();
00775 int num = stream.readInt();
00776 c.clear();
00777 for ( int i = 0; i < num; ++i ) {
00778 int first = stream.readInt();
00779 int second = stream.readInt();
00780 c.push_back ( make_pair(first,second) );
00781 }
00782 }
00783
00784
00785
00786 #endif
00787