Subversion Repositories spk

Rev

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

Rev 185 Rev 196
Line 13... Line 13...
13
 
13
 
14
CFileIO::CFileIO () : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
14
CFileIO::CFileIO () : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
15
{
15
{
16
}
16
}
17
 
17
 
18
CFileIO::CFileIO(const Utils::String &sFilename, bool bBinary) : m_lSize(0), m_bBinary(bBinary), m_bAutoDelete(false)
18
CFileIO::CFileIO(const Utils::WString &sFilename, bool bBinary) : 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(const Utils::String &filename) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
23
CFileIO::CFileIO(const Utils::WString &filename) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
24
{
24
{
25
	open(filename, true);
25
	open(filename, 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)
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(const Utils::String &sFilename, bool bBinary)
44
bool CFileIO::open(const Utils::WString& sFilename, bool bBinary)
45
{
45
{
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.isin('/') ) {
50
	if ( _sFilename.contains('/') ) {
51
		_sDirIO.setDir(_sFilename.tokens("/", 1, -2));
51
		_sDirIO.setDir(_sFilename.tokens(L"/", 1, -2).toString());
52
		_sFile = _sFilename.token("/", -1);
52
		_sFile = _sFilename.token(L"/", -1);
53
	}
53
	}
54
	else
54
	else
55
		_sFile = _sFilename;
55
		_sFile = _sFilename;
56
 
56
 
57
	_sFile.removeFirstSpace();
57
	_sFile.removeFirstSpace();
Line 79... Line 79...
79
		return NULL;
79
		return NULL;
80
	}
80
	}
81
 
81
 
82
	if ( !read(data, iAmount, true) ) {
82
	if ( !read(data, iAmount, true) ) {
83
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to read data from file, %s/%d", _sFilename.c_str(), iAmount);
83
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to read data from file, %s/%d", _sFilename.c_str(), iAmount);
84
		delete data;
84
		delete[] data;
85
		return NULL;
85
		return NULL;
86
	}
86
	}
87
 
87
 
88
	return data;
88
	return data;
89
}
89
}
Line 103... Line 103...
103
		return NULL;
103
		return NULL;
104
	}
104
	}
105
 
105
 
106
	if ( !read(data, m_lSize, true) ) {
106
	if ( !read(data, m_lSize, true) ) {
107
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to read data from file, %s/%d", _sFilename.c_str(), m_lSize);
107
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to read data from file, %s/%d", _sFilename.c_str(), m_lSize);
108
		delete data;
108
		delete[] data;
109
		return NULL;
109
		return NULL;
110
	}
110
	}
111
 
111
 
112
	if ( pSize ) (*pSize) = m_lSize;
112
	if ( pSize ) (*pSize) = m_lSize;
113
	return data;
113
	return data;
Line 160... Line 160...
160
		size_t size = offset - startPos;
160
		size_t size = offset - startPos;
161
		try {
161
		try {
162
			char *data = new char[size];
162
			char *data = new char[size];
163
			File.read(data, size);
163
			File.read(data, size);
164
			writeFile.write(data, size);
164
			writeFile.write(data, size);
165
			delete data;
165
			delete []data;
166
		}
166
		}
167
		catch(std::exception &e) {
167
		catch(std::exception &e) {
168
			CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", _sFilename.c_str(), e.what());
168
			CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", _sFilename.c_str(), e.what());
169
			return false;
169
			return false;
170
		}
170
		}
Line 196... Line 196...
196
 
196
 
197
	File.close();
197
	File.close();
198
	writeFile.close();
198
	writeFile.close();
199
 
199
 
200
	// now copy to original file
200
	// now copy to original file
201
	std::remove(_sFilename);
201
	std::remove(_fileData());
202
	std::rename(CPackages::tempDirectory() + "/temp.tmp", _sFilename);
202
	return std::rename(CPackages::tempDirectory() + "/temp.tmp", _fileData()) != 0;
203
 
-
 
204
	return true;
-
 
205
}
203
}
206
 
204
 
207
 
205
 
208
void CFileIO::setDir(const Utils::String &dir)
206
void CFileIO::setDir(const Utils::WString &dir)
209
{
207
{
210
	if ( _sFile.empty() )	_sDirIO.setDir(dir);
208
	if ( _sFile.empty() )	_sDirIO.setDir(dir);
211
	else					open(dir + "/" + _sFile, m_bBinary);
209
	else					open(dir + "/" + _sFile, m_bBinary);
212
}
210
}
213
 
211
 
Line 258... Line 256...
258
	File.close();
256
	File.close();
259
	this->close();
257
	this->close();
260
	File.setAutoDelete(false);
258
	File.setAutoDelete(false);
261
 
259
 
262
	// now copy to original file
260
	// now copy to original file
263
	if ( std::remove(_sFilename.c_str()) == 0 ) {
261
	if ( std::remove(_fileData()) == 0) {
264
		std::rename(CPackages::tempDirectory() + "/temp.tmp", _sFilename.c_str());
262
		std::rename(CPackages::tempDirectory() + "/temp.tmp", _fileData());
265
		size_t oldSize = m_lSize;
263
		size_t oldSize = m_lSize;
266
		size_t checkFileSize = m_lSize - datasize;
264
		size_t checkFileSize = m_lSize - datasize;
267
		this->_readFileSize();
265
		this->_readFileSize();
268
		if ( checkFileSize != m_lSize ) {
266
		if ( checkFileSize != m_lSize ) {
269
			CLog::log(CLog::Log_IO, 3, Utils::String("WARNING: CFileIO::TruncateFile, file size mismatch, ") + (long)checkFileSize + " => " + (long)m_lSize);
267
			CLog::log(CLog::Log_IO, 3, Utils::String("WARNING: CFileIO::TruncateFile, file size mismatch, ") + (long)checkFileSize + " => " + (long)m_lSize);
Line 359... Line 357...
359
bool CFileIO::writeString ( const Utils::String &data )
357
bool CFileIO::writeString ( const Utils::String &data )
360
{
358
{
361
	return WriteData ( data.c_str(), data.length() );
359
	return WriteData ( data.c_str(), data.length() );
362
}
360
}
363
 
361
 
364
bool CFileIO::Exists(const Utils::String &filename)
362
bool CFileIO::Exists(const Utils::WString &filename)
365
{
363
{
366
	std::fstream File(filename, std::ios::in | std::ios::binary);
364
	std::fstream File(filename, std::ios::in | std::ios::binary);
367
	bool bRet = File.good();
365
	bool bRet = File.good();
368
	File.close();
366
	File.close();
369
	return bRet;
367
	return bRet;
Line 391... Line 389...
391
{
389
{
392
	return _start(_out(), true);
390
	return _start(_out(), true);
393
}
391
}
394
 
392
 
395
bool CFileIO::startModify()
393
bool CFileIO::startModify()
396
{
394
{
397
	return _start(_inout(), true);
395
	return _start(_inout(), true);
398
}
396
}
399
 
397
 
400
bool CFileIO::startAppend()
398
bool CFileIO::startAppend()
401
{
399
{
Line 676... Line 674...
676
	return true;
674
	return true;
677
}
675
}
678
 
676
 
679
 
677
 
680
bool CFileIO::AppendData ( const char *d, size_t size )
678
bool CFileIO::AppendData ( const char *d, size_t size )
681
{
679
{
682
	std::ofstream File(_sFilename, _append());
680
	std::ofstream File(_sFilename, _append());
683
	if ( !File.is_open() ) return false;
681
	if ( !File.is_open() ) return false;
684
 
682
 
685
	// move to the end of the file
683
	// move to the end of the file
686
	//File.seekg(0, std::ios::end);
684
	//File.seekg(0, std::ios::end);
Line 713... Line 711...
713
	if ( m_lSize <= 0 ) {
711
	if ( m_lSize <= 0 ) {
714
		if ( !this->startWrite() ) return false;
712
		if ( !this->startWrite() ) return false;
715
	}
713
	}
716
	else if ( start == m_lSize ) {
714
	else if ( start == m_lSize ) {
717
		if ( !this->startAppend() ) return false;
715
		if ( !this->startAppend() ) return false;
718
	}
716
	}
719
	else {
717
	else {
720
		if ( !this->startModify() ) return false;
718
		if ( !this->startModify() ) return false;
721
		this->seekStart(start);
719
		this->seekStart(start);
722
	}
720
	}
723
 
721
 
724
	return this->write(d, size);
722
	return this->write(d, size);
725
}
723
}
726
 
724
 
727
Utils::String CFileIO::readEndOfLine()
725
Utils::String CFileIO::readEndOfLine()
728
{
726
{
729
	if ( !this->isOpened() ) {
727
	if ( !this->isOpened() ) {
730
		if ( !startRead() )	return "";
728
		if ( !startRead() )	return "";
731
	}
729
	}
732
 
730
 
733
	Utils::String str;
731
	Utils::String str;
734
	std::getline(m_fId, str, '\n');
732
	std::getline(m_fId, str, '\n');
735
	return str;
733
	return str;
736
}
734
}
737
 
735
 
738
Utils::String CFileIO::baseName() const
736
Utils::WString CFileIO::baseName() const
739
{
737
{
740
	return _sFile.tokens(".", 1, -2);
738
	return _sFile.tokens(L".", 1, -2).toString();
741
}
739
}
742
 
740
 
-
 
741
const Utils::WString& CFileIO::fullFilename() const { return _sFilename; }
-
 
742
const Utils::WString& CFileIO::filename() const { return _sFile; }
-
 
743
 
-
 
744
const Utils::String CFileIO::fullFilenameStr() const { return _sFilename.toString(); }
-
 
745
const Utils::String CFileIO::filenameStr() const { return _sFile.toString(); }
-
 
746
 
743
Utils::String CFileIO::extension () const
747
Utils::WString CFileIO::extension () const
744
{
748
{
745
	if ( _sFilename.empty() ) return Utils::String::Null();
749
	if ( _sFilename.empty() ) return Utils::WString::Null();
746
	return _sFilename.token(".", -1);
750
	return _sFilename.token(L".", -1);
747
}
751
}
748
 
752
 
749
Utils::String CFileIO::changeFileExtension(const Utils::String &ext) const
753
Utils::WString CFileIO::changeFileExtension(const Utils::WString &ext) const
750
{
754
{
751
	if ( _sFilename.empty() )
755
	if ( _sFilename.empty() )
752
		return Utils::String::Null();
756
		return Utils::WString::Null();
753
	
757
	
754
	return _sFilename.tokens(".", 1, -2) + "." + ext;
758
	return _sFilename.tokens(L".", 1, -2) + L"." + ext;
755
}
759
}
756
 
760
 
757
bool CFileIO::Remove(const Utils::String &rem)
761
bool CFileIO::Remove(const Utils::WString &rem)
758
{
762
{
759
	//if ( !Exists() ) return false;
763
	//if ( !Exists() ) return false;
760
	return (std::remove(rem) == 0) ? true : false;
764
	return (std::remove(_toFileData(rem)) == 0) ? true : false;
761
}
765
}
762
 
766
 
763
bool CFileIO::remove()
767
bool CFileIO::remove()
764
{
768
{
765
	if ( this->isOpened() ) this->close();
769
	if ( this->isOpened() ) this->close();
766
	if ( !this->exists() ) return false;
770
	if ( !this->exists() ) return false;
767
	if ( std::remove(_sFilename.c_str()) == 0 ) return true;
771
	if ( std::remove(_fileData()) == 0) return true;
768
	return false;
772
	return false;
769
}
773
}
770
 
774
 
771
bool CFileIO::writeFileUTF(std::vector<Utils::String> *lines)
775
bool CFileIO::writeFileUTF(std::vector<Utils::String> *lines)
772
{
776
{
Line 848... Line 852...
848
	out.close();
852
	out.close();
849
 
853
 
850
	return true;
854
	return true;
851
}
855
}
852
 
856
 
-
 
857
bool CFileIO::writeFile(Utils::WStringList* lines)
-
 
858
{
-
 
859
	if (!lines || _sFilename.empty())
-
 
860
		return false;
-
 
861
 
-
 
862
	// we need to create the directory
-
 
863
	if (!_sDirIO.exists())
-
 
864
	{
-
 
865
		if (!_sDirIO.create())
-
 
866
			return false;
-
 
867
	}
-
 
868
 
-
 
869
	std::ofstream out(_sFilename.c_str());
-
 
870
	if (!out)
-
 
871
		return false;
-
 
872
 
-
 
873
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
-
 
874
		out << (*itr)->str.c_str() << std::endl;
-
 
875
 
-
 
876
	out.close();
-
 
877
 
-
 
878
	return true;
-
 
879
}
853
bool CFileIO::writeFile(Utils::CStringList *lines)
880
bool CFileIO::writeFile(Utils::CStringList* lines)
854
{
881
{
855
	if (!lines || _sFilename.empty())
882
	if (!lines || _sFilename.empty())
856
		return false;
883
		return false;
857
 
884
 
858
	// we need to create the directory
885
	// we need to create the directory
Line 870... Line 897...
870
		out << (*itr)->str.c_str() << std::endl;
897
		out << (*itr)->str.c_str() << std::endl;
871
 
898
 
872
	out.close();
899
	out.close();
873
 
900
 
874
	return true;
901
	return true;
875
}
902
}
876
 
903
 
877
bool CFileIO::Rename(const Utils::String &toFile)
904
bool CFileIO::Rename(const Utils::WString &toFile)
878
{
905
{
879
	if ( rename(_sFilename.c_str(), toFile.c_str()) == 0 )
906
	if ( rename(_fileData(), _toFileData(toFile)) == 0)
880
		return true;
907
		return true;
881
	return false;
908
	return false;
882
}
909
}
883
 
910
 
884
Utils::String CFileIO::getWindowsFilename() const
911
Utils::WString CFileIO::getWindowsFilename() const
885
{
912
{
886
	Utils::String returnString = _sFilename;
-
 
887
	returnString = returnString.findReplace("/", "\\");
913
	return _sFilename.findReplace(L"/", L"\\");
888
	return returnString;
-
 
889
}
914
}
890
 
915
 
891
void CFileIO::setCreationTime(time_t time)
916
void CFileIO::setCreationTime(time_t time)
892
{
917
{
893
#ifdef _WIN32
918
#ifdef _WIN32
894
	// Note that LONGLONG is a 64-bit value
919
	// Note that LONGLONG is a 64-bit value
Line 897... Line 922...
897
	FILETIME ft;
922
	FILETIME ft;
898
    ll = Int32x32To64(time, 10000000) + 116444736000000000;
923
    ll = Int32x32To64(time, 10000000) + 116444736000000000;
899
    ft.dwLowDateTime = (DWORD)ll;
924
    ft.dwLowDateTime = (DWORD)ll;
900
    ft.dwHighDateTime = ll >> 32;
925
    ft.dwHighDateTime = ll >> 32;
901
 
926
 
902
	Utils::String windowsFilename = getWindowsFilename();
927
	Utils::WString windowsFilename = getWindowsFilename();
903
 
928
 
904
	WCHAR    str[MAX_PATH];
-
 
905
	MultiByteToWideChar(CP_ACP, NULL, windowsFilename.c_str(), -1, str, windowsFilename.length() + 1);
-
 
906
	HANDLE filename = CreateFile(str, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
929
	HANDLE filename = CreateFile(windowsFilename.c_str(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
907
	// Set the file time on the file
930
	// Set the file time on the file
908
	SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&ft);
931
	SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&ft);
909
	// Close our handle.
932
	// Close our handle.
910
	CloseHandle(filename);
933
	CloseHandle(filename);
911
 
934
 
Line 918... Line 941...
918
}
941
}
919
 
942
 
920
time_t CFileIO::modifiedTime() const
943
time_t CFileIO::modifiedTime() const
921
{
944
{
922
#ifdef _WIN32
945
#ifdef _WIN32
923
	WCHAR    str[MAX_PATH];
-
 
924
	Utils::String windowsFilename = getWindowsFilename();
946
	Utils::WString windowsFilename = getWindowsFilename();
925
	MultiByteToWideChar(CP_ACP, NULL, windowsFilename.c_str(), windowsFilename.length() + 1, str, MAX_PATH);
-
 
926
 
947
 
927
	WIN32_FILE_ATTRIBUTE_DATA wfad;
948
	WIN32_FILE_ATTRIBUTE_DATA wfad;
928
	GetFileAttributesEx(str, GetFileExInfoStandard, &wfad);
949
	GetFileAttributesEx(windowsFilename.c_str(), GetFileExInfoStandard, &wfad);
929
 
950
 
930
	LARGE_INTEGER date, adjust;
951
	LARGE_INTEGER date, adjust;
931
	date.HighPart = wfad.ftLastWriteTime.dwHighDateTime;
952
	date.HighPart = wfad.ftLastWriteTime.dwHighDateTime;
932
	date.LowPart = wfad.ftLastWriteTime.dwLowDateTime;
953
	date.LowPart = wfad.ftLastWriteTime.dwLowDateTime;
933
 
954
 
934
	// 100-nanoseconds = milliseconds * 10000
955
	// 100-nanoseconds = milliseconds * 10000
935
	adjust.QuadPart = 11644473600000 * 10000;
956
	adjust.QuadPart = 11644473600000 * 10000;
936
 
957
 
937
	// removes the diff between 1970 and 1601
958
	// removes the diff between 1970 and 1601
938
	date.QuadPart -= adjust.QuadPart;
959
	date.QuadPart -= adjust.QuadPart;
Line 955... Line 976...
955
/**
976
/**
956
 * Copys the contents of a file to another
977
 * Copys the contents of a file to another
957
 *
978
 *
958
 * Reads and writes the files in block
979
 * Reads and writes the files in block
959
 */
980
 */
960
bool CFileIO::copy(const Utils::String &toFile, bool keepTime)
981
bool CFileIO::copy(const Utils::WString &toFile, bool keepTime)
961
{
982
{
962
	time_t time = creationTime();
983
	time_t time = creationTime();
963
 
984
 
964
	CFileIO File(toFile);
985
	CFileIO File(toFile);
965
	if ( File.write(*this, m_lSize) ) {
986
	if ( File.write(*this, m_lSize) ) {
Line 989... Line 1010...
989
}
1010
}
990
 
1011
 
991
 
1012
 
992
void CFileIO::_updateFilename()
1013
void CFileIO::_updateFilename()
993
{
1014
{
994
	if (_sFilename.empty()) _sExt = Utils::String::Null();
1015
	if (_sFilename.empty()) _sExt = Utils::WString::Null();
995
	_sExt = _sFilename.token(".", -1);
1016
	_sExt = _sFilename.token(L".", -1);
-
 
1017
}
-
 
1018
 
-
 
1019
const char* CFileIO::_fileData() const
-
 
1020
{
-
 
1021
	return std::string(_sFilename.begin(), _sFilename.end()).c_str();
-
 
1022
}
-
 
1023
const char* CFileIO::_toFileData(const Utils::WString &str)
-
 
1024
{
-
 
1025
	return std::string(str.begin(), str.end()).c_str();
996
}
1026
}