Subversion Repositories spk

Rev

Rev 160 | Rev 170 | 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
 
166 cycrow 416
std::vector<Utils::String>* CFileIO::readLines() const
82 cycrow 417
{
166 cycrow 418
	if (_sFilename.empty()) return 0;
82 cycrow 419
 
166 cycrow 420
	std::vector<Utils::String>* file = new std::vector<Utils::String>;
84 cycrow 421
	Utils::String line;
82 cycrow 422
	file->clear();
166 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
}
166 cycrow 432
size_t CFileIO::readLines(std::vector<Utils::String> &to) const
433
{
434
	if (_sFilename.empty()) return 0;
83 cycrow 435
 
166 cycrow 436
	Utils::String line;
437
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
438
	while (getline(infile, line, '\n')) {
439
		to.push_back(Utils::String(line).removeChar((char)0));
440
	}
1 cycrow 441
 
166 cycrow 442
	infile.close();
443
 
444
	return to.size();
445
}
446
 
447
 
1 cycrow 448
CyStringList *CFileIO::ReadLinesStr()
449
{
160 cycrow 450
	if ( _sFilename.empty() ) return 0;
1 cycrow 451
 
452
	CyStringList *file = new CyStringList;
84 cycrow 453
	Utils::String line;
160 cycrow 454
	std::ifstream infile (_sFilename.c_str(), std::ios_base::in);
111 cycrow 455
	while (getline(infile, line, '\n')) {
84 cycrow 456
		file->PushBack(CyString(line.removeChar((char)0)));
1 cycrow 457
	}
458
 
459
	infile.close();
460
 
461
	return file;
462
}
463
 
111 cycrow 464
Utils::CStringList *CFileIO::readLinesStr()
465
{
160 cycrow 466
	if ( _sFilename.empty() ) return 0;
111 cycrow 467
 
468
	Utils::CStringList *file = new Utils::CStringList();
469
	Utils::String line;
160 cycrow 470
	std::ifstream infile (_sFilename.c_str(), std::ios_base::in);
111 cycrow 471
	while (getline(infile, line, '\n'))	{
472
		file->pushBack(line.removeChar((char)0));
473
	}
474
 
475
	infile.close();
476
 
477
	return file;
478
}
479
 
480
 
52 cycrow 481
bool CFileIO::put(const unsigned char c)
1 cycrow 482
{
52 cycrow 483
	if ( !this->isOpened() ) return false;
484
 
485
	m_fId.put(c);
486
	return !m_fId.bad();
1 cycrow 487
}
488
 
123 cycrow 489
bool CFileIO::writeSize(size_t iSize)
1 cycrow 490
{
52 cycrow 491
	if ( !this->isOpened() ) return false;
492
	if ( !this->put(static_cast<unsigned char>(iSize >> 24)) ) return false;
493
	if ( !this->put(static_cast<unsigned char>(iSize >> 16)) ) return false;
494
	if ( !this->put(static_cast<unsigned char>(iSize >> 8)) ) return false;
495
	if ( !this->put(static_cast<unsigned char>(iSize)) ) return false;
496
	m_lSize += 4;
497
	return true;
1 cycrow 498
}
499
 
52 cycrow 500
bool CFileIO::write(CFileIO &file, size_t iSize)
1 cycrow 501
{
52 cycrow 502
	if ( !this->isOpened() ) startWrite();
503
	if ( !this->isOpened() ) return false;
504
 
505
	int iMaxSize = 1000000;
506
 
507
	unsigned char *data;
508
	try {
509
		data = new unsigned char[iMaxSize + 1];
1 cycrow 510
	}
52 cycrow 511
	catch (std::exception &e) {
512
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::write(CFileIO, size_t) unable to malloc data, %d (%s)", iMaxSize + 1, e.what());
513
		return false;
514
	}
1 cycrow 515
 
52 cycrow 516
	int iSizeLeft = iSize;
517
	bool bSuccess = true;
518
	while ( iSizeLeft > 0 ) {
519
		int iDoSize = iMaxSize;
520
		if ( iDoSize > iSizeLeft ) iDoSize = iSizeLeft;
1 cycrow 521
 
52 cycrow 522
		if ( !file.read(data, iDoSize) ) bSuccess = false;
523
		if ( bSuccess && !this->write(data, iDoSize) ) bSuccess = false;
524
		if ( !bSuccess ) break;
525
		iSizeLeft -= iDoSize;
526
	}
1 cycrow 527
 
52 cycrow 528
	delete data;
1 cycrow 529
 
52 cycrow 530
	return bSuccess;
1 cycrow 531
}
532
 
123 cycrow 533
bool CFileIO::write(const char *buf, size_t iSize)
52 cycrow 534
{
535
	if ( !this->isOpened() ) startWrite();
536
	if ( !this->isOpened() ) return false;
537
	try {
538
		m_fId.write((char *)buf, iSize);
539
	}
540
	catch (std::exception &e) {
160 cycrow 541
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
52 cycrow 542
		return false;
543
	}
544
	m_lSize += iSize;
545
	return !m_fId.bad();
546
}
123 cycrow 547
bool CFileIO::write(const unsigned char *buf, size_t iSize)
52 cycrow 548
{
549
	return this->write((char *)buf, iSize);
550
}
551
 
552
bool CFileIO::write(const unsigned char *buf, ...)
553
{
554
	va_list args;
555
	va_start(args, buf);
556
	bool ret = this->_write((char *)buf, args);
557
	va_end(args);
558
	return ret;
559
}
560
bool CFileIO::_write(const char *buf, va_list args) 
561
{
562
	if ( !this->isOpened() ) return false;
563
 
564
	char buffer[10000];
565
 
566
	vsprintf (buffer, buf, args);
567
	try {
568
		int iLen = strlen(buffer);
569
		m_fId.write((char *)buffer, iLen);
570
		m_lSize += iLen;
571
	}
572
	catch (std::exception &e) {
160 cycrow 573
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
52 cycrow 574
		return false;
575
	}
576
	return !m_fId.bad();
577
}
578
 
579
bool CFileIO::write(const char *buf, ...)
580
{
581
	va_list args;
582
	va_start(args, buf);
583
	bool ret = this->_write((char *)buf, args);
584
	va_end(args);
585
	return ret;
586
}
587
 
588
void CFileIO::_seek(int iPos, int iFrom)
589
{
590
	if ( !this->isOpened() ) {
591
		if ( !this->startRead() ) return;
592
	}
593
	if ( m_bSeekP ) m_fId.seekp(iPos, iFrom);
594
	else m_fId.seekg(iPos, iFrom);
595
}
596
 
123 cycrow 597
void CFileIO::seek(size_t iPos)
52 cycrow 598
{
599
	_seek(iPos, std::ios::cur);
600
}
601
 
123 cycrow 602
void CFileIO::seekEnd(size_t iPos)
52 cycrow 603
{
53 cycrow 604
	_seek(iPos, std::ios::end);
52 cycrow 605
}
606
 
123 cycrow 607
void CFileIO::seekStart(size_t iPos)
52 cycrow 608
{
609
	_seek(iPos, std::ios::beg);
610
}
611
 
612
std::fstream &CFileIO::stream()
613
{
614
	return m_fId;
615
}
616
 
617
bool CFileIO::isOpened() const
618
{
619
	if ( m_fId.is_open() )
620
		return true;
621
	return false;
622
}
623
 
624
void CFileIO::close()
625
{
626
	if ( this->isOpened() ) m_fId.close();
84 cycrow 627
	this->_readFileSize();
52 cycrow 628
}
629
 
56 cycrow 630
int CFileIO::readSize()
1 cycrow 631
{
56 cycrow 632
	unsigned char size[4];
633
	if ( this->read(size, 4) ) return (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
634
	return 0;
635
}
636
 
637
bool CFileIO::atEnd() const
638
{
52 cycrow 639
	if ( !this->isOpened() ) return true;
51 cycrow 640
	if ( m_fId.eof() )		 return true;
1 cycrow 641
	return false;
642
}
643
 
82 cycrow 644
bool CFileIO::appendFile ( const Utils::String &filename )
645
{
646
	std::fstream fromFile(filename, _in());
647
	if ( !fromFile.is_open() ) return false;
648
 
160 cycrow 649
	std::fstream toFile(_sFilename, _append());
82 cycrow 650
	if ( !toFile.is_open() ) {
651
		fromFile.close();
652
		return false;
653
	}
654
 
655
	// move to the end of the file
656
	toFile.seekg(0, std::ios::end);
657
 
658
	// get size of file
659
	fromFile.seekg(0, std::ios::end);
121 cycrow 660
	size_t size = static_cast<size_t>(fromFile.tellg());
82 cycrow 661
	fromFile.seekg(0, std::ios::beg);
662
 
663
	char data[500000];
664
	while ( size > 0 )
665
	{
666
		size_t read = 500000;
667
		if ( read > size )
668
			read = size;
669
 
670
		size -= read;
671
 
672
		fromFile.read(data, read);
673
		toFile.write(data, read);
674
	}
675
 
676
	fromFile.close();
677
	toFile.close();
678
	return true;
679
}
680
 
1 cycrow 681
 
682
bool CFileIO::AppendData ( const char *d, size_t size )
683
{
160 cycrow 684
	std::ofstream File(_sFilename, _append());
51 cycrow 685
	if ( !File.is_open() ) return false;
1 cycrow 686
 
687
	// move to the end of the file
58 cycrow 688
	//File.seekg(0, std::ios::end);
1 cycrow 689
 
690
	char *pos = (char *)d;
51 cycrow 691
	while ( size > 0 ) {
1 cycrow 692
		size_t read = 500000;
51 cycrow 693
		if ( read > size ) read = size;
1 cycrow 694
 
695
		size -= read;
696
 
51 cycrow 697
		try {
698
			File.write(pos, read);
699
		} 
700
		catch(std::exception &e) {
160 cycrow 701
			CLog::logf(CLog::Log_IO, 2, "CFileIO::AppendData() unable to write data to file: %s (%s)", _sFilename.c_str(), e.what());
51 cycrow 702
			File.close();
703
			return false;
704
		}
1 cycrow 705
		pos += read;
706
	}
707
 
51 cycrow 708
	File.close();
1 cycrow 709
	return true;
710
}
711
 
712
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
713
{
52 cycrow 714
	if ( start > m_lSize ) return false;
715
	if ( m_lSize <= 0 ) {
716
		if ( !this->startWrite() ) return false;
1 cycrow 717
	}
52 cycrow 718
	else if ( start == m_lSize ) {
719
		if ( !this->startAppend() ) return false;
42 cycrow 720
	}
721
	else {
52 cycrow 722
		if ( !this->startModify() ) return false;
723
		this->seekStart(start);
42 cycrow 724
	}
1 cycrow 725
 
52 cycrow 726
	return this->write(d, size);
727
}
1 cycrow 728
 
52 cycrow 729
Utils::String CFileIO::readEndOfLine()
730
{
731
	if ( !this->isOpened() ) {
82 cycrow 732
		if ( !startRead() )	return "";
1 cycrow 733
	}
734
 
52 cycrow 735
	Utils::String str;
736
	std::getline(m_fId, str, '\n');
737
	return str;
1 cycrow 738
}
739
 
57 cycrow 740
Utils::String CFileIO::baseName() const
1 cycrow 741
{
160 cycrow 742
	return _sFile.tokens(".", 1, -2);
1 cycrow 743
}
744
 
160 cycrow 745
Utils::String CFileIO::extension () const
58 cycrow 746
{
160 cycrow 747
	if ( _sFilename.empty() ) return Utils::String::Null();
748
	return _sFilename.token(".", -1);
58 cycrow 749
}
750
 
124 cycrow 751
Utils::String CFileIO::changeFileExtension(const Utils::String &ext) const
752
{
160 cycrow 753
	if ( _sFilename.empty() )
124 cycrow 754
		return Utils::String::Null();
51 cycrow 755
 
160 cycrow 756
	return _sFilename.tokens(".", 1, -2) + "." + ext;
1 cycrow 757
}
758
 
52 cycrow 759
bool CFileIO::Remove(const Utils::String &rem)
1 cycrow 760
{
52 cycrow 761
	//if ( !Exists() ) return false;
762
	return (std::remove(rem) == 0) ? true : false;
763
}
1 cycrow 764
 
52 cycrow 765
bool CFileIO::remove()
766
{
767
	if ( this->isOpened() ) this->close();
768
	if ( !this->exists() ) return false;
160 cycrow 769
	if ( std::remove(_sFilename.c_str()) == 0 ) return true;
1 cycrow 770
	return false;
771
}
772
 
121 cycrow 773
bool CFileIO::writeFileUTF(std::vector<Utils::String> *lines)
774
{
160 cycrow 775
	if (!lines || _sFilename.empty())
121 cycrow 776
		return false;
777
 
778
	// we need to create the directory
160 cycrow 779
	if (!_sDirIO.exists())
121 cycrow 780
	{
160 cycrow 781
		if (!_sDirIO.create())
121 cycrow 782
			return false;
783
	}
784
 
785
#ifdef _WIN32
786
	TCHAR buf[5000];
160 cycrow 787
	wsprintf(buf, L"%hs", _sFilename.c_str());
121 cycrow 788
	FILE *id = _wfopen(buf, L"wt+,ccs=UTF-8");
789
	if (!id)
790
		return false;
791
 
792
	// write the rest
793
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
794
	{
795
		Utils::String l = (*itr);
796
		if (l.isin('\n'))
797
		{
798
			int max;
799
			Utils::String *strs = l.tokenise("\n", &max);
800
			if (strs && max)
801
			{
802
				for (int i = 0; i < max; i++)
803
				{
804
					Utils::String line = strs[i];
805
					line += "\n";
806
					int size = wsprintf(buf, L"%hs", line.c_str());
807
					fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
808
				}
809
 
810
				CLEANSPLIT(strs, max);
811
			}
812
		}
813
		else
814
		{
815
			l += "\n";
816
			int size = wsprintf(buf, L"%hs", l.c_str());
817
			fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
818
		}
819
	}
820
 
821
	fclose(id);
822
 
823
	return true;
824
#else
825
	//TODO: write utf8 file writing function
826
	return false;
827
#endif
828
}
829
 
1 cycrow 830
bool CFileIO::WriteFile(CyStringList *lines)
831
{
160 cycrow 832
	if ( !lines || _sFilename.empty() )
1 cycrow 833
		return false;
834
 
835
	// we need to create the directory
160 cycrow 836
	if ( !_sDirIO.exists() )
1 cycrow 837
	{
160 cycrow 838
		if ( !_sDirIO.create() )
1 cycrow 839
			return false;
840
	}
841
 
160 cycrow 842
	std::ofstream out(_sFilename.c_str());
1 cycrow 843
	if ( !out )
844
		return false;
845
 
846
	/*
847
	if ( utf )
848
	{
849
		unsigned char smarker[4];
850
		smarker[0] = 0xEF;
851
		smarker[1] = 0xBB;
852
		smarker[2] = 0xBF;
853
		smarker[3] = 0x00;
854
 
855
		out << smarker;
856
	}
857
*/
858
	for ( int i = 0; i < (int)lines->Count(); i++ )
859
	{
860
		CyString l = lines->StringAt(i);
861
		out << l.c_str() << std::endl;
862
	}
863
 
864
	out.close();
865
 
866
	return true;
867
}
868
 
121 cycrow 869
bool CFileIO::writeFile(std::vector<Utils::String> *lines)
870
{
160 cycrow 871
	if (!lines || _sFilename.empty())
121 cycrow 872
		return false;
873
 
874
	// we need to create the directory
160 cycrow 875
	if (!_sDirIO.exists())
121 cycrow 876
	{
160 cycrow 877
		if (!_sDirIO.create())
121 cycrow 878
			return false;
879
	}
880
 
881
 
160 cycrow 882
	std::ofstream out(_sFilename.c_str());
121 cycrow 883
	if (!out)
884
		return false;
885
 
886
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
887
		out << (*itr).c_str() << std::endl;
888
 
889
	out.close();
890
 
891
	return true;
892
}
893
 
894
bool CFileIO::writeFile(Utils::CStringList *lines)
895
{
160 cycrow 896
	if (!lines || _sFilename.empty())
121 cycrow 897
		return false;
898
 
899
	// we need to create the directory
160 cycrow 900
	if (!_sDirIO.exists())
121 cycrow 901
	{
160 cycrow 902
		if (!_sDirIO.create())
121 cycrow 903
			return false;
904
	}
905
 
160 cycrow 906
	std::ofstream out(_sFilename.c_str());
121 cycrow 907
	if (!out)
908
		return false;
909
 
910
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
911
		out << (*itr)->str.c_str() << std::endl;
912
 
913
	out.close();
914
 
915
	return true;
916
}
917
 
158 cycrow 918
bool CFileIO::Rename(const Utils::String &toFile)
1 cycrow 919
{
160 cycrow 920
	if ( rename(_sFilename.c_str(), toFile.c_str()) == 0 )
1 cycrow 921
		return true;
922
	return false;
923
}
924
 
160 cycrow 925
Utils::String CFileIO::getWindowsFilename() const
1 cycrow 926
{
160 cycrow 927
	Utils::String returnString = _sFilename;
928
	returnString = returnString.findReplace("/", "\\");
1 cycrow 929
	return returnString;
930
}
931
 
160 cycrow 932
void CFileIO::setCreationTime(time_t time)
1 cycrow 933
{
934
#ifdef _WIN32
935
	// Note that LONGLONG is a 64-bit value
936
    LONGLONG ll;
937
 
938
	FILETIME ft;
939
    ll = Int32x32To64(time, 10000000) + 116444736000000000;
940
    ft.dwLowDateTime = (DWORD)ll;
941
    ft.dwHighDateTime = ll >> 32;
942
 
160 cycrow 943
	Utils::String windowsFilename = getWindowsFilename();
944
 
52 cycrow 945
	WCHAR    str[MAX_PATH];
160 cycrow 946
	MultiByteToWideChar(CP_ACP, NULL, windowsFilename.c_str(), -1, str, windowsFilename.length() + 1);
52 cycrow 947
	HANDLE filename = CreateFile(str, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1 cycrow 948
	// Set the file time on the file
949
	SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&ft);
950
	// Close our handle.
951
	CloseHandle(filename);
52 cycrow 952
 
1 cycrow 953
#endif
954
}
955
 
160 cycrow 956
time_t CFileIO::creationTime() const
1 cycrow 957
{
52 cycrow 958
	return modifiedTime();
959
}
960
 
160 cycrow 961
time_t CFileIO::modifiedTime() const
52 cycrow 962
{
1 cycrow 963
#ifdef _WIN32
52 cycrow 964
	WCHAR    str[MAX_PATH];
160 cycrow 965
	Utils::String windowsFilename = getWindowsFilename();
966
	MultiByteToWideChar(CP_ACP, NULL, windowsFilename.c_str(), windowsFilename.length() + 1, str, MAX_PATH);
52 cycrow 967
 
1 cycrow 968
	WIN32_FILE_ATTRIBUTE_DATA wfad;
52 cycrow 969
	GetFileAttributesEx(str, GetFileExInfoStandard, &wfad);
1 cycrow 970
 
971
	LARGE_INTEGER date, adjust;
52 cycrow 972
	date.HighPart = wfad.ftLastWriteTime.dwHighDateTime;
973
	date.LowPart = wfad.ftLastWriteTime.dwLowDateTime;
1 cycrow 974
 
975
	// 100-nanoseconds = milliseconds * 10000
976
	adjust.QuadPart = 11644473600000 * 10000;
977
 
978
	// removes the diff between 1970 and 1601
979
	date.QuadPart -= adjust.QuadPart;
980
 
981
	// converts back from 100-nanoseconds to seconds
982
	return (time_t)(date.QuadPart / 10000000);
983
#else
984
	struct stat fileStat;
985
	if ( !stat(GetWindowsFilename().c_str(), &fileStat) )
986
		return (time_t)fileStat.st_atime;
987
#endif
988
	return 0;
989
}
58 cycrow 990
 
991
size_t CFileIO::position()
992
{
121 cycrow 993
	return static_cast<size_t>(m_fId.tellg());
58 cycrow 994
}
995
 
1 cycrow 996
/**
997
 * Copys the contents of a file to another
998
 *
999
 * Reads and writes the files in block
1000
 */
52 cycrow 1001
bool CFileIO::copy(const Utils::String &toFile, bool keepTime)
1 cycrow 1002
{
160 cycrow 1003
	time_t time = creationTime();
1 cycrow 1004
 
52 cycrow 1005
	CFileIO File(toFile);
1006
	if ( File.write(*this, m_lSize) ) {
86 cycrow 1007
		this->close();
1008
		File.close();
160 cycrow 1009
		if ( keepTime )	File.setCreationTime(time);
52 cycrow 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
}
160 cycrow 1031
 
1032
 
1033
void CFileIO::_updateFilename()
1034
{
1035
	if (_sFilename.empty()) _sExt = Utils::String::Null();
1036
	_sExt = _sFilename.token(".", -1);
1037
}