Subversion Repositories spk

Rev

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