Subversion Repositories spk

Rev

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

Rev 120 Rev 121
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 = true) : m_lSize(0), m_bBinary(bBinary), m_bAutoDelete(false)
18
CFileIO::CFileIO(const Utils::String &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(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.ToString(), true);
25
	open(filename.ToString(), true);
26
}
26
}
27
 
27
 
Line 137... Line 137...
137
	std::fstream File(m_sFilename, _in());
137
	std::fstream File(m_sFilename, _in());
138
	if ( !File.is_open() ) return false;
138
	if ( !File.is_open() ) return false;
139
 
139
 
140
	// first find file size
140
	// first find file size
141
	File.seekg(0, std::ios::end);
141
	File.seekg(0, std::ios::end);
142
	size_t fullsize = File.tellg(), size = fullsize;
142
	size_t fullsize = static_cast<size_t>(File.tellg()), size = fullsize;
143
	File.seekg(0, std::ios::beg);
143
	File.seekg(0, std::ios::beg);
144
 
144
 
145
	std::fstream writeFile(CPackages::tempDirectory() + "/temp.tmp", _out());
145
	std::fstream writeFile(CPackages::tempDirectory() + "/temp.tmp", _out());
146
	if ( !File.is_open() ) {
146
	if ( !File.is_open() ) {
147
		File.close();
147
		File.close();
Line 151... Line 151...
151
	size_t off = 0;
151
	size_t off = 0;
152
	size_t startPos = 0;
152
	size_t startPos = 0;
153
	size_t remainingSize = fullsize;
153
	size_t remainingSize = fullsize;
154
 
154
 
155
	while ( off < numOffset ) {
155
	while ( off < numOffset ) {
156
		startPos = File.tellg();
156
		startPos = static_cast<size_t>(File.tellg());
157
		size_t offset = offsets[off++];
157
		size_t offset = offsets[off++];
158
		size_t size = offset - startPos;
158
		size_t size = offset - startPos;
159
		try {
159
		try {
160
			char *data = new char[size];
160
			char *data = new char[size];
161
			File.read(data, size);
161
			File.read(data, size);
Line 220... Line 220...
220
 
220
 
221
	std::fstream file(m_sFilename, (m_bBinary) ? (std::ios::in | std::ios::binary) : (std::ios::in));
221
	std::fstream file(m_sFilename, (m_bBinary) ? (std::ios::in | std::ios::binary) : (std::ios::in));
222
	if ( !file.is_open() ) return;
222
	if ( !file.is_open() ) return;
223
 
223
 
224
	file.seekg(0, std::ios::end);
224
	file.seekg(0, std::ios::end);
225
	m_lSize = file.tellg();
225
	m_lSize = static_cast<size_t>(file.tellg());
226
	file.close();
226
	file.close();
227
}
227
}
228
 
228
 
229
bool CFileIO::WipeFile()
229
bool CFileIO::WipeFile()
230
{
230
{
Line 663... Line 663...
663
	// move to the end of the file
663
	// move to the end of the file
664
	toFile.seekg(0, std::ios::end);
664
	toFile.seekg(0, std::ios::end);
665
 
665
 
666
	// get size of file
666
	// get size of file
667
	fromFile.seekg(0, std::ios::end);
667
	fromFile.seekg(0, std::ios::end);
668
	size_t size = fromFile.tellg();
668
	size_t size = static_cast<size_t>(fromFile.tellg());
669
	fromFile.seekg(0, std::ios::beg);
669
	fromFile.seekg(0, std::ios::beg);
670
 
670
 
671
	char data[500000];
671
	char data[500000];
672
	while ( size > 0 )
672
	while ( size > 0 )
673
	{
673
	{
Line 700... Line 700...
700
	// move to the end of the file
700
	// move to the end of the file
701
	toFile.seekg(0, std::ios::end);
701
	toFile.seekg(0, std::ios::end);
702
 
702
 
703
	// get size of file
703
	// get size of file
704
	fromFile.seekg(0, std::ios::end);
704
	fromFile.seekg(0, std::ios::end);
705
	size_t size = fromFile.tellg();
705
	size_t size = static_cast<size_t>(fromFile.tellg());
706
	fromFile.seekg(0, std::ios::beg);
706
	fromFile.seekg(0, std::ios::beg);
707
 
707
 
708
	char data[500000];
708
	char data[500000];
709
	while ( size > 0 )
709
	while ( size > 0 )
710
	{
710
	{
Line 824... Line 824...
824
{
824
{
825
	if ( !lines || m_sFilename.empty() )
825
	if ( !lines || m_sFilename.empty() )
826
		return false;
826
		return false;
827
 
827
 
828
	// we need to create the directory
828
	// we need to create the directory
829
	if ( !m_sDirIO.Exists() )
829
	if ( !m_sDirIO.exists() )
830
	{
830
	{
831
		if ( !m_sDirIO.Create() )
831
		if ( !m_sDirIO.Create() )
832
			return false;
832
			return false;
833
	}
833
	}
834
 
834
 
Line 875... Line 875...
875
    //TODO: write utf8 file writing function
875
    //TODO: write utf8 file writing function
876
    return false;
876
    return false;
877
#endif
877
#endif
878
}
878
}
879
 
879
 
-
 
880
bool CFileIO::writeFileUTF(std::vector<Utils::String> *lines)
-
 
881
{
-
 
882
	if (!lines || m_sFilename.empty())
-
 
883
		return false;
-
 
884
 
-
 
885
	// we need to create the directory
-
 
886
	if (!m_sDirIO.exists())
-
 
887
	{
-
 
888
		if (!m_sDirIO.Create())
-
 
889
			return false;
-
 
890
	}
-
 
891
 
-
 
892
#ifdef _WIN32
-
 
893
	TCHAR buf[5000];
-
 
894
	wsprintf(buf, L"%hs", m_sFilename.c_str());
-
 
895
	FILE *id = _wfopen(buf, L"wt+,ccs=UTF-8");
-
 
896
	if (!id)
-
 
897
		return false;
-
 
898
 
-
 
899
	// write the rest
-
 
900
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
-
 
901
	{
-
 
902
		Utils::String l = (*itr);
-
 
903
		if (l.isin('\n'))
-
 
904
		{
-
 
905
			int max;
-
 
906
			Utils::String *strs = l.tokenise("\n", &max);
-
 
907
			if (strs && max)
-
 
908
			{
-
 
909
				for (int i = 0; i < max; i++)
-
 
910
				{
-
 
911
					Utils::String line = strs[i];
-
 
912
					line += "\n";
-
 
913
					int size = wsprintf(buf, L"%hs", line.c_str());
-
 
914
					fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
-
 
915
				}
-
 
916
 
-
 
917
				CLEANSPLIT(strs, max);
-
 
918
			}
-
 
919
		}
-
 
920
		else
-
 
921
		{
-
 
922
			l += "\n";
-
 
923
			int size = wsprintf(buf, L"%hs", l.c_str());
-
 
924
			fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
-
 
925
		}
-
 
926
	}
-
 
927
 
-
 
928
	fclose(id);
-
 
929
 
-
 
930
	return true;
-
 
931
#else
-
 
932
	//TODO: write utf8 file writing function
-
 
933
	return false;
-
 
934
#endif
-
 
935
}
-
 
936
 
880
bool CFileIO::WriteFile(std::vector<CyString> *lines)
937
bool CFileIO::WriteFile(std::vector<CyString> *lines)
881
{
938
{
882
	if ( !lines || m_sFilename.empty() )
939
	if ( !lines || m_sFilename.empty() )
883
		return false;
940
		return false;
884
 
941
 
885
	// we need to create the directory
942
	// we need to create the directory
886
	if ( !m_sDirIO.Exists() )
943
	if ( !m_sDirIO.exists() )
887
	{
944
	{
888
		if ( !m_sDirIO.Create() )
945
		if ( !m_sDirIO.Create() )
889
			return false;
946
			return false;
890
	}
947
	}
891
 
948
 
Line 909... Line 966...
909
{
966
{
910
	if ( !lines || m_sFilename.empty() )
967
	if ( !lines || m_sFilename.empty() )
911
		return false;
968
		return false;
912
 
969
 
913
	// we need to create the directory
970
	// we need to create the directory
914
	if ( !m_sDirIO.Exists() )
971
	if ( !m_sDirIO.exists() )
915
	{
972
	{
916
		if ( !m_sDirIO.Create() )
973
		if ( !m_sDirIO.Create() )
917
			return false;
974
			return false;
918
	}
975
	}
919
 
976
 
Line 942... Line 999...
942
	out.close();
999
	out.close();
943
 
1000
 
944
	return true;
1001
	return true;
945
}
1002
}
946
 
1003
 
-
 
1004
bool CFileIO::writeFile(std::vector<Utils::String> *lines)
-
 
1005
{
-
 
1006
	if (!lines || m_sFilename.empty())
-
 
1007
		return false;
-
 
1008
 
-
 
1009
	// we need to create the directory
-
 
1010
	if (!m_sDirIO.exists())
-
 
1011
	{
-
 
1012
		if (!m_sDirIO.Create())
-
 
1013
			return false;
-
 
1014
	}
-
 
1015
 
-
 
1016
 
-
 
1017
	std::ofstream out(m_sFilename.c_str());
-
 
1018
	if (!out)
-
 
1019
		return false;
-
 
1020
 
-
 
1021
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
-
 
1022
		out << (*itr).c_str() << std::endl;
-
 
1023
 
-
 
1024
	out.close();
-
 
1025
 
-
 
1026
	return true;
-
 
1027
}
-
 
1028
 
-
 
1029
bool CFileIO::writeFile(Utils::CStringList *lines)
-
 
1030
{
-
 
1031
	if (!lines || m_sFilename.empty())
-
 
1032
		return false;
-
 
1033
 
-
 
1034
	// we need to create the directory
-
 
1035
	if (!m_sDirIO.exists())
-
 
1036
	{
-
 
1037
		if (!m_sDirIO.Create())
-
 
1038
			return false;
-
 
1039
	}
-
 
1040
 
-
 
1041
	std::ofstream out(m_sFilename.c_str());
-
 
1042
	if (!out)
-
 
1043
		return false;
-
 
1044
 
-
 
1045
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
-
 
1046
		out << (*itr)->str.c_str() << std::endl;
-
 
1047
 
-
 
1048
	out.close();
-
 
1049
 
-
 
1050
	return true;
-
 
1051
}
-
 
1052
 
947
bool CFileIO::Rename(CyString toFile)
1053
bool CFileIO::Rename(CyString toFile)
948
{
1054
{
949
	if ( rename(m_sFilename.c_str(), toFile.c_str()) == 0 )
1055
	if ( rename(m_sFilename.c_str(), toFile.c_str()) == 0 )
950
		return true;
1056
		return true;
951
	return false;
1057
	return false;
Line 1014... Line 1120...
1014
	return 0;
1120
	return 0;
1015
}
1121
}
1016
 
1122
 
1017
size_t CFileIO::position()
1123
size_t CFileIO::position()
1018
{
1124
{
1019
	return m_fId.tellg();
1125
	return static_cast<size_t>(m_fId.tellg());
1020
}
1126
}
1021
 
1127
 
1022
/**
1128
/**
1023
 * Copys the contents of a file to another
1129
 * Copys the contents of a file to another
1024
 *
1130
 *