Subversion Repositories spk

Rev

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