Subversion Repositories spk

Rev

Rev 51 | Rev 53 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 51 Rev 52
Line 345... Line 345...
345
	return this->ReadFromFile(GetFilePointer().c_str());
345
	return this->ReadFromFile(GetFilePointer().c_str());
346
}
346
}
347
 
347
 
348
bool C_File::ReadFromFile (CyString filename)
348
bool C_File::ReadFromFile (CyString filename)
349
{
349
{
350
	FILE *id = fopen ( filename.c_str(), "rb" );
350
	CFileIO File(filename);
351
	if ( !id )
351
	if ( !File.startRead() ) {
352
	{
-
 
353
		m_iLastError = SPKERR_FILEOPEN;
352
		m_iLastError = SPKERR_FILEOPEN;
354
		return false;
353
		return false;
355
	}
-
 
356
 
-
 
357
	if ( !m_lSize )
-
 
358
	{
-
 
359
		fseek ( id, 0, SEEK_END );
-
 
360
		m_lSize = ftell ( id );
-
 
361
		rewind ( id );
-
 
362
	}
354
	}
363
 
355
 
364
	m_iDataCompression = SPKCOMPRESS_NONE;
356
	m_iDataCompression = SPKCOMPRESS_NONE;
365
	m_lDataSize = m_lUncomprDataSize = m_lSize;
357
	m_lDataSize = m_lUncomprDataSize = m_lSize;
366
 
358
 
367
	DeleteData ();
359
	DeleteData ();
368
 
360
 
369
	m_sData = new unsigned char[m_lSize];
361
	m_sData = File.readAll((size_t *)&m_lSize);
370
	if ( !m_sData ) { fclose ( id ); m_iLastError = SPKERR_MALLOC; return false; }
-
 
371
 
-
 
372
	fread ( m_sData, sizeof(unsigned char), m_lSize, id );
-
 
373
	if ( ferror(id) )
362
	if ( !m_sData ) { 
374
	{
-
 
375
		m_iLastError = SPKERR_FILEREAD;
363
		m_iLastError = SPKERR_MALLOC; 
376
		DeleteData ();
-
 
377
		m_lDataSize = 0;
364
		m_lDataSize = 0;
378
		fclose ( id );
-
 
379
		return false;
365
		return false; 
380
	}
366
	}
-
 
367
 
-
 
368
	m_lDataSize = m_lSize;
-
 
369
	m_tTime = File.modifiedTime();
381
 
370
 
382
	m_iLastError = SPKERR_NONE;
371
	m_iLastError = SPKERR_NONE;
383
 
-
 
384
	struct stat fileStat;
-
 
385
	if ( !stat(GetFilePointer().c_str(), &fileStat) )
-
 
386
		m_tTime = fileStat.st_atime;
-
 
387
 
372
 
388
	m_bLoaded = true;
373
	m_bLoaded = true;
389
 
374
 
390
	fclose ( id );
375
	File.close();
391
 
-
 
392
	return true;
376
	return true;
393
}
377
}
394
 
378
 
395
/*
379
/*
396
	Func:	ReadFromData
380
	Func:	ReadFromData
Line 411... Line 395...
411
 
395
 
412
	return true;
396
	return true;
413
}
397
}
414
 
398
 
415
 
399
 
-
 
400
bool C_File::readFromFile(CFileIO &File, long lSize, bool bDoSize)
-
 
401
{
-
 
402
	return this->readFromFile(File.stream(), lSize, bDoSize);
-
 
403
}
416
bool C_File::readFromFile(std::fstream &stream, long lSize, bool bDoSize)
404
bool C_File::readFromFile(std::fstream &stream, long lSize, bool bDoSize)
417
{
405
{
418
	m_lDataSize = lSize;
406
	m_lDataSize = lSize;
419
	try {
407
	try {
420
		m_sData = new unsigned char[m_lDataSize];
408
		m_sData = new unsigned char[m_lDataSize];
421
	}
409
	}
422
	catch(std::exception &e) {
410
	catch(std::exception &e) {
423
		CLog::logf(CLog::Log_IO, 2, "C_File::readFromFile() unable to malloc, %d (%s)", lSize, e.what());
411
		CLog::logf(CLog::Log_IO, 2, "C_File::readFromFile() unable to malloc, %d (%s)", lSize, e.what());
424
		return false;
412
		return false;
425
	}
413
	}
426
 
414
 
427
	if ( bDoSize ) {
415
	if ( bDoSize ) {
428
		unsigned char s[4];
416
		unsigned char s[4];
429
		stream.read((char *)s, 4);
417
		stream.read((char *)s, 4);
430
	}
418
	}
431
 
419
 
432
	try { 
420
	try { 
433
		stream.read((char *)m_sData, m_lDataSize); 
421
		stream.read((char *)m_sData, m_lDataSize); 
434
	}
422
	}
435
	catch(std::exception &e) {
423
	catch(std::exception &e) {
Line 447... Line 435...
447
			size	- amount of data to read from file
435
			size	- amount of data to read from file
448
			dosize	- Read the 4 character size
436
			dosize	- Read the 4 character size
449
	Func:	Reads a data stream from a currently open file
437
	Func:	Reads a data stream from a currently open file
450
			Can be used to read directly from a SPK Package
438
			Can be used to read directly from a SPK Package
451
			dosize will read the initial 4 character uncompressed size if needed
439
			dosize will read the initial 4 character uncompressed size if needed
452
*/
440
*/
453
bool C_File::ReadFromFile ( FILE *id, long size, bool dosize )
441
bool C_File::ReadFromFile ( FILE *id, long size, bool dosize )
454
{
442
{
455
	// remove data
443
	// remove data
456
 
444
 
457
	m_lDataSize = size ;
445
	m_lDataSize = size ;
458
	m_sData = new unsigned char[m_lDataSize];
446
	m_sData = new unsigned char[m_lDataSize];
459
	if ( dosize )
447
	if ( dosize )
460
	{
448
	{
461
		unsigned char s[4];
449
		unsigned char s[4];
462
		fread ( s, sizeof(unsigned char), 4, id );
450
		fread ( s, sizeof(unsigned char), 4, id );
463
	}
451
	}
464
 
452
 
465
	fread ( m_sData, sizeof(unsigned char), m_lDataSize, id );
453
	fread ( m_sData, sizeof(unsigned char), m_lDataSize, id );
466
 
454
 
467
	if ( ferror (id) )
455
	if ( ferror (id) )
468
	{
456
	{
469
		DeleteData ();
457
		DeleteData ();
470
		m_lDataSize = 0;
458
		m_lDataSize = 0;
Line 474... Line 462...
474
	return true;
462
	return true;
475
}
463
}
476
 
464
 
477
 
465
 
478
void C_File::copyData(const unsigned char *data, size_t size)
466
void C_File::copyData(const unsigned char *data, size_t size)
479
{
467
{
480
	m_lDataSize = (long)size;
468
	m_lDataSize = (long)size;
481
	delete m_sData;
469
	delete m_sData;
482
	m_sData = new unsigned char[size];
470
	m_sData = new unsigned char[size];
483
	memcpy(m_sData, data, size);
471
	memcpy(m_sData, data, size);
484
}
472
}
Line 551... Line 539...
551
	Func:	ReadFileSize()
539
	Func:	ReadFileSize()
552
	Return:	Returns the file size read
540
	Return:	Returns the file size read
553
	Desc:	Opens the file and seeks to the end
541
	Desc:	Opens the file and seeks to the end
554
*/
542
*/
555
long C_File::ReadFileSize ()
543
long C_File::ReadFileSize ()
556
{
544
{
557
 
-
 
558
	FILE *id = fopen ( GetFilePointer().c_str(), "rb" );
545
	CFileIO File(GetFilePointer());
559
	if ( id )
-
 
560
	{
-
 
561
		fseek ( id, 0, SEEK_END );
-
 
562
		m_lSize = ftell ( id );
546
	if ( File.exists() ) m_lSize = File.fileSize();
563
		fclose ( id );
-
 
564
	}
-
 
565
 
547
 
566
	m_lUncomprDataSize = m_lSize;
548
	m_lUncomprDataSize = m_lSize;
567
 
549
 
568
	return m_lSize;
550
	return m_lSize;
569
}
551
}
570
 
552
 
571
/*
553
/*
572
	Func:	ReadLastModifed()
554
	Func:	ReadLastModifed()
573
	Desc:	Reads the last modified time of the file and returns
555
	Desc:	Reads the last modified time of the file and returns
574
			Uses seperate rountines for Windows and Linux
556
			Uses seperate rountines for Windows and Linux
575
*/
557
*/
576
time_t C_File::ReadLastModified ()
558
time_t C_File::ReadLastModified ()
577
{
559
{
578
	CyString file = GetFilePointer();
560
	CyString file = GetFilePointer();
579
	if ( file.Empty() )
561
	if ( file.Empty() )
580
		return m_tTime;
562
		return m_tTime;
581
 
563
 
582
	#ifndef _WIN32
564
	#ifndef _WIN32
Line 589... Line 571...
589
	return m_tTime;
571
	return m_tTime;
590
}
572
}
591
 
573
 
592
bool C_File::CheckValidFilePointer ()
574
bool C_File::CheckValidFilePointer ()
593
{
575
{
594
	CyString filename = GetFilePointer();
576
	return CFileIO::Exists(GetFilePointer().ToString());
595
	if ( filename.Empty() )
-
 
596
		return false;
-
 
597
 
-
 
598
	FILE *id = fopen ( filename.c_str(), "rb+" );
-
 
599
	if ( !id )
-
 
600
		return false;
-
 
601
 
-
 
602
	fclose ( id );
-
 
603
	return true;
-
 
604
}
577
}
605
 
578
 
606
bool C_File::ReadSignedFile()
579
bool C_File::ReadSignedFile()
607
{
580
{
608
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) )
581
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) )
Line 749... Line 722...
749
*/
722
*/
750
 
723
 
751
bool C_File::CompressFile ( CProgressInfo *progress )
724
bool C_File::CompressFile ( CProgressInfo *progress )
752
{
725
{
753
	CyString file = this->GetFilePointer();
726
	CyString file = this->GetFilePointer();
754
	if ( !CFileIO(this->GetFilePointer()).Exists() )
727
	if ( !CFileIO(this->GetFilePointer()).exists() )
755
	{
728
	{
756
		if ( !this->WriteToFile("tempuncompr.dat", m_sData, m_lDataSize) )
729
		if ( !this->WriteToFile("tempuncompr.dat", m_sData, m_lDataSize) )
757
			return false;
730
			return false;
758
		file = "tempuncompr.dat";
731
		file = "tempuncompr.dat";
759
	}
732
	}
Line 804... Line 777...
804
 
777
 
805
					fclose(id);
778
					fclose(id);
806
				}
779
				}
807
			}
780
			}
808
		}
781
		}
809
		CFileIO("tempcompr.dat").Remove();
782
		CFileIO::Remove("tempcompr.dat");
810
 
783
 
811
		fclose(fIn);
784
		fclose(fIn);
812
	}
785
	}
813
 
786
 
814
	CFileIO("tempuncompr.dat").Remove();
787
	CFileIO::Remove("tempuncompr.dat");
815
 
788
 
816
	return ret;
789
	return ret;
817
}
790
}
818
 
791
 
819
bool C_File::ChangeCompression ( int compressionType, CProgressInfo *progress )
792
bool C_File::ChangeCompression ( int compressionType, CProgressInfo *progress )