Subversion Repositories spk

Rev

Rev 223 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 223 Rev 227
Line 3... Line 3...
3
#ifdef _WIN32
3
#ifdef _WIN32
4
#include <windows.h>
4
#include <windows.h>
5
#endif
5
#endif
6
 
6
 
7
#include <locale>
7
#include <locale>
-
 
8
#include <codecvt>
8
 
9
 
9
#include "DirIO.h"
10
#include "DirIO.h"
10
#include "File.h"
11
#include "File.h"
11
#include "Logging/Log.h"
12
#include "Logging/Log.h"
12
#include "Packages.h"
13
#include "Packages.h"
Line 73... Line 74...
73
	unsigned char *data;
74
	unsigned char *data;
74
	try {
75
	try {
75
		data = new unsigned char[iAmount + 1];
76
		data = new unsigned char[iAmount + 1];
76
	}
77
	}
77
	catch (std::exception &e) {
78
	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
		CLog::logf(CLog::Log_IO, 2, L"ERROR: CFileIO::read(size_t) unable to malloc data, %d (%hs)", iAmount + 1, e.what());
79
		return NULL;
80
		return NULL;
80
	}
81
	}
81
 
82
 
82
	if ( !read(data, iAmount, true) ) {
83
	if ( !read(data, iAmount, true) ) {
83
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to read data from file, %s/%d", _sFilename.c_str(), iAmount);
84
		CLog::logf(CLog::Log_IO, 2, L"ERROR: CFileIO::read(size_t) unable to read data from file, %s/%d", _sFilename.c_str(), iAmount);
84
		delete[] data;
85
		delete[] data;
85
		return NULL;
86
		return NULL;
86
	}
87
	}
87
 
88
 
88
	return data;
89
	return data;
Line 97... Line 98...
97
	unsigned char *data;
98
	unsigned char *data;
98
	try {
99
	try {
99
		data = new unsigned char[m_lSize + 1];
100
		data = new unsigned char[m_lSize + 1];
100
	}
101
	}
101
	catch (std::exception &e) {
102
	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
		CLog::logf(CLog::Log_IO, 2, L"ERROR: CFileIO::readAll() unable to malloc data, %d (%hs)", m_lSize + 1, e.what());
103
		return NULL;
104
		return NULL;
104
	}
105
	}
105
 
106
 
106
	if ( !read(data, m_lSize, true) ) {
107
	if ( !read(data, m_lSize, true) ) {
107
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to read data from file, %s/%d", _sFilename.c_str(), m_lSize);
108
		CLog::logf(CLog::Log_IO, 2, L"ERROR: CFileIO::readAll() unable to read data from file, %s/%d", _sFilename.c_str(), m_lSize);
108
		delete[] data;
109
		delete[] data;
109
		return NULL;
110
		return NULL;
110
	}
111
	}
111
 
112
 
112
	if ( pSize ) (*pSize) = m_lSize;
113
	if ( pSize ) (*pSize) = m_lSize;
Line 121... Line 122...
121
	if (iSize > m_lSize) iSize = m_lSize;
122
	if (iSize > m_lSize) iSize = m_lSize;
122
	try {
123
	try {
123
		m_fId.read((char*)buf, iSize);
124
		m_fId.read((char*)buf, iSize);
124
	}
125
	}
125
	catch (std::exception& e) {
126
	catch (std::exception& e) {
126
		CLog::logf(CLog::Log_IO, 3, "ERROR: CFileIO::read() unable to read from file: %s (%s)", _sFilename.c_str(), e.what());
127
		CLog::logf(CLog::Log_IO, 3, L"ERROR: CFileIO::read() unable to read from file: %s (%hs)", _sFilename.c_str(), e.what());
127
		return false;
128
		return false;
128
	}
129
	}
129
 
130
 
130
	if (bEndChar) buf[iSize] = '\0';
131
	if (bEndChar) buf[iSize] = '\0';
131
 
132
 
Line 139... Line 140...
139
	if (iSize > m_lSize) iSize = m_lSize;
140
	if (iSize > m_lSize) iSize = m_lSize;
140
	try {
141
	try {
141
		m_fId.read((char*)buf, iSize);
142
		m_fId.read((char*)buf, iSize);
142
	}
143
	}
143
	catch (std::exception& e) {
144
	catch (std::exception& e) {
144
		CLog::logf(CLog::Log_IO, 3, "ERROR: CFileIO::read() unable to read from file: %s (%s)", _sFilename.c_str(), e.what());
145
		CLog::logf(CLog::Log_IO, 3, L"ERROR: CFileIO::read() unable to read from file: %s (%hs)", _sFilename.c_str(), e.what());
145
		return false;
146
		return false;
146
	}
147
	}
147
 
148
 
148
	if (bEndChar) buf[iSize] = '\0';
149
	if (bEndChar) buf[iSize] = '\0';
149
 
150
 
Line 181... Line 182...
181
			File.read(data, size);
182
			File.read(data, size);
182
			writeFile.write(data, size);
183
			writeFile.write(data, size);
183
			delete []data;
184
			delete []data;
184
		}
185
		}
185
		catch(std::exception &e) {
186
		catch(std::exception &e) {
186
			CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", _sFilename.c_str(), e.what());
187
			CLog::logf(CLog::Log_IO, 2, L"CFileIO::WritePartFilea() unable to read data from file: %s (%hs)", _sFilename.c_str(), e.what());
187
			return false;
188
			return false;
188
		}
189
		}
189
 
190
 
190
		size_t datasize = offsets[off++];
191
		size_t datasize = offsets[off++];
191
		File.seekg(datasize, std::ios::beg);
192
		File.seekg(datasize, std::ios::beg);
Line 201... Line 202...
201
			try {
202
			try {
202
				File.read(data, amountLeft);
203
				File.read(data, amountLeft);
203
				writeFile.write(data, amountLeft);
204
				writeFile.write(data, amountLeft);
204
			}
205
			}
205
			catch(std::exception &e) {
206
			catch(std::exception &e) {
206
				CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", _sFilename.c_str(), e.what());
207
				CLog::logf(CLog::Log_IO, 2, L"CFileIO::WritePartFilea() unable to read data from file: %s (%hs)", _sFilename.c_str(), e.what());
207
				return false;
208
				return false;
208
			}
209
			}
209
 
210
 
210
			remainingSize -= amountLeft;
211
			remainingSize -= amountLeft;
211
			amountLeft = 1000000;
212
			amountLeft = 1000000;
Line 330... Line 331...
330
	char *data;
331
	char *data;
331
	try {
332
	try {
332
		data = new char[m_lSize + 1];
333
		data = new char[m_lSize + 1];
333
	}
334
	}
334
	catch (std::exception &e) {
335
	catch (std::exception &e) {
335
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to malloc storage, %d (%s)", m_lSize + 1, e.what());
336
		CLog::logf(CLog::Log_IO, 2, L"CFileIO::ReadToData() unable to malloc storage, %d (%hs)", m_lSize + 1, e.what());
336
		file.close();
337
		file.close();
337
		return NULL;
338
		return NULL;
338
	}
339
	}
339
 
340
 
340
	try {
341
	try {
341
		file.read((char *)data, m_lSize);
342
		file.read((char *)data, m_lSize);
342
	}
343
	}
343
	catch (std::exception &e) {
344
	catch (std::exception &e) {
344
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file (%s)", e.what());
345
		CLog::logf(CLog::Log_IO, 2, L"CFileIO::ReadToData() unable to read data from file (%hs)", e.what());
345
		file.close();
346
		file.close();
346
		return NULL;
347
		return NULL;
347
	}
348
	}
348
 
349
 
349
	*size = m_lSize;
350
	*size = m_lSize;
Line 355... Line 356...
355
bool CFileIO::WriteData ( const char *data, size_t size )
356
bool CFileIO::WriteData ( const char *data, size_t size )
356
{
357
{
357
	if ( NoFile() )
358
	if ( NoFile() )
358
		return false;
359
		return false;
359
 
360
 
360
	std::fstream File(_fileData(), _out());
361
	std::fstream File(_sFilename.c_str(), _out());
361
	if ( !File.is_open() ) return false;
362
	if ( !File.is_open() ) return false;
362
 
363
 
363
	bool ret = true;
364
	bool ret = true;
364
	try {
365
	try {
365
		File.write(data, size);
366
		File.write(data, size);
366
	}
367
	}
367
	catch (std::exception &e) {
368
	catch (std::exception &e) {
368
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file: %s (%s)", _sFilename.c_str(), e.what());
369
		CLog::logf(CLog::Log_IO, 2, L"CFileIO::ReadToData() unable to read data from file: %s (%hs)", _sFilename.c_str(), e.what());
369
		ret = false;
370
		ret = false;
370
	}
371
	}
371
 
372
 
372
	File.close();
373
	File.close();
373
 
374
 
Line 386... Line 387...
386
	bool bRet = File.good();
387
	bool bRet = File.good();
387
	File.close();
388
	File.close();
388
	return bRet;
389
	return bRet;
389
}
390
}
390
 
391
 
391
bool CFileIO::ExistsOld () const
-
 
392
{
-
 
393
	return this->exists();
-
 
394
}
-
 
395
bool CFileIO::exists () const
392
bool CFileIO::exists () const
396
{
393
{
397
	if ( this->isOpened() ) return true;
394
	if ( this->isOpened() ) return true;
398
	std::fstream File(_sFilename, _in());
395
	std::fstream File(_sFilename, _in());
399
	bool bRet = File.good();
396
	bool bRet = File.good();
Line 407... Line 404...
407
}
404
}
408
 
405
 
409
bool CFileIO::startWrite()
406
bool CFileIO::startWrite()
410
{
407
{
411
	return _start(_out(), true);
408
	return _start(_out(), true);
412
}
409
}
413
 
410
 
414
bool CFileIO::startModify()
411
bool CFileIO::startModify()
415
{
412
{
416
	return _start(_inout(), true);
413
	return _start(_inout(), true);
417
}
414
}
418
 
415
 
419
bool CFileIO::startAppend()
416
bool CFileIO::startAppend()
420
{
417
{
421
	return _start(_append(), false);
418
	return _start(_append(), false);
422
}
419
}
423
 
420
 
Line 425... Line 422...
425
{
422
{
426
	if ( !_sFilename.empty() ) {
423
	if ( !_sFilename.empty() ) {
427
		if ( this->isOpened() ) this->close();
424
		if ( this->isOpened() ) this->close();
428
 
425
 
429
		m_fId.open(_sFilename, iFlags);
426
		m_fId.open(_sFilename, iFlags);
430
	}
427
	}
431
	m_bSeekP = bSeekP;
428
	m_bSeekP = bSeekP;
432
	return m_fId.is_open();
429
	return m_fId.is_open();
433
}
430
}
434
 
431
 
435
size_t CFileIO::readLines(std::vector<Utils::String>& to) const
432
size_t CFileIO::readLines(std::vector<Utils::String>& to) const
Line 438... Line 435...
438
 
435
 
439
	Utils::String line;
436
	Utils::String line;
440
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
437
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
441
	while (getline(infile, line, '\n')) {
438
	while (getline(infile, line, '\n')) {
442
		to.push_back(Utils::String(line).removeChar((char)0));
439
		to.push_back(Utils::String(line).removeChar((char)0));
443
	}
440
	}
444
 
441
 
445
	infile.close();
442
	infile.close();
446
 
443
 
447
	return to.size();
444
	return to.size();
448
}
445
}
449
 
446
 
450
size_t CFileIO::readLines(std::vector<Utils::WString>& to) const
447
size_t CFileIO::readLines(std::vector<Utils::WString>& to) const
451
{
448
{
452
	if (_sFilename.empty()) return 0;
449
	if (_sFilename.empty()) return 0;
453
 
450
 
454
	std::wstring line;
451
	std::wstring line;
455
	std::wifstream infile(_sFilename.c_str(), std::ios_base::in);
452
	std::wifstream infile(_sFilename.c_str(), std::ios_base::in);
-
 
453
 
-
 
454
	if (getline(infile, line, L'\n'))
-
 
455
	{
-
 
456
		if (line.length() > 3)
-
 
457
		{
-
 
458
			// check for UTF-8 header
-
 
459
			if (line[0] == 239 && line[1] == 187 && line[2] == 191)
-
 
460
			{
-
 
461
				infile.close();
-
 
462
				return readLinesUTF(to);
-
 
463
			}
-
 
464
			to.push_back(Utils::WString(line).remove((char)0));
-
 
465
		}
456
	while (getline(infile, line, L'\n')) {
466
		while (getline(infile, line, L'\n')) {
457
		to.push_back(Utils::WString(line).remove((char)0));
467
			to.push_back(Utils::WString(line).remove((char)0));
-
 
468
		}
458
	}
469
	}
-
 
470
 
459
 
471
 
460
	infile.close();
472
	infile.close();
461
 
473
 
462
	return to.size();
474
	return to.size();
463
}
475
}
-
 
476
size_t CFileIO::readLinesUTF(std::vector<Utils::WString>& to) const
-
 
477
{
-
 
478
	if (_sFilename.empty()) return 0;
-
 
479
 
-
 
480
	std::wstring line;
-
 
481
	std::wifstream infile(_sFilename.c_str(), std::ios_base::in);
-
 
482
	infile.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
464
 
483
 
-
 
484
	if (getline(infile, line, L'\n'))
-
 
485
	{
-
 
486
		if (line.length() > 3)
-
 
487
		{
-
 
488
			// check for UTF-8 header
-
 
489
			if (line[0] == 239 && line[1] == 187 && line[2] == 191)
-
 
490
				line = line.erase(0, 3);
-
 
491
			to.push_back(Utils::WString(line).remove((char)0).remove(65279));
-
 
492
		}
-
 
493
		while (getline(infile, line, L'\n')) {
-
 
494
			to.push_back(Utils::WString(line).remove((char)0).remove(65279));
-
 
495
		}
-
 
496
	}
-
 
497
 
-
 
498
 
-
 
499
	infile.close();
-
 
500
 
-
 
501
	return to.size();
-
 
502
}
-
 
503
 
465
size_t CFileIO::readLines(Utils::WStringList& to) const
504
size_t CFileIO::readLines(Utils::WStringList& to) const
466
{
505
{
467
	if (_sFilename.empty()) return 0;
506
	if (_sFilename.empty()) return 0;
468
 
507
 
469
	std::wstring line;
508
	std::wstring line;
470
	std::wifstream infile(_sFilename.c_str(), std::ios_base::in);
509
	std::wifstream infile(_sFilename.c_str(), std::ios_base::in);
Line 495... Line 534...
495
	m_lSize += 4;
534
	m_lSize += 4;
496
	return true;
535
	return true;
497
}
536
}
498
 
537
 
499
bool CFileIO::write(CFileIO &file, size_t iSize)
538
bool CFileIO::write(CFileIO &file, size_t iSize)
500
{
539
{
501
	if ( !this->isOpened() ) startWrite();
540
	if ( !this->isOpened() ) startWrite();
502
	if ( !this->isOpened() ) return false;
541
	if ( !this->isOpened() ) return false;
503
 
542
 
504
	int iMaxSize = 1000000;
543
	int iMaxSize = 1000000;
505
 
544
 
506
	unsigned char *data;
545
	unsigned char *data;
507
	try {
546
	try {
508
		data = new unsigned char[iMaxSize + 1];
547
		data = new unsigned char[iMaxSize + 1];
509
	}
548
	}
510
	catch (std::exception &e) {
549
	catch (std::exception &e) {
511
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::write(CFileIO, size_t) unable to malloc data, %d (%s)", iMaxSize + 1, e.what());
550
		CLog::logf(CLog::Log_IO, 2, L"ERROR: CFileIO::write(CFileIO, size_t) unable to malloc data, %d (%hs)", iMaxSize + 1, e.what());
512
		return false;
551
		return false;
513
	}
552
	}
514
 
553
 
515
	int iSizeLeft = iSize;
554
	int iSizeLeft = iSize;
516
	bool bSuccess = true;
555
	bool bSuccess = true;
517
	while ( iSizeLeft > 0 ) {
556
	while ( iSizeLeft > 0 ) {
518
		int iDoSize = iMaxSize;
557
		int iDoSize = iMaxSize;
519
		if ( iDoSize > iSizeLeft ) iDoSize = iSizeLeft;
558
		if ( iDoSize > iSizeLeft ) iDoSize = iSizeLeft;
520
 
559
 
521
		if ( !file.read(data, iDoSize) ) bSuccess = false;
560
		if ( !file.read(data, iDoSize) ) bSuccess = false;
522
		if ( bSuccess && !this->write(data, iDoSize) ) bSuccess = false;
561
		if ( bSuccess && !this->write(data, iDoSize) ) bSuccess = false;
523
		if ( !bSuccess ) break;
562
		if ( !bSuccess ) break;
524
		iSizeLeft -= iDoSize;
563
		iSizeLeft -= iDoSize;
525
	}
564
	}
526
 
565
 
527
	delete data;
566
	delete data;
528
 
567
 
529
	return bSuccess;
568
	return bSuccess;
530
}
569
}
531
 
570
 
532
bool CFileIO::write(const char *buf, size_t iSize)
571
bool CFileIO::write(const char *buf, size_t iSize)
Line 535... Line 574...
535
	if ( !this->isOpened() ) return false;
574
	if ( !this->isOpened() ) return false;
536
	try {
575
	try {
537
		m_fId.write((char*)buf, iSize);
576
		m_fId.write((char*)buf, iSize);
538
	}
577
	}
539
	catch (std::exception &e) {
578
	catch (std::exception &e) {
540
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
579
		CLog::logf(CLog::Log_IO, 2, L"CFileIO::write() unable to write to file: %s (%hs)", _sFilename.c_str(), e.what());
541
		return false;
580
		return false;
542
	}
581
	}
543
	m_lSize += iSize;
582
	m_lSize += iSize;
544
	return !m_fId.bad();
583
	return !m_fId.bad();
545
}
584
}
546
bool CFileIO::write(const unsigned char *buf, size_t iSize)
585
bool CFileIO::write(const unsigned char *buf, size_t iSize)
547
{
586
{
548
	return this->write((char *)buf, iSize);
587
	return this->write((char *)buf, iSize);
549
}
588
}
550
 
589
 
551
bool CFileIO::write(const unsigned char *buf, ...)
590
bool CFileIO::write(const unsigned char *buf, ...)
552
{
591
{
553
	va_list args;
592
	va_list args;
554
	va_start(args, buf);
593
	va_start(args, buf);
555
	bool ret = this->_write((char *)buf, args);
594
	bool ret = this->_write((char *)buf, args);
556
	va_end(args);
595
	va_end(args);
557
	return ret;
596
	return ret;
558
}
597
}
559
bool CFileIO::_write(const char* buf, va_list args)
598
bool CFileIO::_write(const char* buf, va_list args)
560
{
599
{
561
	if (!this->isOpened()) return false;
600
	if (!this->isOpened()) return false;
562
 
601
 
563
	char buffer[10000];
602
	char buffer[10000];
564
 
603
 
565
	vsprintf(buffer, buf, args);
604
	vsprintf(buffer, buf, args);
566
	try {
605
	try {
567
		int iLen = strlen(buffer);
606
		int iLen = strlen(buffer);
568
		m_fId.write((char*)buffer, iLen);
607
		m_fId.write((char*)buffer, iLen);
569
		m_lSize += iLen;
608
		m_lSize += iLen;
570
	}
609
	}
571
	catch (std::exception& e) {
610
	catch (std::exception& e) {
572
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
611
		CLog::logf(CLog::Log_IO, 2, L"CFileIO::write() unable to write to file: %s (%hs)", _sFilename.c_str(), e.what());
573
		return false;
612
		return false;
574
	}
613
	}
575
	return !m_fId.bad();
614
	return !m_fId.bad();
576
}
615
}
577
bool CFileIO::_write(const wchar_t* buf, va_list args)
616
bool CFileIO::_write(const wchar_t* buf, va_list args)
Line 589... Line 628...
589
		size_t iLen = wcslen(buffer);
628
		size_t iLen = wcslen(buffer);
590
		m_fId.write((char*)buffer, iLen);
629
		m_fId.write((char*)buffer, iLen);
591
		m_lSize += iLen;
630
		m_lSize += iLen;
592
	}
631
	}
593
	catch (std::exception& e) {
632
	catch (std::exception& e) {
594
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %ls (%s)", _sFilename.c_str(), e.what());
633
		CLog::logf(CLog::Log_IO, 2, L"CFileIO::write() unable to write to file: %ls (%hs)", _sFilename.c_str(), e.what());
595
		return false;
634
		return false;
596
	}
635
	}
597
	return !m_fId.bad();
636
	return !m_fId.bad();
598
}
637
}
599
 
638
 
Line 668... Line 707...
668
	if ( !fromFile.is_open() ) return false;
707
	if ( !fromFile.is_open() ) return false;
669
 
708
 
670
	std::fstream toFile(_sFilename, _append());
709
	std::fstream toFile(_sFilename, _append());
671
	if ( !toFile.is_open() ) {
710
	if ( !toFile.is_open() ) {
672
		fromFile.close();
711
		fromFile.close();
673
		return false;
712
		return false;
674
	}
713
	}
675
 
714
 
676
	// move to the end of the file
715
	// move to the end of the file
677
	toFile.seekg(0, std::ios::end);
716
	toFile.seekg(0, std::ios::end);
678
 
717
 
679
	// get size of file
718
	// get size of file
680
	fromFile.seekg(0, std::ios::end);
719
	fromFile.seekg(0, std::ios::end);
681
	size_t size = static_cast<size_t>(fromFile.tellg());
720
	size_t size = static_cast<size_t>(fromFile.tellg());
Line 717... Line 756...
717
 
756
 
718
		try {
757
		try {
719
			File.write(pos, read);
758
			File.write(pos, read);
720
		} 
759
		} 
721
		catch(std::exception &e) {
760
		catch(std::exception &e) {
722
			CLog::logf(CLog::Log_IO, 2, "CFileIO::AppendData() unable to write data to file: %s (%s)", _sFilename.c_str(), e.what());
761
			CLog::logf(CLog::Log_IO, 2, L"CFileIO::AppendData() unable to write data to file: %s (%hs)", _sFilename.c_str(), e.what());
723
			File.close();
762
			File.close();
724
			return false;
763
			return false;
725
		}
764
		}
726
		pos += read;
765
		pos += read;
727
	}
766
	}
Line 800... Line 839...
800
#endif
839
#endif
801
}
840
}
802
 
841
 
803
bool CFileIO::Rename(const Utils::WString& from, const Utils::WString &to) 
842
bool CFileIO::Rename(const Utils::WString& from, const Utils::WString &to) 
804
{
843
{
805
	if (std::rename(_toFileData(from).c_str(), _toFileData(to).c_str()) == 0)
844
	if (_wrename(from.c_str(), to.c_str()) == 0)
806
		return true;
845
		return true;
807
	return false;
846
	return false;
808
}
847
}
809
 
848
 
810
bool CFileIO::remove()
849
bool CFileIO::remove()
Line 813... Line 852...
813
	if ( !this->exists() ) return false;
852
	if ( !this->exists() ) return false;
814
	if ( CFileIO::Remove(_sFilename)) return true;
853
	if ( CFileIO::Remove(_sFilename)) return true;
815
	return false;
854
	return false;
816
}
855
}
817
 
856
 
818
bool CFileIO::writeFileUTF(std::vector<Utils::WString>* lines)
857
bool CFileIO::writeFileUTF(const std::vector<Utils::WString>* lines)
819
{
858
{
820
	if (!lines || _sFilename.empty())
859
	if (!lines || _sFilename.empty())
821
		return false;
860
		return false;
822
 
861
 
823
	// we need to create the directory
862
	// we need to create the directory
Line 837... Line 876...
837
	{
876
	{
838
		Utils::WString l = (*itr);
877
		Utils::WString l = (*itr);
839
		if (l.contains('\n'))
878
		if (l.contains('\n'))
840
		{
879
		{
841
			std::vector<Utils::WString> strs;
880
			std::vector<Utils::WString> strs;
842
			if(l.tokenise(L"\n", strs) && !strs.empty())
881
			if (l.tokenise(L"\n", strs) && !strs.empty())
843
			{
882
			{
844
				for(auto itr = strs.begin(); itr != strs.end(); itr++)
883
				for (auto itr = strs.begin(); itr != strs.end(); itr++)
845
				{
884
				{
846
					Utils::WString line = *itr;
885
					Utils::WString line = *itr;
847
					line += L"\n";
886
					line += L"\n";
848
					fwrite(l.c_str(), sizeof(TCHAR), wcslen(l.c_str()), id);
887
					fwrite(l.c_str(), sizeof(TCHAR), wcslen(l.c_str()), id);
-
 
888
				}
-
 
889
			}
-
 
890
		}
-
 
891
		else
-
 
892
		{
-
 
893
			l += L"\n";
-
 
894
			fwrite(l.c_str(), sizeof(TCHAR), wcslen(l.c_str()), id);
-
 
895
		}
-
 
896
	}
-
 
897
 
-
 
898
	fclose(id);
-
 
899
 
-
 
900
	return true;
-
 
901
#else
-
 
902
	//TODO: write utf8 file writing function
-
 
903
	return false;
-
 
904
#endif
-
 
905
}
-
 
906
bool CFileIO::writeFileUTF(const Utils::WStringList &lines)
-
 
907
{
-
 
908
	if (lines.empty() || _sFilename.empty())
-
 
909
		return false;
-
 
910
 
-
 
911
	// we need to create the directory
-
 
912
	if (!_sDirIO.exists())
-
 
913
	{
-
 
914
		if (!_sDirIO.create())
-
 
915
			return false;
-
 
916
	}
-
 
917
 
-
 
918
#ifdef _WIN32
-
 
919
	FILE* id = _wfopen(_sFilename.c_str(), L"wt+,ccs=UTF-8");
-
 
920
	if (!id)
-
 
921
		return false;
-
 
922
 
-
 
923
	// write the rest
-
 
924
	for (auto itr = lines.begin(); itr != lines.end(); itr++)
-
 
925
	{
-
 
926
		Utils::WString l = (*itr)->str;
-
 
927
		if (l.contains('\n'))
-
 
928
		{
-
 
929
			std::vector<Utils::WString> strs;
-
 
930
			if (l.tokenise(L"\n", strs) && !strs.empty())
-
 
931
			{
-
 
932
				for (auto itr = strs.begin(); itr != strs.end(); itr++)
-
 
933
				{
-
 
934
					Utils::WString line = *itr;
-
 
935
					line += L"\n";
-
 
936
					fwrite(line.c_str(), sizeof(TCHAR), wcslen(line.c_str()), id);
849
				}
937
				}
850
			}
938
			}
851
		}
939
		}
852
		else
940
		else
853
		{
941
		{