Subversion Repositories spk

Rev

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

Rev 42 Rev 51
Line 6... Line 6...
6
 
6
 
7
#include <locale>
7
#include <locale>
8
 
8
 
9
#include "DirIO.h"
9
#include "DirIO.h"
10
#include "File.h"
10
#include "File.h"
-
 
11
#include "Logging/Log.h"
11
 
12
 
12
CFileIO::CFileIO ()
13
CFileIO::CFileIO () : m_bOpened(false), m_lSize(0), m_bBinary(true)
13
{
14
{
14
	m_bOpened = false;
-
 
15
	m_lSize = 0;
-
 
16
	m_bBinary = true;
-
 
17
	m_fIdOld = NULL;
-
 
18
}
15
}
19
 
16
 
20
CFileIO::CFileIO(CyString filename)
17
CFileIO::CFileIO(CyString filename) : m_bOpened(false), m_lSize(0), m_bBinary(true)
21
{
18
{
22
	m_bOpened = false;
-
 
23
	m_lSize = 0;
-
 
24
	m_bBinary = true;
19
	Open ( filename, true );
25
	m_fIdOld = NULL;
20
}
26
 
21
 
27
	Open ( filename, true );
-
 
28
}
-
 
29
 
-
 
30
CFileIO::CFileIO(C_File *file)
22
CFileIO::CFileIO(C_File *file) : m_bOpened(false), m_lSize(0), m_bBinary(true)
31
{
23
{
32
	m_bOpened = false;
-
 
33
	m_lSize = 0;
-
 
34
	m_bBinary = true;
-
 
35
	m_fIdOld = NULL;
-
 
36
 
-
 
37
	Open ( file->GetFilePointer(), true );
24
	Open ( file->GetFilePointer(), true );
38
}
25
}
39
 
26
 
40
CFileIO::~CFileIO()
27
CFileIO::~CFileIO()
41
{
28
{
42
	if ( m_bOpened )
29
	if ( m_bOpened ) m_fId.close();
43
	{
-
 
44
		if ( m_fIdOld )
-
 
45
			fclose(m_fIdOld);
-
 
46
		else
-
 
47
			m_fId.close();
-
 
48
	}
-
 
49
}
30
}
50
 
31
 
51
bool CFileIO::Open ( CyString filename, bool binary )
32
bool CFileIO::Open ( CyString filename, bool binary )
52
{
33
{
53
	m_bBinary = binary;
34
	m_bBinary = binary;
54
	m_sFilename = filename;
35
	m_sFilename = filename.ToString();
55
	m_sFilename = m_sFilename.FindReplace ( ";\\";, "/" );
36
	m_sFilename = m_sFilename.findReplace(";\\";, "/");
-
 
37
	m_sFilename = m_sFilename.findReplace("//", "/");
56
 
38
 
57
	if ( m_sFilename.IsIn('/') )
39
	if ( m_sFilename.isin('/') ) {
58
	{
-
 
59
		m_sDirIO.SetDir(m_sFilename.GetToken ( "/&quot;, 1, m_sFilename.NumToken ( "/" ) - 1 ));
40
		m_sDirIO.SetDir(m_sFilename.tokens(&quot;/", 1, -2));
60
		m_sFile = m_sFilename.GetToken ( "/&quot;, m_sFilename.NumToken ( "/" ) );
41
		m_sFile = m_sFilename.token(&quot;/", -1);
61
	}
42
	}
62
	else
43
	else
63
		m_sFile = filename;
44
		m_sFile = filename.ToString();
64
 
45
 
65
	ReadFileSize();
46
	this->_readFileSize();
66
	return true;
47
	return true;
67
}
48
}
68
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
49
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
69
{
50
{
70
	if ( NoFile() )
51
	if ( NoFile() )	return false;
-
 
52
 
-
 
53
	std::fstream File(m_sFilename, _in());
71
		return false;
54
	if ( !File.is_open() ) return false;
72
 
55
 
73
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "rb+" : "r+" );
56
	// first find file size
74
	if ( !id )
57
	File.seekg(0, std::ios::end);
-
 
58
	size_t fullsize = File.tellg(), size = fullsize;
75
		return false;
59
	File.seekg(0, std::ios::beg);
76
 
60
 
77
	// first find file size
-
 
78
	fseek ( id, 0, SEEK_END );
-
 
79
	size_t fullsize = ftell ( id ), size = fullsize;
-
 
80
	fseek ( id, 0, SEEK_SET );
-
 
81
 
-
 
82
	FILE *writeId = fopen ( "temp.tmp", (m_bBinary) ? "wb" : "w" );
61
	std::fstream writeFile("temp.tmp", _out());
83
	if ( !writeId )
62
	if ( !File.is_open() ) {
84
	{
-
 
85
		fclose ( id );
63
		File.close();
86
		return false;
64
		return false;
87
	}
65
	}
88
 
66
 
89
	size_t off = 0;
67
	size_t off = 0;
90
 
-
 
91
	size_t startPos = 0;
68
	size_t startPos = 0;
92
	size_t remainingSize = fullsize;
69
	size_t remainingSize = fullsize;
-
 
70
 
93
	while ( off < numOffset )
71
	while ( off < numOffset ) {
94
	{
-
 
95
		startPos = ftell(id);
72
		startPos = File.tellg();
96
		size_t offset = offsets[off++];
73
		size_t offset = offsets[off++];
97
		size_t size = offset - startPos;
74
		size_t size = offset - startPos;
-
 
75
		try {
98
		char *data = new char[size];
76
			char *data = new char[size];
99
		fread ( data, sizeof(unsigned char), size, id );
77
			File.read(data, size);
100
		fwrite ( data, sizeof(unsigned char), size, writeId );
78
			writeFile.write(data, size);
101
		delete data;
79
			delete data;
-
 
80
		}
-
 
81
		catch(std::exception &e) {
-
 
82
			CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", m_sFilename.c_str(), e.what());
-
 
83
			return false;
-
 
84
		}
-
 
85
 
102
		size_t datasize = offsets[off++];
86
		size_t datasize = offsets[off++];
103
		fseek ( id, datasize, SEEK_CUR );
87
		File.seekg(datasize, std::ios::beg);
104
		remainingSize = fullsize - offset - datasize;
88
		remainingSize = fullsize - offset - datasize;
105
	}
89
	}
106
 
90
 
107
	if ( remainingSize )
91
	if ( remainingSize ) {
108
	{
-
 
109
		char data[1000000];
92
		char data[1000000];
110
		size_t amountLeft = 1000000;
93
		size_t amountLeft = 1000000;
111
		while ( remainingSize )
94
		while ( remainingSize )
112
		{
95
		{
113
			if ( amountLeft > remainingSize )
96
			if ( amountLeft > remainingSize ) amountLeft = remainingSize;
-
 
97
			try {
114
				amountLeft = remainingSize;
98
				File.read(data, amountLeft);
115
			fread ( data, sizeof(unsigned char), amountLeft, id );
99
				writeFile.write(data, amountLeft);
-
 
100
			}
116
			fwrite ( data, sizeof(unsigned char), amountLeft, writeId );
101
			catch(std::exception &e) {
-
 
102
				CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", m_sFilename.c_str(), e.what());
-
 
103
				return false;
-
 
104
			}
117
 
105
 
118
			remainingSize -= amountLeft;
106
			remainingSize -= amountLeft;
119
			amountLeft = 1000000;
107
			amountLeft = 1000000;
120
		}
108
		}
121
	}
109
	}
122
 
110
 
123
	fclose ( writeId );
111
	File.close();
124
	fclose ( id );
112
	writeFile.close();
125
 
113
 
126
	// now copy to original file
114
	// now copy to original file
127
	remove ( m_sFilename.c_str() );
115
	std::remove(m_sFilename);
128
	rename ( "temp.tmp", m_sFilename.c_str() );
116
	std::rename("temp.tmp", m_sFilename);
129
 
117
 
130
	return true;
118
	return true;
131
}
119
}
132
 
120
 
133
 
121
 
134
void CFileIO::SetDir ( CyString dir )
122
void CFileIO::SetDir ( CyString dir )
135
{
123
{
136
	if ( m_sFile.Empty() )
-
 
137
		m_sDirIO.SetDir(dir);
124
	if ( m_sFile.empty() )	m_sDirIO.SetDir(dir);
138
	else
-
 
139
		Open ( dir + "/" + m_sFile, m_bBinary );
125
	else					Open(dir + "/" + m_sFile, m_bBinary);
140
}
126
}
141
 
127
 
142
void CFileIO::ReadFileSize ()
128
void CFileIO::_readFileSize ()
143
{
129
{
144
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "rb" : "r" );
-
 
145
	if ( !id )
130
	m_lSize = 0;
146
	{
131
 
-
 
132
	std::fstream file(m_sFilename, (m_bBinary) ? (std::ios::in | std::ios::binary) : (std::ios::in));
147
		m_lSize = 0;
133
	if ( !file.is_open() ) return;
148
		return;
-
 
149
	}
134
 
150
	fseek ( id, 0, SEEK_END );
135
	file.seekg(0, std::ios::end);
151
	m_lSize = ftell ( id );
136
	m_lSize = file.tellg();
152
	fseek ( id, 0, SEEK_SET );
-
 
153
	fclose ( id );
137
	file.close();
154
}
138
}
155
 
139
 
156
bool CFileIO::WipeFile()
140
bool CFileIO::WipeFile()
157
{
141
{
158
	if ( NoFile() )
142
	if ( NoFile() )	return false;
159
		return false;
143
	std::ofstream file;
160
 
-
 
161
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "rb+" : "r+" );
144
	file.open(m_sFilename, std::ios::out | std::ios::trunc);
162
	if ( !id )
-
 
163
		return false;
145
	bool bDone = file.is_open();
164
	fclose(id);
146
	file.close();
165
 
147
 
166
	return true;
148
	return bDone;
167
}
149
}
168
int CFileIO::TruncateFile ( size_t offset, size_t datasize )
150
int CFileIO::TruncateFile ( size_t offset, size_t datasize )
169
{
151
{
170
	if ( NoFile() )
152
	if ( NoFile() )
171
		return FILEERR_NOFILE;
153
		return FILEERR_NOFILE;
Line 180... Line 162...
180
	fseek ( id, 0, SEEK_SET );
162
	fseek ( id, 0, SEEK_SET );
181
 
163
 
182
	if ( (offset + datasize) > fullsize )
164
	if ( (offset + datasize) > fullsize )
183
		return FILEERR_TOSMALL;
165
		return FILEERR_TOSMALL;
184
//	char data[500000];
166
//	char data[500000];
185
 
167
 
186
#ifdef _WIN32
168
#ifdef _WIN32
187
	FILE *writeId = fopen ( "temp.tmp", (m_bBinary) ? "wb" : "w" );
169
	FILE *writeId = fopen ( "temp.tmp", (m_bBinary) ? "wb" : "w" );
188
	if ( !writeId )
170
	if ( !writeId )
189
	{
171
	{
190
		fclose ( id );
172
		fclose ( id );
Line 208... Line 190...
208
 
190
 
209
	fclose ( writeId );
191
	fclose ( writeId );
210
	fclose ( id );
192
	fclose ( id );
211
 
193
 
212
	// now copy to original file
194
	// now copy to original file
213
	remove ( m_sFilename.c_str() );
195
	std::remove(m_sFilename.c_str());
214
	rename ( ";temp.tmp", m_sFilename.c_str() );
196
	std::rename(";temp.tmp", m_sFilename.c_str());
215
 
197
 
216
#else
198
#else
217
	// move to beginning of file data to remove
199
	// move to beginning of file data to remove
218
	fseek ( id, offset, SEEK_SET );
200
	fseek ( id, offset, SEEK_SET );
219
 
201
 
Line 231... Line 213...
231
		// read data
213
		// read data
232
		fseek ( id, readpos, SEEK_SET );
214
		fseek ( id, readpos, SEEK_SET );
233
		fread ( data, sizeof(unsigned char), read, id );
215
		fread ( data, sizeof(unsigned char), read, id );
234
		size -= read;
216
		size -= read;
235
		readpos += read;
217
		readpos += read;
236
 
218
 
237
		// now seek back and write
219
		// now seek back and write
238
		fseek ( id, writepos, SEEK_SET );
220
		fseek ( id, writepos, SEEK_SET );
239
		fwrite ( data, sizeof(unsigned char), read, id );
221
		fwrite ( data, sizeof(unsigned char), read, id );
240
		writepos += read;
222
		writepos += read;
241
 
223
 
242
	}
224
	}
243
 
225
 
244
	truncate ( m_sFilename.c_str(), fullsize - datasize );
226
	truncate ( m_sFilename.c_str(), fullsize - datasize );
245
	fclose ( id );
227
	fclose ( id );
246
#endif
228
#endif
247
 
229
 
248
	return FILEERR_NONE;
230
	return FILEERR_NONE;
249
}
231
}
250
 
232
 
-
 
233
int CFileIO::_in()
-
 
234
{
-
 
235
	if ( m_bBinary ) return std::ios::in | std::ios::binary;
-
 
236
	return std::ios::in;
-
 
237
}
-
 
238
 
-
 
239
int CFileIO::_out()
-
 
240
{
-
 
241
	if ( m_bBinary ) return std::ios::out | std::ios::binary;
-
 
242
	return std::ios::out;
-
 
243
}
-
 
244
 
-
 
245
int CFileIO::_append()
-
 
246
{
-
 
247
	if ( m_bBinary ) return std::ios::out | std::ios::binary | std::ios::app;
-
 
248
	return std::ios::out | std::ios::app;
-
 
249
}
-
 
250
 
251
char *CFileIO::ReadToData ( size_t *size )
251
char *CFileIO::ReadToData ( size_t *size )
252
{
252
{
253
	*size = 0;
253
	*size = 0;
254
 
254
 
255
	if ( NoFile() )
255
	if ( NoFile() ) return NULL;
256
		return NULL;
-
 
257
 
-
 
258
	if ( !m_lSize )
-
 
259
		ReadFileSize();
256
	if ( !m_lSize )	this->_readFileSize();
260
 
-
 
261
	if ( !m_lSize )
257
	if ( !m_lSize ) return NULL;
262
		return NULL;
-
 
263
 
258
 
264
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "rb" : "r" );
259
	std::fstream file(m_sFilename, _in());
-
 
260
	if ( !file.is_open() ) return NULL;
-
 
261
 
265
	if ( !id )
262
	char *data;
-
 
263
	try {
-
 
264
		data = new char[m_lSize + 1];
-
 
265
	}
-
 
266
	catch (std::exception &e) {
-
 
267
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to malloc storage, %d (%s)", m_lSize + 1, e.what());
-
 
268
		file.close();
266
		return NULL;
269
		return NULL;
-
 
270
	}
267
 
271
 
-
 
272
	try {
268
	char *data = new char[m_lSize];
273
		file.read((char *)data, m_lSize);
269
 
274
	}
270
	fread ( data, sizeof(char), m_lSize, id );
275
	catch (std::exception &e) {
271
	if ( ferror (id) )
276
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file (%s)", e.what());
272
	{
-
 
273
		fclose ( id );
277
		file.close();
274
		return NULL;
278
		return NULL;
275
	}
279
	}
276
 
280
 
277
	*size = m_lSize;
281
	*size = m_lSize;
278
	fclose ( id );
282
	file.close();
279
 
283
 
280
	return data;
284
	return data;
281
}
285
}
282
 
286
 
283
bool CFileIO::WriteData ( const char *data, size_t size )
287
bool CFileIO::WriteData ( const char *data, size_t size )
284
{
288
{
285
	if ( NoFile() )
289
	if ( NoFile() )
286
		return false;
290
		return false;
287
 
291
 
288
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "wb" : "w" );
292
	std::fstream File(m_sFilename, _out());
-
 
293
	if ( !File.is_open() ) return false;
-
 
294
 
-
 
295
	bool ret = true;
289
	if ( !id )
296
	try {
290
		return false;
297
		File.write(data, size);
291
 
298
	}
292
	fwrite ( data, size, sizeof(char), id );
299
	catch (std::exception &e) {
-
 
300
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file: %s (%s)", m_sFilename.c_str(), e.what());
293
	fclose ( id );
301
		ret = false;
-
 
302
	}
294
 
303
 
295
	ReadFileSize();
304
	File.close();
296
 
305
 
-
 
306
	if ( ret ) this->_readFileSize();
297
	return true;
307
	return ret;
298
}
308
}
299
 
309
 
300
bool CFileIO::WriteString ( CyString data )
310
bool CFileIO::WriteString ( CyString data )
301
{
311
{
302
	return WriteData ( data.c_str(), data.Length() );
312
	return WriteData ( data.c_str(), data.Length() );
303
}
313
}
304
 
314
 
305
bool CFileIO::Exists ()
315
bool CFileIO::Exists ()
306
{
316
{
307
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "rb" : "r" );
-
 
308
	if ( !id )
-
 
309
		return false;
-
 
310
 
-
 
311
	fclose ( id );
-
 
312
	return true;
-
 
313
}
-
 
314
 
-
 
315
bool CFileIO::StartReadOld()
-
 
316
{
-
 
317
	if ( !m_sFilename.Empty() )
317
	std::fstream File(m_sFilename, _in());
318
	{
-
 
319
		if ( m_bOpened )
318
	bool bRet = File.good();
320
			StopRead();
319
	File.close();
321
 
-
 
322
		m_fIdOld = fopen(m_sFilename.c_str(), "rb");
-
 
323
		if ( !m_fIdOld )
-
 
324
			return false;
-
 
325
		m_bOpened = true;
-
 
326
		return true;
-
 
327
	}
-
 
328
	return false;
320
	return bRet;
329
}
321
}
330
 
322
 
331
bool CFileIO::StartRead()
323
bool CFileIO::StartRead()
332
{
324
{
333
	if ( !m_sFilename.Empty() )
325
	if ( !m_sFilename.empty() ) {
334
	{
-
 
335
		if ( m_bOpened )
326
		if ( m_bOpened ) StopRead();
336
			StopRead();
-
 
337
 
327
 
338
		m_fId.open(m_sFilename.c_str(), std::ios_base::in);
328
		m_fId.open(m_sFilename, _in());
339
		if ( m_fId.is_open() )
329
		if ( m_fId.is_open() ) {
340
		{
-
 
341
			m_bOpened = true;
330
			m_bOpened = true;
342
			return true;
331
			return true;
343
		}
332
		}
344
	}
333
	}
345
	return false;
334
	return false;
346
}
335
}
347
 
336
 
348
std::vector<CyString> *CFileIO::ReadLines()
337
std::vector<CyString> *CFileIO::ReadLines()
349
{
338
{
350
	if ( m_sFilename.Empty() )
339
	if ( m_sFilename.empty() ) return 0;
351
		return 0;
-
 
352
 
340
 
353
	std::vector<CyString> *file = new std::vector<CyString>;
341
	std::vector<CyString> *file = new std::vector<CyString>;
354
	std::string line;
342
	std::string line;
355
	file->clear();
343
	file->clear();
356
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
344
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
Line 366... Line 354...
366
	return file;
354
	return file;
367
}
355
}
368
 
356
 
369
CyStringList *CFileIO::ReadLinesStr()
357
CyStringList *CFileIO::ReadLinesStr()
370
{
358
{
371
	if ( m_sFilename.Empty() )
359
	if ( m_sFilename.empty() ) return 0;
372
		return 0;
-
 
373
 
360
 
374
	CyStringList *file = new CyStringList;
361
	CyStringList *file = new CyStringList;
375
	std::string line;
362
	std::string line;
376
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
363
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
377
	while (getline(infile, line, '\n'))
364
	while (getline(infile, line, '\n'))
Line 386... Line 373...
386
	return file;
373
	return file;
387
}
374
}
388
 
375
 
389
void CFileIO::StopRead()
376
void CFileIO::StopRead()
390
{
377
{
391
	if ( m_bOpened )
-
 
392
	{
-
 
393
		if ( m_fIdOld )
-
 
394
			fclose(m_fIdOld);
-
 
395
		else
-
 
396
			m_fId.close();
378
	if ( m_fId.is_open() ) m_fId.close();
397
	}
-
 
398
	m_fIdOld = NULL;
-
 
399
	m_bOpened = false;
379
	m_bOpened = false;
400
}
380
}
401
 
381
 
402
bool CFileIO::IsOpened()
382
bool CFileIO::IsOpened()
403
{
383
{
404
	if ( !m_bOpened )
-
 
405
		return false;
-
 
406
	if ( m_fIdOld )
-
 
407
		return true;
-
 
408
	if ( m_fId.is_open() )
384
	if ( m_fId.is_open() )
409
		return true;
385
		return true;
410
	return false;
386
	return false;
411
}
387
}
412
 
388
 
413
CyString CFileIO::ReadToEndLine(bool autoclose)
389
CyString CFileIO::ReadToEndLine(bool autoclose)
414
{
390
{
415
	if ( !this->IsOpened() )
391
	if ( !this->IsOpened() )
416
	{
392
	{
417
		if ( !StartRead() )
393
		if ( !StartRead() )
418
			return "";
394
			return "";
419
	}
395
	}
420
 
396
 
421
	int pos = 0;
397
	int pos = 0;
422
	char sLine[10000];
398
	char sLine[10000];
423
	char cur = 0;
399
	char cur = 0;
424
 
400
 
425
	if ( m_fIdOld )
-
 
426
	{
-
 
427
		while ( !feof(m_fIdOld) )
-
 
428
		{
-
 
429
			cur = fgetc(m_fIdOld);
-
 
430
			if ( cur == '\n' || cur == '\r' )
-
 
431
				break;
-
 
432
			sLine[pos++] = cur;
-
 
433
		}
-
 
434
	}
-
 
435
	else
-
 
436
	{
-
 
437
		std::string line;
401
	std::string line;
438
		if ( getline(m_fId, line, '\n') )
402
	if ( getline(m_fId, line, '\n') )
439
			return CyString(line);
403
		return CyString(line);
440
	}
-
 
441
 
404
 
442
	sLine[pos] = 0;
405
	sLine[pos] = 0;
443
	if ( pos )
-
 
444
		return CyString(sLine);
406
	if ( pos ) return CyString(sLine);
445
 
407
 
446
	if ( autoclose )
408
	if ( autoclose )
447
		StopRead();
409
		StopRead();
448
	return "";
410
	return "";
449
}
411
}
450
 
412
 
451
bool CFileIO::AtEnd()
413
bool CFileIO::AtEnd()
452
{
414
{
453
	if ( !m_bOpened )
415
	if ( !this->IsOpened() ) return true;
454
		return true;
-
 
455
 
-
 
456
	if ( m_fIdOld )
-
 
457
	{
-
 
458
		if ( feof(m_fIdOld) )
-
 
459
			return true;
-
 
460
	}
-
 
461
	else
-
 
462
	{
-
 
463
		if ( m_fId.eof() )
416
	if ( m_fId.eof() )		 return true;
464
			return true;
-
 
465
	}
-
 
466
 
-
 
467
	return false;
417
	return false;
468
}
418
}
469
 
419
 
470
bool CFileIO::AppendFile ( CyString filename )
420
bool CFileIO::AppendFile ( CyString filename )
471
{
421
{
472
	FILE *id = fopen ( filename.c_str(), (m_bBinary) ? "rb" : "r" );
422
	std::fstream fromFile(filename.ToString().c_str(), _in());
473
	if ( !id )
-
 
474
		return false;
423
	if ( !fromFile.is_open() ) return false;
475
 
424
 
476
	FILE *aid = fopen ( m_sFilename.c_str(), (m_bBinary) ? "ab" : "a" );
425
	std::fstream toFile(m_sFilename, _append());
477
	if ( !aid )
426
	if ( !toFile.is_open() ) {
478
	{
-
 
479
		return false;
427
		fromFile.close();
480
		fclose ( id );
428
		return false;
481
	}
429
	}
482
 
430
 
483
	// move to the end of the file
431
	// move to the end of the file
484
	fseek ( aid, 0, SEEK_END );
432
	toFile.seekg(0, std::ios::end);
485
 
433
 
486
	// get size of file
434
	// get size of file
487
	fseek ( id, 0, SEEK_END );
435
	fromFile.seekg(0, std::ios::end);
488
	size_t size = ftell ( id );
436
	size_t size = fromFile.tellg();
489
	fseek ( id, 0, SEEK_SET );
437
	fromFile.seekg(0, std::ios::beg);
490
 
438
 
491
	char data[500000];
439
	char data[500000];
492
	while ( size > 0 )
440
	while ( size > 0 )
493
	{
441
	{
494
		size_t read = 500000;
442
		size_t read = 500000;
495
		if ( read > size )
443
		if ( read > size )
496
			read = size;
444
			read = size;
497
 
445
 
498
		size -= read;
446
		size -= read;
499
 
447
 
500
		fread ( data, sizeof(char), read, id );
448
		fromFile.read(data, read);
501
		fwrite ( data, sizeof(char), read, aid );
449
		toFile.write(data, read);
502
	}
450
	}
503
 
451
 
504
	fclose ( aid );
452
	fromFile.close();
505
	fclose ( id );
453
	toFile.close();
506
	return true;
454
	return true;
507
}
455
}
508
 
456
 
509
 
457
 
510
bool CFileIO::AppendData ( const char *d, size_t size )
458
bool CFileIO::AppendData ( const char *d, size_t size )
511
{
459
{
512
	FILE *aid = fopen ( m_sFilename.c_str(), (m_bBinary) ? "ab" : "a" );
460
	std::fstream File(m_sFilename, _append());
513
	if ( !aid )
-
 
514
		return false;
461
	if ( !File.is_open() ) return false;
515
 
462
 
516
	// move to the end of the file
463
	// move to the end of the file
517
	fseek ( aid, 0, SEEK_END );
464
	File.seekg(0, std::ios::end);
518
 
465
 
519
	char *pos = (char *)d;
466
	char *pos = (char *)d;
520
	while ( size > 0 )
467
	while ( size > 0 ) {
521
	{
-
 
522
		size_t read = 500000;
468
		size_t read = 500000;
523
		if ( read > size )
469
		if ( read > size ) read = size;
524
			read = size;
-
 
525
 
470
 
526
		size -= read;
471
		size -= read;
527
 
472
 
-
 
473
		try {
528
		fwrite ( pos, sizeof(char), read, aid );
474
			File.write(pos, read);
-
 
475
		} 
-
 
476
		catch(std::exception &e) {
-
 
477
			CLog::logf(CLog::Log_IO, 2, "CFileIO::AppendData() unable to write data to file: %s (%s)", m_sFilename.c_str(), e.what());
-
 
478
			File.close();
-
 
479
			return false;
-
 
480
		}
529
		pos += read;
481
		pos += read;
530
	}
482
	}
531
 
483
 
532
	fclose ( aid );
484
	File.close();
533
	return true;
485
	return true;
534
}
486
}
535
 
487
 
536
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
488
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
537
{
489
{
538
	FILE *aid = fopen ( m_sFilename.c_str(), (m_bBinary) ? "ab" : "a" );
490
	FILE *aid = fopen ( m_sFilename.c_str(), (m_bBinary) ? "ab" : "a" );
539
	if ( !aid )
491
	if ( !aid )
540
		return false;
492
		return false;
541
 
493
 
542
	// move to the end of the file
494
	// move to the end of the file
Line 544... Line 496...
544
	size_t end = ftell(aid);
496
	size_t end = ftell(aid);
545
	if ( start > end )
497
	if ( start > end )
546
	{
498
	{
547
		fclose ( aid);
499
		fclose ( aid);
548
		return false;
500
		return false;
549
	}
501
	}
550
 
502
 
551
	if ( start == end ) {
503
	if ( start == end ) {
552
		fseek(aid, 0, SEEK_END);
504
		fseek(aid, 0, SEEK_END);
553
	}
505
	}
554
	else {
506
	else {
Line 568... Line 520...
568
		pos += read;
520
		pos += read;
569
	}
521
	}
570
 
522
 
571
	fclose ( aid );
523
	fclose ( aid );
572
	return true;
524
	return true;
573
}
525
}
574
 
526
 
575
CyString CFileIO::GetBaseName()
527
CyString CFileIO::GetBaseName()
576
{
528
{
577
	return m_sFile.GetTokenRev(".", 1, -1);
529
	return m_sFile.token(".", -2);
578
}
530
}
579
CyString CFileIO::GetFileExtension ()
531
CyString CFileIO::GetFileExtension ()
580
{
532
{
581
	if ( m_sFilename.Empty() )
533
	if ( m_sFilename.empty() ) return NullString;
582
		return NullString;
-
 
583
 
-
 
584
	CyString ext = m_sFilename.GetToken ( ".", m_sFilename.NumToken ( "." ) );
534
	return m_sFilename.token(".", -1);
585
	return ext;
-
 
586
}
535
}
587
 
536
 
588
CyString CFileIO::ChangeFileExtension ( CyString ext )
537
CyString CFileIO::ChangeFileExtension ( CyString ext )
589
{
538
{
590
	if ( m_sFilename.Empty() )
539
	if ( m_sFilename.empty() )
591
		return NullString;
540
		return NullString;
592
 
541
	
593
	CyString noext = m_sFilename.GetToken ( ".", 1, m_sFilename.NumToken ( &quot;." ) - 1 );
542
	return m_sFilename.tokens(&quot;.", 1, -2) + "." + ext.ToString();
594
	return noext + "." + ext;
-
 
595
}
543
}
596
 
544
 
597
bool CFileIO::Remove()
545
bool CFileIO::Remove()
598
{
546
{
599
	if ( !Exists() )
547
	if ( !Exists() )
600
		return false;
548
		return false;
601
 
549
 
602
	if ( remove(m_sFilename.c_str()) == 0 )
550
	if ( std::remove(m_sFilename.c_str()) == 0 )
603
		return true;
551
		return true;
604
 
552
 
605
	return false;
553
	return false;
606
}
554
}
607
 
555
 
608
bool CFileIO::WriteFileUTF(std::vector<CyString> *lines)
556
bool CFileIO::WriteFileUTF(std::vector<CyString> *lines)
609
{
557
{
610
	if ( !lines || m_sFilename.Empty() )
558
	if ( !lines || m_sFilename.empty() )
611
		return false;
559
		return false;
612
 
560
 
613
	// we need to create the directory
561
	// we need to create the directory
614
	if ( !m_sDirIO.Exists() )
562
	if ( !m_sDirIO.Exists() )
615
	{
563
	{
Line 662... Line 610...
662
#endif
610
#endif
663
}
611
}
664
 
612
 
665
bool CFileIO::WriteFile(std::vector<CyString> *lines)
613
bool CFileIO::WriteFile(std::vector<CyString> *lines)
666
{
614
{
667
	if ( !lines || m_sFilename.Empty() )
615
	if ( !lines || m_sFilename.empty() )
668
		return false;
616
		return false;
669
 
617
 
670
	// we need to create the directory
618
	// we need to create the directory
671
	if ( !m_sDirIO.Exists() )
619
	if ( !m_sDirIO.Exists() )
672
	{
620
	{
Line 690... Line 638...
690
	return true;
638
	return true;
691
}
639
}
692
 
640
 
693
bool CFileIO::WriteFile(CyStringList *lines)
641
bool CFileIO::WriteFile(CyStringList *lines)
694
{
642
{
695
	if ( !lines || m_sFilename.Empty() )
643
	if ( !lines || m_sFilename.empty() )
696
		return false;
644
		return false;
697
 
645
 
698
	// we need to create the directory
646
	// we need to create the directory
699
	if ( !m_sDirIO.Exists() )
647
	if ( !m_sDirIO.Exists() )
700
	{
648
	{