Advanced Strategic Command
basestrm.cpp
Go to the documentation of this file.
1 
5 /*
6  This file is part of Advanced Strategic Command; http://www.asc-hq.de
7  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING. If not, write to the
21  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22  Boston, MA 02111-1307 USA
23 */
24 
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <cstdlib>
28 #include <stdlib.h>
29 #include <string>
30 #include <list>
31 
32 #include "global.h"
33 
34 #ifdef HAVE_LIMITS
35  #include <limits>
36 #endif
37 
38 
39 #include <sys/stat.h>
40 
41 #include <bzlib.h>
42 
43 #include "global.h"
44 #include "basestrm.h"
45 
46 #ifdef _DOS_
47  #include "dos/fileio.h"
48 #else
49  #ifdef _WIN32_
50  #include "win32/fileio.h"
51  #else
52  #ifdef _UNIX_
53  #include "unix/fileio.h"
54  #endif
55  #endif
56 #endif
57 
58 //#include sdlheader
59 #include <SDL_endian.h>
60 
61 
62 #include "util/messaginghub.h"
63 
64 
65  const int maxSearchDirNum = 30;
66  int searchDirNum = 0;
67  char* ascDirectory[maxSearchDirNum] = { NULL, NULL, NULL, NULL, NULL,
68  NULL, NULL, NULL, NULL, NULL };
69 
70 
71 
72 
73 #pragma pack(1)
74 struct trleheader {
75  unsigned short int id;
76  unsigned short int size;
77  Uint8 rle;
78  unsigned short int x;
79  unsigned short int y;
80 };
81 
82 #pragma pack()
83 
84 #define bzip_xor_byte 'M'
85 
86 const char* containermagic = "NCBM";
87 
88 const char* LZ_SIGNATURE = "MBLZW16";
89 const char* RLE_SIGNATURE = "MBRLE1";
90 const char* BZIP_SIGNATURE = "MBZLB2X!";
91 
92 
93 
94 
95 
99 
101  {
102  size = 0;
103  buf = NULL;
104  }
105 
106 CharBuf :: CharBuf ( int _size )
107 
108  {
109  size = _size;
110  buf = new char[ size ];
111  }
112 
113 void CharBuf :: resize ( int newsize )
114  {
115  char* nb = new char[newsize];
116  for ( int i = 0; i < size; i++ )
117  nb[i] = buf[i];
118 
119  delete[] buf;
120  buf = nb;
121  }
122 
124  {
125  if ( buf )
126  delete[] buf;
127  buf = NULL;
128  }
129 
133 
134 
135 
137 {
138  required = m;
139 }
140 
142 {
143  _filename = fileName ;
144 }
145 
146 
147 tinvalidmode :: tinvalidmode ( const ASCString& _fileName, tnstream::IOMode org_mode, tnstream::IOMode requested_mode )
148  : tfileerror ( _fileName )
149 {
150  orgmode = org_mode;
151  requestmode = requested_mode;
152 }
153 
154 
155 
157  : tfileerror ( _fileName )
158 {
159 
160 }
161 
162 
163 tinternalerror::tinternalerror ( const char* filename, int l )
164 {
165  linenum = l;
166  sourcefilename = filename;
167 }
168 
169 
170 
171 tinvalidversion :: tinvalidversion ( const ASCString& _fileName, int ex, int fnd )
172  : tfileerror ( _fileName ), expected ( ex ), found ( fnd )
173 {
174 }
175 
177 {
178  ASCString s;
179  if ( expected < found )
180  s.format( "File/module %s has invalid version.\nExpected file version %d\nFound file version %d\nThe file/module is newer than your application\nPlease install the latest version of ASC from www.asc-hq.org", getFileName().c_str(), expected, found );
181  else
182  s.format ( "File/module %s has invalid version.\nExpected file version %d\nFound file version %d\nThis is a bug, please report it!", getFileName().c_str(), expected, found );
183 
184  return s;
185 }
186 
187 
188 
192 
193 
195  : devicename ( "-abstract tnstream-" ) {}
196 
197 void tnstream::seek ( int pos )
198 {
199  throw tfileerror ( "Seeking not supported for stream " + getDeviceName() );
200 }
201 
202 void tnstream::readrlepict( void** pnter, bool allocated, int* size)
203 {
204  trleheader hd;
205  int w;
206  Uint8* q;
207 
208  hd.id = readWord();
209  hd.size = readWord();
210  hd.rle = readUint8();
211  hd.x = readWord();
212  hd.y = readWord();
213 
214  if (hd.id == 16973) {
215  if (!allocated)
216  *pnter = new Uint8 [ hd.size + sizeof(hd) ];
217  memcpy( *pnter, &hd, sizeof(hd));
218  q = (Uint8*) (*pnter) + sizeof(hd);
219 
220  readdata( q, hd.size); // endian ok ?
221  *size = hd.size + sizeof(hd);
222  }
223  else {
224  w = (hd.id + 1) * (hd.size + 1) + 4 ;
225  if (!allocated)
226  *pnter = new Uint8 [ w ];
227  memcpy ( *pnter, &hd, sizeof ( hd ));
228  q = (Uint8*) (*pnter) + sizeof(hd);
229  readdata ( q, w - sizeof(hd) ); // endian ok ?
230  *size = w;
231  }
232 }
233 
234 
235 
236 void tnstream :: writerlepict ( const void* buf )
237 {
238  writeImage( buf, true );
239 }
240 
241 void tnstream :: writeImage ( const void* buf, bool compress )
242 {
243  if ( compress ) {
244  char* tempbuf = new char [ 0xffff ];
245  if ( tempbuf ) {
246  int size = compressrle ( buf, tempbuf );
247  trleheader* hd = (trleheader*) tempbuf;
248  writeWord( hd->id );
249  writeWord( hd->size );
250  ( hd->rle );
251  writeWord( hd->x );
252  writeWord( hd->y );
253 
254  writedata ( hd+1, size - sizeof(*hd) );
255  delete[] tempbuf;
256  } else
257  compress = false;
258  }
259 
260  if ( !compress ) {
261  Uint16* pw = (Uint16*) buf;
262  writeWord(pw[0] );
263  writeWord(pw[1] );
264  writedata ( pw+2, ( pw[0] + 1 ) * ( pw[1] + 1 ) );
265  }
266 }
267 
268 
270 {
271  return devicename;
272 }
273 
275 {
276  return devicename;
277 }
278 
280 {
281  return "";
282 }
283 
284 int tnstream::readInt ( void )
285 {
286  int i;
287  readdata2 ( i );
288  return SDL_SwapLE32( i );
289 }
290 
291 int tnstream::readWord ( void )
292 {
293  Uint16 w;
294  readdata2 ( w );
295  return SDL_SwapLE16( w );
296 }
297 
299 {
300  char c;
301  readdata2 ( c );
302  return c;
303 }
304 
305 Uint8 tnstream::readUint8 ( void )
306 {
307  Uint8 c;
308  readdata2 ( c );
309  return c;
310 }
311 
312 float SwapFloat( float f )
313 {
314  union
315  {
316  float f;
317  Uint8 b[4];
318  } dat1, dat2;
319 
320  dat1.f = f;
321  dat2.b[0] = dat1.b[3];
322  dat2.b[1] = dat1.b[2];
323  dat2.b[2] = dat1.b[1];
324  dat2.b[3] = dat1.b[0];
325  return dat2.f;
326 }
327 
328 float tnstream::readFloat ( void )
329 {
330  float c;
331  readdata2 ( c );
332 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
333  c = SwapFloat(c);
334 #endif
335  return c;
336 }
337 
338 #if SIZE_T_not_identical_to_INT
339 
340 void tnstream::writeInt ( size_t i )
341 {
342 #ifdef HAVE_LIMITS
343  // assert( i <= numeric_limits<int>::max());
344 #endif
345  writeInt( int(i) );
346 }
347 
348 #endif
349 
350 void tnstream::writeInt ( unsigned int i )
351 {
352  i = SDL_SwapLE32(i);
353  writedata2 ( i );
354 }
355 
356 void tnstream::writeInt ( bool b )
357 {
358  int i = b;
359  i = SDL_SwapLE32(i);
360  writedata2 ( i );
361 }
362 
363 void tnstream::writeInt ( int i )
364 {
365  i = SDL_SwapLE32(i);
366  writedata2 ( i );
367 }
368 
369 void tnstream::writeWord ( int w )
370 {
371  Uint16 w2 = SDL_SwapLE16( Uint16(w) );
372  writedata2 ( w2 );
373 }
374 
376 {
377  writedata2 ( c );
378 }
379 
380 void tnstream::writeUint8 ( Uint8 c )
381 {
382  writedata2 ( c );
383 }
384 
385 void tnstream::writeFloat ( float f )
386 {
387  writedata2 ( f );
388 }
389 
390 void tnstream::readpchar(char** pc, int maxlength )
391 {
392  int actpos2 = 0;
393 
394  int maxav = 100000;
395 
396  if ( maxlength )
397  if ( maxav > maxlength )
398  maxav = maxlength;
399 
400  CharBuf charbuf ( maxav );
401 
402  maxav--;
403 
404 
405  char* pch2 = charbuf.buf;
406 
407  int loop = 0;
408 
409  do {
410  actpos2++;
411  if ( loop )
412  pch2++;
413 
414  loop++;
415 
416  readdata( pch2, 1 ); // endian ok !
417 
418  } while (*pch2 != 0 && actpos2 < maxav ); /* enddo */
419 
420  if ( actpos2 >= maxav ) {
421  pch2[1] = 0;
422  actpos2++;
423 
424  if ( pch2[0] ) {
425  char temp;
426  do {
427  readdata( &temp, 1 ); // endian ok !
428  } while ( temp ); /* enddo */
429  }
430  }
431 
432  pch2 = new char [ actpos2 ];
433 
434  memcpy ( pch2, charbuf.buf, actpos2 );
435 
436  *pc = pch2;
437 }
438 
439 
440 void tnstream::readpnchar(char** pc, int maxlength )
441 {
442  int actpos2 = 0;
443 
444  int maxav = 10000;
445 
446  if ( maxlength )
447  if ( maxav > maxlength )
448  maxav = maxlength;
449 
450 
451  CharBuf charbuf ( maxav );
452 
453  maxav--;
454 
455 
456  char* pch2 = charbuf.buf;
457 
458  int ende = 0;
459 
460  do {
461  char bt;
462 
463  int red = readdata( &bt, 1, 0 );
464 
465  if ( red < 1 ) {
466  ende = 2;
467  *pch2 = 0;
468  } else
469  if ( bt == '\n' || bt == 0 ) {
470  *pch2 = 0;
471  ende = 1;
472  } else
473  if ( bt != '\r' ) {
474  *pch2 = bt;
475  pch2++;
476  actpos2++;
477  }
478 
479  } while ( !ende && actpos2 < maxav ); /* enddo */
480 
481 
482  if ( !ende ) {
483  if ( actpos2 >= maxav ) {
484  *pch2 = 0;
485 
486  if ( pch2[0] ) {
487  char temp;
488  do {
489 
490  int red = readdata( &temp, 1, 0 );
491  if ( red < 1 ) {
492  temp = 0;
493  } else
494  if ( temp == '\n' )
495  temp = 0;
496 
497  } while ( temp ); /* enddo */
498  }
499  }
500  }
501  if ( ende == 2 ) {
502  if ( !actpos2 )
503  *pc = NULL;
504  else
505  *pc = strdup ( charbuf.buf );
506  } else
507  *pc = strdup ( charbuf.buf );
508 }
509 
510 
511 bool tnstream::readTextString ( ASCString& s, bool includeCR )
512 {
513  s = "";
514  char c;
515  int red;
516  int end = 0;
517  do {
518  red = readdata( &c, 1, 0 ); // endian ok !
519  if ( red < 1 ) {
520  end = 2;
521  } else
522  if ( (c == '\n' && !includeCR) || c == 0 ) {
523  end = 1;
524  } else
525  if ( c != '\r' )
526  s += c;
527  } while ( red && !end );
528  if ( end == 2)
529  return false;
530  else
531  return true;
532 }
533 
534 
535 ASCString tnstream::readString ( bool includeCR )
536 {
537  ASCString s;
538  bool data = readTextString ( s, includeCR );
539  if ( !data && s.empty() )
540  throw treadafterend ( getLocation() );
541  return s;
542 }
543 
544 
545 void tnstream::writeString(const string& pc, bool binary )
546 {
547  if ( binary )
548  writepchar ( pc.c_str() );
549  else
550  writedata ( pc.data(), pc.length() );
551 }
552 
553 
554 void tnstream::writepchar(const char* pc)
555 {
556  if ( pc ) {
557  const char *pch1 = pc;
558  int loop = 0;
559 
560  do {
561  if ( loop )
562  pch1++;
563  writedata( pch1, 1 );
564  loop++;
565  } while ( *pch1 != 0 ); /* enddo */
566  } else {
567  char pch1 = 0;
568  writedata ( &pch1, 1 );
569  }
570 
571 }
572 
574 
575 
576 
577 
579 {
580  buf = NULL;
581  int bufused = 0;
582  int memreserved = 0;
583  int blocksize = 500000;
584  int red;
585  do {
586  if ( bufused + blocksize > memreserved ) {
587  int newsize = memreserved + blocksize;
588  void* newbuf = malloc (newsize);
589  if ( buf ) {
590  memcpy ( newbuf, buf, bufused );
591  free ( buf );
592  }
593  buf = newbuf;
594  memreserved = newsize;
595  }
596  char* cp = (char*) buf;
597  red = stream->readdata ( cp + bufused, blocksize, 0 ); // endian ok !
598  bufused += red;
599 
600  } while ( red == blocksize );
601  size = bufused;
602  pos = 0;
603 
604  devicename = stream->getDeviceName();
605 }
606 
608 {
609  return devicename + " (memory buffered)";
610 }
611 
613 {
614  if ( buf )
615  free ( buf );
616 }
617 
618 
619 void MemoryStreamCopy :: writedata ( const void* buf, int size )
620 {
622 }
623 
624 int MemoryStreamCopy :: readdata ( void* buffer, int _size, bool excpt )
625 {
626  char* cp = (char*) buf;
627  if ( pos + _size > size ) {
628  if ( excpt )
629  throw treadafterend ( getDeviceName() );
630  else {
631  int tr = size-pos;
632  memcpy ( buffer, cp+pos, tr );
633  pos += tr;
634  return tr;
635  }
636 
637  } else {
638  memcpy ( buffer, cp+pos, _size );
639  pos += _size;
640  return _size;
641  }
642 }
643 
644 void MemoryStreamCopy :: seek ( int newpos )
645 {
646  if ( newpos > size || newpos < 0 )
647  throw treadafterend ( getDeviceName() );
648 
649  pos = newpos;
650 }
651 
652 
654 
655 
656 static int stream_seek( struct SDL_RWops *context, int offset, int whence)
657 {
658  MemoryStreamCopy* stream = (MemoryStreamCopy*) context->hidden.unknown.data1;
659  if ( whence == SEEK_SET )
660  stream->seek ( offset );
661  else
662  if ( whence == SEEK_CUR )
663  stream->seek ( offset + stream->getPosition() );
664  else
665  if ( whence == SEEK_END )
666  stream->seek ( offset + stream->getSize() );
667  return stream->getPosition();
668 }
669 
670 
671 static int stream_read(SDL_RWops *context, void *ptr, int size, int maxnum)
672 
673 {
674  MemoryStreamCopy* stream = (MemoryStreamCopy*) context->hidden.unknown.data1;
675  size_t nread = stream->readdata ( ptr, size * maxnum, 0 );
676 
677  return(nread / size);
678 }
679 
680 static int stream_close(SDL_RWops *context)
681 {
682  if ( context ) {
683  if ( context->hidden.unknown.data1 ) {
684  MemoryStreamCopy* stream = (MemoryStreamCopy*) context->hidden.unknown.data1;
685  delete stream;
686  }
687  SDL_FreeRW(context);
688  }
689  return(0);
690 }
691 
692 
693 SDL_RWops *SDL_RWFromStream( tnstream* stream )
694 {
695  MemoryStreamCopy* msb = new MemoryStreamCopy ( stream );
696 
697  SDL_RWops *rwops;
698 
699  rwops = SDL_AllocRW();
700  if ( rwops != NULL ) {
701  rwops->seek = stream_seek;
702  rwops->read = stream_read;
703  rwops->write = NULL;
704  rwops->close = stream_close;
705  rwops->hidden.unknown.data1 = msb;
706  }
707  return(rwops);
708 }
709 
711 
712 
713 
714 
715 tncontainerstream :: tncontainerstream ( const char* containerfilename, ContainerIndexer* indexer, int dirLevel )
716  : tn_file_buf_stream ( containerfilename, reading ), index(NULL)
717 {
718  num = 0;
719  char magic[4];
720  readdata ( &magic, 4 ); // endian ok !
721  if ( strncmp ( magic, containermagic, 4 ) == 0) {
722  int pos = readInt();
723  seek ( pos );
724  num = readInt();
725  index = new tcontainerindex[num];
726  for ( int i = 0; i < num; i++ ) {
727 
728  bool __loadName = readInt();
729  index[i].start = readInt();
730  index[i].end = readInt();
731  if ( __loadName ) {
732  readpchar ( &index[i].name );
733 
734  #if CASE_SENSITIVE_FILE_NAMES == 1
735  // quick hack to be able to use existing CON files.
736  char *c = index[i].name;
737  while ( *c ) {
738  *c = tolower( *c );
739  c++;
740  }
741  #endif
742 
743  indexer->addfile ( index[i].name, this, dirLevel );
744  } else
745  index[i].name = NULL;
746  }
747  }
748  actfile = NULL;
749  containerfilepos = 0;
750 }
751 
752 
754 {
755  int i = 0;
756  while ( i < num && stricmp ( index[i].name, name ) )
757  i++;
758 
759  if ( i >= num )
760  return -1;
761  else
762  return index[i].end - index[i].start + 1;
763 
764 }
765 
766 
767 void tncontainerstream :: opencontainerfile ( const char* name )
768 {
769  if ( actfile ) {
770  ASCString err = ASCString("two files simultaneously: ") + actfile->name + " and " + name;
771  throw tfileerror ( err );
772  }
773 
774  containerfilepos = 0;
775  int i = 0;
776  while ( i < num && stricmp ( index[i].name, name ) )
777  i++;
778 
779  if ( i >= num )
780  throw tfileerror ( name );
781 
782  displayLogMessage( 9, ASCString("opencontainerfile ") + name );
783  containerfilepos = 0;
784  seek ( index[i].start );
785  actfile = &index[i];
786 }
787 
788 int tncontainerstream :: readcontainerdata ( void* buf, int size, bool excpt )
789 {
790  if ( actfile->start + containerfilepos + size > actfile->end+1 ) {
791  if ( excpt )
792  throw treadafterend ( actfile->name );
793  else {
794  int got = readdata ( buf, (actfile->end+1 - actfile->start) - containerfilepos , excpt );
795  containerfilepos+=got;
796  return got;
797 
798  }
799  }
800 
801  readdata ( buf, size );
802  containerfilepos+=size;
803  return size;
804 }
805 
807 {
808  actfile = NULL;
809 }
810 
812 {
813  actname = 0;
814  return getnextname();
815 }
816 
818 {
819  if ( actname < num )
820  return index[actname++].name;
821  else
822  return NULL;
823 
824 }
825 
826 
827 
829 {
830  for ( int i = 0; i < num; i++ )
831  if ( index[i].name )
832  delete[] index[i].name;
833  delete[] index;
834 }
835 
836 
837 
838 
840 
842  public:
843  struct FileIndex {
847  };
848  protected:
849 
850  dynamic_array<FileIndex> index[256]; // not very efficient, but who cares :-)
851 
854  struct {
855  int alpha;
856  int index;
857  } namesearch; // next entry to return
858  public:
859  ContainerCollector ( void );
860  void init ( const char* wildcard );
861  void addfile ( const char* filename, const pncontainerstream stream, int directoryLevel );
862  // pncontainerstream getfile ( const char* filename );
863  FileIndex* getfile ( const ASCString& filename );
864  FileIndex* getfirstname ( void );
865  FileIndex* getnextname ( void );
867  virtual ~ContainerCollector();
868 };
869 
870 
872 
873 char* constructFileName( char* buf, int directoryLevel, const char* path, const char* filename )
874 {
875  if ( buf ) {
876 
877  const char* filename2 = filename;
878  buf[0] = 0;
879 
880  // filenames beginning with / or ~/ have an absolute path ; ignore variable path for them
881  if ( ! (filename && (filename[0] == pathdelimitter || (filename[0]=='~' && filename[1] == pathdelimitter)) )) {
882  if ( path )
883  strcpy ( buf, path);
884  else
885  if ( directoryLevel >= 0 && ascDirectory[ directoryLevel ] )
886  strcpy ( buf, ascDirectory[ directoryLevel ]);
887  }
888 
889  appendbackslash ( buf );
890 
891  if ( filename && strchr ( filename, pathdelimitter )) {
892 
893  char name2[ maxFileStringSize ];
894  // filename contains directories
895  strcpy ( name2, filename );
896  int i = strlen ( name2 )-1;
897  while ( name2[i] != pathdelimitter )
898  i--;
899 
900  name2[i+1] = 0;
901 
902  filename2 = &filename[i+1];
903 
904  // filename2 is now the pure filename without directory
905  // name2 is the directory
906 
907  if ( buf[0] && name2[0]==pathdelimitter )
908  strcpy ( buf, name2+1);
909  else
910  strcpy ( buf, name2);
911  }
912 
913  if ( buf[0] == '~' && buf[1] == pathdelimitter ) {
914  char* home = getenv ( "HOME" );
915  if ( home ) {
916  char temp[ maxFileStringSize ];
917  strcpy ( temp, buf );
918  strcpy ( buf, home );
919  appendbackslash ( buf );
920  strcat ( buf, &temp[2]);
921  }
922  }
923 
924 
925  appendbackslash ( buf );
926 
927  if ( filename2 )
928  strcat ( buf, filename2 );
929  }
930 
931  return buf;
932 }
933 
934 bool isPathRelative( const ASCString& path )
935 {
936  if ( path.length() < 2 )
937  return true;
938 
939  if ( path[0] == '~' && path[1] == pathdelimitter )
940  return false;
941 
942 #ifdef WIN32
943  if ( path[1] == ':' && path[2] == pathdelimitter )
944  return false;
945 
946 #endif
947 
948  if ( path[0] == pathdelimitter )
949  return false;
950 
951  return true;
952 }
953 
954 ASCString constructFileName( int directoryLevel, const ASCString& path, ASCString filename )
955 {
956  ASCString result;
957 
958  // filenames beginning with / or ~/ have an absolute path ; ignore variable path for them
959  if ( isPathRelative( filename )) {
960  if ( !path.empty() )
961  result += path;
962  else
963  if ( directoryLevel >= 0 && ascDirectory[ directoryLevel ] )
964  result += ascDirectory[ directoryLevel ];
965  }
966 
967  appendbackslash ( result );
968 
969  if ( !filename.empty() && filename.find( pathdelimitter )!= ASCString::npos ) {
970  ASCString dir = filename;
971 
972  dir.erase( dir.rfind( pathdelimitter ) + 1);
973 
974  filename.erase( 0, filename.find( pathdelimitter ) + 1 );
975 
976  if ( dir.find( pathdelimitter ) == 0 )
977  dir.erase( 0, 1 );
978 
979  result = dir;
980  }
981 
982  if ( result.length() > 2 && result[0] == '~' && result[1] == pathdelimitter ) {
983  char* home = getenv ( "HOME" );
984  if ( home ) {
985  ASCString temp = result;
986  result = home;
987  appendbackslash ( result );
988  result += temp.substr( 2 );
989  }
990  }
991 
992 
993  appendbackslash ( result );
994 
995  result += filename;
996 
997  return result;
998 }
999 
1000 
1001 
1005  int found;
1006  };
1007 
1008 void locateFile ( const ASCString& filename, FileLocation* loc )
1009 {
1010  loc->found = 0;
1011  ContainerCollector::FileIndex* idx = containercollector.getfile ( filename );
1012  int maxnum;
1013  if ( idx ) {
1014  maxnum = idx->directoryLevel+1;
1015  loc->directoryLevel = idx->directoryLevel;
1016  loc->found = 1;
1017  loc->container = idx->container;
1018  } else {
1019  maxnum = searchDirNum;
1020  loc->container = NULL;
1021  loc->directoryLevel = -1;
1022  }
1023 
1024  if ( maxnum ) {
1025  int localfound = 0;
1026  for ( int i = 0; i < maxnum && !localfound; i++ ) {
1027  char buf[2000];
1028  FILE* fp = fopen ( constructFileName ( buf, i, NULL, filename.c_str()), "r" );
1029  if ( fp ) {
1030  localfound = loc->found = 1;
1031  fclose ( fp );
1032  loc->container = NULL;
1033  loc->directoryLevel = i;
1034  }
1035  }
1036  } else {
1037  char buf[2000];
1038  FILE* fp = fopen ( constructFileName ( buf, -1, ".", filename.c_str()), "r" );
1039  if ( fp ) {
1040  loc->found = 1;
1041  fclose ( fp );
1042  loc->container = NULL;
1043  loc->directoryLevel = -2;
1044  }
1045  }
1046 }
1047 
1049 
1050 
1051 
1053 {
1054  return containercollector.listContainer();
1055 }
1056 
1057 
1059 {
1060  containernum = 0;
1061 }
1062 
1063 void ContainerCollector :: init ( const char* wildcard )
1064 {
1065  for ( int i = 0; i < searchDirNum; i++ ) {
1066  DIR *dirp;
1067  struct ASC_direct *direntp;
1068 
1069  char buf[ maxFileStringSize ];
1070  char buf2[ maxFileStringSize ];
1071  char buf3 [ maxFileStringSize ];
1072  dirp = opendir( extractPath ( buf2, constructFileName ( buf, i, NULL, wildcard )));
1073  extractFileName ( buf3, buf );
1074  if( dirp != NULL ) {
1075  for(;;) {
1076  direntp = readdir( dirp );
1077  if ( direntp == NULL ) {
1078  break;
1079  }
1080  if ( patimat ( buf3, direntp->d_name )) {
1081  container[containernum++] = new tncontainerstream( constructFileName ( buf, i, buf2, direntp->d_name), this, i);
1082  if ( MessagingHub::Instance().getVerbosity() >= 2 )
1083  printf("container %s mounted\n", buf );
1084  }
1085  }
1086  closedir( dirp );
1087  }
1088  }
1089 }
1090 
1091 void ContainerCollector :: addfile ( const char* filename, const pncontainerstream stream, int directoryLevel )
1092 {
1093  int found = 0;
1094  FileIndex* cci = NULL;
1095 
1096  int i1 = toupper ( filename[0] );
1097  for ( int i = 0; i <= index[i1].getlength(); i++ )
1098  if ( index[i1][i].name.compare_ci ( filename ) == 0 ) {
1099  if ( index[i1][i].directoryLevel <= directoryLevel )
1100  return;
1101  else {
1102  cci = &(index[i1][i]);
1103  found = 1;
1104  }
1105  }
1106 
1107  if ( !found )
1108  cci = &( index[i1][ index[i1].getlength()+1 ] );
1109 
1110  cci->name = filename;
1111  cci->container = stream;
1112  cci->directoryLevel = directoryLevel;
1113 }
1114 
1116 {
1117  int i1 = toupper ( filename[0] );
1118  for ( int i = 0; i <= index[i1].getlength(); i++ )
1119  if ( index[i1][i].name.compare_ci ( filename) == 0 )
1120  return &index[i1][i];
1121 
1122  return NULL;
1123 }
1124 
1125 
1127 {
1128  namesearch.alpha = 0;
1129  namesearch.index = 0;
1130  return getnextname();
1131 }
1132 
1134 {
1135  while ( index[namesearch.alpha].getlength() < namesearch.index) {
1136  if ( namesearch.alpha == 255 )
1137  return NULL;
1138  namesearch.alpha++;
1139  namesearch.index = 0;
1140  } /* endwhile */
1141  return &index[namesearch.alpha][namesearch.index++];
1142 }
1143 
1145 {
1146  ASCString s;
1147  for ( int i = 0; i < containernum; i++ )
1148  s += container[i]->getLocation() + "\n";
1149 
1150  return s;
1151 
1152 }
1153 
1154 
1155 
1157 {
1158  int i;
1159  for (i = 0; i < containernum; i++ )
1160  delete container[i];
1161  containernum = 0;
1162 }
1163 
1164 
1165 
1167 
1169  : stream ( compressedStream )
1170 {
1171 }
1172 void CompressionStreamAdapter::writecmpdata ( const void* buf, int size )
1173 {
1174  stream->writedata( buf, size );
1175 }
1176 
1177 int CompressionStreamAdapter::readcmpdata ( void* buf, int size, bool excpt )
1178 {
1179  return stream->readdata( buf, size, excpt );
1180 }
1181 
1182 
1184  public:
1185  bz_stream bzs;
1186  static const int outputbufsize = 100000;
1188 
1190  bzs.bzalloc = NULL;
1191  bzs.bzfree = NULL;
1192  bzs.opaque = NULL;
1193  };
1194 
1196  BZ2_bzCompressEnd ( &bzs );
1197  }
1198 };
1199 
1201 {
1202  data = new PrivateCompressionData();
1203  BZ2_bzCompressInit ( &data->bzs, 5, 0, 0 );
1204 
1205  stream = strm;
1206 }
1207 
1208 
1209 void libbzip_compression :: writedata ( const void* buf, int size )
1210 {
1211  char* cbuf = (char*) buf;
1212 
1213  data->bzs.next_in = cbuf ;
1214  data->bzs.avail_in = size ;
1215  data->bzs.total_in_lo32 = 0 ;
1216  data->bzs.total_in_hi32 = 0 ;
1217 
1218  while ( data->bzs.total_in_lo32 < size ) {
1219 
1220  data->bzs.next_out = data->outputbuf;
1221  data->bzs.avail_out = data->outputbufsize;
1222  data->bzs.total_out_lo32 = 0;
1223  data->bzs.total_out_hi32 = 0;
1224 
1225  int res = BZ2_bzCompress ( &data->bzs, BZ_RUN );
1226  if ( res < 0 )
1227  throw StreamCompressionError ( "MBZLB2 compression :: writedata", res );
1228 
1229  for ( int i = 0; i < data->bzs.total_out_lo32; i++ )
1230  data->outputbuf[i] ^= bzip_xor_byte;
1231 
1232  if ( data->bzs.total_out_lo32 > 0 )
1233  stream->writecmpdata ( data->outputbuf, data->bzs.total_out_lo32 );
1234  }
1235 }
1236 
1237 
1239 {
1240  int res;
1241  do {
1242  data->bzs.next_in = data->outputbuf;
1243  data->bzs.avail_in = 0;
1244 
1245  data->bzs.next_out = data->outputbuf;
1246  data->bzs.avail_out = data->outputbufsize;
1247  data->bzs.total_out_lo32 = 0;
1248  data->bzs.total_out_hi32 = 0;
1249 
1250  res = BZ2_bzCompress ( &data->bzs, BZ_FINISH );
1251  if ( res < 0 )
1252  throw StreamCompressionError ( "MBZLB2 compression :: closecompression", res );
1253 
1254  for ( int i = 0; i < data->bzs.total_out_lo32; i++ )
1255  data->outputbuf[i] ^= bzip_xor_byte;
1256  stream->writecmpdata ( data->outputbuf, data->bzs.total_out_lo32 );
1257 
1258  } while ( res != BZ_STREAM_END );
1259 
1260 
1261  BZ2_bzCompressEnd ( &data->bzs );
1262 }
1263 
1265 {
1266  delete data;
1267 }
1268 
1269 
1270 
1272  public:
1273  bz_stream bzs;
1274  static const int inputbufsize = 100000;
1278 
1280  bzs.bzalloc = NULL;
1281  bzs.bzfree = NULL;
1282  bzs.opaque = NULL;
1283 
1284  inputbufused = 0;
1285  inputbufread = 0;
1286 
1287  }
1288 
1290  BZ2_bzDecompressEnd ( &bzs );
1291  }
1292 
1293 };
1294 
1296 {
1297  data = new PrivateDecompressionData();
1298  BZ2_bzDecompressInit ( &data->bzs, 0, 0 );
1299  stream = strm;
1300 }
1301 
1302 
1303 int libbzip_decompression :: readdata ( void* buf, int size, bool excpt )
1304 {
1305  int decompressed = 0;
1306  char* cbuf = (char*) buf;
1307 
1308  data->bzs.next_in = cbuf ;
1309  data->bzs.avail_in = size ;
1310  data->bzs.total_in_lo32 = 0 ;
1311  data->bzs.total_in_hi32 = 0 ;
1312 
1313  int abrt = 0;
1314 
1315  while ( decompressed < size && !abrt ) {
1316  if ( data->inputbufread >= data->inputbufused ) {
1317  data->inputbufused = stream->readcmpdata ( data->inputbuf, data->inputbufsize, 0 );
1318 
1319  if ( !data->inputbufused && excpt )
1320  throw StreamCompressionError ( "Decompressor :: out of data", 0 );
1321 
1322 
1323  for ( int i = 0; i < data->inputbufused; i++ )
1324  data->inputbuf[i] ^= bzip_xor_byte;
1325 
1326  data->inputbufread = 0;
1327  }
1328  data->bzs.next_in = data->inputbuf + data->inputbufread;
1329  data->bzs.avail_in = data->inputbufused - data->inputbufread;
1330  data->bzs.total_in_lo32 = 0;
1331  data->bzs.total_in_hi32 = 0;
1332 
1333  data->bzs.next_out = cbuf + decompressed;
1334  data->bzs.avail_out = size - decompressed;
1335  data->bzs.total_out_lo32 = 0;
1336  data->bzs.total_out_hi32 = 0;
1337 
1338  int res = BZ2_bzDecompress ( &data->bzs );
1339  decompressed += data->bzs.total_out_lo32;
1340  data->inputbufread += data->bzs.total_in_lo32;
1341 
1342  if ( decompressed < size ) {
1343  if ( res == BZ_STREAM_END ) {
1344  if ( excpt )
1345  throw treadafterend ( "BZ_decompress_stream" );
1346  abrt = 1;
1347  } else {
1348  if ( res != BZ_OK ) {
1349  if ( excpt ) {
1350  if ( res == BZ_MEM_ERROR )
1351  throw OutOfMemoryError ( -1 );
1352  else
1353  throw StreamCompressionError ( "MBZLB2 decompression :: readdata", res );
1354  }
1355  abrt = 1;
1356  }
1357  }
1358  }
1359  }
1360  return decompressed;
1361 }
1362 
1363 
1365 {
1366  delete data;
1367 }
1368 
1369 
1370 
1372 
1373 /*
1374 t_compressor_2ndbuf_filter :: t_compressor_2ndbuf_filter ( t_compressor_stream_interface* strm )
1375 {
1376  stream = strm;
1377 }
1378 
1379 void t_compressor_2ndbuf_filter :: writecmpdata ( const void* buf, int size )
1380 {
1381  stream->writecmpdata ( buf, size );
1382 }
1383 
1384 int t_compressor_2ndbuf_filter :: readcmpdata ( void* buf, int size, bool excpt )
1385 {
1386  int got = 0;
1387 
1388  char* pc = (char*) buf;
1389 
1390  while ( size && _queue.size() ) {
1391  *pc = _queue.front();
1392  _queue.pop();
1393  pc++;
1394  size--;
1395  got++;
1396  }
1397 
1398  if ( size )
1399  got += stream->readcmpdata ( pc, size, excpt );
1400 
1401  return got;
1402 }
1403 
1404 void t_compressor_2ndbuf_filter :: insert_data_into_queue ( const void* buf, int size )
1405 {
1406  char* pc = (char*) buf;
1407  for (int i = 0; i < size; i++) {
1408  _queue.push ( *pc );
1409  pc++;
1410  }
1411 }
1412 */
1413 
1414 
1415 
1417 {
1418  mmd = md;
1419  bzip_compress = NULL;
1420  bzip_decompress = NULL;
1421 }
1422 
1424 {
1425  if ( mmd == 1 ) {
1426  char buf[10];
1427 
1428  int maxlen = strlen ( BZIP_SIGNATURE ) + 1;
1429  int bufdatanum = readcmpdata ( buf, maxlen, 0 );
1430  int siglen = 0;
1431 
1432  if ( bufdatanum == maxlen ) {
1433 
1434  if ( strncmp ( &buf[1], LZ_SIGNATURE, 9 ) == 0 && !buf[0]) {
1435  status = 110;
1436  siglen = 0;
1437  } else
1438  if ( strncmp ( &buf[1], RLE_SIGNATURE, 9 ) == 0 && !buf[0]) {
1439  status = 111;
1440  siglen = 0;
1441  } else
1442  if ( strncmp ( buf, BZIP_SIGNATURE, 9 ) == 0 ) {
1443  status = 112;
1444  siglen = strlen ( BZIP_SIGNATURE ) + 1;
1445  bzip_decompress = new libbzip_decompression ( this );
1446  } else
1447  status= 109;
1448 
1449  } else
1450  status = 109;
1451 
1452  for ( int i = siglen; i < bufdatanum; i++ )
1453  _queue.push ( buf[i] );
1454 
1455 
1456  } else {
1457  status = 201;
1458  bzip_compress = new libbzip_compression ( this );
1459  writecmpdata ( BZIP_SIGNATURE, strlen ( BZIP_SIGNATURE ) + 1 );
1460  }
1461 }
1462 
1463 
1464 int tanycompression :: readdata ( void* rbuf, int size, bool excpt )
1465 {
1466  int red = 0;
1467  if ( size ) {
1468  switch ( status ) {
1469  case 109: red += readlzwdata ( rbuf, size, excpt );
1470  break;
1471  case 110:
1472  case 111: red += tlzwstreamcompression :: readdata ( rbuf, size, excpt );
1473  break;
1474  case 112: red += bzip_decompress -> readdata ( rbuf, size, excpt );
1475  break;
1476  } /* endswitch */
1477  }
1478  return red;
1479 }
1480 
1481 
1482 
1483 void tanycompression :: writedata ( const void* buf, int size )
1484 {
1485  bzip_compress -> writedata ( buf, size );
1486  // writecmpdata ( buf, size );
1487 }
1488 
1489 
1490 int tanycompression :: readlzwdata ( void* buf, int size, bool excpt )
1491 {
1492  if ( _queue.size() ) {
1493  int got = 0;
1494 
1495  char* pc = (char*) buf;
1496 
1497  while ( size && _queue.size() ) {
1498  *pc = _queue.front();
1499  _queue.pop();
1500  pc++;
1501  size--;
1502  got++;
1503  } /* endwhile */
1504 
1505  if ( size )
1506  got += readcmpdata ( pc, size, excpt );
1507 
1508  return got;
1509  } else
1510  return readcmpdata ( buf, size, excpt );
1511 }
1512 
1513 void tanycompression :: writelzwdata ( const void* buf, int size )
1514 {
1515  writecmpdata ( buf, size );
1516 }
1517 
1518 
1520 {
1521  if ( bzip_compress ) {
1522  bzip_compress->close_compression ( );
1523  delete bzip_compress;
1524  bzip_compress = NULL;
1525  }
1526 }
1527 
1528 
1530 {
1533 
1534  if ( bzip_decompress ) {
1535  delete bzip_decompress;
1536  bzip_decompress = NULL;
1537  }
1538 
1539 }
1540 
1541 
1543 
1544 /*
1545 void tn_lzw_bufstream :: writedata ( const void* buf, int size )
1546 {
1547  tlzwstreamcompression :: writedata ( buf, size );
1548 }
1549 
1550 int tn_lzw_bufstream :: readdata ( void* buf, int size, bool excpt )
1551 {
1552  return tlzwstreamcompression :: readdata ( buf, size, excpt );
1553 }
1554 
1555 int tn_lzw_bufstream :: readlzwdata ( void* buf, int size, bool excpt )
1556 {
1557  return tnbufstream :: readdata ( buf, size, excpt );
1558 }
1559 
1560 void tn_lzw_bufstream :: writelzwdata ( const void* buf, int size )
1561 {
1562  tnbufstream :: writedata ( buf, size );
1563 }
1564 
1565 tn_lzw_bufstream :: ~tn_lzw_bufstream ()
1566 {
1567  tlzwstreamcompression :: close();
1568  tnbufstream :: close();
1569 }
1570 
1571 */
1572 
1574 
1575 void tn_lzw_file_buf_stream :: writedata ( const void* buf, int size )
1576 {
1577  tanycompression :: writedata ( buf, size );
1578 }
1579 
1580 int tn_lzw_file_buf_stream :: readdata ( void* buf, int size, bool excpt )
1581 {
1582  return tanycompression :: readdata ( buf, size, excpt );
1583 }
1584 
1585 int tn_lzw_file_buf_stream:: readcmpdata ( void* buf, int size, bool excpt )
1586 {
1587  return tn_file_buf_stream :: readdata ( buf, size, excpt );
1588 }
1589 
1590 void tn_lzw_file_buf_stream :: writecmpdata ( const void* buf, int size )
1591 {
1592  tn_file_buf_stream :: writedata ( buf, size );
1593 }
1594 
1596 {
1597  close_compression ();
1599 }
1600 
1601 
1603 
1604 
1606 {
1607  #ifdef logfiles
1608  FILE* fp = fopen ( "files.lst", "at" );
1609  fprintf ( fp, "%s\n", name );
1610  fclose ( fp );
1611  #endif
1612 
1613  strm = NULL;
1614  inp = 0;
1615  containerstream = NULL;
1616 
1617  FileLocation fl;
1618  if ( mode == tnstream::reading ) {
1619  locateFile ( name, &fl );
1620 
1621  if ( !fl.found )
1622  throw tfileerror ( name );
1623 
1624  } else {
1625  fl.directoryLevel = 0;
1626  fl.container = NULL;
1627  }
1628 
1629 
1630  if ( fl.container == NULL ) {
1631  char string[2000];
1632 
1633  ASCString fileNameComplete = constructFileName ( string, fl.directoryLevel, NULL, name.c_str());
1634  strm = new tn_file_buf_stream ( fileNameComplete, mode );
1635  inp = 1;
1636  devicename = fileNameComplete;
1637  location = fileNameComplete;
1638 
1639  } else {
1640  containerstream = fl.container;
1641  if ( containerstream ) {
1642  inp = 2;
1643  containerstream->opencontainerfile ( name.c_str() );
1644  devicename = name;
1645  location = name + " located inside " + containerstream->getDeviceName();
1646  } else
1647  throw tfileerror ( name );
1648  }
1649  fname = name;
1650 
1652 }
1653 
1655 {
1656  if ( containerstream )
1657  return containerstream->getDeviceName();
1658  else
1659  return "";
1660 }
1661 
1662 
1664 {
1665  return location;
1666 }
1667 
1669 {
1670  if ( inp == 2 )
1671  return containerstream->getSize();
1672  else
1673  return strm->getSize();
1674 }
1675 
1676 void tn_c_lzw_filestream :: writecmpdata ( const void* buf, int size )
1677 {
1678  if ( inp == 2 )
1680  else
1681  strm->writedata ( buf, size );
1682 }
1683 
1684 int tn_c_lzw_filestream :: readcmpdata ( void* buf, int size, bool excpt )
1685 {
1686  if ( inp == 2 )
1687  return containerstream->readcontainerdata ( buf, size, excpt );
1688  else
1689  return strm->readdata ( buf, size, excpt );
1690 };
1691 
1692 
1693 void tn_c_lzw_filestream :: writedata ( const void* buf, int size )
1694 {
1695  tanycompression :: writedata ( buf, size );
1696 }
1697 
1698 int tn_c_lzw_filestream :: readdata ( void* buf, int size, bool excpt )
1699 {
1700  if ( tanycompression :: mode == readingdirect && !tempbuf.size() )
1701  if ( inp == 2 )
1702  return containerstream->readcontainerdata ( buf, size, excpt );
1703  else
1704  return strm->readdata ( buf, size, excpt );
1705  else
1706  return tanycompression :: readdata ( buf, size, excpt );
1707 };
1708 
1709 
1711 {
1712  if ( inp == 2 )
1713  return containerstream->get_time();
1714  else
1715  return strm->get_time();
1716 }
1717 
1719 {
1720  try {
1721  displayLogMessage( 9, "~tn_c_lzw_filestream " + getLocation() );
1722 
1723  close_compression ();
1724  close();
1725  if ( inp == 1 ) {
1726  delete strm;
1727  strm = NULL;
1728  } else {
1729  displayLogMessage( 9, ASCString("~tn_c_lzw_filestream -> closecontainerfile ") );
1730  containerstream->closecontainerfile();
1731  }
1732  } catch ( ... ) {
1733  displayLogMessage( 9, ASCString("~tn_c_lzw_filestream : caught exception") );
1734  }
1735 }
1736 
1737 
1738 
1740 
1741 static const char asciiCodingTable[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
1742  'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
1743  'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
1744  'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
1745  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
1746  'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
1747  'w', 'x', 'y', 'z', '0', '1', '2', '3',
1748  '4', '5', '6', '7', '8', '9', '+', '/' };
1749 
1750 
1752 
1753 void ASCIIEncodingStream::writedata ( const void* buf, int size ) {
1754  const Uint8* c = (const Uint8*)buf;
1755  for ( int i = 0; i < size; ++i )
1756  put ( c[i] );
1757 }
1758 int ASCIIEncodingStream::readdata ( void* buf, int size, bool excpt ) {
1759  throw tinvalidmode ( "Base64SerializingStream", reading, writing );
1760 }
1761 
1763  {
1764  if ( shift == 0 ) {
1765  buf = c >> 6;
1766  shift = 2;
1767  result += asciiCodingTable[c & 63];
1768  } else if ( shift == 2 ) {
1769  buf |= ( c << 2) & 63;
1770  result += asciiCodingTable[buf];
1771  buf = c >> 4;
1772  shift = 4;
1773  } else if ( shift==4 ) {
1774  buf |= (c << 4) & 63;
1775  result += asciiCodingTable[buf];
1776  shift = 0;
1777  result += asciiCodingTable[int((c>>2) & 63)];
1778  }
1779  }
1781  if ( shift )
1782  result += asciiCodingTable[buf & 63];
1783  result += "#";
1784  }
1785 
1787  {
1788  flush();
1789  return result;
1790  }
1791 
1792 void ASCIIDecodingStream :: generateTable()
1793 {
1794  for ( int i = 0; i< 256; ++i )
1795  reverse[i] = -1;
1796  for ( int i = 0; i < sizeof(asciiCodingTable); ++i )
1797  reverse[ int(asciiCodingTable[i])] = i;
1798 }
1799 
1800 int ASCIIDecodingStream :: get()
1801 {
1802  if ( length < data.length() ) {
1803  int c = data.at(length);
1804  if ( c == '#' )
1805  throw treadafterend("ASCIIDecodingStream");
1806  ++length;
1807 
1808  if ( reverse[c] == -1 )
1809  throw ASCmsgException("Invalid ASCII data to decode");
1810 
1811  return reverse[c];
1812  } else
1813  throw treadafterend("ASCIIDecodingStream");
1814 }
1815 
1816 ASCIIDecodingStream :: ASCIIDecodingStream( const ASCString& data) : shift(0), buf(0), length(0)
1817 {
1818  this->data = data;
1819  generateTable();
1820 }
1821 
1822 void ASCIIDecodingStream :: writedata ( const void* buf, int size )
1823 {
1824  throw tinvalidmode ( "ASCIIDecodingStream", writing, reading );
1825 }
1826 
1827 int ASCIIDecodingStream :: readdata ( void* buffer, int size, bool excpt )
1828 {
1829  int i = 0;
1830  try {
1831  Uint8* cbuf = (Uint8*) buffer;
1832  for ( i = 0; i < size; ++i ) {
1833  if ( shift == 0 ) {
1834  int c = get();
1835  int c2 = get();
1836  cbuf[i] = c | ((c2 << 6) & 0xff);
1837  shift = 2;
1838  buf = c2 >> 2;
1839  } else if ( shift == 2 ) {
1840  int c = get();
1841  cbuf[i] = buf | ((c << 4) & 0xff );
1842  buf = c >> 4;
1843  shift = 4;
1844  } else if ( shift == 4 ) {
1845  int c = get();
1846  cbuf[i] = buf | (c << 2 );
1847  shift = 0;
1848  }
1849  }
1850  }
1851  catch ( treadafterend trae ) {
1852  if ( excpt )
1853  throw trae;
1854  }
1855  return i;
1856 }
1857 
1858 
1860  : adapter( outputstream), compressor( &adapter ), closed(false) {
1861 
1862 }
1863 
1864 void StreamCompressionFilter :: writedata ( const void* buf, int size )
1865 {
1866  compressor.writedata(buf,size);
1867 }
1868 
1869 int StreamCompressionFilter :: readdata ( void* buf, int size, bool excpt )
1870 {
1871  throw tinvalidmode ( "StreamCompressionFilter", reading, writing );
1872 }
1873 
1875 {
1876  if ( closed )
1877  return;
1878 
1879  compressor.close_compression();
1880  closed = true;
1881 }
1882 
1884 {
1885  close();
1886 }
1887 
1888 
1889 StreamDecompressionFilter :: StreamDecompressionFilter( tnstream* inputstream ) : adapter( inputstream), decompressor( &adapter ) {
1890 
1891 }
1892 
1893 void StreamDecompressionFilter :: writedata ( const void* buf, int size )
1894 {
1895  throw tinvalidmode ( "StreamCompressionFilter", writing, reading );
1896 }
1897 
1898 int StreamDecompressionFilter :: readdata ( void* buf, int size, bool excpt )
1899 {
1900  return decompressor.readdata(buf,size,excpt);
1901 }
1902 
1904 int compressrle ( const void* p, void* q)
1905 {
1906  trleheader* sourcehead = (trleheader*) p;
1907  if ( sourcehead->id == 16973 ) {
1908  memcpy ( q, p, sourcehead->size + sizeof ( trleheader ) );
1909  return sourcehead->size + sizeof ( trleheader );
1910  }
1911 
1912 
1913  char* s = (char*) p;
1914  char* d = (char*) q;
1915 
1916  trleheader* header = (trleheader*) q;
1917 
1918  Uint16 x,y;
1919  int size;
1920  {
1921  Uint16* pw = (Uint16*) s;
1922  x = pw[0];
1923  y = pw[1];
1924 
1925  header->x = x;
1926  header->y = y;
1927 
1928  x++;
1929  y++;
1930 
1931  size = x * y;
1932 
1933  header->id = 16973;
1934  }
1935 
1936  {
1937  int bts[256];
1938  memset ( bts, 0, sizeof ( bts ));
1939  for ( int i = 0; i < size ;i++ )
1940  bts[int(s[i+4])]++;
1941 
1942  int min = 70000;
1943  for ( int i = 0; i < 256; i++ )
1944  if ( bts[i] < min ) {
1945  min = bts[i];
1946  header->rle = i;
1947  }
1948 
1949  }
1950 
1951  s+=4;
1952  d+=sizeof ( trleheader );
1953 
1954  {
1955  char* startpos = d;
1956  int xp;
1957 
1958  for (int j = 0; j < y ; j++ ) {
1959  xp = 0;
1960  unsigned char num;
1961  unsigned char actbyte ;
1962 
1963  do {
1964  num = 1;
1965  actbyte = *s;
1966 
1967  while ( xp+num < x && num < 255 && s[num] == actbyte )
1968  num++;
1969 
1970  if ( num > 2 || actbyte == header->rle ) {
1971  *(d++) = header->rle;
1972  *(d++) = num;
1973  *(d++) = actbyte;
1974  } else {
1975  *(d++) = actbyte;
1976  if ( num > 1 )
1977  *(d++) = actbyte;
1978  }
1979 
1980  s += num;
1981  xp += num;
1982  } while ( xp < x );
1983 
1984 
1985  } /* endfor */
1986 
1987  header->size = d - startpos;
1988  }
1989  return header->size + sizeof ( trleheader );
1990 }
1991 
1993 
1994 
1995 
1996 
1997 bool patimat (const char *pat, const char *str, bool forceCaseInsensitivity )
1998 {
1999  switch (*pat)
2000  {
2001  case '\0':
2002  return !*str;
2003 
2004  case '*' :
2005  return patimat(pat+1, str, forceCaseInsensitivity) || (*str && patimat(pat, str+1, forceCaseInsensitivity));
2006 
2007  case '?' :
2008  return *str && patimat(pat+1, str+1, forceCaseInsensitivity);
2009 
2010  default :
2011  if ( forceCaseInsensitivity ||
2013  true
2014 #else
2015  false
2016 #endif
2017  )
2018  return (toupper(*pat) == toupper(*str)) && patimat(pat+1, str+1, forceCaseInsensitivity);
2019  else
2020  return (*pat == *str) && patimat(pat+1, str+1, forceCaseInsensitivity );
2021  }
2022 }
2023 
2024 bool patimat (const ASCString& pat, const ASCString& str, bool forceCaseInsensitivity)
2025 {
2026  return patimat( pat.c_str(), str.c_str(), forceCaseInsensitivity );
2027 }
2028 
2029 
2030 tfindfile :: tfindfile ( ASCString name, SearchPosition searchPosition, SearchTypes searchTypes )
2031 {
2032  convertPathDelimitters ( name );
2033 
2034  if ( searchPosition == DefaultDir )
2035  searchPosition = AllDirs;
2036 
2037 
2038  if ( searchDirNum == 0 )
2039  searchPosition = CurrentDir;
2040 
2041  found = 0;
2042  act = 0;
2043  if ( name.empty() )
2044  return;
2045 
2046  ASCString directory[maxSearchDirNum];
2047  int dirNum;
2048  ASCString wildcard;
2049 
2050  int ppos = name.rfind ( pathdelimitterstring );
2051  if ( ppos != name.npos ) {
2052  // name contains a directory entry
2053 
2054 
2055  // checking if absolute or relative path
2056  bool absolute = false;
2057  if ( name[0] == pathdelimitter )
2058  absolute = true;
2059 
2060  if ( has_drive_letters && name.length() > 3 && name.find ( ":\\", 1 ) != name.npos )
2061  absolute = true;
2062 
2063  if ( absolute || searchPosition == CurrentDir ) {
2064  directory[0].assign ( name, 0, ppos );
2065  dirNum = 1;
2066  } else {
2067  ASCString strippedPath;
2068  strippedPath.assign ( name, 0, ppos );
2069  if ( strippedPath.find ( ASCString(".") + pathdelimitterstring ) == 0 )
2070  strippedPath.erase( 0, 2);
2071 
2072  int upDir = 0;
2073  while ( strippedPath.find ( ASCString("..") + pathdelimitterstring ) == 0 ) {
2074  upDir++;
2075  strippedPath.erase ( 0, 3 );
2076  }
2077 
2078  int dirsToProcess;
2079  if ( searchPosition == AllDirs ) {
2080  dirsToProcess = searchDirNum;
2081  } else
2082  dirsToProcess = 1;
2083 
2084  dirNum = 0;
2085  for ( int i = 0; i < dirsToProcess; i++ ) {
2086  ASCString dir = ascDirectory[i];
2087 
2088  // removing the trailing pathdelimitterstring
2089  dir.erase ( dir.length() -1 );
2090 
2091  for ( int j = 0; j < upDir; j++ ) {
2092  int pos = dir.rfind ( pathdelimitterstring );
2093  if ( pos > 0 && pos == dir.npos )
2094  dir.erase ( pos );
2095  }
2096 
2097  // append the trailing pathdelimitterstring again
2098  dir += pathdelimitterstring;
2099 
2100  directory[dirNum++] = dir + strippedPath;
2101  }
2102 
2103  if ( !dirNum ) {
2104  directory[0] = ".";
2105  dirNum = 1;
2106  }
2107 
2108  }
2109 
2110  wildcard.assign ( name, ppos+1, name.npos );
2111 
2112  } else {
2113  if ( searchDirNum ) {
2114  for (int i = 0; i < searchDirNum; i++ )
2115  directory[i] = ascDirectory[i];
2116  dirNum = searchDirNum;
2117  } else {
2118  directory[0] = ".";
2119  dirNum = 1;
2120  }
2121  wildcard = name;
2122  }
2123 
2124  if ( searchTypes == All || searchTypes == OutsideContainer )
2125  for ( int i = 0; i < dirNum; i++ ) {
2126  DIR *dirp;
2127  struct ASC_direct *direntp;
2128 
2129  dirp = opendir( directory[i].c_str() );
2130  if( dirp != NULL ) {
2131  for(;;) {
2132  direntp = readdir( dirp );
2133  if ( direntp == NULL )
2134  break;
2135 
2136  if ( patimat ( wildcard.c_str(), direntp->d_name )) {
2137  int localfound = 0;
2138  for ( int j = 0; j < found; j++ )
2139  if ( strcmpi ( fileInfo[j].name.c_str(), direntp->d_name ) == 0 )
2140  localfound++;
2141 
2142  if ( !localfound ) {
2143  FileInfo fi;
2144  fi.name = direntp->d_name;
2145  fi.directoryLevel = i ;
2146  fi.isInContainer = false ;
2147  fi.location = directory[i];
2148 
2149  char buf[1000];
2150  ASCString fullName = constructFileName( buf, i, NULL, direntp->d_name );
2151 
2152  struct stat statbuf;
2153  stat( fullName.c_str(), &statbuf);
2154 
2155  fi.size = statbuf.st_size ;
2156  fi.date = statbuf.st_mtime;
2157 
2158  fileInfo.push_back ( fi );
2159 
2160  found++;
2161  }
2162  }
2163  }
2164  closedir( dirp );
2165  }
2166  }
2167 
2168 
2169 
2170  if ( searchTypes == All || searchTypes == InsideContainer ) {
2171  const ContainerCollector::FileIndex* c = containercollector.getfirstname();
2172  while ( c ) {
2173  if ( patimat ( name.c_str(), c->name ) ) {
2174  int f = 0;
2175  for ( int i = 0; i < found; i++ )
2176  if ( stricmp ( c->name.c_str(), fileInfo[i].name.c_str() ) == 0 ) {
2177  if ( fileInfo[i].directoryLevel <= c->directoryLevel )
2178  f = 1;
2179  else {
2180  FileInfo& fi = fileInfo[i];
2181  fi.name = c->name ;
2182  fi.isInContainer = true;
2184  fi.location = c->container->getDeviceName();
2185 
2186  fi.size = c->container->getstreamsize();
2187  fi.date = c->container->get_time();
2188  f = 1;
2189  }
2190  }
2191 
2192  if ( !f ) {
2193  FileInfo fi;
2194  fi.name = c->name ;
2195  fi.directoryLevel = c->directoryLevel ;
2196  fi.isInContainer = true ;
2197  fi.location = c->container->getDeviceName() ;
2198 
2199  fi.size = c->container->getstreamsize() ;
2200  fi.date = c->container->get_time() ;
2201  fileInfo.push_back ( fi );
2202  found++;
2203  }
2204  }
2205  c = containercollector.getnextname();
2206  }
2207  }
2208 }
2209 
2210 
2211 ASCString tfindfile :: getnextname ( int* loc, bool* inContainer, ASCString* location )
2212 {
2213  if ( act < found ) {
2214  if ( loc )
2215  *loc = fileInfo[act].directoryLevel;
2216 
2217  if ( inContainer )
2218  *inContainer = fileInfo[act].isInContainer;
2219 
2220  if ( location )
2221  *location = fileInfo[act].location;
2222 
2223  /*
2224  if ( directoryLevel[act] >= 0 && this->location[act] != ascDirectory[directoryLevel[act]] ) {
2225  ASCString s = this->location[act];
2226  appendbackslash ( s );
2227  s += names[act++];
2228  return s;
2229  } else */
2230  return fileInfo[act++].name;
2231  } else {
2232  if ( loc )
2233  *loc = -1;
2234 
2235  return "";
2236  }
2237 }
2238 
2240 {
2241  if ( act < found ) {
2242  fi = fileInfo[act++];
2243  return true;
2244  } else
2245  return false;
2246 }
2247 
2248 
2250 
2251 
2252 
2253 
2254 
2255 int checkforvaliddirectory ( char* dir )
2256 {
2257  int stat = 0;
2258 
2259  DIR *dirp;
2260  struct ASC_direct *direntp;
2261 
2262 /* char temp[200];
2263  int l = strlen(dir) - 1;
2264  for (int i = 0; i < l; i++) {
2265  temp[i] = dir[i];
2266  }
2267  temp[i] = 0;
2268 */
2269  dirp = opendir( dir );
2270  if( dirp != NULL ) {
2271  for(;;) {
2272  direntp = readdir( dirp );
2273  if ( direntp == NULL )
2274  break;
2275 
2276  if ( strcmp ( direntp -> d_name, ".") == 0 )
2277  stat = 1;
2278  }
2279  closedir( dirp );
2280  }
2281 
2282  return stat;
2283 }
2284 
2286 
2288  {
2289  used = 0;
2290  allocated = 0;
2291  buf = 0;
2292  memset ( dummy, 0, sizeof ( dummy ));
2293  }
2294 
2296  {
2297  if ( buf ) {
2298  delete[] buf;
2299  buf = NULL;
2300  }
2301  }
2302 
2304  {
2305  if ( stream ) {
2306  stream->writeInt ( 0 );
2307  stream->writeInt ( used );
2308  stream->writeInt ( allocated );
2309  for ( int i = 0; i < 10; i++ )
2310  stream->writeInt ( dummy[i] );
2311  if ( used > 0 )
2312  stream->writedata ( buf, used );
2313  }
2314  }
2315 
2317  {
2318  if ( stream ) {
2319  stream->readInt();
2320  used = stream->readInt();
2321  allocated = stream->readInt();
2322  for ( int i = 0; i< 10; i++ )
2323  dummy[i] = stream->readInt();
2324 
2325  if ( buf ) {
2326  delete[] buf;
2327  buf = NULL;
2328  }
2329  if ( used > 0 || allocated > 0 ) {
2330  allocated = max(allocated,used);
2331  buf = new Uint8[allocated];
2332  stream->readdata ( buf, used );
2333  }
2334  }
2335  }
2336 
2337 
2339 {
2340 
2341  blocksize = 1024;
2342  buf = lbuf;
2343  _mode = lmode;
2344 
2345  if ( !buf )
2346  throw tfileerror ( "memorystream" );
2347 
2348 
2349  if ( _mode == reading ) {
2350  pointer = buf->buf;
2351  actmempos = 0;
2352  } else
2353  if ( _mode == writing ) { // neuen Puffer anlegen
2354  if ( buf->buf ) {
2355  delete[] buf->buf;
2356  buf->buf = NULL;
2357  }
2358  buf->buf = new Uint8[blocksize];
2359  buf->allocated = blocksize;
2360  buf->used = 0;
2361  pointer = buf->buf;
2362  actmempos = 0;
2363  }
2364  if ( _mode == appending ) {
2365  pointer = buf->buf;
2366  actmempos = buf->used;
2367  _mode = writing;
2368  }
2369 }
2370 
2371 void MemoryStream :: writedata ( const void* nbuf, int size )
2372 {
2373  if ( _mode != writing )
2374  throw tinvalidmode ( "memorystream", _mode, writing );
2375 
2376  if ( buf->used + size > buf->allocated ) {
2377  int newsize = ((buf->used + size + blocksize - 1) / blocksize);
2378  newsize *= blocksize;
2379  Uint8* tmp = new Uint8[newsize];
2380  memcpy ( tmp, buf->buf, buf->used );
2381  delete[] buf->buf;
2382  buf->buf = tmp;
2383  buf->allocated = newsize;
2384  }
2385 
2386  memcpy ( &buf->buf[buf->used], nbuf, size );
2387  buf->used += size;
2388 }
2389 
2390 
2391 int MemoryStream :: readdata ( void* nbuf, int size, bool excpt )
2392 {
2393  if (_mode != reading )
2394  throw tinvalidmode ( "memorystream", _mode, reading );
2395 
2396  if ( actmempos + size > buf->used ) {
2397  if ( excpt )
2398  throw treadafterend ( "memory stream" );
2399  else
2400  size = buf->used-actmempos;
2401  }
2402 
2403  memcpy ( nbuf, &buf->buf[actmempos], size );
2404  actmempos += size;
2405  return size;
2406 }
2407 
2409 {
2410  if ( _mode == writing )
2411  return 1;
2412  else
2413  return actmempos < buf->used;
2414 }
2415 
2416 
2417 
2418 ASCString getnextfilenumname ( const ASCString& first, const ASCString& suffix, int num )
2419 {
2420  ASCString name;
2421 
2422  if ( num < 0 )
2423  num = 0;
2424 
2425  do {
2426  name = first;
2427  while ( name.length() - first.length() + ASCString::toString(num).length() < 3 )
2428  name += "0";
2429 
2430  name += ASCString::toString(num) + "." + suffix;
2431 
2432  tfindfile ff ( name );
2433  ASCString c = ff.getnextname();
2434  if ( c.empty() )
2435  return name;
2436 
2437  num++;
2438  } while ( true );
2439 
2440  return "";
2441 }
2442 
2443 
2444 bool exist ( const ASCString& s )
2445 {
2446  tfindfile ff ( s );
2447  return !ff.getnextname().empty();
2448 }
2449 
2450 
2451 
2452 #include "oldlzw.cpp"
2453 
2455 }
2456 
2457 void opencontainer ( const char* wildcard )
2458 {
2459  if ( !searchDirNum )
2460  addSearchPath(".");
2461 
2462  containercollector.init ( wildcard );
2463 }
2464 
2465 
2466 
2467 
2468 time_t get_filetime ( const char* fileName )
2469 {
2470  FileLocation fl;
2471  locateFile ( fileName, &fl );
2472 
2473  if ( fl.found ) {
2474  if ( fl.container )
2475  return fl.container->get_time();
2476  else {
2477  struct stat stbuf;
2478  char buf[ maxFileStringSize ];
2479  if ( !stat ( constructFileName ( buf, fl.directoryLevel, NULL, fileName), &stbuf) )
2480  return ( stbuf.st_mtime);
2481  else
2482  return -1;
2483  }
2484  } else
2485  return -1;
2486 }
2487 
2488 
2489 int filesize( const char *name)
2490 {
2491  struct stat buf;
2492 
2493  if ( !stat (name, &buf))
2494  return (buf.st_size);
2495  else
2496  return -1;
2497 }
2498 
2499 
2500 bool directoryExist ( const ASCString& path )
2501 {
2502  bool existence = false;
2503 
2504  DIR *dirp = opendir( path.c_str() );
2505 
2506  if( dirp ) {
2507  if ( readdir( dirp ) )
2508  existence = true;
2509  else
2510  existence = false;
2511 
2512  closedir( dirp );
2513  }
2514  return existence;
2515 }
2516 
2517 void addSearchPath ( const ASCString& path )
2518 {
2519  if ( !path.empty() ) {
2520  ASCString s = constructFileName ( -3, path, "" );
2521 
2522  if ( directoryExist( s.c_str() )) {
2523  bool found = false;
2524  for ( int i = 0; i < searchDirNum; i++ )
2525  if ( s == ascDirectory[i] )
2526  found = true;
2527 
2528  if ( !found )
2529  ascDirectory[ searchDirNum++ ] = strdup ( s.c_str() );
2530  }
2531  }
2532 }
2533 
2535 {
2536  return searchDirNum;
2537 }
2538 
2540 {
2541  if ( i < searchDirNum )
2542  return ascDirectory[i];
2543  else
2544  return "";
2545 }
2546 
2547 
2548 char* extractPath ( char* buf, const char* filename )
2549 {
2550  if ( buf && filename ) {
2551  if ( strchr ( filename, pathdelimitter )) {
2552  strcpy ( buf, filename );
2553  int i = strlen ( buf )-1;
2554  while ( buf[i] != pathdelimitter )
2555  i--;
2556 
2557  buf[i+1] = 0;
2558  } else
2559  strcpy ( buf, "./" );
2560  }
2561  return buf;
2562 }
2563 
2564 char* extractFileName ( char* buf, const char* filename )
2565 {
2566  if ( buf && filename ) {
2567  if ( strchr ( filename, pathdelimitter )) {
2568  int i = strlen ( filename )-1;
2569  while ( filename[i] != pathdelimitter )
2570  i--;
2571 
2572  strcpy ( buf, filename +i+1);
2573  } else
2574  strcpy ( buf, filename );
2575  }
2576  return buf;
2577 }
2578 
2580 {
2581  char buf[10000];
2582  return extractFileName( buf, filename.c_str() );
2583 }
2584 
2586 {
2587  char buf[10000];
2588  extractFileName( buf, filename.c_str() );
2589  char* c = strchr ( buf, '.' );
2590  if ( c )
2591  *c = 0;
2592  return ASCString(buf);
2593 }
2594 
2595 
2596 void appendbackslash ( char* string )
2597 {
2598  if ( strlen ( string ) && string[strlen ( string ) -1] != pathdelimitter )
2599  strcat ( string, pathdelimitterstring );
2600 }
2601 
2602 void appendbackslash ( ASCString& string )
2603 {
2604  if ( !string.empty() && string[ string.length() -1] != pathdelimitter )
2605  string += pathdelimitterstring ;
2606 }
2607 
2608 
2609 int createDirectory ( const char* name )
2610 {
2611  #ifdef _UNIX_
2612  char *nname;
2613  int i;
2614 
2615  if (name == NULL || (nname=strdup(name)) == NULL)
2616  return -1;
2617  i = strlen(nname);
2618  /* leave one '/' */
2619  while (i>1 && nname[i-1] == '/')
2620  nname[--i] = '\0';
2621  i = mkdir ( nname, 0700 );
2622  free(nname);
2623 
2624  return i;
2625  #else
2626  return mkdir ( name );
2627  #endif
2628 }
2629 
2630 
2632 {
2633  size_type slash = find_last_of ( pathdelimitterstring );
2634  size_type point = find_last_of ( "." );
2635  if ( point == npos )
2636  return "";
2637  else
2638  if ( slash == npos || slash < point )
2639  return substr(point+1);
2640  else
2641  return "";
2642 }
2643 
2644 
2645 
2647 {
2648  int pos;
2649  while ( (pos = path.find ( foreignPathDelimitterString )) != path.npos )
2650  path.replace ( pos, 1, pathdelimitterstring );
2651 }
Buffers the complete contents of a stream in memory.
Definition: basestrm.h:148
virtual int readcmpdata(void *buf, int size, bool excpt=true)
Definition: basestrm.cpp:1177
static const char asciiCodingTable[64]
Definition: basestrm.cpp:1741
#define stricmp
virtual ~tn_c_lzw_filestream()
Definition: basestrm.cpp:1718
An in-memory storage of streamed data.
Definition: basestrm.h:172
void writedata(const void *buf, int size)
writes data to the stream
Definition: basestrm.cpp:619
virtual void seek(int newpos)
Sets the stream pointer to a new location. An exception is thrown if the stream does not support seek...
Definition: basestrm.cpp:197
ASCString getResult()
Definition: basestrm.cpp:1786
int readcontainerdata(void *buf, int size, bool excpt=true)
Definition: basestrm.cpp:788
tinternalerror(const char *filename, int l)
Definition: basestrm.cpp:163
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
tanycompression(int md)
Definition: basestrm.cpp:1416
void close_compression(void)
Definition: basestrm.cpp:1519
virtual ~tncontainerstream()
Definition: basestrm.cpp:828
const ASCString & getFileName() const
Definition: simplestream.h:80
ASCString getSearchPath(int i)
Definition: basestrm.cpp:2539
void writedata(const void *buf, int size)
writes data to the stream
Definition: basestrm.cpp:1575
ASCString getnextfilenumname(const ASCString &first, const ASCString &suffix, int num)
Definition: basestrm.cpp:2418
virtual void writedata(const void *buf, int size)
writes data to the stream
Definition: basestrm.cpp:1753
virtual ~libbzip_compression()
Definition: basestrm.cpp:1264
unsigned short int size
Definition: basestrm.cpp:76
int readdata(void *buf, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:1698
float SwapFloat(float f)
Definition: basestrm.cpp:312
int readdata(void *buf, int size, bool excpt=true)
Definition: basestrm.cpp:1464
virtual ASCString getLocation()
returns the location of the stream.
Definition: basestrm.cpp:274
pncontainerstream container
Definition: basestrm.cpp:845
virtual void writecmpdata(const void *buf, int size)
Definition: basestrm.cpp:1172
int dataavail(void)
Definition: basestrm.cpp:2408
int checkforvaliddirectory(char *dir)
Definition: basestrm.cpp:2255
static const int inputbufsize
Definition: basestrm.cpp:1274
FileIndex * getfirstname(void)
Definition: basestrm.cpp:1126
unsigned short int y
Definition: basestrm.cpp:79
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
virtual int readdata(void *buf, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:1869
virtual ASCString getDeviceName()
returns the name of the stream.
Definition: basestrm.cpp:269
ASCString getnextname(int *loc=NULL, bool *inContainer=NULL, ASCString *location=NULL)
Returns the next entry of the internal file list.
Definition: basestrm.cpp:2211
static int stream_read(SDL_RWops *context, void *ptr, int size, int maxnum)
Definition: basestrm.cpp:671
char * getfirstname(void)
Definition: basestrm.cpp:811
int readdata(void *buf, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:1580
const int maxSearchDirNum
Definition: basestrm.cpp:65
tn_c_lzw_filestream(const ASCString &name, IOMode mode)
Definition: basestrm.cpp:1605
tncontainerstream(const char *containerfilename, ContainerIndexer *indexer, int directoryLevel)
Definition: basestrm.cpp:715
virtual void writedata(const void *buf, int size)
writes data to the stream
Some old LZW encoding and decoding stuff. Included by basestrm.cpp.
DIR * opendir(const char *)
void addSearchPath(const ASCString &path)
Definition: basestrm.cpp:2517
MemoryStream(MemoryStreamStorage *lbuf, IOMode mode)
Definition: basestrm.cpp:2338
ContainerCollector containercollector
Definition: basestrm.cpp:871
unsigned short int x
Definition: basestrm.cpp:78
virtual void readrlepict(void **pnter, bool allocated, int *size)
Writes an RLE compressed or uncompressed image from the stream.
Definition: basestrm.cpp:202
int searchDirNum
Definition: basestrm.cpp:66
virtual int readWord(void)
Reads a 16 bit unsigned Integer. In the stream little-endian byte order is used and a translation is ...
Definition: basestrm.cpp:291
char * ascDirectory[maxSearchDirNum]
Definition: basestrm.cpp:67
virtual int getSize(void)
returns the size of the stream or -1 if the stream does not have a size
Definition: simplestream.h:70
unsigned short int id
Definition: basestrm.cpp:75
virtual void addfile(const char *filename, const pncontainerstream stream, int directoryLevel)=0
const int maxFileStringSize
Definition: basestrm.h:62
Global platform dependant definitions. This file just branches to the platform specific files in thei...
The interface for all kinds of IO stream.
void close(void)
Definition: oldlzw.cpp:458
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
if(!yyg->yy_init)
Definition: scanner.cpp:695
#define blocksize
Definition: dialog.cpp:1428
const char * BZIP_SIGNATURE
Definition: basestrm.cpp:90
IOMode _mode
Definition: basestrm.h:208
const char * foreignPathDelimitterString
Definition: unix/fileio.cpp:26
int readdata(void *buf, int size, bool excpt=true)
Definition: oldlzw.cpp:314
virtual Uint8 readUint8(void)
Reads a 8 bit unsigned Integer.
Definition: basestrm.cpp:305
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
void writetostream(tnstream *stream)
persist the contents in another stream
Definition: basestrm.cpp:2303
ASCString devicename
SearchPosition
Definition: basestrm.h:527
static const int outputbufsize
Definition: basestrm.cpp:1186
CharBuf(void)
Definition: basestrm.cpp:100
char * extractPath(char *buf, const char *filename)
Definition: basestrm.cpp:2548
virtual int readdata(void *buf, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:1758
ASCString getArchive()
returns the archive name if the stream is located inside an archive; or "" if not.
Definition: basestrm.cpp:1654
struct direct * readdir(DIR *)
virtual void writecmpdata(const void *buf, int size)=0
#define strcmpi
virtual void writerlepict(const void *buf)
Writes an image to the stream and compresses it using RLE.
Definition: basestrm.cpp:236
ASCString extractFileName_withoutSuffix(const ASCString &filename)
Definition: basestrm.cpp:2585
int getPosition(void)
Definition: basestrm.h:160
void writedata(const void *buf, int size)
Definition: basestrm.cpp:1483
virtual void writeString(const string &pc, bool binary=true)
writes the C++ String pc to the stream.
Definition: basestrm.cpp:545
char * malloc(int)
virtual float readFloat(void)
Reads a flaot variable.
Definition: basestrm.cpp:328
OutOfMemoryError(int m)
Definition: basestrm.cpp:136
int readdata(void *buf, int size, bool excpt=true)
Definition: basestrm.cpp:1303
static int stream_close(SDL_RWops *context)
Definition: basestrm.cpp:680
virtual time_t get_time(void)
treadafterend(const ASCString &fileName)
Definition: basestrm.cpp:156
const int magic
Definition: stack.cpp:36
CompressionStreamAdapter(tnstream *compressedStream)
Definition: basestrm.cpp:1168
Uint16 x
Definition: basegfx.h:83
Uint8 * pointer
Definition: basestrm.h:207
int filesize(const char *name)
Definition: basestrm.cpp:2489
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
~CharBuf()
Definition: basestrm.cpp:123
tinvalidversion(const ASCString &fileName, int ex, int fnd)
Definition: basestrm.cpp:171
Definition: ndir.h:53
void resize(int newsize)
Definition: basestrm.cpp:113
virtual void writeImage(const void *buf, bool compress=true)
Writes an image to the stream.
Definition: basestrm.cpp:241
virtual int readdata(void *buf, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:1898
char outputbuf[outputbufsize]
Definition: basestrm.cpp:1187
#define ASC_direct
Definition: unix/fileio.h:91
virtual int readlzwdata(void *buf, int size, bool excpt=true)
Definition: basestrm.cpp:1490
char * buf
Definition: basestrm.h:68
int getcontainerfilesize(const char *name)
Definition: basestrm.cpp:753
bool directoryExist(const ASCString &path)
Definition: basestrm.cpp:2500
void put(Uint8 c)
Definition: basestrm.cpp:1762
bool isPathRelative(const ASCString &path)
Definition: basestrm.cpp:934
void close_compression(void)
Definition: basestrm.cpp:1238
int getSearchPathNum()
Definition: basestrm.cpp:2534
FileIndex * getfile(const ASCString &filename)
Definition: basestrm.cpp:1115
void displayLogMessage(int msgVerbosity, const char *message,...)
int directoryLevel
Definition: basestrm.cpp:1003
tinvalidmode(const ASCString &fileName, tnstream::IOMode org_mode, tnstream::IOMode requested_mode)
Definition: basestrm.cpp:147
void init(const char *wildcard)
Definition: basestrm.cpp:1063
int actmempos
Definition: basestrm.h:209
int getSize(void)
returns the size of the stream or -1 if the stream does not have a size
Definition: basestrm.h:161
virtual void writeUint8(Uint8 c)
Writes a 8 bit unsigned Integer.
Definition: basestrm.cpp:380
Uint16 y
Definition: basegfx.h:84
libbzip_compression(CompressionStreamInterface *strm)
Definition: basestrm.cpp:1200
Searching for files.
Definition: basestrm.h:484
ASCString name
Definition: basestrm.h:489
virtual int readdata(void *buf, int size, bool excpt=true)=0
Reads data from the stream.
static int stream_seek(struct SDL_RWops *context, int offset, int whence)
Definition: basestrm.cpp:656
void locateFile(const ASCString &filename, FileLocation *loc)
Definition: basestrm.cpp:1008
tfindfile(ASCString name, SearchPosition searchPosition=DefaultDir, SearchTypes searchTypes=All)
Searches for files matching the wildcard name in all search paths specified for ASC and inside the AS...
Definition: basestrm.cpp:2030
The interface for the various streams that ASC offers, like file and memory streams.
int compressrle(const void *p, void *q)
Definition: basestrm.cpp:1904
void init(void)
Definition: basestrm.cpp:1423
ASCString location
Definition: basestrm.h:492
char * constructFileName(char *buf, int directoryLevel, const char *path, const char *filename)
Definition: basestrm.cpp:873
ASCString getLocation()
returns the location of the stream.
Definition: basestrm.cpp:1663
libbzip_decompression(CompressionStreamInterface *strm)
Definition: basestrm.cpp:1295
Uint16 size
Definition: basegfx.h:81
void convertPathDelimitters(ASCString &path)
converts path delimitters from foreign operating systems to the ones used by the current operating sy...
Definition: basestrm.cpp:2646
const char pathdelimitter
Definition: unix/fileio.cpp:23
int createDirectory(const char *name)
Definition: basestrm.cpp:2609
ASCString suffix()
return the suffix of the file or an empty string if the file doesn't have a suffix.
Definition: basestrm.cpp:2631
char inputbuf[inputbufsize]
Definition: basestrm.cpp:1275
virtual int readcmpdata(void *buf, int size, bool excpt=true)=0
virtual int readdata(void *buffer, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:1827
virtual int getSize(void)
returns the size of the stream or -1 if the stream does not have a size
Definition: basestrm.cpp:1668
virtual void writebuffer(void)=0
Definition: basestrm.cpp:2454
dynamic_array< FileIndex > index[256]
Definition: basestrm.cpp:850
virtual void seek(int newpos)
Sets the stream pointer to a new location. An exception is thrown if the stream does not support seek...
const int red
Definition: basegfx.h:91
virtual void writeCharacter(char c)
Writes a 8 bit character.
Definition: basestrm.cpp:375
ASCString listContainer()
Definition: basestrm.cpp:1144
virtual int readdata(void *nbuf, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:2391
int blocksize
Definition: basestrm.h:206
const char * pathdelimitterstring
Definition: unix/fileio.cpp:24
Uint16 id
Definition: basegfx.h:80
ASCString listContainer()
Definition: basestrm.cpp:1052
DEPRECATED Use containers of the C++ Standard Template Library instead! Container for managing object...
Definition: basestrm.h:121
void addfile(const char *filename, const pncontainerstream stream, int directoryLevel)
Definition: basestrm.cpp:1091
const char * RLE_SIGNATURE
Definition: basestrm.cpp:89
void opencontainer(const char *wildcard)
Definition: basestrm.cpp:2457
void appendbackslash(char *string)
Definition: basestrm.cpp:2596
char * getnextname(void)
Definition: basestrm.cpp:817
virtual ASCString readString(bool includeCR=false)
Reads and returns a string.
Definition: basestrm.cpp:535
class tncontainerstream * pncontainerstream
Definition: basestrm.h:406
virtual void writedata(const void *buf, int size)
writes data to the stream
Definition: basestrm.cpp:1893
MemoryStreamStorage * buf
Definition: basestrm.h:210
time_t get_filetime(const char *fileName)
Definition: basestrm.cpp:2468
virtual void close(void)
Definition: simplestream.h:41
const char * containermagic
Definition: basestrm.cpp:86
virtual void readpnchar(char **pc, int maxlength=0)
Reads a string.
Definition: basestrm.cpp:440
const bool has_drive_letters
Definition: unix/fileio.cpp:30
void seek(int newpos)
Sets the stream pointer to a new location. An exception is thrown if the stream does not support seek...
Definition: basestrm.cpp:644
char * extractFileName(char *buf, const char *filename)
Definition: basestrm.cpp:2564
const char * LZ_SIGNATURE
Definition: basestrm.cpp:88
int readdata(void *buf, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:624
virtual ~libbzip_decompression()
Definition: basestrm.cpp:1364
ASCIIDecodingStream(const ASCString &data)
Definition: basestrm.cpp:1816
void readfromstream(tnstream *stream)
read persisted contents from another stream
Definition: basestrm.cpp:2316
virtual void writedata(const void *nbuf, int size)
writes data to the stream
Definition: basestrm.cpp:2371
virtual ASCString getArchive()
returns the archive name if the stream is located inside an archive; or "" if not.
Definition: basestrm.cpp:279
virtual void writeFloat(float f)
Write a floating point variable.
Definition: basestrm.cpp:385
FileIndex * getnextname(void)
Definition: basestrm.cpp:1133
virtual void writeWord(int w)
Writes a 16 bit unsigned Integer. In the stream little-endian byte order is used and a translation is...
Definition: basestrm.cpp:369
virtual void readpchar(char **pc, int maxlength=0)
Reads a string.
Definition: basestrm.cpp:390
int readcmpdata(void *buf, int size, bool excpt=true)
Definition: basestrm.cpp:1684
pncontainerstream container
Definition: basestrm.cpp:1004
virtual time_t get_time(void)
Definition: basestrm.cpp:1710
virtual char readCharacter(void)
Read a 8 bit character.
Definition: basestrm.cpp:298
ASCString getMessage() const
Definition: basestrm.cpp:176
virtual void writepchar(const char *pc)
write the C-String pc to the stream
Definition: basestrm.cpp:554
int size
Definition: basestrm.h:67
tfileerror(void)
Definition: simplestream.h:81
queue< Uint8, CDQ > tempbuf
Definition: basestrm.h:297
bool exist(const ASCString &s)
does a file s exist (wildcards allowed)
Definition: basestrm.cpp:2444
const T & max(const T &a, const T &b, const T &c)
Definition: misc.h:97
#define writedata2(a)
Definition: basestrm.h:648
#define CASE_SENSITIVE_FILE_NAMES
const T & min(const T &a, const T &b, const T &c)
Definition: misc.h:80
SDL_RWops * SDL_RWFromStream(tnstream *stream)
Definition: basestrm.cpp:693
void closecontainerfile(void)
Definition: basestrm.cpp:806
virtual int readdata(void *buf, int size, bool excpt=true)
Reads data from the stream.
#define bzip_xor_byte
Definition: basestrm.cpp:84
StreamCompressionFilter(tnstream *outputstream)
Definition: basestrm.cpp:1859
int readcmpdata(void *buf, int size, bool excpt=true)
Definition: basestrm.cpp:1585
void writedata(const void *buf, int size)
Definition: basestrm.cpp:1209
virtual bool readTextString(ASCString &s, bool includeCR=false)
Reads a string.
Definition: basestrm.cpp:511
void writedata(const void *buf, int size)
writes data to the stream
Definition: basestrm.cpp:1693
MemoryStreamCopy(tnstream *stream)
Definition: basestrm.cpp:578
bool patimat(const char *pat, const char *str, bool forceCaseInsensitivity)
Definition: basestrm.cpp:1997
void closedir(DIR *)
virtual ~ContainerCollector()
Definition: basestrm.cpp:1156
void writecmpdata(const void *buf, int size)
Definition: basestrm.cpp:1590
virtual void writedata(const void *buf, int size)
writes data to the stream
Definition: basestrm.cpp:1864
ASCString getLocation()
returns the location of the stream.
Definition: basestrm.cpp:607
Uint8 rle
Definition: basegfx.h:82
void writecmpdata(const void *buf, int size)
Definition: basestrm.cpp:1676
IOMode
the modes in which a stream can operate
dynamic_array< pncontainerstream > container
Definition: basestrm.cpp:852
const int found
Definition: basestrm.h:107
StreamDecompressionFilter(tnstream *inputstream)
Definition: basestrm.cpp:1889
void opencontainerfile(const char *name)
Definition: basestrm.cpp:767
virtual void writedata(const void *buf, int size)=0
writes data to the stream
const int expected
Definition: basestrm.h:106
struct ContainerCollector::@4 namesearch
#define readdata2(a)
Definition: basestrm.h:649
virtual void writelzwdata(const void *buf, int size)
Definition: basestrm.cpp:1513
virtual void writedata(const void *buf, int size)
writes data to the stream
Definition: basestrm.cpp:1822