Subversion Repositories spk

Rev

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