Subversion Repositories spk

Rev

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

Rev 196 Rev 197
Line 46... Line 46...
46
	m_bBinary = bBinary;
46
	m_bBinary = bBinary;
47
	_sFilename = sFilename.asFilename();
47
	_sFilename = sFilename.asFilename();
48
 
48
 
49
	// check if there are any directories, and split the file and directory parts
49
	// check if there are any directories, and split the file and directory parts
50
	if ( _sFilename.contains('/') ) {
50
	if ( _sFilename.contains('/') ) {
51
		_sDirIO.setDir(_sFilename.tokens(L"/", 1, -2).toString());
51
		_sDirIO.setDir(_sFilename.tokens(L"/", 1, -2));
52
		_sFile = _sFilename.token(L"/", -1);
52
		_sFile = _sFilename.token(L"/", -1);
53
	}
53
	}
54
	else
54
	else
55
		_sFile = _sFilename;
55
		_sFile = _sFilename;
56
 
56
 
Line 111... Line 111...
111
 
111
 
112
	if ( pSize ) (*pSize) = m_lSize;
112
	if ( pSize ) (*pSize) = m_lSize;
113
	return data;
113
	return data;
114
}
114
}
115
 
115
 
116
bool CFileIO::read(unsigned char *buf, size_t iSize, bool bEndChar)
116
bool CFileIO::read(wchar_t* buf, size_t iSize, bool bEndChar)
117
{
117
{
118
	if ( !this->isOpened() ) startRead();
118
	if (!this->isOpened()) startRead();
119
	if ( !this->isOpened() ) return false;
119
	if (!this->isOpened()) return false;
120
 
120
 
121
	if ( iSize > m_lSize ) iSize = m_lSize;
121
	if (iSize > m_lSize) iSize = m_lSize;
122
	try {
122
	try {
123
		m_fId.read((char *)buf, iSize);
123
		m_fId.read((char*)buf, iSize);
124
	}
124
	}
125
	catch (std::exception &e) {
125
	catch (std::exception& e) {
-
 
126
		CLog::logf(CLog::Log_IO, 3, "ERROR: CFileIO::read() unable to read from file: %s (%s)", _sFilename.c_str(), e.what());
-
 
127
		return false;
-
 
128
	}
-
 
129
 
-
 
130
	if (bEndChar) buf[iSize] = '\0';
-
 
131
 
-
 
132
	return !m_fId.bad();
-
 
133
}
-
 
134
bool CFileIO::read(unsigned char* buf, size_t iSize, bool bEndChar)
-
 
135
{
-
 
136
	if (!this->isOpened()) startRead();
-
 
137
	if (!this->isOpened()) return false;
-
 
138
 
-
 
139
	if (iSize > m_lSize) iSize = m_lSize;
-
 
140
	try {
-
 
141
		m_fId.read((char*)buf, iSize);
-
 
142
	}
-
 
143
	catch (std::exception& e) {
126
		CLog::logf(CLog::Log_IO, 3, "ERROR: CFileIO::read() unable to read from file: %s (%s)", _sFilename.c_str(), e.what());
144
		CLog::logf(CLog::Log_IO, 3, "ERROR: CFileIO::read() unable to read from file: %s (%s)", _sFilename.c_str(), e.what());
127
		return false;
145
		return false;
128
	}
146
	}
129
 
147
 
130
	if ( bEndChar ) buf[iSize] = '\0';
148
	if (bEndChar) buf[iSize] = '\0';
131
 
149
 
132
	return !m_fId.bad();
150
	return !m_fId.bad();
133
}
151
}
134
 
152
 
135
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
153
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
Line 196... Line 214...
196
 
214
 
197
	File.close();
215
	File.close();
198
	writeFile.close();
216
	writeFile.close();
199
 
217
 
200
	// now copy to original file
218
	// now copy to original file
201
	std::remove(_fileData());
219
	CFileIO::Remove(_sFilename);
202
	return std::rename(CPackages::tempDirectory() + "/temp.tmp", _fileData()) != 0;
220
	return CFileIO::Rename(CPackages::tempDirectory() + L"/temp.tmp", _sFilename);
203
}
221
}
204
 
222
 
205
 
223
 
206
void CFileIO::setDir(const Utils::WString &dir)
224
void CFileIO::setDir(const Utils::WString &dir)
207
{
225
{
Line 256... Line 274...
256
	File.close();
274
	File.close();
257
	this->close();
275
	this->close();
258
	File.setAutoDelete(false);
276
	File.setAutoDelete(false);
259
 
277
 
260
	// now copy to original file
278
	// now copy to original file
261
	if ( std::remove(_fileData()) == 0) {
279
	if (CFileIO::Remove(_sFilename)) {
262
		std::rename(CPackages::tempDirectory() + "/temp.tmp", _fileData());
280
		if (CFileIO::Rename(CPackages::tempDirectory() + L"/temp.tmp", _sFilename))
-
 
281
		{
263
		size_t oldSize = m_lSize;
282
			size_t oldSize = m_lSize;
264
		size_t checkFileSize = m_lSize - datasize;
283
			size_t checkFileSize = m_lSize - datasize;
265
		this->_readFileSize();
284
			this->_readFileSize();
266
		if ( checkFileSize != m_lSize ) {
285
			if (checkFileSize != m_lSize) {
267
			CLog::log(CLog::Log_IO, 3, Utils::String("WARNING: CFileIO::TruncateFile, file size mismatch, ") + (long)checkFileSize + " => " + (long)m_lSize);
286
				CLog::log(CLog::Log_IO, 3, Utils::WString(L"WARNING: CFileIO::TruncateFile, file size mismatch, ") + (long)checkFileSize + L" => " + (long)m_lSize);
-
 
287
			}
-
 
288
			return FILEERR_NONE;
268
		}
289
		}
269
		return FILEERR_NONE;
290
		return FILEERR_NOWRITE;
270
	}
291
	}
271
 
292
 
272
	return FILEERR_NOWRITE;
293
	return FILEERR_NOWRITE;
273
}
294
}
274
 
295
 
Line 359... Line 380...
359
	return WriteData ( data.c_str(), data.length() );
380
	return WriteData ( data.c_str(), data.length() );
360
}
381
}
361
 
382
 
362
bool CFileIO::Exists(const Utils::WString &filename)
383
bool CFileIO::Exists(const Utils::WString &filename)
363
{
384
{
364
	std::fstream File(filename, std::ios::in | std::ios::binary);
385
	std::wfstream File(filename, std::ios::in | std::ios::binary);
365
	bool bRet = File.good();
386
	bool bRet = File.good();
366
	File.close();
387
	File.close();
367
	return bRet;
388
	return bRet;
368
}
389
}
369
 
390
 
Line 440... Line 461...
440
	infile.close();
461
	infile.close();
441
 
462
 
442
	return to.size();
463
	return to.size();
443
}
464
}
444
 
465
 
-
 
466
size_t CFileIO::readLines(std::vector<Utils::WString>& to) const
-
 
467
{
-
 
468
	if (_sFilename.empty()) return 0;
-
 
469
 
-
 
470
	std::wstring line;
-
 
471
	std::wifstream infile(_sFilename.c_str(), std::ios_base::in);
-
 
472
	while (getline(infile, line, L'\n')) {
-
 
473
		to.push_back(Utils::WString(line).remove((char)0));
-
 
474
	}
-
 
475
 
-
 
476
	infile.close();
-
 
477
 
-
 
478
	return to.size();
-
 
479
}
-
 
480
 
445
size_t CFileIO::readLines(Utils::CStringList& to) const
481
size_t CFileIO::readLines(Utils::CStringList& to) const
446
{
482
{
447
	if (_sFilename.empty()) return 0;
483
	if (_sFilename.empty()) return 0;
448
 
484
 
449
	Utils::String line;
485
	Utils::String line;
Line 529... Line 565...
529
bool CFileIO::write(const char *buf, size_t iSize)
565
bool CFileIO::write(const char *buf, size_t iSize)
530
{
566
{
531
	if ( !this->isOpened() ) startWrite();
567
	if ( !this->isOpened() ) startWrite();
532
	if ( !this->isOpened() ) return false;
568
	if ( !this->isOpened() ) return false;
533
	try {
569
	try {
534
		m_fId.write((char *)buf, iSize);
570
		m_fId.write((char*)buf, iSize);
535
	}
571
	}
536
	catch (std::exception &e) {
572
	catch (std::exception &e) {
537
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
573
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
538
		return false;
574
		return false;
539
	}
575
	}
Line 551... Line 587...
551
	va_start(args, buf);
587
	va_start(args, buf);
552
	bool ret = this->_write((char *)buf, args);
588
	bool ret = this->_write((char *)buf, args);
553
	va_end(args);
589
	va_end(args);
554
	return ret;
590
	return ret;
555
}
591
}
556
bool CFileIO::_write(const char *buf, va_list args) 
592
bool CFileIO::_write(const char* buf, va_list args)
557
{
593
{
558
	if ( !this->isOpened() ) return false;
594
	if (!this->isOpened()) return false;
559
 
595
 
560
	char buffer[10000];
596
	char buffer[10000];
561
 
597
 
562
	vsprintf (buffer, buf, args);
598
	vsprintf(buffer, buf, args);
563
	try {
599
	try {
564
		int iLen = strlen(buffer);
600
		int iLen = strlen(buffer);
565
		m_fId.write((char *)buffer, iLen);
601
		m_fId.write((char*)buffer, iLen);
566
		m_lSize += iLen;
602
		m_lSize += iLen;
567
	}
603
	}
568
	catch (std::exception &e) {
604
	catch (std::exception& e) {
569
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
605
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
570
		return false;
606
		return false;
571
	}
607
	}
572
	return !m_fId.bad();
608
	return !m_fId.bad();
573
}
609
}
-
 
610
bool CFileIO::_write(const wchar_t* buf, va_list args)
-
 
611
{
-
 
612
	if (!this->isOpened()) return false;
-
 
613
 
-
 
614
	wchar_t buffer[10000];
-
 
615
 
-
 
616
#ifdef _WIN32
-
 
617
	vswprintf_s(buffer, 10000, buf, args);
-
 
618
#else
-
 
619
	vswprintf(buffer, buf, args);
-
 
620
#endif
-
 
621
	try {
-
 
622
		size_t iLen = wcslen(buffer);
-
 
623
		m_fId.write((char*)buffer, iLen);
-
 
624
		m_lSize += iLen;
-
 
625
	}
-
 
626
	catch (std::exception& e) {
-
 
627
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %ls (%s)", _sFilename.c_str(), e.what());
-
 
628
		return false;
-
 
629
	}
-
 
630
	return !m_fId.bad();
-
 
631
}
574
 
632
 
575
bool CFileIO::write(const char *buf, ...)
633
bool CFileIO::write(const char *buf, ...)
576
{
634
{
577
	va_list args;
635
	va_list args;
578
	va_start(args, buf);
636
	va_start(args, buf);
Line 720... Line 778...
720
	}
778
	}
721
 
779
 
722
	return this->write(d, size);
780
	return this->write(d, size);
723
}
781
}
724
 
782
 
725
Utils::String CFileIO::readEndOfLine()
783
Utils::String CFileIO::readEndOfLineStr()
726
{
784
{
727
	if ( !this->isOpened() ) {
785
	if (!this->isOpened()) {
728
		if ( !startRead() )	return "";
786
		if (!startRead())	return "";
-
 
787
	}
-
 
788
 
-
 
789
	std::string str;
-
 
790
	std::getline(m_fId, str, '\n');
-
 
791
	return str;
-
 
792
}
-
 
793
Utils::WString CFileIO::readEndOfLine()
-
 
794
{
-
 
795
	if (!this->isOpened()) {
-
 
796
		if (!startRead())	return L"";
729
	}
797
	}
730
 
798
 
731
	Utils::String str;
799
	std::string str;
732
	std::getline(m_fId, str, '\n');
800
	std::getline(m_fId, str, '\n');
733
	return str;
801
	return str;
734
}
802
}
735
 
803
 
736
Utils::WString CFileIO::baseName() const
804
Utils::WString CFileIO::baseName() const
737
{
805
{
738
	return _sFile.tokens(L".", 1, -2).toString();
806
	return _sFile.tokens(L".", 1, -2);
739
}
807
}
740
 
808
 
741
const Utils::WString& CFileIO::fullFilename() const { return _sFilename; }
809
const Utils::WString& CFileIO::fullFilename() const { return _sFilename; }
742
const Utils::WString& CFileIO::filename() const { return _sFile; }
810
const Utils::WString& CFileIO::filename() const { return _sFile; }
743
 
811
 
Line 756... Line 824...
756
		return Utils::WString::Null();
824
		return Utils::WString::Null();
757
	
825
	
758
	return _sFilename.tokens(L".", 1, -2) + L"." + ext;
826
	return _sFilename.tokens(L".", 1, -2) + L"." + ext;
759
}
827
}
760
 
828
 
761
bool CFileIO::Remove(const Utils::WString &rem)
829
bool CFileIO::Remove(const Utils::WString& rem)
762
{
830
{
-
 
831
#ifdef _WIN32
-
 
832
	return DeleteFile(rem.c_str());
-
 
833
#else
763
	//if ( !Exists() ) return false;
834
	//if ( !Exists() ) return false;
764
	return (std::remove(_toFileData(rem)) == 0) ? true : false;
835
	return (std::remove(_toFileData(rem)) == 0) ? true : false;
-
 
836
#endif
-
 
837
}
-
 
838
 
-
 
839
bool CFileIO::Rename(const Utils::WString& from, const Utils::WString &to) 
-
 
840
{
-
 
841
	if (std::rename(_toFileData(from).c_str(), _toFileData(to).c_str()) == 0)
-
 
842
		return true;
-
 
843
	return false;
765
}
844
}
766
 
845
 
767
bool CFileIO::remove()
846
bool CFileIO::remove()
768
{
847
{
769
	if ( this->isOpened() ) this->close();
848
	if ( this->isOpened() ) this->close();
770
	if ( !this->exists() ) return false;
849
	if ( !this->exists() ) return false;
771
	if ( std::remove(_fileData()) == 0) return true;
850
	if ( CFileIO::Remove(_sFilename)) return true;
772
	return false;
851
	return false;
773
}
852
}
774
 
853
 
775
bool CFileIO::writeFileUTF(std::vector<Utils::String> *lines)
854
bool CFileIO::writeFileUTF(std::vector<Utils::String>* lines)
776
{
855
{
777
	if (!lines || _sFilename.empty())
856
	if (!lines || _sFilename.empty())
778
		return false;
857
		return false;
779
 
858
 
780
	// we need to create the directory
859
	// we need to create the directory
Line 784... Line 863...
784
			return false;
863
			return false;
785
	}
864
	}
786
 
865
 
787
#ifdef _WIN32
866
#ifdef _WIN32
788
	TCHAR buf[5000];
867
	TCHAR buf[5000];
789
	wsprintf(buf, L"%hs", _sFilename.c_str());
-
 
790
	FILE *id = _wfopen(buf, L"wt+,ccs=UTF-8");
868
	FILE* id = _wfopen(_sFilename.c_str(), L"wt+,ccs=UTF-8");
791
	if (!id)
869
	if (!id)
792
		return false;
870
		return false;
793
 
871
 
794
	// write the rest
872
	// write the rest
795
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
873
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
796
	{
874
	{
797
		Utils::String l = (*itr);
875
		Utils::String l = (*itr);
798
		if (l.isin('\n'))
876
		if (l.isin('\n'))
799
		{
877
		{
800
			int max;
878
			int max;
801
			Utils::String *strs = l.tokenise("\n", &max);
879
			Utils::String* strs = l.tokenise("\n", &max);
802
			if (strs && max)
880
			if (strs && max)
803
			{
881
			{
804
				for (int i = 0; i < max; i++)
882
				for (int i = 0; i < max; i++)
805
				{
883
				{
806
					Utils::String line = strs[i];
884
					Utils::String line = strs[i];
Line 827... Line 905...
827
	//TODO: write utf8 file writing function
905
	//TODO: write utf8 file writing function
828
	return false;
906
	return false;
829
#endif
907
#endif
830
}
908
}
831
 
909
 
-
 
910
bool CFileIO::writeFileUTF(std::vector<Utils::WString>* lines)
-
 
911
{
-
 
912
	if (!lines || _sFilename.empty())
-
 
913
		return false;
-
 
914
 
-
 
915
	// we need to create the directory
-
 
916
	if (!_sDirIO.exists())
-
 
917
	{
-
 
918
		if (!_sDirIO.create())
-
 
919
			return false;
-
 
920
	}
-
 
921
 
-
 
922
#ifdef _WIN32
-
 
923
	FILE* id = _wfopen(_sFilename.c_str(), L"wt+,ccs=UTF-8");
-
 
924
	if (!id)
-
 
925
		return false;
-
 
926
 
-
 
927
	// write the rest
-
 
928
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
-
 
929
	{
-
 
930
		Utils::WString l = (*itr);
-
 
931
		if (l.contains('\n'))
-
 
932
		{
-
 
933
			std::vector<Utils::WString> strs;
-
 
934
			if(l.tokenise(L"\n", strs) && !strs.empty())
-
 
935
			{
-
 
936
				for(auto itr = strs.begin(); itr != strs.end(); itr++)
-
 
937
				{
-
 
938
					Utils::WString line = *itr;
-
 
939
					line += L"\n";
-
 
940
					fwrite(l.c_str(), sizeof(TCHAR), wcslen(l.c_str()), id);
-
 
941
				}
-
 
942
			}
-
 
943
		}
-
 
944
		else
-
 
945
		{
-
 
946
			l += L"\n";
-
 
947
			fwrite(l.c_str(), sizeof(TCHAR), wcslen(l.c_str()), id);
-
 
948
		}
-
 
949
	}
-
 
950
 
-
 
951
	fclose(id);
-
 
952
 
-
 
953
	return true;
-
 
954
#else
-
 
955
	//TODO: write utf8 file writing function
-
 
956
	return false;
-
 
957
#endif
-
 
958
}
-
 
959
 
832
bool CFileIO::writeFile(std::vector<Utils::String> *lines)
960
bool CFileIO::writeFile(std::vector<Utils::String> *lines)
833
{
961
{
834
	if (!lines || _sFilename.empty())
962
	if (!lines || _sFilename.empty())
835
		return false;
963
		return false;
836
 
964
 
Line 852... Line 980...
852
	out.close();
980
	out.close();
853
 
981
 
854
	return true;
982
	return true;
855
}
983
}
856
 
984
 
-
 
985
bool CFileIO::writeFile(const std::vector<Utils::WString> &lines) const
-
 
986
{
-
 
987
	if (lines.empty() || _sFilename.empty())
-
 
988
		return false;
-
 
989
 
-
 
990
	// we need to create the directory
-
 
991
	if (!_sDirIO.exists())
-
 
992
	{
-
 
993
		if (!_sDirIO.create())
-
 
994
			return false;
-
 
995
	}
-
 
996
 
-
 
997
 
-
 
998
	std::wofstream out(_sFilename.c_str());
-
 
999
	if (!out)
-
 
1000
		return false;
-
 
1001
 
-
 
1002
	for (auto itr = lines.begin(); itr != lines.end(); itr++)
-
 
1003
		out << (*itr).c_str() << std::endl;
-
 
1004
 
-
 
1005
	out.close();
-
 
1006
 
-
 
1007
	return true;
-
 
1008
}
-
 
1009
 
-
 
1010
 
857
bool CFileIO::writeFile(Utils::WStringList* lines)
1011
bool CFileIO::writeFile(Utils::WStringList* lines)
858
{
1012
{
859
	if (!lines || _sFilename.empty())
1013
	if (!lines || _sFilename.empty())
860
		return false;
1014
		return false;
861
 
1015
 
Line 864... Line 1018...
864
	{
1018
	{
865
		if (!_sDirIO.create())
1019
		if (!_sDirIO.create())
866
			return false;
1020
			return false;
867
	}
1021
	}
868
 
1022
 
869
	std::ofstream out(_sFilename.c_str());
1023
	std::wofstream out(_sFilename.c_str());
870
	if (!out)
1024
	if (!out)
871
		return false;
1025
		return false;
872
 
1026
 
873
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
1027
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
874
		out << (*itr)->str.c_str() << std::endl;
1028
		out << (*itr)->str.c_str() << std::endl;
Line 900... Line 1054...
900
 
1054
 
901
	return true;
1055
	return true;
902
}
1056
}
903
 
1057
 
904
bool CFileIO::Rename(const Utils::WString &toFile)
1058
bool CFileIO::Rename(const Utils::WString &toFile)
905
{
1059
{	
906
	if ( rename(_fileData(), _toFileData(toFile)) == 0)
1060
	return CFileIO::Rename(_sFilename, toFile);
907
		return true;
-
 
908
	return false;
-
 
909
}
1061
}
910
 
1062
 
911
Utils::WString CFileIO::getWindowsFilename() const
1063
Utils::WString CFileIO::getWindowsFilename() const
912
{
1064
{
913
	return _sFilename.findReplace(L"/", L"\\");
1065
	return _sFilename.findReplace(L"/", L"\\");
Line 1014... Line 1166...
1014
{
1166
{
1015
	if (_sFilename.empty()) _sExt = Utils::WString::Null();
1167
	if (_sFilename.empty()) _sExt = Utils::WString::Null();
1016
	_sExt = _sFilename.token(L".", -1);
1168
	_sExt = _sFilename.token(L".", -1);
1017
}
1169
}
1018
 
1170
 
1019
const char* CFileIO::_fileData() const
1171
std::string CFileIO::_fileData() const
1020
{
1172
{
1021
	return std::string(_sFilename.begin(), _sFilename.end()).c_str();
1173
	return _toFileData(_sFilename);
1022
}
1174
}
1023
const char* CFileIO::_toFileData(const Utils::WString &str)
1175
std::string CFileIO::_toFileData(const Utils::WString &str)
1024
{
1176
{
1025
	return std::string(str.begin(), str.end()).c_str();
1177
	return std::string(str.begin(), str.end());
1026
}
1178
}