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
CPK3 Class Reference

#include <pk3.h>

Classes

struct  TZipDirFileHeader
 
struct  TZipDirHeader
 
struct  TZipLocalHeader
 

Public Member Functions

 CPK3 ()
 
 CPK3 (FILE *n_f)
 
 CPK3 (const char *filename)
 
 ~CPK3 ()
 
bool CheckPK3 (FILE *f)
 
bool Open (const char *filename)
 
bool ExtractFile (const char *lp_name)
 
bool ExtractFile (const char *lp_name, const char *new_filename)
 
char * ExtractFile (int index, int *file_size)
 
char * ExtractFile (const char *lpname, int *file_size)
 
int FileExists (const char *lpname)
 
bool Close (void)
 
void PrintFileContent ()
 

Detailed Description

Definition at line 47 of file pk3.h.

Constructor & Destructor Documentation

CPK3::CPK3 ( )
inline

Definition at line 65 of file pk3.h.

65 : CPK3() : m_nEntries( 0 ) {}
CPK3::CPK3 ( FILE *  n_f)

Definition at line 181 of file pk3.cpp.

References CheckPK3().

182 {
183  CheckPK3( n_f );
184 }
CPK3::CPK3 ( const char *  filename)

Definition at line 186 of file pk3.cpp.

References Open().

187 {
188  Open( filename );
189 }
CPK3::~CPK3 ( )
inline

Definition at line 68 of file pk3.h.

68 {}

Member Function Documentation

bool CPK3::CheckPK3 ( FILE *  f)

Definition at line 193 of file pk3.cpp.

References bogus_sizet, CPK3::TZipDirFileHeader::cmntLen, CPK3::TZipDirHeader::correctByteOrder(), CPK3::TZipDirFileHeader::correctByteOrder(), CPK3::TZipDirHeader::dirSize, CPK3::TZipDirFileHeader::fnameLen, i, j, CPK3::TZipDirHeader::nDirEntries, CPK3::TZipDirHeader::sig, CPK3::TZipDirFileHeader::sig, CPK3::TZipDirHeader::SIGNATURE, CPK3::TZipDirFileHeader::SIGNATURE, sizeof(), and CPK3::TZipDirFileHeader::xtraLen.

Referenced by CPK3(), and Open().

194 {
195  if (f == NULL)
196  return false;
197  TZipDirHeader dh;
198 
199  fseek( f, -(int) sizeof (dh), SEEK_END );
200 
201  long dhOffset = ftell( f );
202  memset( &dh, 0, sizeof (dh) );
203 
204  bogus_sizet = fread( &dh, sizeof (dh), 1, f );
205  dh.correctByteOrder();
206  //Check
207  if (dh.sig != TZipDirHeader::SIGNATURE) {
208  cerr<<"PK3 -- BAD DIR HEADER SIGNATURE, NOT A PK3 FILE !"<<endl;
209  exit( 1 );
210  return false;
211  }
212  //Go to the beginning of the directory.
213  fseek( f, dhOffset-dh.dirSize, SEEK_SET );
214 
215  //Allocate the data buffer, and read the whole thing.
216  m_pDirData = new char[dh.dirSize+dh.nDirEntries*sizeof (*m_papDir)];
217  if (!m_pDirData) {
218  cerr<<"PK3 -- ERROR ALLOCATING DATA BUFFER !"<<endl;
219  exit( 1 );
220  return false;
221  }
222  memset( m_pDirData, 0, dh.dirSize+dh.nDirEntries*sizeof (*m_papDir) );
223  bogus_sizet = fread( m_pDirData, dh.dirSize, 1, f );
224 
225  //Now process each entry.
226  char *pfh = m_pDirData;
227  m_papDir = (const TZipDirFileHeader**) (m_pDirData+dh.dirSize);
228 
229  bool ret = true;
230  for (int i = 0; i < dh.nDirEntries && ret == true; i++) {
231  TZipDirFileHeader &fh = *(TZipDirFileHeader*) pfh;
232  fh.correctByteOrder();
233 
234  //Store the address of nth file for quicker access.
235  m_papDir[i] = &fh;
236  //Check the directory entry integrity.
237  if (fh.sig != TZipDirFileHeader::SIGNATURE) {
238  cerr<<"PK3 -- ERROR BAD DIRECTORY SIGNATURE !"<<endl;
239  exit( 1 );
240  ret = false;
241  } else {
242  pfh += sizeof (fh);
243  //Convert UNIX slashes to DOS backlashes.
244  for (int j = 0; j < fh.fnameLen; j++)
245  if (pfh[j] == '/')
246  pfh[j] = '\\';
247  //Skip name, extra and comment fields.
248  pfh += fh.fnameLen+fh.xtraLen+fh.cmntLen;
249  }
250  }
251  if (ret != true) {
252  delete[] m_pDirData;
253  } else {
254  m_nEntries = dh.nDirEntries;
255  this->f = f;
256  }
257  return ret;
258 }
bool CPK3::Close ( void  )

Definition at line 378 of file pk3.cpp.

References fclose.

379 {
380  fclose( f );
381  delete[] m_pDirData;
382  m_nEntries = 0;
383 
384  return true;
385 }
bool CPK3::ExtractFile ( const char *  lp_name)

Definition at line 271 of file pk3.cpp.

Referenced by ExtractFile().

272 {
273  return ExtractFile( lp_name, lp_name );
274 }
bool CPK3::ExtractFile ( const char *  lp_name,
const char *  new_filename 
)

Definition at line 276 of file pk3.cpp.

References ExtractFile(), fclose, fopen, and size.

277 {
278  //open file tp write data
279  FILE *new_f = NULL;
280  int size = -1;
281 
282  char *data_content = ExtractFile( lp_name, &size );
283  if (data_content) {
284  if (size != -1) {
285  new_f = fopen( new_filename, "wb" );
286  fwrite( data_content, 1, size, new_f );
287  fclose( new_f );
288  delete data_content;
289  return true;
290  }
291  }
292  return false; //probably file not found
293 }
char * CPK3::ExtractFile ( int  index,
int file_size 
)

Definition at line 324 of file pk3.cpp.

References buffer.

325 {
326  char *buffer;
327  int flength = GetFileLen( index );
328 
329  buffer = new char[flength];
330  if (!buffer) {
331  cerr<<"Unable to allocate memory, probably to low memory !!!"<<endl;
332  return NULL;
333  } else {
334  if ( true == ReadFile( index, buffer ) ) {
335  //everything went well !!!
336  } else {
337  cerr<<"\nThe file was found in the archive, but I was unable to extract it. Maybe the archive is broken."<<endl;
338  }
339  }
340  *file_size = flength;
341  return buffer;
342 }
char * CPK3::ExtractFile ( const char *  lpname,
int file_size 
)

Definition at line 344 of file pk3.cpp.

References buffer, i, index, PK3LENGTH, and vsstrcmp().

345 {
346  char str[PK3LENGTH];
347  int index = -1;
348  char *buffer;
349 
350  memset( &str, 0, sizeof (str) );
351  for (int i = 0; index == -1 && i < m_nEntries; i++) {
352  GetFilename( i, str );
353  int result = vsstrcmp( lpname, str );
354  if (result == 0)
355  index = i;
356  }
357  //if the file isn't in the archive
358  if (index == -1)
359  return false;
360  int flength = GetFileLen( index );
361 
362  buffer = new char[flength];
363  if (!buffer) {
364  printf( "Unable to allocate memory, probably to low memory !!!\n" );
365  return NULL;
366  } else {
367  if ( true == ReadFile( index, buffer ) ) {
368  //everything went well !!!
369  } else {
370  printf( "\nThe file was found in the archive, but I was unable to " \
371  "extract it. Maybe the archive is broken.\n" );
372  }
373  }
374  *file_size = flength;
375  return buffer;
376 }
int CPK3::FileExists ( const char *  lpname)

Definition at line 306 of file pk3.cpp.

References i, PK3LENGTH, and vsstrcmp().

Referenced by VSFileSystem::FileExists().

307 {
308  char str[PK3LENGTH];
309  int idx = -1;
310 
311  memset( &str, 0, sizeof (str) );
312  for (int i = 0; idx == -1 && i < m_nEntries; i++) {
313  GetFilename( i, str );
314  int result = vsstrcmp( lpname, str );
315  if (result == 0) {
316  cerr<<"FOUND IN PK3 FILE : "<<lpname<<" with index="<<i<<endl;
317  idx = i;
318  }
319  }
320  //if the file isn't in the archive idx=-1
321  return idx;
322 }
bool CPK3::Open ( const char *  filename)

Definition at line 260 of file pk3.cpp.

References CheckPK3(), and fopen.

Referenced by CPK3(), and VSFileSystem::FileExists().

261 {
262  f = fopen( filename, "rb" );
263  if (f) {
264  strcpy( pk3filename, filename );
265  return CheckPK3( f );
266  } else {
267  return false;
268  }
269 }
void CPK3::PrintFileContent ( )

Definition at line 387 of file pk3.cpp.

References i.

388 {
389  printf( "PK3 File: %s\n", pk3filename );
390  printf( "files count: %d\n\n", m_nEntries );
391  for (int i = 0; i < m_nEntries; i++) {}
392 }

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