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;
60
}
59
}
-
 
60
bool CFileIO::Open ( CyString filename, bool binary )
-
 
61
{
-
 
62
	return open(filename.ToString(), binary);
-
 
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();
65
	if ( !this->isOpened() ) return NULL;
68
	if ( !this->isOpened() ) return NULL;
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);
Line 370... Line 373...
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
 
377
 
375
bool CFileIO::StartRead() { return startRead(); }
-
 
376
 
-
 
377
bool CFileIO::startRead()
378
bool CFileIO::startRead()
378
{
379
{
379
	return _start(_in(), false);
380
	return _start(_in(), false);
380
}
381
}
381
 
382
 
Line 403... Line 404...
403
	}
404
	}
404
	m_bSeekP = bSeekP;
405
	m_bSeekP = bSeekP;
405
	return m_fId.is_open();
406
	return m_fId.is_open();
406
}
407
}
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();
-
 
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 580... Line 597...
580
std::fstream &CFileIO::stream()
597
std::fstream &CFileIO::stream()
581
{
598
{
582
	return m_fId;
599
	return m_fId;
583
}
600
}
584
 
601
 
585
void CFileIO::StopRead()
-
 
586
{
-
 
587
	this->close();
-
 
588
}
-
 
589
 
-
 
590
bool CFileIO::isOpened() const
602
bool CFileIO::isOpened() const
591
{
603
{
592
	if ( m_fId.is_open() )
604
	if ( m_fId.is_open() )
593
		return true;
605
		return true;
594
	return false;
606
	return false;
Line 604... Line 616...
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;
618
}
626
}
619
 
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;
-
 
663
}
-
 
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());
623
	if ( !fromFile.is_open() ) return false;
668
	if ( !fromFile.is_open() ) return false;
624
 
669
 
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;