Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VSFileSystem::VSFile Class Reference

#include <vsfilesystem.h>

Public Member Functions

char * get_pk3_data ()
 
 VSFile ()
 
 VSFile (const char *buffer, long size, VSFileType type=ZoneBuffer, VSFileMode=ReadOnly)
 
 VSFile (const char *filename, VSFileType type=UnknownFile, VSFileMode=ReadOnly)
 
 ~VSFile ()
 
FILE * GetFP ()
 
char * GetFileBuffer ()
 
VSError OpenReadOnly (const char *filename, VSFileType type=UnknownFile)
 
VSError OpenReadOnly (const string &filename, VSFileType type=UnknownFile)
 
VSError OpenReadWrite (const char *filename, VSFileType type=UnknownFile)
 
VSError OpenReadWrite (const string &filename, VSFileType type=UnknownFile)
 
VSError OpenCreateWrite (const char *filename, VSFileType type=UnknownFile)
 
VSError OpenCreateWrite (const string &filename, VSFileType type=UnknownFile)
 
void Close ()
 
size_t Read (void *ptr, size_t length)
 
VSError ReadLine (void *ptr, size_t length)
 
string ReadFull ()
 
size_t Write (const void *ptr, size_t length)
 
size_t Write (const string &content)
 
VSError WriteLine (const void *ptr)
 
void WriteFull (void *ptr)
 
void GoAfterEOL (unsigned int length)
 
void GoAfterEOL ()
 
template<class A >
int Fscanf (const char *format, A *a)
 
template<class A , class B >
int Fscanf (const char *format, A *a, B *b)
 
template<class A , class B , class C >
int Fscanf (const char *format, A *a, B *b, C *c)
 
template<class A , class B , class C , class D >
int Fscanf (const char *format, A *a, B *b, C *c, D *d)
 
template<class A , class B , class C , class D , class E >
int Fscanf (const char *format, A *a, B *b, C *c, D *d, E *e)
 
template<class A , class B , class C , class D , class E , class F >
int Fscanf (const char *format, A *a, B *b, C *c, D *d, E *e, F *f)
 
int Fprintf (const char *format,...)
 
long Size ()
 
void Begin ()
 
long GetPosition ()
 
void End ()
 
void GoTo (long offset)
 
void Clear ()
 
bool Eof ()
 
bool Valid ()
 
const string & GetFilename () const
 
const string & GetDirectory () const
 
const string & GetSubDirectory () const
 
const string & GetRoot () const
 
void SetFilename (const string &filename)
 
void SetDirectory (const string &directory)
 
void SetSubDirectory (const string &subdirectory)
 
void SetRoot (const string &root)
 
string GetFullPath ()
 
string GetAbsPath ()
 
void SetType (VSFileType type)
 
void SetAltType (VSFileType type)
 
VSFileType GetType () const
 
VSFileType GetAltType () const
 
void SetIndex (int index)
 
void SetVolume (VSVolumeType big)
 
bool UseVolume ()
 

Friends

class VSImage
 

Detailed Description

Definition at line 296 of file vsfilesystem.h.

Constructor & Destructor Documentation

VSFileSystem::VSFile::VSFile ( )

Definition at line 1338 of file vsfilesystem.cpp.

1339 {
1340  private_init();
1341 }
VSFileSystem::VSFile::VSFile ( const char *  buffer,
long  size,
VSFileType  type = ZoneBuffer,
VSFileMode  mode = ReadOnly 
)

Definition at line 1343 of file vsfilesystem.cpp.

References bufsize, VsnetOSS::memcpy(), size, VSFileSystem::VSFSBig, and VSFileSystem::ZoneBuffer.

1344 {
1345  private_init();
1346  this->size = bufsize;
1347  this->pk3_extracted_file = new char[bufsize+1];
1348  memcpy( this->pk3_extracted_file, buffer, bufsize );
1349  pk3_extracted_file[bufsize] = 0;
1350  this->file_type = this->alt_type = ZoneBuffer;
1351  this->file_mode = mode;
1352  //To say we want to read in volume even if it is not the case then it will read in pk3_extracted_file
1353  this->volume_type = VSFSBig;
1354 }
VSFileSystem::VSFile::VSFile ( const char *  filename,
VSFileType  type = UnknownFile,
VSFileMode  mode = ReadOnly 
)

Definition at line 1356 of file vsfilesystem.cpp.

References VSFileSystem::CreateWrite, OpenCreateWrite(), OpenReadOnly(), OpenReadWrite(), VSFileSystem::ReadOnly, and VSFileSystem::ReadWrite.

1357 {
1358  private_init();
1359  if (mode == ReadOnly)
1360  this->OpenReadOnly( filename, type );
1361  else if (mode == ReadWrite)
1362  this->OpenReadWrite( filename, type );
1363  else if (mode == CreateWrite)
1364  this->OpenCreateWrite( filename, type );
1365 }
VSFileSystem::VSFile::~VSFile ( )

Definition at line 1367 of file vsfilesystem.cpp.

References fclose.

1368 {
1369  if (fp) {
1370  fclose( fp );
1371  this->fp = NULL;
1372  }
1373  if (pk3_extracted_file)
1374  delete[] pk3_extracted_file;
1375 }

Member Function Documentation

void VSFileSystem::VSFile::Begin ( )

Definition at line 1763 of file vsfilesystem.cpp.

References VSFileSystem::q_volume_format, VSFileSystem::ReadOnly, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, and VSFileSystem::VSFSNone.

1764 {
1765  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone || this->file_mode != ReadOnly) {
1766  fseek( this->fp, 0, SEEK_SET );
1767  } else {
1768  if (q_volume_format == vfmtVSR)
1769  offset = 0;
1770  else if (q_volume_format == vfmtPK3)
1771  offset = 0;
1772  }
1773 }
void VSFileSystem::VSFile::Clear ( )

Definition at line 1818 of file vsfilesystem.cpp.

References fclose, fopen, VSFileSystem::GetError(), GetFullPath(), VSFileSystem::ReadOnly, VSFileSystem::UseVolumes, VSExit(), and VSFileSystem::VSFSNone.

1819 {
1820  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone || file_mode != ReadOnly) {
1821  fclose( fp );
1822  this->fp = fopen( this->GetFullPath().c_str(), "w+b" );
1823  //This should not happen
1824  if (!fp) {
1825  GetError( "Clear" );
1826  VSExit( 1 );
1827  }
1828  } else {
1829  cerr<<"!!! ERROR : Writing is not supported within resource/volume files"<<endl;
1830  VSExit( 1 );
1831  }
1832 }
void VSFileSystem::VSFile::Close ( void  )

Definition at line 1865 of file vsfilesystem.cpp.

References VSFileSystem::AnimFile, VSFileSystem::CockpitFile, VSFileSystem::current_path, VSFileSystem::current_subdirectory, VSFileSystem::current_type, VSFileSystem::DisplayType(), fclose, VSFileSystem::q_volume_format, VSFileSystem::ReadOnly, size, VSFileSystem::UnitFile, VSFileSystem::UnknownFile, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, VSFS_DEBUG(), VSFileSystem::VSFSNone, VSFileSystem::VSSpriteFile, and VSFileSystem::ZoneBuffer.

Referenced by BaseComputer::actionConfirmedSaveGame(), BaseComputer::actionSaveGame(), AnimatedTexture::AnimatedTexture(), Animation::Animation(), AUDCreateSoundMP3(), AUDLoadSoundFile(), ROLES::buildroles(), ROLES::buildscripts(), VsnetDownload::Client::File::childAppend(), CloseFile(), GameStarSystem::createBackground(), EnvironmentMapGeneratorMain(), getAITypes(), GetHelperPlayerSaveGame(), getProgramSource(), FileUtil::HashCompute(), XMLDOM::VSFileXMLSerializer::importXML(), Cockpit::Init(), InitUnitTables(), Texture::Load(), LoadAccounts(), AnimatedTexture::LoadFrame(), Mesh::LoadMeshes(), AnimatedTexture::LoadVideoSource(), Nebula::LoadXML(), easyDomFactory< configNode >::LoadXML(), Unit::makeMasterPartList(), SaveGame::ParseSaveGame(), parseTurretSizes(), StarSystemGent::readColorGrads(), StarSystemGent::readentity(), readnames(), AccountServer::recvMsg(), SaveFileCopy(), Screenshot(), AccountServer::sendAuthorized(), NetServer::start(), VSSprite::VSSprite(), XMLSerializer::Write(), VSImage::WriteImage(), FileUtil::WriteSaveFiles(), SaveGame::WriteSaveGame(), Unit::WriteUnit(), and VsnetDownload::Client::File::~File().

1866 {
1867  if (this->file_type >= ZoneBuffer && this->file_type != UnknownFile && this->pk3_extracted_file) {
1868  delete this->pk3_extracted_file;
1869  this->pk3_extracted_file = NULL;
1870  return;
1871  }
1872  if ( this->valid && this->file_mode == ReadOnly
1873  && (file_type == UnitFile || file_type == AnimFile || file_type == VSSpriteFile || file_type == CockpitFile) ) {
1874  assert( current_path.size() > 1 );
1875  current_path.pop_back();
1876  current_subdirectory.pop_back();
1877  current_type.pop_back();
1878  if (VSFS_DEBUG() > 2) {
1879  cerr<<"END OF ";
1880  DisplayType( this->file_type );
1881  cerr<<endl<<endl;
1882  }
1883  }
1884  if (!UseVolumes[file_type] || this->volume_type == VSFSNone || file_mode != ReadOnly) {
1885  fclose( this->fp );
1886  this->fp = NULL;
1887  } else {
1888  if (q_volume_format == vfmtVSR) {} else if (q_volume_format == vfmtPK3) {
1889  if (pk3_extracted_file)
1890  delete[] pk3_extracted_file;
1891  pk3_extracted_file = NULL;
1892  }
1893  }
1894  this->size = -1;
1895  this->valid = false;
1896  this->filename = "";
1897  this->directoryname = "";
1898  this->subdirectoryname = "";
1899  this->rootname = "";
1900  this->offset = 0;
1901  this->file_index = -1;
1902 }
void VSFileSystem::VSFile::End ( )

Definition at line 1775 of file vsfilesystem.cpp.

References VSFileSystem::q_volume_format, VSFileSystem::ReadOnly, size, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, and VSFileSystem::VSFSNone.

1776 {
1777  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone || this->file_mode != ReadOnly) {
1778  fseek( this->fp, 0, SEEK_END );
1779  } else {
1780  if (q_volume_format == vfmtVSR)
1781  offset = size;
1782  else if (q_volume_format == vfmtPK3)
1783  offset = size;
1784  }
1785 }
bool VSFileSystem::VSFile::Eof ( )

Definition at line 1847 of file vsfilesystem.cpp.

References VSFileSystem::q_volume_format, VSFileSystem::ReadOnly, Size(), VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, VSFileSystem::vs_feof(), and VSFileSystem::VSFSNone.

Referenced by AnimatedTexture::LoadAni(), StarSystemGent::readColorGrads(), and readnames().

1848 {
1849  bool eof = false;
1850  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone || file_mode != ReadOnly) {
1851  eof = vs_feof( this->fp );
1852  } else {
1853  if (q_volume_format == vfmtVSR) {} else if (q_volume_format == vfmtPK3) {
1854  eof = (this->Size() < 0) || ( offset == (unsigned long)this->Size() );
1855  }
1856  }
1857  return eof;
1858 }
int VSFileSystem::VSFile::Fprintf ( const char *  format,
  ... 
)

Definition at line 1714 of file vsfilesystem.cpp.

References VSFileSystem::UseVolumes, VSExit(), and VSFileSystem::VSFSNone.

Referenced by dotabs(), Tab(), XMLElement::Write(), XMLnode::Write(), GalaxyXML::SGalaxy::writeGalaxy(), GalaxyXML::Galaxy::writeGalaxy(), and GalaxyXML::SGalaxy::writeSector().

1715 {
1716  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone) {
1717  va_list ap;
1718  va_start( ap, format );
1719 
1720  return vfprintf( this->fp, format, ap );
1721  } else {
1722  cerr<<"!!! ERROR : Writing is not supported within resource/volume files"<<endl;
1723  VSExit( 1 );
1724  }
1725  return 0;
1726 }
template<class A >
int VSFileSystem::VSFile::Fscanf ( const char *  format,
A *  a 
)
inline

Definition at line 382 of file vsfilesystem.h.

References GoAfterEOL(), VsnetOSS::memcpy(), VSFileSystem::q_volume_format, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, and VSFileSystem::vfmtVSR.

Referenced by AnimatedTexture::AnimatedTexture(), Animation::Animation(), AnimatedTexture::LoadAni(), StarSystemGent::readentity(), and VSSprite::VSSprite().

383  {
384  int ret = -1;
385  if (!UseVolumes[this->alt_type]) {
386  ret = fscanf( fp, format, a );
387  } else {
388  if (q_volume_format == vfmtPK3) {
389  checkExtracted();
390  int ret = -1, readbytes = 0;
391  int length = strlen( format );
392  int newlength = length+3;
393  char *newformat = new char[newlength];
394  memset( newformat, 0, newlength );
395  memcpy( newformat, format, length );
396  strcat( newformat, "%n" );
397  ret = sscanf( this->pk3_extracted_file+offset, newformat, a, &readbytes );
398  delete[] newformat;
399  std::cerr<<"FSCANF : sscanf read "<<readbytes<<" bytes - OFFSET="<<offset<<" VALUES : a="<<(*a)<<std::endl;
400  this->offset += readbytes;
401  this->GoAfterEOL();
402  } else if (q_volume_format == vfmtVSR) {}
403  }
404  return ret;
405  }
template<class A , class B >
int VSFileSystem::VSFile::Fscanf ( const char *  format,
A *  a,
B *  b 
)
inline

Definition at line 407 of file vsfilesystem.h.

References GoAfterEOL(), VsnetOSS::memcpy(), VSFileSystem::q_volume_format, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, and VSFileSystem::vfmtVSR.

408  {
409  int ret = -1;
410  if (!UseVolumes[this->alt_type]) {
411  ret = fscanf( fp, format, a, b );
412  } else {
413  if (q_volume_format == vfmtPK3) {
414  checkExtracted();
415  int ret = -1, readbytes = 0;
416  int length = strlen( format );
417  int newlength = length+3;
418  char *newformat = new char[newlength];
419  memset( newformat, 0, newlength );
420  memcpy( newformat, format, length );
421  strcat( newformat, "%n" );
422  ret = sscanf( this->pk3_extracted_file+offset, newformat, a, b, &readbytes );
423  delete[] newformat;
424  std::cerr<<"FSCANF : sscanf read "<<readbytes<<" bytes - OFFSET="<<offset<<" VALUES : a="<<(*a)<<", b="<<(*b)
425  <<std::endl;
426  this->offset += readbytes;
427  this->GoAfterEOL();
428  } else if (q_volume_format == vfmtVSR) {}
429  }
430  return ret;
431  }
template<class A , class B , class C >
int VSFileSystem::VSFile::Fscanf ( const char *  format,
A *  a,
B *  b,
C *  c 
)
inline

Definition at line 433 of file vsfilesystem.h.

References GoAfterEOL(), VsnetOSS::memcpy(), VSFileSystem::q_volume_format, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, and VSFileSystem::vfmtVSR.

434  {
435  int ret = -1;
436  if (!UseVolumes[this->alt_type]) {
437  ret = fscanf( fp, format, a, b, c );
438  } else {
439  if (q_volume_format == vfmtPK3) {
440  checkExtracted();
441  int ret = -1, readbytes = 0;
442  int length = strlen( format );
443  int newlength = length+3;
444  char *newformat = new char[newlength];
445  memset( newformat, 0, newlength );
446  memcpy( newformat, format, length );
447  strcat( newformat, "%n" );
448  ret = sscanf( this->pk3_extracted_file+offset, newformat, a, b, c, &readbytes );
449  delete[] newformat;
450  std::cerr<<"FSCANF : sscanf read "<<readbytes<<" bytes - OFFSET="<<offset<<" VALUES : a="<<(*a)<<", b="<<(*b)
451  <<", c="<<(*c)<<std::endl;
452  this->offset += readbytes;
453  this->GoAfterEOL();
454  } else if (q_volume_format == vfmtVSR) {}
455  }
456  return ret;
457  }
template<class A , class B , class C , class D >
int VSFileSystem::VSFile::Fscanf ( const char *  format,
A *  a,
B *  b,
C *  c,
D *  d 
)
inline

Definition at line 459 of file vsfilesystem.h.

References GoAfterEOL(), VsnetOSS::memcpy(), VSFileSystem::q_volume_format, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, and VSFileSystem::vfmtVSR.

460  {
461  int ret = -1;
462  if (!UseVolumes[this->alt_type]) {
463  ret = fscanf( fp, format, a, b, c, d );
464  } else {
465  if (q_volume_format == vfmtPK3) {
466  checkExtracted();
467  int ret = -1, readbytes = 0;
468  int length = strlen( format );
469  int newlength = length+3;
470  char *newformat = new char[newlength];
471  memset( newformat, 0, newlength );
472  memcpy( newformat, format, length );
473  strcat( newformat, "%n" );
474  ret = sscanf( this->pk3_extracted_file+offset, newformat, a, b, c, d, &readbytes );
475  delete[] newformat;
476  this->offset += readbytes;
477  this->GoAfterEOL();
478  } else if (q_volume_format == vfmtVSR) {}
479  }
480  return ret;
481  }
template<class A , class B , class C , class D , class E >
int VSFileSystem::VSFile::Fscanf ( const char *  format,
A *  a,
B *  b,
C *  c,
D *  d,
E *  e 
)
inline

Definition at line 483 of file vsfilesystem.h.

References GoAfterEOL(), VsnetOSS::memcpy(), VSFileSystem::q_volume_format, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, and VSFileSystem::vfmtVSR.

484  {
485  int ret = -1;
486  if (!UseVolumes[this->alt_type]) {
487  ret = fscanf( fp, format, a, b, c, d, e );
488  } else {
489  if (q_volume_format == vfmtPK3) {
490  checkExtracted();
491  int ret = -1, readbytes = 0;
492  int length = strlen( format );
493  int newlength = length+3;
494  char *newformat = new char[newlength];
495  memset( newformat, 0, newlength );
496  memcpy( newformat, format, length );
497  strcat( newformat, "%n" );
498  ret = sscanf( this->pk3_extracted_file+offset, newformat, a, b, c, d, e, &readbytes );
499  delete[] newformat;
500  this->offset += readbytes;
501  this->GoAfterEOL();
502  } else if (q_volume_format == vfmtVSR) {}
503  }
504  return ret;
505  }
template<class A , class B , class C , class D , class E , class F >
int VSFileSystem::VSFile::Fscanf ( const char *  format,
A *  a,
B *  b,
C *  c,
D *  d,
E *  e,
F *  f 
)
inline

Definition at line 507 of file vsfilesystem.h.

References GoAfterEOL(), VsnetOSS::memcpy(), VSFileSystem::q_volume_format, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, and VSFileSystem::vfmtVSR.

508  {
509  int ret = -1;
510  if (!UseVolumes[this->alt_type]) {
511  ret = fscanf( fp, format, a, b, c, d, e, f );
512  } else {
513  if (q_volume_format == vfmtPK3) {
514  checkExtracted();
515  int ret = -1, readbytes = 0;
516  int length = strlen( format );
517  int newlength = length+3;
518  char *newformat = new char[newlength];
519  memset( newformat, 0, newlength );
520  memcpy( newformat, format, length );
521  strcat( newformat, "%n" );
522  ret = sscanf( this->pk3_extracted_file+offset, newformat, a, b, c, d, e, f, &readbytes );
523  delete[] newformat;
524  this->offset += readbytes;
525  this->GoAfterEOL();
526  } else if (q_volume_format == vfmtVSR) {}
527  }
528  return ret;
529  }
char* VSFileSystem::VSFile::get_pk3_data ( )
inline

Definition at line 325 of file vsfilesystem.h.

326  {
327  return pk3_extracted_file;
328  }
string VSFileSystem::VSFile::GetAbsPath ( )

Definition at line 1922 of file vsfilesystem.cpp.

References VSFileSystem::pathAppend().

1923 {
1924  string tmp = this->directoryname;
1925  pathAppend( tmp, this->subdirectoryname );
1926  pathAppend( tmp, this->filename );
1927  return tmp;
1928 }
VSFileType VSFileSystem::VSFile::GetAltType ( ) const
inline

Definition at line 587 of file vsfilesystem.h.

588  {
589  return alt_type;
590  }
const string& VSFileSystem::VSFile::GetDirectory ( ) const
inline

Definition at line 548 of file vsfilesystem.h.

549  {
550  return this->directoryname;
551  }
char* VSFileSystem::VSFile::GetFileBuffer ( )
inline

Definition at line 339 of file vsfilesystem.h.

340  {
341  return this->pk3_extracted_file;
342  }
const string& VSFileSystem::VSFile::GetFilename ( ) const
inline

Definition at line 544 of file vsfilesystem.h.

Referenced by AnimatedTexture::LoadAni(), Mesh::LoadMeshes(), AnimatedTexture::LoadVideoSource(), and VSFileSystem::LookForFile().

545  {
546  return this->filename;
547  }
FILE* VSFileSystem::VSFile::GetFP ( )
inline

Definition at line 335 of file vsfilesystem.h.

336  {
337  return this->fp;
338  } //This is still needed for special cases (when loading PNG files)
string VSFileSystem::VSFile::GetFullPath ( )

Definition at line 1913 of file vsfilesystem.cpp.

References VSFileSystem::pathAppend().

Referenced by Clear(), GetHelperPlayerSaveGame(), OpenReadOnly(), ReadFull(), XMLSerializer::Write(), and Unit::WriteUnit().

1914 {
1915  string tmp = this->rootname;
1916  pathAppend( tmp, this->directoryname );
1917  pathAppend( tmp, this->subdirectoryname );
1918  pathAppend( tmp, this->filename );
1919  return tmp;
1920 }
long VSFileSystem::VSFile::GetPosition ( )

Definition at line 1834 of file vsfilesystem.cpp.

References VSFileSystem::q_volume_format, VSFileSystem::ReadOnly, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, and VSFileSystem::VSFSNone.

Referenced by GetPosition().

1835 {
1836  long ret = 0;
1837  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone || file_mode != ReadOnly) {
1838  ret = ftell( this->fp );
1839  } else {
1840  if (q_volume_format == vfmtVSR) {} else if (q_volume_format == vfmtPK3) {
1841  ret = offset;
1842  }
1843  }
1844  return ret;
1845 }
const string& VSFileSystem::VSFile::GetRoot ( ) const
inline

Definition at line 556 of file vsfilesystem.h.

Referenced by getAITypes(), InitUnitTables(), and Unit::makeMasterPartList().

557  {
558  return this->rootname;
559  }
const string& VSFileSystem::VSFile::GetSubDirectory ( ) const
inline

Definition at line 552 of file vsfilesystem.h.

Referenced by VSFileSystem::LookForFile().

553  {
554  return this->subdirectoryname;
555  }
VSFileType VSFileSystem::VSFile::GetType ( ) const
inline

Definition at line 583 of file vsfilesystem.h.

Referenced by AnimatedTexture::Load(), AnimatedTexture::LoadAni(), and AnimatedTexture::LoadVideoSource().

584  {
585  return file_type;
586  }
void VSFileSystem::VSFile::GoAfterEOL ( unsigned int  length)

Definition at line 1952 of file vsfilesystem.cpp.

References size.

1953 {
1954  while ( this->offset < length && this->offset < this->size
1955  && (this->pk3_extracted_file[offset] == '\r' || this->pk3_extracted_file[offset] == '\n') )
1956  this->offset++;
1957 }
void VSFileSystem::VSFile::GoAfterEOL ( )

Definition at line 1958 of file vsfilesystem.cpp.

References size.

Referenced by Fscanf(), and ReadLine().

1959 {
1960  while ( this->offset < this->size && (this->pk3_extracted_file[offset] == '\r' || this->pk3_extracted_file[offset] == '\n') )
1961  this->offset++;
1962 }
void VSFileSystem::VSFile::GoTo ( long  offset)

Definition at line 1787 of file vsfilesystem.cpp.

References VSFileSystem::q_volume_format, VSFileSystem::ReadOnly, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, and VSFileSystem::VSFSNone.

Referenced by isBFXM(), Mesh::LoadMeshes(), and SetPosition().

1788 {
1789  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone || this->file_mode != ReadOnly) {
1790  fseek( this->fp, foffset, SEEK_SET );
1791  } else {
1792  if (q_volume_format == vfmtVSR)
1793  offset = foffset;
1794  else if (q_volume_format == vfmtPK3)
1795  offset = foffset;
1796  }
1797 }
VSError VSFileSystem::VSFile::OpenCreateWrite ( const char *  filename,
VSFileType  type = UnknownFile 
)

Definition at line 1542 of file vsfilesystem.cpp.

References VSFileSystem::AccountFile, VSFileSystem::CreateDirectoryHome(), VSFileSystem::CreateWrite, VSFileSystem::datadir, VSFileSystem::FileNotFound, fopen, getStarSystemSector(), VSFileSystem::homedir, VSFileSystem::LocalPermissionDenied, VSFileSystem::Ok, VSFileSystem::savedunitpath, VSFileSystem::SaveFile, VSFileSystem::sharedsectors, VSFileSystem::sharedtextures, VSFileSystem::SystemFile, VSFileSystem::TextureFile, VSFileSystem::UnitFile, VSFileSystem::universe_name, VSFileSystem::UnknownFile, and VSFileSystem::ZoneBuffer.

Referenced by BaseComputer::actionConfirmedSaveGame(), VsnetDownload::Client::File::childSetSize(), CopySavedShips(), GetHelperPlayerSaveGame(), OpenCreateWrite(), AccountServer::recvMsg(), SaveFileCopy(), VSFile(), XMLSerializer::Write(), VSImage::WriteImage(), FileUtil::WriteSaveFiles(), SaveGame::WriteSaveGame(), and Unit::WriteUnit().

1543 {
1544  if (type >= ZoneBuffer && type != UnknownFile)
1545  return FileNotFound;
1546  this->file_type = this->alt_type = type;
1547  this->filename = string( filenam );
1548  this->file_mode = CreateWrite;
1549  if (type == SystemFile) {
1550  string dirpath( sharedsectors+"/"+universe_name );
1551  CreateDirectoryHome( dirpath );
1552  CreateDirectoryHome( dirpath+"/"+getStarSystemSector( this->filename ) );
1553  string fpath( homedir+"/"+dirpath+"/"+this->filename );
1554  this->fp = fopen( fpath.c_str(), "wb" );
1555  if (!fp)
1556  return LocalPermissionDenied;
1557  } else if (type == TextureFile) {
1558  string fpath( homedir+"/"+sharedtextures+"/"+this->filename );
1559  this->fp = fopen( fpath.c_str(), "wb" );
1560  if (!fp)
1561  return LocalPermissionDenied;
1562  } else if (type == UnitFile) {
1563  string fpath( homedir+"/"+savedunitpath+"/"+this->filename );
1564  this->rootname = homedir;
1565  this->directoryname = savedunitpath;
1566  this->fp = fopen( fpath.c_str(), "wb" );
1567  if (!fp)
1568  return LocalPermissionDenied;
1569  } else if (type == SaveFile) {
1570  string fpath( homedir+"/save/"+this->filename );
1571  this->fp = fopen( fpath.c_str(), "wb" );
1572  if (!fp)
1573  return LocalPermissionDenied;
1574  } else if (type == AccountFile) {
1575  string fpath( datadir+"/accounts/"+this->filename );
1576  this->fp = fopen( fpath.c_str(), "wb" );
1577  if (!fp)
1578  return LocalPermissionDenied;
1579  } else if (type == UnknownFile) {
1580  string fpath( homedir+"/"+this->filename );
1581  this->rootname = homedir;
1582  this->directoryname = "";
1583  this->fp = fopen( fpath.c_str(), "wb" );
1584  if (!fp)
1585  return LocalPermissionDenied;
1586  }
1587  return Ok;
1588 }
VSError VSFileSystem::VSFile::OpenCreateWrite ( const string &  filename,
VSFileType  type = UnknownFile 
)
inline

Definition at line 359 of file vsfilesystem.h.

References OpenCreateWrite().

360  {
361  return OpenCreateWrite( filename.c_str(), type );
362  }
VSError VSFileSystem::VSFile::OpenReadOnly ( const char *  filename,
VSFileType  type = UnknownFile 
)

Definition at line 1416 of file vsfilesystem.cpp.

References VSFileSystem::AnimFile, VSFileSystem::CockpitFile, VSFileSystem::current_directory, VSFileSystem::current_path, VSFileSystem::current_subdirectory, VSFileSystem::current_type, VSFileSystem::DisplayType(), VSFileSystem::failed, accountXML::file, VSFileSystem::FileExists(), VSFileSystem::FileNotFound, fopen, GetFullPath(), VSFileSystem::LookForFile(), VSFileSystem::Ok, VSFileSystem::q_volume_format, VSFileSystem::ReadOnly, VSFileSystem::Rootdir, VSFileSystem::UnitFile, VSFileSystem::UnknownFile, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, VSExit(), VSFS_DEBUG(), VSFileSystem::VSFSNone, VSFileSystem::VSSpriteFile, and VSFileSystem::ZoneBuffer.

Referenced by BaseComputer::actionSaveGame(), AnimatedTexture::AnimatedTexture(), Animation::Animation(), AUDCreateSoundMP3(), AUDLoadSoundFile(), ROLES::buildroles(), ROLES::buildscripts(), AnimatedTexture::Clone(), CopySavedShips(), GameStarSystem::createBackground(), AnimatedTexture::CreateVideoTexture(), EnvironmentMapGeneratorMain(), Universe::Generate1(), getAITypes(), VSFileSystem::GetCorrectStarSysPath(), GetHelperPlayerSaveGame(), getProgramSource(), FileUtil::HashCompute(), XMLDOM::VSFileXMLSerializer::importXML(), Cockpit::Init(), InitUnitTables(), Texture::Load(), LoadAccounts(), LoadFile(), AnimatedTexture::LoadFrame(), Mesh::LoadMeshes(), Nebula::LoadXML(), easyDomFactory< configNode >::LoadXML(), Unit::makeMasterPartList(), OpenReadOnly(), SaveGame::ParseSaveGame(), parseTurretSizes(), StarSystemGent::readColorGrads(), StarSystemGent::readentity(), readnames(), AccountServer::recvMsg(), SaveFileCopy(), Screenshot(), AccountServer::sendAuthorized(), NetServer::start(), VSFile(), and VSSprite::VSSprite().

1417 {
1418  string filestr;
1419  int found = -1;
1420  this->file_type = this->alt_type = type;
1421  this->file_mode = ReadOnly;
1422  this->filename = string( file );
1423  failed = "";
1424 
1425  VSError err = Ok;
1426  if ( VSFS_DEBUG() )
1427  cerr<<"Loading a "<<type<<" : "<<file<<endl;
1428  if (type < ZoneBuffer || type == UnknownFile) {
1429  //It is a "classic file"
1430  if (!UseVolumes[type]) {
1431  if (type == UnknownFile) {
1432  //We look in the current_path or for a full relative path to either homedir or datadir
1433  if (current_path.back() != "") {
1434  string filestr1 = current_directory.back()
1435  +"/"+current_subdirectory.back()+"/"+string( file );
1436  filestr = current_path.back()+"/"+filestr1;
1437  if ( ( found = FileExists( current_path.back(), filestr1 ) ) < 0 )
1438  failed += "\t"+filestr+" NOT FOUND !\n";
1439  }
1440  if (found < 0) {
1441  for (unsigned int ij = 0; ij < Rootdir.size() && found < 0; ij++) {
1442  filestr = Rootdir[ij]+"/"+file;
1443  found = FileExists( Rootdir[ij], file );
1444  if (found < 0)
1445  failed += "\tRootdir : "+filestr+" NOT FOUND !\n";
1446  }
1447  //Look for relative (to datadir) or absolute named file
1448  if (found < 0) {
1449  filestr = file;
1450  if ( ( found = FileExists( "", filestr ) ) < 0 )
1451  failed += "\tAbs or rel : "+filestr+" NOT FOUND !\n";
1452  }
1453  }
1454  if (found < 0) {
1455  if ( VSFS_DEBUG() )
1456  cerr<<failed<<endl;
1457  this->valid = false;
1458  err = FileNotFound;
1459  } else {
1460  if ( ( this->fp = fopen( filestr.c_str(), "rb" ) ) == NULL ) {
1461  cerr<<"!!! SERIOUS ERROR : failed to open Unknown file "<<filestr<<" - this should not happen"<<endl;
1462  VSExit( 1 );
1463  }
1464  this->valid = true;
1465  if (VSFS_DEBUG() > 1)
1466  cerr<<filestr<<" SUCCESS !!!"<<endl;
1467  }
1468  } else {
1469  err = VSFileSystem::LookForFile( *this, type, file_mode );
1470  if (err > Ok) {
1471  this->valid = false;
1472  return FileNotFound;
1473  }
1474  filestr = this->GetFullPath();
1475  this->fp = fopen( filestr.c_str(), "rb" );
1476  if (!this->fp) {
1477  cerr<<"!!! SERIOUS ERROR : failed to open "<<filestr<<" - this should not happen"<<endl;
1478  this->valid = false;
1479  return FileNotFound; //fault!
1480  }
1481  this->valid = true;
1482  }
1483  } else {
1484  if (q_volume_format == vfmtVSR) {} else if (q_volume_format == vfmtPK3) {
1485  //Here we look for the file but we don't really open it, we just check if it exists
1486  err = VSFileSystem::LookForFile( *this, type, file_mode );
1487  if (err > Ok) {
1488  this->valid = false;
1489  return FileNotFound;
1490  }
1491  //Test if we have found a file in another FileType's dir and if it doesn't use volumes
1492  //If so we open the file as a normal one
1493  if ( this->volume_type == VSFSNone || (this->alt_type != this->file_type && !UseVolumes[this->alt_type]) ) {
1494  filestr = this->GetFullPath();
1495  this->fp = fopen( filestr.c_str(), "rb" );
1496  if (!this->fp) {
1497  cerr<<"!!! SERIOUS ERROR : failed to open "<<filestr<<" - this should not happen"<<endl;
1498  this->valid = false;
1499  return FileNotFound; //fault
1500  }
1501  }
1502  this->valid = true;
1503  }
1504  }
1505  if (err <= Ok) {
1506  //We save the current path only when loading a unit, an animation, a sprite or a cockpit
1507  if ( (type == UnitFile || type == AnimFile || type == VSSpriteFile || type == CockpitFile) ) {
1508  current_path.push_back( this->rootname );
1509  current_subdirectory.push_back( this->subdirectoryname );
1510  current_type.push_back( this->alt_type );
1511  if (VSFS_DEBUG() > 1) {
1512  cerr<<endl<<"BEGINNING OF ";
1513  DisplayType( type );
1514  cerr<<endl;
1515  }
1516  }
1517  }
1518  } else {
1519  //This is a "buffer file"
1520  if (!this->pk3_extracted_file)
1521  err = FileNotFound;
1522  else
1523  err = Ok;
1524  }
1525  return err;
1526 }
VSError VSFileSystem::VSFile::OpenReadOnly ( const string &  filename,
VSFileType  type = UnknownFile 
)
inline

Definition at line 347 of file vsfilesystem.h.

References OpenReadOnly().

348  {
349  return OpenReadOnly( filename.c_str(), type );
350  }
VSError VSFileSystem::VSFile::OpenReadWrite ( const char *  filename,
VSFileType  type = UnknownFile 
)

Definition at line 1530 of file vsfilesystem.cpp.

References VSFileSystem::FileNotFound, VSFileSystem::Ok, VSFileSystem::ReadWrite, VSFileSystem::UnknownFile, and VSFileSystem::ZoneBuffer.

Referenced by OpenReadWrite(), and VSFile().

1531 {
1532  if (type >= ZoneBuffer && type != UnknownFile)
1533  return FileNotFound;
1534  this->file_type = this->alt_type = type;
1535  this->file_mode = ReadWrite;
1536 
1537  return Ok;
1538 }
VSError VSFileSystem::VSFile::OpenReadWrite ( const string &  filename,
VSFileType  type = UnknownFile 
)
inline

Definition at line 353 of file vsfilesystem.h.

References OpenReadWrite().

354  {
355  return OpenReadWrite( filename.c_str(), type );
356  }
size_t VSFileSystem::VSFile::Read ( void *  ptr,
size_t  length 
)

Definition at line 1590 of file vsfilesystem.cpp.

References VsnetOSS::memcpy(), VSFileSystem::q_volume_format, size, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, and VSFileSystem::VSFSNone.

Referenced by AUDCreateSoundMP3(), AUDLoadSoundFile(), CopyFile(), VsnetDownload::Server::DownloadItemFile::copyFromFile(), GetHelperPlayerSaveGame(), FileUtil::HashCompute(), XMLDOM::VSFileXMLSerializer::importXML(), isBFXM(), Mesh::LoadMeshes(), readc(), readf(), ReadFloat(), readi(), ReadInt(), reads(), and ReadString().

1591 {
1592  size_t nbread = 0;
1593  if (!UseVolumes[this->alt_type] || this->volume_type == VSFSNone) {
1594  assert( fp != NULL );
1595  nbread = fread( ptr, 1, length, this->fp );
1596  } else {
1597  if (q_volume_format == vfmtVSR) {} else if (q_volume_format == vfmtPK3) {
1598  checkExtracted();
1599  if (length > this->size-this->offset)
1600  length = this->size-this->offset;
1601  memcpy( ptr, (pk3_extracted_file+offset), length );
1602  offset += length;
1603  nbread = length;
1604  }
1605  }
1606  return nbread;
1607 }
string VSFileSystem::VSFile::ReadFull ( )

Definition at line 1651 of file vsfilesystem.cpp.

References VSFileSystem::GetError(), GetFullPath(), VSFileSystem::q_volume_format, Size(), VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, and VSFileSystem::VSFSNone.

Referenced by CopySavedShips(), LoadAccounts(), Nebula::LoadXML(), easyDomFactory< configNode >::LoadXML(), SaveGame::ParseSaveGame(), SaveFileCopy(), AccountServer::sendAuthorized(), and NetServer::start().

1652 {
1653  if (this->Size() < 0) {
1654  cerr<<"Attempt to call ReadFull on a bad file "<<this->filename<<" "<<this->Size()<<" "<<this->GetFullPath().c_str()<<endl;
1655  return string();
1656  }
1657  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone) {
1658  char *content = new char[this->Size()+1];
1659  content[this->Size()] = 0;
1660  int readsize = fread( content, 1, this->Size(), this->fp );
1661  if (this->Size() != readsize) {
1662  cerr<<"Only read "<<readsize<<" out of "<<this->Size()<<" bytes of "<<this->filename<<endl;
1663  GetError( "ReadFull" );
1664  if (readsize <= 0)
1665  return string();
1666  else
1667  content[readsize] = '\0';
1668  }
1669  string res( content );
1670  delete[] content;
1671  return res;
1672  } else {
1673  if (q_volume_format == vfmtVSR) {} else if (q_volume_format == vfmtPK3) {
1674  checkExtracted();
1675  offset = this->Size();
1676  return string( pk3_extracted_file );
1677  }
1678  }
1679  return string( "" );
1680 }
VSError VSFileSystem::VSFile::ReadLine ( void *  ptr,
size_t  length 
)

Definition at line 1609 of file vsfilesystem.cpp.

References GoAfterEOL(), i, VSFileSystem::Ok, VSFileSystem::q_volume_format, size, VSFileSystem::Unspecified, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, VSFS_DEBUG(), and VSFileSystem::VSFSNone.

Referenced by ROLES::buildroles(), ROLES::buildscripts(), getProgramSource(), AnimatedTexture::LoadAni(), SaveGame::ParseSaveGame(), parseTurretSizes(), StarSystemGent::readColorGrads(), readnames(), and AccountServer::recvMsg().

1610 {
1611  char *ret;
1612  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone) {
1613  ret = fgets( (char*) ptr, length, this->fp );
1614  if (!ret)
1615  return Unspecified;
1616  } else {
1617  if (q_volume_format == vfmtVSR) {} else if (q_volume_format == vfmtPK3) {
1618  checkExtracted();
1619  ret = (char*) ptr;
1620 
1621  bool nl_found = false;
1622  unsigned int i = 0;
1623  if (VSFS_DEBUG() > 1)
1624  cerr<<"READLINE STARTING OFFSET="<<offset;
1625  for (i = 0; !nl_found && i < length && offset < size; offset++, i++) {
1626  if (pk3_extracted_file[offset] == '\n' || pk3_extracted_file[offset] == '\r') {
1627  nl_found = true;
1628  if (VSFS_DEBUG() > 1) {
1629  if (pk3_extracted_file[offset] == '\n')
1630  cerr<<"\\n ";
1631  if (pk3_extracted_file[offset] == '\r')
1632  cerr<<"\\r ";
1633  }
1634  } else {
1635  ret[i] = pk3_extracted_file[offset];
1636  if (VSFS_DEBUG() > 1)
1637  cerr<<std::hex<<ret[i]<<" ";
1638  }
1639  }
1640  this->GoAfterEOL( length );
1641  ret[i] = 0;
1642  if (VSFS_DEBUG() > 1)
1643  cerr<<std::dec<<" - read "<<i<<" char - "<<ret<<endl;
1644  if (!nl_found)
1645  return Unspecified;
1646  }
1647  }
1648  return Ok;
1649 }
void VSFileSystem::VSFile::SetAltType ( VSFileType  type)

Definition at line 1934 of file vsfilesystem.cpp.

Referenced by VSFileSystem::LookForFile().

1935 {
1936  this->alt_type = type;
1937 }
void VSFileSystem::VSFile::SetDirectory ( const string &  directory)
inline

Definition at line 565 of file vsfilesystem.h.

Referenced by VSFileSystem::LookForFile().

566  {
567  this->directoryname = directory;
568  }
void VSFileSystem::VSFile::SetFilename ( const string &  filename)
inline

Definition at line 561 of file vsfilesystem.h.

Referenced by VSFileSystem::LookForFile().

562  {
563  this->filename = filename;
564  }
void VSFileSystem::VSFile::SetIndex ( int  index)

Definition at line 1938 of file vsfilesystem.cpp.

References index.

Referenced by VSFileSystem::LookForFile().

1939 {
1940  this->file_index = index;
1941 }
void VSFileSystem::VSFile::SetRoot ( const string &  root)
inline

Definition at line 573 of file vsfilesystem.h.

Referenced by VSFileSystem::LookForFile().

574  {
575  this->rootname = root;
576  }
void VSFileSystem::VSFile::SetSubDirectory ( const string &  subdirectory)
inline

Definition at line 569 of file vsfilesystem.h.

Referenced by VSFileSystem::LookForFile().

570  {
571  this->subdirectoryname = subdirectory;
572  }
void VSFileSystem::VSFile::SetType ( VSFileType  type)

Definition at line 1930 of file vsfilesystem.cpp.

1931 {
1932  this->file_type = type;
1933 }
void VSFileSystem::VSFile::SetVolume ( VSVolumeType  big)

Definition at line 1943 of file vsfilesystem.cpp.

Referenced by VSFileSystem::LookForFile().

1944 {
1945  this->volume_type = big;
1946 }
long VSFileSystem::VSFile::Size ( )

Definition at line 1799 of file vsfilesystem.cpp.

References VSFileSystem::q_volume_format, VSFileSystem::ReadOnly, size, stat::st_size, VSFileSystem::UseVolumes, VSFileSystem::vfmtPK3, VSFileSystem::vfmtVSR, and VSFileSystem::VSFSNone.

Referenced by VsnetDownload::Server::Manager::addCmdDownload(), AUDCreateSoundMP3(), AUDLoadSoundFile(), ROLES::buildroles(), ROLES::buildscripts(), Eof(), GetHelperPlayerSaveGame(), LoadAccounts(), Mesh::LoadMeshes(), Nebula::LoadXML(), easyDomFactory< configNode >::LoadXML(), parseTurretSizes(), ReadFull(), and AccountServer::sendAuthorized().

1800 {
1801  if (size == 0) {
1802  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone || file_mode != ReadOnly) {
1803  struct stat st;
1804  if ( (fp != NULL) && fstat( fileno( fp ), &st ) == 0 )
1805  return this->size = st.st_size;
1806  return -1;
1807  } else {
1808  if (q_volume_format == vfmtVSR) {} else if (q_volume_format == vfmtPK3) {
1809  checkExtracted();
1810  return this->size;
1811  }
1812  }
1813  return -1;
1814  }
1815  return this->size;
1816 }
bool VSFileSystem::VSFile::UseVolume ( )

Definition at line 1947 of file vsfilesystem.cpp.

References VSFileSystem::UseVolumes, and VSFileSystem::VSFSNone.

1948 {
1949  return UseVolumes[alt_type] && volume_type != VSFSNone;
1950 }
bool VSFileSystem::VSFile::Valid ( )

Definition at line 1860 of file vsfilesystem.cpp.

Referenced by Texture::Load(), and VSSprite::ReadTexture().

1861 {
1862  return valid;
1863 }
size_t VSFileSystem::VSFile::Write ( const void *  ptr,
size_t  length 
)

Definition at line 1682 of file vsfilesystem.cpp.

References VSFileSystem::Ok, VSFileSystem::UseVolumes, VSExit(), and VSFileSystem::VSFSNone.

Referenced by VsnetDownload::Client::File::childAppend(), CopyFile(), CopySavedShips(), GetHelperPlayerSaveGame(), SaveFileCopy(), Write(), FileUtil::WriteSaveFiles(), SaveGame::WriteSaveGame(), and Unit::WriteUnit().

1683 {
1684  if (!UseVolumes[this->alt_type] || this->volume_type == VSFSNone) {
1685  size_t nbwritten = fwrite( ptr, 1, length, this->fp );
1686  return nbwritten;
1687  } else {
1688  cerr<<"!!! ERROR : Writing is not supported within resource/volume files"<<endl;
1689  VSExit( 1 );
1690  }
1691  return Ok;
1692 }
size_t VSFileSystem::VSFile::Write ( const string &  content)

Definition at line 1694 of file vsfilesystem.cpp.

References Write().

1695 {
1696  std::string::size_type length = content.length();
1697  return this->Write( content.c_str(), length );
1698 }
void VSFileSystem::VSFile::WriteFull ( void *  ptr)

Definition at line 1711 of file vsfilesystem.cpp.

1712 {}
VSError VSFileSystem::VSFile::WriteLine ( const void *  ptr)

Definition at line 1700 of file vsfilesystem.cpp.

References VSFileSystem::Ok, VSFileSystem::UseVolumes, VSExit(), and VSFileSystem::VSFSNone.

Referenced by AccountServer::recvMsg().

1701 {
1702  if (!UseVolumes[alt_type] || this->volume_type == VSFSNone) {
1703  fputs( (const char*) ptr, this->fp );
1704  } else {
1705  cerr<<"!!! ERROR : Writing is not supported within resource/volume files"<<endl;
1706  VSExit( 1 );
1707  }
1708  return Ok;
1709 }

Friends And Related Function Documentation

friend class VSImage
friend

Definition at line 596 of file vsfilesystem.h.


The documentation for this class was generated from the following files: