Subversion Repositories spk

Rev

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

Rev 57 Rev 58
Line 253... Line 253...
253
	File.close();
253
	File.close();
254
	this->close();
254
	this->close();
255
	File.setAutoDelete(false);
255
	File.setAutoDelete(false);
256
 
256
 
257
	// now copy to original file
257
	// now copy to original file
258
	std::remove(m_sFilename.c_str());
258
	if ( std::remove(m_sFilename.c_str()) == 0 ) {
259
	std::rename("temp.tmp", m_sFilename.c_str());
259
		std::rename("temp.tmp", m_sFilename.c_str());
-
 
260
		return FILEERR_NONE;
-
 
261
	}
260
 
262
 
261
	return FILEERR_NONE;
263
	return FILEERR_NOWRITE;
262
}
264
}
263
 
265
 
264
int CFileIO::_in() const
266
int CFileIO::_in() const
265
{
267
{
266
	if ( m_bBinary ) return std::ios::in | std::ios::binary;
268
	if ( m_bBinary ) return std::ios::in | std::ios::binary;
Line 274... Line 276...
274
}
276
}
275
int CFileIO::_inout() const
277
int CFileIO::_inout() const
276
{
278
{
277
	if ( m_bBinary ) return std::ios::in | std::ios::out | std::ios::binary;
279
	if ( m_bBinary ) return std::ios::in | std::ios::out | std::ios::binary;
278
	return std::ios::in | std::ios::out;
280
	return std::ios::in | std::ios::out;
279
}
281
}
280
 
282
 
281
int CFileIO::_append() const
283
int CFileIO::_append() const
282
{
284
{
283
	if ( m_bBinary ) return std::ios::out | std::ios::binary | std::ios::app;
285
	if ( m_bBinary ) return std::ios::out | std::ios::binary | std::ios::app;
284
	return std::ios::out | std::ios::app;
286
	return std::ios::out | std::ios::app;
285
}
287
}
Line 296... Line 298...
296
	if ( !file.is_open() ) return NULL;
298
	if ( !file.is_open() ) return NULL;
297
 
299
 
298
	char *data;
300
	char *data;
299
	try {
301
	try {
300
		data = new char[m_lSize + 1];
302
		data = new char[m_lSize + 1];
301
	}
303
	}
302
	catch (std::exception &e) {
304
	catch (std::exception &e) {
303
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to malloc storage, %d (%s)", m_lSize + 1, e.what());
305
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to malloc storage, %d (%s)", m_lSize + 1, e.what());
304
		file.close();
306
		file.close();
305
		return NULL;
307
		return NULL;
306
	}
308
	}
307
 
309
 
308
	try {
310
	try {
309
		file.read((char *)data, m_lSize);
311
		file.read((char *)data, m_lSize);
310
	}
312
	}
311
	catch (std::exception &e) {
313
	catch (std::exception &e) {
312
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file (%s)", e.what());
314
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file (%s)", e.what());
313
		file.close();
315
		file.close();
314
		return NULL;
316
		return NULL;
315
	}
317
	}
316
 
318
 
317
	*size = m_lSize;
319
	*size = m_lSize;
318
	file.close();
320
	file.close();
319
 
321
 
320
	return data;
322
	return data;
321
}
323
}
322
 
324
 
323
bool CFileIO::WriteData ( const char *data, size_t size )
325
bool CFileIO::WriteData ( const char *data, size_t size )
324
{
326
{
325
	if ( NoFile() )
327
	if ( NoFile() )
326
		return false;
328
		return false;
327
 
329
 
Line 338... Line 340...
338
	}
340
	}
339
 
341
 
340
	File.close();
342
	File.close();
341
 
343
 
342
	if ( ret ) this->_readFileSize();
344
	if ( ret ) this->_readFileSize();
343
	return ret;
345
	return ret;
344
}
346
}
345
 
347
 
346
bool CFileIO::WriteString ( CyString data )
348
bool CFileIO::WriteString ( CyString data )
347
{
349
{
348
	return WriteData ( data.c_str(), data.Length() );
350
	return WriteData ( data.c_str(), data.Length() );
349
}
351
}
350
 
352
 
Line 592... Line 594...
592
}
594
}
593
 
595
 
594
void CFileIO::close()
596
void CFileIO::close()
595
{
597
{
596
	if ( this->isOpened() ) m_fId.close();
598
	if ( this->isOpened() ) m_fId.close();
597
}
599
}
598
 
600
 
599
int CFileIO::readSize()
601
int CFileIO::readSize()
600
{
602
{
601
	unsigned char size[4];
603
	unsigned char size[4];
602
	if ( this->read(size, 4) ) return (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
604
	if ( this->read(size, 4) ) return (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
603
	return 0;
605
	return 0;
604
}
606
}
605
 
607
 
606
bool CFileIO::AtEnd() const
608
bool CFileIO::AtEnd() const
607
{
609
{
608
	return this->atEnd();
610
	return this->atEnd();
609
}
611
}
610
bool CFileIO::atEnd() const
612
bool CFileIO::atEnd() const
611
{
613
{
612
	if ( !this->isOpened() ) return true;
614
	if ( !this->isOpened() ) return true;
Line 621... Line 623...
621
 
623
 
622
	std::fstream toFile(m_sFilename, _append());
624
	std::fstream toFile(m_sFilename, _append());
623
	if ( !toFile.is_open() ) {
625
	if ( !toFile.is_open() ) {
624
		fromFile.close();
626
		fromFile.close();
625
		return false;
627
		return false;
626
	}
628
	}
627
 
629
 
628
	// move to the end of the file
630
	// move to the end of the file
629
	toFile.seekg(0, std::ios::end);
631
	toFile.seekg(0, std::ios::end);
630
 
632
 
631
	// get size of file
633
	// get size of file
632
	fromFile.seekg(0, std::ios::end);
634
	fromFile.seekg(0, std::ios::end);
633
	size_t size = fromFile.tellg();
635
	size_t size = fromFile.tellg();
Line 642... Line 644...
642
 
644
 
643
		size -= read;
645
		size -= read;
644
 
646
 
645
		fromFile.read(data, read);
647
		fromFile.read(data, read);
646
		toFile.write(data, read);
648
		toFile.write(data, read);
647
	}
649
	}
648
 
650
 
649
	fromFile.close();
651
	fromFile.close();
650
	toFile.close();
652
	toFile.close();
651
	return true;
653
	return true;
652
}
654
}
653
 
655
 
654
 
656
 
655
bool CFileIO::AppendData ( const char *d, size_t size )
657
bool CFileIO::AppendData ( const char *d, size_t size )
656
{
658
{
657
	std::fstream File(m_sFilename, _append());
659
	std::ofstream File(m_sFilename, _append());
658
	if ( !File.is_open() ) return false;
660
	if ( !File.is_open() ) return false;
659
 
661
 
660
	// move to the end of the file
662
	// move to the end of the file
661
	File.seekg(0, std::ios::end);
663
	//File.seekg(0, std::ios::end);
662
 
664
 
663
	char *pos = (char *)d;
665
	char *pos = (char *)d;
664
	while ( size > 0 ) {
666
	while ( size > 0 ) {
665
		size_t read = 500000;
667
		size_t read = 500000;
666
		if ( read > size ) read = size;
668
		if ( read > size ) read = size;
Line 677... Line 679...
677
		}
679
		}
678
		pos += read;
680
		pos += read;
679
	}
681
	}
680
 
682
 
681
	File.close();
683
	File.close();
682
	return true;
684
	return true;
683
}
685
}
684
 
686
 
685
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
687
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
686
{
688
{
687
	if ( start > m_lSize ) return false;
689
	if ( start > m_lSize ) return false;
688
	if ( m_lSize <= 0 ) {
690
	if ( m_lSize <= 0 ) {
689
		if ( !this->startWrite() ) return false;
691
		if ( !this->startWrite() ) return false;
Line 701... Line 703...
701
 
703
 
702
Utils::String CFileIO::readEndOfLine()
704
Utils::String CFileIO::readEndOfLine()
703
{
705
{
704
	if ( !this->isOpened() ) {
706
	if ( !this->isOpened() ) {
705
		if ( !StartRead() )	return "";
707
		if ( !StartRead() )	return "";
706
	}
708
	}
707
 
709
 
708
	Utils::String str;
710
	Utils::String str;
709
	std::getline(m_fId, str, '\n');
711
	std::getline(m_fId, str, '\n');
710
	return str;
712
	return str;
711
}
713
}
712
 
714
 
713
Utils::String CFileIO::baseName() const
715
Utils::String CFileIO::baseName() const
714
{
716
{
715
	return m_sFile.token(".", -2);
717
	return m_sFile.token(".", -2);
716
}
718
}
717
CyString CFileIO::GetFileExtension ()
719
CyString CFileIO::GetFileExtension ()
718
{
720
{
719
	if ( m_sFilename.empty() ) return NullString;
721
	if ( m_sFilename.empty() ) return NullString;
-
 
722
	return m_sFilename.token(".", -1);
-
 
723
}
-
 
724
 
-
 
725
Utils::String CFileIO::extension ()
-
 
726
{
-
 
727
	if ( m_sFilename.empty() ) return "";
720
	return m_sFilename.token(".", -1);
728
	return m_sFilename.token(".", -1);
721
}
729
}
722
 
730
 
723
CyString CFileIO::ChangeFileExtension ( CyString ext )
731
CyString CFileIO::ChangeFileExtension ( CyString ext )
724
{
732
{
Line 733... Line 741...
733
	//if ( !Exists() ) return false;
741
	//if ( !Exists() ) return false;
734
	return (std::remove(rem) == 0) ? true : false;
742
	return (std::remove(rem) == 0) ? true : false;
735
}
743
}
736
 
744
 
737
bool CFileIO::remove()
745
bool CFileIO::remove()
738
{
746
{
739
	if ( this->isOpened() ) this->close();
747
	if ( this->isOpened() ) this->close();
740
	if ( !this->exists() ) return false;
748
	if ( !this->exists() ) return false;
741
	if ( std::remove(m_sFilename.c_str()) == 0 ) return true;
749
	if ( std::remove(m_sFilename.c_str()) == 0 ) return true;
742
	return false;
750
	return false;
743
}
751
}
744
 
752
 
745
bool CFileIO::WriteFileUTF(std::vector<CyString> *lines)
753
bool CFileIO::WriteFileUTF(std::vector<CyString> *lines)
746
{
754
{
747
	if ( !lines || m_sFilename.empty() )
755
	if ( !lines || m_sFilename.empty() )
748
		return false;
756
		return false;
749
 
757
 
750
	// we need to create the directory
758
	// we need to create the directory
751
	if ( !m_sDirIO.Exists() )
759
	if ( !m_sDirIO.Exists() )
752
	{
760
	{
753
		if ( !m_sDirIO.Create() )
761
		if ( !m_sDirIO.Create() )
Line 764... Line 772...
764
	// write the rest
772
	// write the rest
765
	for ( int i = 0; i < (int)lines->size(); i++ )
773
	for ( int i = 0; i < (int)lines->size(); i++ )
766
	{
774
	{
767
		CyString l = lines->at(i);
775
		CyString l = lines->at(i);
768
		if ( l.IsIn('\n') )
776
		if ( l.IsIn('\n') )
769
		{
777
		{
770
			int max;
778
			int max;
771
			CyString *strs = l.SplitToken("\n", &max);
779
			CyString *strs = l.SplitToken("\n", &max);
772
			if ( strs && max )
780
			if ( strs && max )
773
			{
781
			{
774
				for ( int i = 0; i < max; i++ )
782
				for ( int i = 0; i < max; i++ )
Line 933... Line 941...
933
	if ( !stat(GetWindowsFilename().c_str(), &fileStat) )
941
	if ( !stat(GetWindowsFilename().c_str(), &fileStat) )
934
		return (time_t)fileStat.st_atime;
942
		return (time_t)fileStat.st_atime;
935
#endif
943
#endif
936
	return 0;
944
	return 0;
937
}
945
}
-
 
946
 
-
 
947
size_t CFileIO::position()
-
 
948
{
-
 
949
	return m_fId.tellg();
-
 
950
}
-
 
951
 
938
/**
952
/**
939
 * Copys the contents of a file to another
953
 * Copys the contents of a file to another
940
 *
954
 *
941
 * Reads and writes the files in block
955
 * Reads and writes the files in block
942
 */
956
 */