Subversion Repositories spk

Rev

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

Rev 81 Rev 82
Line 15... Line 15...
15
{
15
{
16
}
16
}
17
 
17
 
18
/*CFileIO::CFileIO(const Utils::String &sFilename, bool bBinary = true) : m_lSize(0), m_bBinary(bBinary), m_bAutoDelete(false)
18
/*CFileIO::CFileIO(const Utils::String &sFilename, bool bBinary = true) : m_lSize(0), m_bBinary(bBinary), m_bAutoDelete(false)
19
{
19
{
20
	Open(sFilename, bBinary);
20
	open(sFilename, bBinary);
21
}
21
}
22
*/
22
*/
23
CFileIO::CFileIO(CyString filename) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
23
CFileIO::CFileIO(CyString filename) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
24
{
24
{
25
	Open(filename, true);
25
	open(filename.ToString(), true);
26
}
26
}
27
 
27
 
28
CFileIO::CFileIO(C_File *file) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
28
CFileIO::CFileIO(C_File *file) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
29
{
29
{
30
	Open(file->GetFilePointer(), true);
30
	open(file->GetFilePointer().ToString(), true);
31
}
31
}
32
 
32
 
33
CFileIO::~CFileIO()
33
CFileIO::~CFileIO()
34
{
34
{
35
	if ( this->isOpened() ) m_fId.close();
35
	if ( this->isOpened() ) m_fId.close();
Line 39... Line 39...
39
void CFileIO::setAutoDelete(bool bDelete)
39
void CFileIO::setAutoDelete(bool bDelete)
40
{
40
{
41
	m_bAutoDelete = true;
41
	m_bAutoDelete = true;
42
}
42
}
43
 
43
 
44
bool CFileIO::Open ( CyString filename, bool binary )
44
bool CFileIO::open(const Utils::String &sFilename, bool bBinary)
45
{
45
{
46
	m_bBinary = binary;
46
	m_bBinary = bBinary;
47
	m_sFilename = filename.ToString();
47
	m_sFilename = sFilename.asFilename();
48
	m_sFilename = m_sFilename.findReplace("\\", "/");
-
 
49
	m_sFilename = m_sFilename.findReplace("//", "/");
-
 
50
 
48
 
-
 
49
	// check if there are any directories, and split the file and directory parts
51
	if ( m_sFilename.isin('/') ) {
50
	if ( m_sFilename.isin('/') ) {
52
		m_sDirIO.SetDir(m_sFilename.tokens("/", 1, -2));
51
		m_sDirIO.SetDir(m_sFilename.tokens("/", 1, -2));
53
		m_sFile = m_sFilename.token("/", -1);
52
		m_sFile = m_sFilename.token("/", -1);
54
	}
53
	}
55
	else
54
	else
56
		m_sFile = filename.ToString();
55
		m_sFile = m_sFilename;
57
 
56
 
58
	this->_readFileSize();
57
	this->_readFileSize();
59
	return true;
58
	return true;
-
 
59
}
-
 
60
bool CFileIO::Open ( CyString filename, bool binary )
-
 
61
{
-
 
62
	return open(filename.ToString(), binary);
60
}
63
}
61
 
64
 
62
unsigned char *CFileIO::read(size_t iAmount)
65
unsigned char *CFileIO::read(size_t iAmount)
63
{
66
{
64
	if ( !this->isOpened() ) startRead();
67
	if ( !this->isOpened() ) startRead();
Line 324... Line 327...
324
}
327
}
325
 
328
 
326
bool CFileIO::WriteData ( const char *data, size_t size )
329
bool CFileIO::WriteData ( const char *data, size_t size )
327
{
330
{
328
	if ( NoFile() )
331
	if ( NoFile() )
329
		return false;
332
		return false;
330
 
333
 
331
	std::fstream File(m_sFilename, _out());
334
	std::fstream File(m_sFilename, _out());
332
	if ( !File.is_open() ) return false;
335
	if ( !File.is_open() ) return false;
333
 
336
 
334
	bool ret = true;
337
	bool ret = true;
335
	try {
338
	try {
Line 344... Line 347...
344
 
347
 
345
	if ( ret ) this->_readFileSize();
348
	if ( ret ) this->_readFileSize();
346
	return ret;
349
	return ret;
347
}
350
}
348
 
351
 
349
bool CFileIO::WriteString ( CyString data )
352
bool CFileIO::writeString ( const Utils::String &data )
350
{
353
{
351
	return WriteData ( data.c_str(), data.Length() );
354
	return WriteData ( data.c_str(), data.length() );
352
}
355
}
353
 
356
 
354
bool CFileIO::Exists(const Utils::String &filename)
357
bool CFileIO::Exists(const Utils::String &filename)
355
{
358
{
356
	std::fstream File(filename, std::ios::in | std::ios::binary);
359
	std::fstream File(filename, std::ios::in | std::ios::binary);
357
	bool bRet = File.good();
360
	bool bRet = File.good();
358
	File.close();
361
	File.close();
359
	return bRet;
362
	return bRet;
360
}
363
}
361
 
364
 
362
bool CFileIO::ExistsOld () const
365
bool CFileIO::ExistsOld () const
363
{
366
{
364
	return this->exists();
367
	return this->exists();
365
}
368
}
366
bool CFileIO::exists () const
369
bool CFileIO::exists () const
367
{
370
{
368
	if ( this->isOpened() ) return true;
371
	if ( this->isOpened() ) return true;
369
	std::fstream File(m_sFilename, _in());
372
	std::fstream File(m_sFilename, _in());
370
	bool bRet = File.good();
373
	bool bRet = File.good();
371
	File.close();
374
	File.close();
372
	return bRet;
375
	return bRet;
373
}
376
}
374
 
-
 
375
bool CFileIO::StartRead() { return startRead(); }
-
 
376
 
377
 
377
bool CFileIO::startRead()
378
bool CFileIO::startRead()
378
{
379
{
379
	return _start(_in(), false);
380
	return _start(_in(), false);
380
}
381
}
381
 
382
 
382
bool CFileIO::startWrite()
383
bool CFileIO::startWrite()
383
{
384
{
384
	return _start(_out(), true);
385
	return _start(_out(), true);
385
}
386
}
386
 
387
 
387
bool CFileIO::startModify()
388
bool CFileIO::startModify()
388
{
389
{
389
	return _start(_inout(), true);
390
	return _start(_inout(), true);
390
}
391
}
391
 
392
 
Line 398... Line 399...
398
{
399
{
399
	if ( !m_sFilename.empty() ) {
400
	if ( !m_sFilename.empty() ) {
400
		if ( this->isOpened() ) this->close();
401
		if ( this->isOpened() ) this->close();
401
 
402
 
402
		m_fId.open(m_sFilename, iFlags);
403
		m_fId.open(m_sFilename, iFlags);
403
	}
404
	}
404
	m_bSeekP = bSeekP;
405
	m_bSeekP = bSeekP;
405
	return m_fId.is_open();
406
	return m_fId.is_open();
406
}
407
}
-
 
408
 
-
 
409
std::vector<Utils::String> *CFileIO::readLines()
-
 
410
{
-
 
411
	if ( m_sFilename.empty() ) return 0;
-
 
412
 
-
 
413
	std::vector<Utils::String> *file = new std::vector<Utils::String>;
-
 
414
	std::string line;
-
 
415
	file->clear();
-
 
416
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
-
 
417
	while (getline(infile, line, '\n')) {
-
 
418
		file->push_back(Utils::String(line).removeChar((char)0));
-
 
419
	}
-
 
420
 
-
 
421
	infile.close();
407
 
422
 
-
 
423
	return file;
-
 
424
}
408
std::vector<CyString> *CFileIO::ReadLines()
425
std::vector<CyString> *CFileIO::ReadLines()
409
{
426
{
410
	if ( m_sFilename.empty() ) return 0;
427
	if ( m_sFilename.empty() ) return 0;
411
 
428
 
412
	std::vector<CyString> *file = new std::vector<CyString>;
429
	std::vector<CyString> *file = new std::vector<CyString>;
Line 416... Line 433...
416
	while (getline(infile, line, '\n'))
433
	while (getline(infile, line, '\n'))
417
	{
434
	{
418
		CyString l = line;
435
		CyString l = line;
419
		l.RemoveChar((char)0);
436
		l.RemoveChar((char)0);
420
		file->push_back(l);
437
		file->push_back(l);
421
	}
438
	}
422
 
439
 
423
	infile.close();
440
	infile.close();
424
 
441
 
425
	return file;
442
	return file;
426
}
443
}
Line 443... Line 460...
443
 
460
 
444
	return file;
461
	return file;
445
}
462
}
446
 
463
 
447
bool CFileIO::put(const unsigned char c)
464
bool CFileIO::put(const unsigned char c)
448
{
465
{
449
	if ( !this->isOpened() ) return false;
466
	if ( !this->isOpened() ) return false;
450
 
467
 
451
	m_fId.put(c);
468
	m_fId.put(c);
452
	return !m_fId.bad();
469
	return !m_fId.bad();
453
}
470
}
454
 
471
 
455
bool CFileIO::writeSize(unsigned int iSize)
472
bool CFileIO::writeSize(unsigned int iSize)
456
{
473
{
457
	if ( !this->isOpened() ) return false;
474
	if ( !this->isOpened() ) return false;
458
	if ( !this->put(static_cast<unsigned char>(iSize >> 24)) ) return false;
475
	if ( !this->put(static_cast<unsigned char>(iSize >> 24)) ) return false;
459
	if ( !this->put(static_cast<unsigned char>(iSize >> 16)) ) return false;
476
	if ( !this->put(static_cast<unsigned char>(iSize >> 16)) ) return false;
460
	if ( !this->put(static_cast<unsigned char>(iSize >> 8)) ) return false;
477
	if ( !this->put(static_cast<unsigned char>(iSize >> 8)) ) return false;
461
	if ( !this->put(static_cast<unsigned char>(iSize)) ) return false;
478
	if ( !this->put(static_cast<unsigned char>(iSize)) ) return false;
462
	m_lSize += 4;
479
	m_lSize += 4;
Line 469... Line 486...
469
	if ( !this->isOpened() ) return false;
486
	if ( !this->isOpened() ) return false;
470
 
487
 
471
	int iMaxSize = 1000000;
488
	int iMaxSize = 1000000;
472
 
489
 
473
	unsigned char *data;
490
	unsigned char *data;
474
	try {
491
	try {
475
		data = new unsigned char[iMaxSize + 1];
492
		data = new unsigned char[iMaxSize + 1];
476
	}
493
	}
477
	catch (std::exception &e) {
494
	catch (std::exception &e) {
478
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::write(CFileIO, size_t) unable to malloc data, %d (%s)", iMaxSize + 1, e.what());
495
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::write(CFileIO, size_t) unable to malloc data, %d (%s)", iMaxSize + 1, e.what());
479
		return false;
496
		return false;
Line 578... Line 595...
578
}
595
}
579
 
596
 
580
std::fstream &CFileIO::stream()
597
std::fstream &CFileIO::stream()
581
{
598
{
582
	return m_fId;
599
	return m_fId;
583
}
-
 
584
 
-
 
585
void CFileIO::StopRead()
-
 
586
{
-
 
587
	this->close();
-
 
588
}
600
}
589
 
601
 
590
bool CFileIO::isOpened() const
602
bool CFileIO::isOpened() const
591
{
603
{
592
	if ( m_fId.is_open() )
604
	if ( m_fId.is_open() )
Line 595... Line 607...
595
}
607
}
596
 
608
 
597
void CFileIO::close()
609
void CFileIO::close()
598
{
610
{
599
	if ( this->isOpened() ) m_fId.close();
611
	if ( this->isOpened() ) m_fId.close();
600
}
612
}
601
 
613
 
602
int CFileIO::readSize()
614
int CFileIO::readSize()
603
{
615
{
604
	unsigned char size[4];
616
	unsigned char size[4];
605
	if ( this->read(size, 4) ) return (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
617
	if ( this->read(size, 4) ) return (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
606
	return 0;
618
	return 0;
607
}
619
}
608
 
620
 
609
bool CFileIO::AtEnd() const
-
 
610
{
-
 
611
	return this->atEnd();
-
 
612
}
-
 
613
bool CFileIO::atEnd() const
621
bool CFileIO::atEnd() const
614
{
622
{
615
	if ( !this->isOpened() ) return true;
623
	if ( !this->isOpened() ) return true;
616
	if ( m_fId.eof() )		 return true;
624
	if ( m_fId.eof() )		 return true;
617
	return false;
625
	return false;
-
 
626
}
-
 
627
 
-
 
628
bool CFileIO::appendFile ( const Utils::String &filename )
-
 
629
{
-
 
630
	std::fstream fromFile(filename, _in());
-
 
631
	if ( !fromFile.is_open() ) return false;
-
 
632
 
-
 
633
	std::fstream toFile(m_sFilename, _append());
-
 
634
	if ( !toFile.is_open() ) {
-
 
635
		fromFile.close();
-
 
636
		return false;
-
 
637
	}
-
 
638
 
-
 
639
	// move to the end of the file
-
 
640
	toFile.seekg(0, std::ios::end);
-
 
641
 
-
 
642
	// get size of file
-
 
643
	fromFile.seekg(0, std::ios::end);
-
 
644
	size_t size = fromFile.tellg();
-
 
645
	fromFile.seekg(0, std::ios::beg);
-
 
646
 
-
 
647
	char data[500000];
-
 
648
	while ( size > 0 )
-
 
649
	{
-
 
650
		size_t read = 500000;
-
 
651
		if ( read > size )
-
 
652
			read = size;
-
 
653
 
-
 
654
		size -= read;
-
 
655
 
-
 
656
		fromFile.read(data, read);
-
 
657
		toFile.write(data, read);
-
 
658
	}
-
 
659
 
-
 
660
	fromFile.close();
-
 
661
	toFile.close();
-
 
662
	return true;
618
}
663
}
619
 
664
 
620
bool CFileIO::AppendFile ( CyString filename )
665
bool CFileIO::AppendFile ( CyString filename )
621
{
666
{
622
	std::fstream fromFile(filename.ToString().c_str(), _in());
667
	std::fstream fromFile(filename.ToString().c_str(), _in());
Line 703... Line 748...
703
}
748
}
704
 
749
 
705
Utils::String CFileIO::readEndOfLine()
750
Utils::String CFileIO::readEndOfLine()
706
{
751
{
707
	if ( !this->isOpened() ) {
752
	if ( !this->isOpened() ) {
708
		if ( !StartRead() )	return "";
753
		if ( !startRead() )	return "";
709
	}
754
	}
710
 
755
 
711
	Utils::String str;
756
	Utils::String str;
712
	std::getline(m_fId, str, '\n');
757
	std::getline(m_fId, str, '\n');
713
	return str;
758
	return str;