Subversion Repositories spk

Rev

Rev 86 | Rev 111 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#include "File_IO.h"
2
 
3
#ifdef _WIN32
4
#include <windows.h>
5
#endif
6
 
7
#include <locale>
8
 
9
#include "DirIO.h"
10
#include "File.h"
51 cycrow 11
#include "Logging/Log.h"
81 cycrow 12
#include "Packages.h"
1 cycrow 13
 
52 cycrow 14
CFileIO::CFileIO () : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
1 cycrow 15
{
16
}
17
 
52 cycrow 18
/*CFileIO::CFileIO(const Utils::String &sFilename, bool bBinary = true) : m_lSize(0), m_bBinary(bBinary), m_bAutoDelete(false)
1 cycrow 19
{
82 cycrow 20
	open(sFilename, bBinary);
1 cycrow 21
}
52 cycrow 22
*/
23
CFileIO::CFileIO(CyString filename) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
24
{
82 cycrow 25
	open(filename.ToString(), true);
52 cycrow 26
}
1 cycrow 27
 
52 cycrow 28
CFileIO::CFileIO(C_File *file) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
1 cycrow 29
{
82 cycrow 30
	open(file->GetFilePointer().ToString(), true);
1 cycrow 31
}
32
 
33
CFileIO::~CFileIO()
34
{
52 cycrow 35
	if ( this->isOpened() ) m_fId.close();
36
	if ( m_bAutoDelete ) this->remove();
1 cycrow 37
}
38
 
52 cycrow 39
void CFileIO::setAutoDelete(bool bDelete)
40
{
41
	m_bAutoDelete = true;
42
}
43
 
82 cycrow 44
bool CFileIO::open(const Utils::String &sFilename, bool bBinary)
1 cycrow 45
{
82 cycrow 46
	m_bBinary = bBinary;
47
	m_sFilename = sFilename.asFilename();
1 cycrow 48
 
82 cycrow 49
	// check if there are any directories, and split the file and directory parts
51 cycrow 50
	if ( m_sFilename.isin('/') ) {
51
		m_sDirIO.SetDir(m_sFilename.tokens("/", 1, -2));
52
		m_sFile = m_sFilename.token("/", -1);
1 cycrow 53
	}
54
	else
82 cycrow 55
		m_sFile = m_sFilename;
1 cycrow 56
 
95 cycrow 57
	m_sFile.removeFirstSpace();
58
	m_sFile.removeChar(13);
59
 
51 cycrow 60
	this->_readFileSize();
1 cycrow 61
	return true;
62
}
82 cycrow 63
bool CFileIO::Open ( CyString filename, bool binary )
64
{
65
	return open(filename.ToString(), binary);
66
}
52 cycrow 67
 
68
unsigned char *CFileIO::read(size_t iAmount)
69
{
70
	if ( !this->isOpened() ) startRead();
71
	if ( !this->isOpened() ) return NULL;
72
 
73
	if ( iAmount > m_lSize ) iAmount = m_lSize;
74
 
75
	unsigned char *data;
76
	try {
77
		data = new unsigned char[iAmount + 1];
78
	}
79
	catch (std::exception &e) {
80
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to malloc data, %d (%s)", iAmount + 1, e.what());
81
		return NULL;
82
	}
83
 
84
	if ( !read(data, iAmount, true) ) {
85
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to read data from file, %s/%d", m_sFilename.c_str(), iAmount);
86
		delete data;
87
		return NULL;
88
	}
89
 
90
	return data;
91
}
92
 
93
unsigned char *CFileIO::readAll(size_t *pSize)
94
{
95
	if ( pSize ) (*pSize) = 0;
96
	if ( !this->isOpened() ) startRead();
97
	if ( !this->isOpened() ) return NULL;
98
 
99
	unsigned char *data;
100
	try {
101
		data = new unsigned char[m_lSize + 1];
102
	}
103
	catch (std::exception &e) {
104
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to malloc data, %d (%s)", m_lSize + 1, e.what());
105
		return NULL;
106
	}
107
 
108
	if ( !read(data, m_lSize, true) ) {
109
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to read data from file, %s/%d", m_sFilename.c_str(), m_lSize);
110
		delete data;
111
		return NULL;
112
	}
113
 
114
	if ( pSize ) (*pSize) = m_lSize;
115
	return data;
116
}
117
 
118
bool CFileIO::read(unsigned char *buf, size_t iSize, bool bEndChar)
119
{
120
	if ( !this->isOpened() ) startRead();
121
	if ( !this->isOpened() ) return false;
122
 
53 cycrow 123
	if ( iSize > m_lSize ) iSize = m_lSize;
52 cycrow 124
	try {
125
		m_fId.read((char *)buf, iSize);
126
	}
127
	catch (std::exception &e) {
128
		CLog::logf(CLog::Log_IO, 3, "ERROR: CFileIO::read() unable to read from file: %s (%s)", m_sFilename.c_str(), e.what());
129
		return false;
130
	}
131
 
132
	if ( bEndChar ) buf[iSize] = '\0';
133
 
134
	return !m_fId.bad();
135
}
136
 
1 cycrow 137
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
138
{
51 cycrow 139
	if ( NoFile() )	return false;
1 cycrow 140
 
51 cycrow 141
	std::fstream File(m_sFilename, _in());
142
	if ( !File.is_open() ) return false;
1 cycrow 143
 
144
	// first find file size
51 cycrow 145
	File.seekg(0, std::ios::end);
146
	size_t fullsize = File.tellg(), size = fullsize;
147
	File.seekg(0, std::ios::beg);
1 cycrow 148
 
81 cycrow 149
	std::fstream writeFile(CPackages::tempDirectory() + "/temp.tmp", _out());
51 cycrow 150
	if ( !File.is_open() ) {
151
		File.close();
1 cycrow 152
		return false;
153
	}
154
 
155
	size_t off = 0;
156
	size_t startPos = 0;
157
	size_t remainingSize = fullsize;
51 cycrow 158
 
159
	while ( off < numOffset ) {
160
		startPos = File.tellg();
1 cycrow 161
		size_t offset = offsets[off++];
162
		size_t size = offset - startPos;
51 cycrow 163
		try {
164
			char *data = new char[size];
165
			File.read(data, size);
166
			writeFile.write(data, size);
167
			delete data;
168
		}
169
		catch(std::exception &e) {
170
			CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", m_sFilename.c_str(), e.what());
171
			return false;
172
		}
173
 
1 cycrow 174
		size_t datasize = offsets[off++];
51 cycrow 175
		File.seekg(datasize, std::ios::beg);
1 cycrow 176
		remainingSize = fullsize - offset - datasize;
177
	}
178
 
51 cycrow 179
	if ( remainingSize ) {
1 cycrow 180
		char data[1000000];
181
		size_t amountLeft = 1000000;
182
		while ( remainingSize )
183
		{
51 cycrow 184
			if ( amountLeft > remainingSize ) amountLeft = remainingSize;
185
			try {
186
				File.read(data, amountLeft);
187
				writeFile.write(data, amountLeft);
188
			}
189
			catch(std::exception &e) {
190
				CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", m_sFilename.c_str(), e.what());
191
				return false;
192
			}
1 cycrow 193
 
194
			remainingSize -= amountLeft;
195
			amountLeft = 1000000;
196
		}
197
	}
198
 
51 cycrow 199
	File.close();
200
	writeFile.close();
1 cycrow 201
 
202
	// now copy to original file
51 cycrow 203
	std::remove(m_sFilename);
81 cycrow 204
	std::rename(CPackages::tempDirectory() + "/temp.tmp", m_sFilename);
1 cycrow 205
 
206
	return true;
207
}
208
 
209
 
210
void CFileIO::SetDir ( CyString dir )
211
{
51 cycrow 212
	if ( m_sFile.empty() )	m_sDirIO.SetDir(dir);
213
	else					Open(dir + "/" + m_sFile, m_bBinary);
1 cycrow 214
}
215
 
51 cycrow 216
void CFileIO::_readFileSize ()
1 cycrow 217
{
51 cycrow 218
	m_lSize = 0;
219
 
220
	std::fstream file(m_sFilename, (m_bBinary) ? (std::ios::in | std::ios::binary) : (std::ios::in));
221
	if ( !file.is_open() ) return;
222
 
223
	file.seekg(0, std::ios::end);
224
	m_lSize = file.tellg();
225
	file.close();
1 cycrow 226
}
227
 
228
bool CFileIO::WipeFile()
229
{
51 cycrow 230
	if ( NoFile() )	return false;
231
	std::ofstream file;
232
	file.open(m_sFilename, std::ios::out | std::ios::trunc);
233
	bool bDone = file.is_open();
234
	file.close();
1 cycrow 235
 
51 cycrow 236
	return bDone;
1 cycrow 237
}
52 cycrow 238
 
239
 
1 cycrow 240
int CFileIO::TruncateFile ( size_t offset, size_t datasize )
241
{
52 cycrow 242
	if ( NoFile() ) return FILEERR_NOFILE;
1 cycrow 243
 
52 cycrow 244
	if ( !this->startRead() ) return FILEERR_NOOPEN;
245
	if ( (offset + datasize) > m_lSize ) return FILEERR_TOSMALL;
1 cycrow 246
 
81 cycrow 247
	CFileIO File(CPackages::tempDirectory() + "/temp.tmp");
52 cycrow 248
	if ( !File.startWrite() ) return FILEERR_NOWRITE;
249
	File.setAutoDelete(true);
1 cycrow 250
 
52 cycrow 251
	if ( !File.write(*this, offset) ) return false;
1 cycrow 252
 
253
	// next fseek after and write
52 cycrow 254
	this->seek(datasize);
255
	size_t size = m_lSize - offset - datasize;
42 cycrow 256
	if ( size > 0 ) {
52 cycrow 257
		File.write(*this, size);
42 cycrow 258
	}
1 cycrow 259
 
52 cycrow 260
	File.close();
261
	this->close();
262
	File.setAutoDelete(false);
1 cycrow 263
 
264
	// now copy to original file
58 cycrow 265
	if ( std::remove(m_sFilename.c_str()) == 0 ) {
81 cycrow 266
		std::rename(CPackages::tempDirectory() + "/temp.tmp", m_sFilename.c_str());
85 cycrow 267
		size_t oldSize = m_lSize;
84 cycrow 268
		size_t checkFileSize = m_lSize - datasize;
269
		this->_readFileSize();
270
		if ( checkFileSize != m_lSize ) {
271
			CLog::log(CLog::Log_IO, 3, Utils::String("WARNING: CFileIO::TruncateFile, file size mismatch, ") + (long)checkFileSize + " => " + (long)m_lSize);
272
		}
58 cycrow 273
		return FILEERR_NONE;
274
	}
1 cycrow 275
 
58 cycrow 276
	return FILEERR_NOWRITE;
1 cycrow 277
}
278
 
52 cycrow 279
int CFileIO::_in() const
51 cycrow 280
{
281
	if ( m_bBinary ) return std::ios::in | std::ios::binary;
282
	return std::ios::in;
283
}
284
 
52 cycrow 285
int CFileIO::_out() const
51 cycrow 286
{
287
	if ( m_bBinary ) return std::ios::out | std::ios::binary;
288
	return std::ios::out;
289
}
52 cycrow 290
int CFileIO::_inout() const
291
{
292
	if ( m_bBinary ) return std::ios::in | std::ios::out | std::ios::binary;
293
	return std::ios::in | std::ios::out;
294
}
51 cycrow 295
 
52 cycrow 296
int CFileIO::_append() const
51 cycrow 297
{
298
	if ( m_bBinary ) return std::ios::out | std::ios::binary | std::ios::app;
299
	return std::ios::out | std::ios::app;
300
}
301
 
1 cycrow 302
char *CFileIO::ReadToData ( size_t *size )
303
{
304
	*size = 0;
305
 
51 cycrow 306
	if ( NoFile() ) return NULL;
307
	if ( !m_lSize )	this->_readFileSize();
308
	if ( !m_lSize ) return NULL;
1 cycrow 309
 
51 cycrow 310
	std::fstream file(m_sFilename, _in());
311
	if ( !file.is_open() ) return NULL;
1 cycrow 312
 
51 cycrow 313
	char *data;
314
	try {
315
		data = new char[m_lSize + 1];
316
	}
317
	catch (std::exception &e) {
318
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to malloc storage, %d (%s)", m_lSize + 1, e.what());
319
		file.close();
1 cycrow 320
		return NULL;
51 cycrow 321
	}
1 cycrow 322
 
51 cycrow 323
	try {
324
		file.read((char *)data, m_lSize);
325
	}
326
	catch (std::exception &e) {
327
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file (%s)", e.what());
328
		file.close();
1 cycrow 329
		return NULL;
330
	}
331
 
332
	*size = m_lSize;
51 cycrow 333
	file.close();
1 cycrow 334
 
335
	return data;
336
}
337
 
338
bool CFileIO::WriteData ( const char *data, size_t size )
339
{
340
	if ( NoFile() )
341
		return false;
342
 
51 cycrow 343
	std::fstream File(m_sFilename, _out());
344
	if ( !File.is_open() ) return false;
1 cycrow 345
 
51 cycrow 346
	bool ret = true;
347
	try {
348
		File.write(data, size);
349
	}
350
	catch (std::exception &e) {
351
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file: %s (%s)", m_sFilename.c_str(), e.what());
352
		ret = false;
353
	}
1 cycrow 354
 
51 cycrow 355
	File.close();
1 cycrow 356
 
51 cycrow 357
	if ( ret ) this->_readFileSize();
358
	return ret;
1 cycrow 359
}
360
 
82 cycrow 361
bool CFileIO::writeString ( const Utils::String &data )
1 cycrow 362
{
82 cycrow 363
	return WriteData ( data.c_str(), data.length() );
1 cycrow 364
}
365
 
52 cycrow 366
bool CFileIO::Exists(const Utils::String &filename)
1 cycrow 367
{
52 cycrow 368
	std::fstream File(filename, std::ios::in | std::ios::binary);
369
	bool bRet = File.good();
370
	File.close();
371
	return bRet;
372
}
373
 
374
bool CFileIO::ExistsOld () const
375
{
376
	return this->exists();
377
}
378
bool CFileIO::exists () const
379
{
380
	if ( this->isOpened() ) return true;
51 cycrow 381
	std::fstream File(m_sFilename, _in());
382
	bool bRet = File.good();
383
	File.close();
384
	return bRet;
1 cycrow 385
}
386
 
52 cycrow 387
bool CFileIO::startRead()
1 cycrow 388
{
52 cycrow 389
	return _start(_in(), false);
390
}
391
 
392
bool CFileIO::startWrite()
393
{
394
	return _start(_out(), true);
395
}
396
 
397
bool CFileIO::startModify()
398
{
399
	return _start(_inout(), true);
400
}
401
 
402
bool CFileIO::startAppend()
403
{
404
	return _start(_append(), false);
405
}
406
 
407
bool CFileIO::_start(int iFlags, bool bSeekP)
408
{
51 cycrow 409
	if ( !m_sFilename.empty() ) {
52 cycrow 410
		if ( this->isOpened() ) this->close();
1 cycrow 411
 
52 cycrow 412
		m_fId.open(m_sFilename, iFlags);
1 cycrow 413
	}
52 cycrow 414
	m_bSeekP = bSeekP;
415
	return m_fId.is_open();
1 cycrow 416
}
417
 
82 cycrow 418
std::vector<Utils::String> *CFileIO::readLines()
419
{
420
	if ( m_sFilename.empty() ) return 0;
421
 
422
	std::vector<Utils::String> *file = new std::vector<Utils::String>;
84 cycrow 423
	Utils::String line;
82 cycrow 424
	file->clear();
425
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
426
	while (getline(infile, line, '\n')) {
427
		file->push_back(Utils::String(line).removeChar((char)0));
428
	}
429
 
430
	infile.close();
431
 
432
	return file;
433
}
83 cycrow 434
 
1 cycrow 435
std::vector<CyString> *CFileIO::ReadLines()
436
{
51 cycrow 437
	if ( m_sFilename.empty() ) return 0;
1 cycrow 438
 
439
	std::vector<CyString> *file = new std::vector<CyString>;
84 cycrow 440
	Utils::String line;
1 cycrow 441
	file->clear();
442
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
443
	while (getline(infile, line, '\n'))
444
	{
445
		CyString l = line;
446
		l.RemoveChar((char)0);
447
		file->push_back(l);
448
	}
449
 
450
	infile.close();
451
 
452
	return file;
453
}
454
 
455
CyStringList *CFileIO::ReadLinesStr()
456
{
51 cycrow 457
	if ( m_sFilename.empty() ) return 0;
1 cycrow 458
 
459
	CyStringList *file = new CyStringList;
84 cycrow 460
	Utils::String line;
1 cycrow 461
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
462
	while (getline(infile, line, '\n'))
463
	{
84 cycrow 464
		file->PushBack(CyString(line.removeChar((char)0)));
1 cycrow 465
	}
466
 
467
	infile.close();
468
 
469
	return file;
470
}
471
 
52 cycrow 472
bool CFileIO::put(const unsigned char c)
1 cycrow 473
{
52 cycrow 474
	if ( !this->isOpened() ) return false;
475
 
476
	m_fId.put(c);
477
	return !m_fId.bad();
1 cycrow 478
}
479
 
52 cycrow 480
bool CFileIO::writeSize(unsigned int iSize)
1 cycrow 481
{
52 cycrow 482
	if ( !this->isOpened() ) return false;
483
	if ( !this->put(static_cast<unsigned char>(iSize >> 24)) ) return false;
484
	if ( !this->put(static_cast<unsigned char>(iSize >> 16)) ) return false;
485
	if ( !this->put(static_cast<unsigned char>(iSize >> 8)) ) return false;
486
	if ( !this->put(static_cast<unsigned char>(iSize)) ) return false;
487
	m_lSize += 4;
488
	return true;
1 cycrow 489
}
490
 
52 cycrow 491
bool CFileIO::write(CFileIO &file, size_t iSize)
1 cycrow 492
{
52 cycrow 493
	if ( !this->isOpened() ) startWrite();
494
	if ( !this->isOpened() ) return false;
495
 
496
	int iMaxSize = 1000000;
497
 
498
	unsigned char *data;
499
	try {
500
		data = new unsigned char[iMaxSize + 1];
1 cycrow 501
	}
52 cycrow 502
	catch (std::exception &e) {
503
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::write(CFileIO, size_t) unable to malloc data, %d (%s)", iMaxSize + 1, e.what());
504
		return false;
505
	}
1 cycrow 506
 
52 cycrow 507
	int iSizeLeft = iSize;
508
	bool bSuccess = true;
509
	while ( iSizeLeft > 0 ) {
510
		int iDoSize = iMaxSize;
511
		if ( iDoSize > iSizeLeft ) iDoSize = iSizeLeft;
1 cycrow 512
 
52 cycrow 513
		if ( !file.read(data, iDoSize) ) bSuccess = false;
514
		if ( bSuccess && !this->write(data, iDoSize) ) bSuccess = false;
515
		if ( !bSuccess ) break;
516
		iSizeLeft -= iDoSize;
517
	}
1 cycrow 518
 
52 cycrow 519
	delete data;
1 cycrow 520
 
52 cycrow 521
	return bSuccess;
1 cycrow 522
}
523
 
52 cycrow 524
bool CFileIO::write(const char *buf, unsigned int iSize)
525
{
526
	if ( !this->isOpened() ) startWrite();
527
	if ( !this->isOpened() ) return false;
528
	try {
529
		m_fId.write((char *)buf, iSize);
530
	}
531
	catch (std::exception &e) {
532
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", m_sFilename.c_str(), e.what());
533
		return false;
534
	}
535
	m_lSize += iSize;
536
	return !m_fId.bad();
537
}
538
bool CFileIO::write(const unsigned char *buf, unsigned int iSize)
539
{
540
	return this->write((char *)buf, iSize);
541
}
542
 
543
bool CFileIO::write(const unsigned char *buf, ...)
544
{
545
	va_list args;
546
	va_start(args, buf);
547
	bool ret = this->_write((char *)buf, args);
548
	va_end(args);
549
	return ret;
550
}
551
bool CFileIO::_write(const char *buf, va_list args) 
552
{
553
	if ( !this->isOpened() ) return false;
554
 
555
	char buffer[10000];
556
 
557
	vsprintf (buffer, buf, args);
558
	try {
559
		int iLen = strlen(buffer);
560
		m_fId.write((char *)buffer, iLen);
561
		m_lSize += iLen;
562
	}
563
	catch (std::exception &e) {
564
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", m_sFilename.c_str(), e.what());
565
		return false;
566
	}
567
	return !m_fId.bad();
568
}
569
 
570
bool CFileIO::write(const char *buf, ...)
571
{
572
	va_list args;
573
	va_start(args, buf);
574
	bool ret = this->_write((char *)buf, args);
575
	va_end(args);
576
	return ret;
577
}
578
 
579
void CFileIO::_seek(int iPos, int iFrom)
580
{
581
	if ( !this->isOpened() ) {
582
		if ( !this->startRead() ) return;
583
	}
584
	if ( m_bSeekP ) m_fId.seekp(iPos, iFrom);
585
	else m_fId.seekg(iPos, iFrom);
586
}
587
 
588
void CFileIO::seek(unsigned int iPos)
589
{
590
	_seek(iPos, std::ios::cur);
591
}
592
 
53 cycrow 593
void CFileIO::seekEnd(unsigned int iPos)
52 cycrow 594
{
53 cycrow 595
	_seek(iPos, std::ios::end);
52 cycrow 596
}
597
 
598
void CFileIO::seekStart(unsigned int iPos)
599
{
600
	_seek(iPos, std::ios::beg);
601
}
602
 
603
std::fstream &CFileIO::stream()
604
{
605
	return m_fId;
606
}
607
 
608
bool CFileIO::isOpened() const
609
{
610
	if ( m_fId.is_open() )
611
		return true;
612
	return false;
613
}
614
 
615
void CFileIO::close()
616
{
617
	if ( this->isOpened() ) m_fId.close();
84 cycrow 618
	this->_readFileSize();
52 cycrow 619
}
620
 
56 cycrow 621
int CFileIO::readSize()
1 cycrow 622
{
56 cycrow 623
	unsigned char size[4];
624
	if ( this->read(size, 4) ) return (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
625
	return 0;
626
}
627
 
628
bool CFileIO::atEnd() const
629
{
52 cycrow 630
	if ( !this->isOpened() ) return true;
51 cycrow 631
	if ( m_fId.eof() )		 return true;
1 cycrow 632
	return false;
633
}
634
 
82 cycrow 635
bool CFileIO::appendFile ( const Utils::String &filename )
636
{
637
	std::fstream fromFile(filename, _in());
638
	if ( !fromFile.is_open() ) return false;
639
 
640
	std::fstream toFile(m_sFilename, _append());
641
	if ( !toFile.is_open() ) {
642
		fromFile.close();
643
		return false;
644
	}
645
 
646
	// move to the end of the file
647
	toFile.seekg(0, std::ios::end);
648
 
649
	// get size of file
650
	fromFile.seekg(0, std::ios::end);
651
	size_t size = fromFile.tellg();
652
	fromFile.seekg(0, std::ios::beg);
653
 
654
	char data[500000];
655
	while ( size > 0 )
656
	{
657
		size_t read = 500000;
658
		if ( read > size )
659
			read = size;
660
 
661
		size -= read;
662
 
663
		fromFile.read(data, read);
664
		toFile.write(data, read);
665
	}
666
 
667
	fromFile.close();
668
	toFile.close();
669
	return true;
670
}
671
 
1 cycrow 672
bool CFileIO::AppendFile ( CyString filename )
673
{
51 cycrow 674
	std::fstream fromFile(filename.ToString().c_str(), _in());
675
	if ( !fromFile.is_open() ) return false;
1 cycrow 676
 
51 cycrow 677
	std::fstream toFile(m_sFilename, _append());
678
	if ( !toFile.is_open() ) {
679
		fromFile.close();
1 cycrow 680
		return false;
681
	}
682
 
683
	// move to the end of the file
51 cycrow 684
	toFile.seekg(0, std::ios::end);
1 cycrow 685
 
686
	// get size of file
51 cycrow 687
	fromFile.seekg(0, std::ios::end);
688
	size_t size = fromFile.tellg();
689
	fromFile.seekg(0, std::ios::beg);
1 cycrow 690
 
691
	char data[500000];
692
	while ( size > 0 )
693
	{
694
		size_t read = 500000;
695
		if ( read > size )
696
			read = size;
697
 
698
		size -= read;
699
 
51 cycrow 700
		fromFile.read(data, read);
701
		toFile.write(data, read);
1 cycrow 702
	}
703
 
51 cycrow 704
	fromFile.close();
705
	toFile.close();
1 cycrow 706
	return true;
707
}
708
 
709
 
710
bool CFileIO::AppendData ( const char *d, size_t size )
711
{
58 cycrow 712
	std::ofstream File(m_sFilename, _append());
51 cycrow 713
	if ( !File.is_open() ) return false;
1 cycrow 714
 
715
	// move to the end of the file
58 cycrow 716
	//File.seekg(0, std::ios::end);
1 cycrow 717
 
718
	char *pos = (char *)d;
51 cycrow 719
	while ( size > 0 ) {
1 cycrow 720
		size_t read = 500000;
51 cycrow 721
		if ( read > size ) read = size;
1 cycrow 722
 
723
		size -= read;
724
 
51 cycrow 725
		try {
726
			File.write(pos, read);
727
		} 
728
		catch(std::exception &e) {
729
			CLog::logf(CLog::Log_IO, 2, "CFileIO::AppendData() unable to write data to file: %s (%s)", m_sFilename.c_str(), e.what());
730
			File.close();
731
			return false;
732
		}
1 cycrow 733
		pos += read;
734
	}
735
 
51 cycrow 736
	File.close();
1 cycrow 737
	return true;
738
}
739
 
740
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
741
{
52 cycrow 742
	if ( start > m_lSize ) return false;
743
	if ( m_lSize <= 0 ) {
744
		if ( !this->startWrite() ) return false;
1 cycrow 745
	}
52 cycrow 746
	else if ( start == m_lSize ) {
747
		if ( !this->startAppend() ) return false;
42 cycrow 748
	}
749
	else {
52 cycrow 750
		if ( !this->startModify() ) return false;
751
		this->seekStart(start);
42 cycrow 752
	}
1 cycrow 753
 
52 cycrow 754
	return this->write(d, size);
755
}
1 cycrow 756
 
52 cycrow 757
Utils::String CFileIO::readEndOfLine()
758
{
759
	if ( !this->isOpened() ) {
82 cycrow 760
		if ( !startRead() )	return "";
1 cycrow 761
	}
762
 
52 cycrow 763
	Utils::String str;
764
	std::getline(m_fId, str, '\n');
765
	return str;
1 cycrow 766
}
767
 
57 cycrow 768
Utils::String CFileIO::baseName() const
1 cycrow 769
{
51 cycrow 770
	return m_sFile.token(".", -2);
1 cycrow 771
}
772
CyString CFileIO::GetFileExtension ()
773
{
51 cycrow 774
	if ( m_sFilename.empty() ) return NullString;
775
	return m_sFilename.token(".", -1);
1 cycrow 776
}
777
 
58 cycrow 778
Utils::String CFileIO::extension ()
779
{
780
	if ( m_sFilename.empty() ) return "";
781
	return m_sFilename.token(".", -1);
782
}
783
 
1 cycrow 784
CyString CFileIO::ChangeFileExtension ( CyString ext )
785
{
51 cycrow 786
	if ( m_sFilename.empty() )
1 cycrow 787
		return NullString;
51 cycrow 788
 
789
	return m_sFilename.tokens(".", 1, -2) + "." + ext.ToString();
1 cycrow 790
}
791
 
52 cycrow 792
bool CFileIO::Remove(const Utils::String &rem)
1 cycrow 793
{
52 cycrow 794
	//if ( !Exists() ) return false;
795
	return (std::remove(rem) == 0) ? true : false;
796
}
1 cycrow 797
 
52 cycrow 798
bool CFileIO::remove()
799
{
800
	if ( this->isOpened() ) this->close();
801
	if ( !this->exists() ) return false;
802
	if ( std::remove(m_sFilename.c_str()) == 0 ) return true;
1 cycrow 803
	return false;
804
}
805
 
806
bool CFileIO::WriteFileUTF(std::vector<CyString> *lines)
807
{
51 cycrow 808
	if ( !lines || m_sFilename.empty() )
1 cycrow 809
		return false;
810
 
811
	// we need to create the directory
812
	if ( !m_sDirIO.Exists() )
813
	{
814
		if ( !m_sDirIO.Create() )
815
			return false;
816
	}
817
 
818
#ifdef _WIN32
819
	TCHAR buf[5000];
820
	wsprintf(buf, L"%hs", m_sFilename.c_str());
821
	FILE *id = _wfopen(buf, L"wt+,ccs=UTF-8");
822
	if ( !id )
823
		return false;
824
 
825
	// write the rest
826
	for ( int i = 0; i < (int)lines->size(); i++ )
827
	{
828
		CyString l = lines->at(i);
829
		if ( l.IsIn('\n') )
830
		{
831
			int max;
832
			CyString *strs = l.SplitToken("\n", &max);
833
			if ( strs && max )
834
			{
835
				for ( int i = 0; i < max; i++ )
836
				{
837
					CyString line = strs[i];
838
					line += "\n";
839
					int size = wsprintf(buf, L"%hs", line.c_str());
840
					fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
841
				}
842
 
843
				CLEANSPLIT(strs, max);
844
			}
845
		}
846
		else
847
		{
848
			l += "\n";
849
			int size = wsprintf(buf, L"%hs", l.c_str());
850
			fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
851
		}
852
	}
853
 
854
	fclose(id);
855
 
856
	return true;
857
#else
858
    //TODO: write utf8 file writing function
859
    return false;
860
#endif
861
}
862
 
863
bool CFileIO::WriteFile(std::vector<CyString> *lines)
864
{
51 cycrow 865
	if ( !lines || m_sFilename.empty() )
1 cycrow 866
		return false;
867
 
868
	// we need to create the directory
869
	if ( !m_sDirIO.Exists() )
870
	{
871
		if ( !m_sDirIO.Create() )
872
			return false;
873
	}
874
 
875
 
876
	std::ofstream out(m_sFilename.c_str());
877
	if ( !out )
878
		return false;
879
 
880
	for ( int i = 0; i < (int)lines->size(); i++ )
881
	{
882
		CyString l = lines->at(i);
883
		out << l.c_str() << std::endl;
884
	}
885
 
886
	out.close();
887
 
888
	return true;
889
}
890
 
891
bool CFileIO::WriteFile(CyStringList *lines)
892
{
51 cycrow 893
	if ( !lines || m_sFilename.empty() )
1 cycrow 894
		return false;
895
 
896
	// we need to create the directory
897
	if ( !m_sDirIO.Exists() )
898
	{
899
		if ( !m_sDirIO.Create() )
900
			return false;
901
	}
902
 
903
	std::ofstream out(m_sFilename.c_str());
904
	if ( !out )
905
		return false;
906
 
907
	/*
908
	if ( utf )
909
	{
910
		unsigned char smarker[4];
911
		smarker[0] = 0xEF;
912
		smarker[1] = 0xBB;
913
		smarker[2] = 0xBF;
914
		smarker[3] = 0x00;
915
 
916
		out << smarker;
917
	}
918
*/
919
	for ( int i = 0; i < (int)lines->Count(); i++ )
920
	{
921
		CyString l = lines->StringAt(i);
922
		out << l.c_str() << std::endl;
923
	}
924
 
925
	out.close();
926
 
927
	return true;
928
}
929
 
930
bool CFileIO::Rename(CyString toFile)
931
{
932
	if ( rename(m_sFilename.c_str(), toFile.c_str()) == 0 )
933
		return true;
934
	return false;
935
}
936
 
937
CyString CFileIO::GetWindowsFilename()
938
{
939
	CyString returnString = m_sFilename;
940
	returnString = returnString.FindReplace("/", "\\");
941
	return returnString;
942
}
943
 
944
void CFileIO::SetCreationTime(time_t time)
945
{
946
#ifdef _WIN32
947
	// Note that LONGLONG is a 64-bit value
948
    LONGLONG ll;
949
 
950
	FILETIME ft;
951
    ll = Int32x32To64(time, 10000000) + 116444736000000000;
952
    ft.dwLowDateTime = (DWORD)ll;
953
    ft.dwHighDateTime = ll >> 32;
954
 
52 cycrow 955
	WCHAR    str[MAX_PATH];
956
	MultiByteToWideChar(CP_ACP, NULL, GetWindowsFilename().c_str(), -1, str, GetWindowsFilename().Length() + 1);
957
	HANDLE filename = CreateFile(str, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1 cycrow 958
	// Set the file time on the file
959
	SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&ft);
960
	// Close our handle.
961
	CloseHandle(filename);
52 cycrow 962
 
1 cycrow 963
#endif
964
}
965
 
966
time_t CFileIO::GetCreationTime()
967
{
52 cycrow 968
	return modifiedTime();
969
}
970
 
971
time_t CFileIO::modifiedTime()
972
{
1 cycrow 973
#ifdef _WIN32
52 cycrow 974
	WCHAR    str[MAX_PATH];
975
	MultiByteToWideChar(CP_ACP, NULL, GetWindowsFilename().c_str(), -1, str, GetWindowsFilename().Length() + 1);
976
 
1 cycrow 977
	WIN32_FILE_ATTRIBUTE_DATA wfad;
52 cycrow 978
	GetFileAttributesEx(str, GetFileExInfoStandard, &wfad);
1 cycrow 979
 
980
	LARGE_INTEGER date, adjust;
52 cycrow 981
	date.HighPart = wfad.ftLastWriteTime.dwHighDateTime;
982
	date.LowPart = wfad.ftLastWriteTime.dwLowDateTime;
1 cycrow 983
 
984
	// 100-nanoseconds = milliseconds * 10000
985
	adjust.QuadPart = 11644473600000 * 10000;
986
 
987
	// removes the diff between 1970 and 1601
988
	date.QuadPart -= adjust.QuadPart;
989
 
990
	// converts back from 100-nanoseconds to seconds
991
	return (time_t)(date.QuadPart / 10000000);
992
#else
993
	struct stat fileStat;
994
	if ( !stat(GetWindowsFilename().c_str(), &fileStat) )
995
		return (time_t)fileStat.st_atime;
996
#endif
997
	return 0;
998
}
58 cycrow 999
 
1000
size_t CFileIO::position()
1001
{
1002
	return m_fId.tellg();
1003
}
1004
 
1 cycrow 1005
/**
1006
 * Copys the contents of a file to another
1007
 *
1008
 * Reads and writes the files in block
1009
 */
52 cycrow 1010
bool CFileIO::copy(const Utils::String &toFile, bool keepTime)
1 cycrow 1011
{
1012
	time_t time = GetCreationTime();
1013
 
52 cycrow 1014
	CFileIO File(toFile);
1015
	if ( File.write(*this, m_lSize) ) {
86 cycrow 1016
		this->close();
1017
		File.close();
52 cycrow 1018
		if ( keepTime )	File.SetCreationTime(time);
1019
		return true;
1 cycrow 1020
	}
52 cycrow 1021
/*
1022
	std::fstream f(GetWindowsFilename().c_str(), std::fstream::in | std::fstream::binary);
1023
	f << std::noskipws;
1024
	std::istream_iterator<unsigned char> begin(f);
1025
	std::istream_iterator<unsigned char> end;
1026
 
1027
	std::fstream f2(toFile.c_str(), std::fstream::out | std::fstream::trunc | std::fstream::binary);
1028
	std::ostream_iterator<char> begin2(f2);
1029
 
1030
	std::copy(begin, end, begin2);
1031
	return f2.good();
1032
*/
1033
	return false;
1034
}
1 cycrow 1035
 
52 cycrow 1036
size_t CFileIO::fileSize() const
1037
{
1038
	return m_lSize;
1 cycrow 1039
}