Subversion Repositories spk

Rev

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