Subversion Repositories spk

Rev

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

Rev 196 Rev 197
Line 46... Line 46...
46
	m_bBinary = bBinary;
46
	m_bBinary = bBinary;
47
	_sFilename = sFilename.asFilename();
47
	_sFilename = sFilename.asFilename();
48
 
48
 
49
	// check if there are any directories, and split the file and directory parts
49
	// check if there are any directories, and split the file and directory parts
50
	if ( _sFilename.contains('/') ) {
50
	if ( _sFilename.contains('/') ) {
51
		_sDirIO.setDir(_sFilename.tokens(L"/", 1, -2).toString());
51
		_sDirIO.setDir(_sFilename.tokens(L"/", 1, -2));
52
		_sFile = _sFilename.token(L"/", -1);
52
		_sFile = _sFilename.token(L"/", -1);
53
	}
53
	}
54
	else
54
	else
55
		_sFile = _sFilename;
55
		_sFile = _sFilename;
56
 
56
 
Line 111... Line 111...
111
 
111
 
112
	if ( pSize ) (*pSize) = m_lSize;
112
	if ( pSize ) (*pSize) = m_lSize;
113
	return data;
113
	return data;
114
}
114
}
115
 
115
 
116
bool CFileIO::read(unsigned char *buf, size_t iSize, bool bEndChar)
116
bool CFileIO::read(wchar_t* buf, size_t iSize, bool bEndChar)
117
{
117
{
118
	if ( !this->isOpened() ) startRead();
118
	if (!this->isOpened()) startRead();
119
	if ( !this->isOpened() ) return false;
119
	if (!this->isOpened()) return false;
120
 
120
 
121
	if ( iSize > m_lSize ) iSize = m_lSize;
121
	if (iSize > m_lSize) iSize = m_lSize;
-
 
122
	try {
-
 
123
		m_fId.read((char*)buf, iSize);
-
 
124
	}
-
 
125
	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
		return false;
-
 
128
	}
-
 
129
 
-
 
130
	if (bEndChar) buf[iSize] = '\0';
-
 
131
 
-
 
132
	return !m_fId.bad();
-
 
133
}
-
 
134
bool CFileIO::read(unsigned char* buf, size_t iSize, bool bEndChar)
-
 
135
{
-
 
136
	if (!this->isOpened()) startRead();
-
 
137
	if (!this->isOpened()) return false;
-
 
138
 
-
 
139
	if (iSize > m_lSize) iSize = m_lSize;
122
	try {
140
	try {
123
		m_fId.read((char *)buf, iSize);
141
		m_fId.read((char*)buf, iSize);
124
	}
142
	}
125
	catch (std::exception &e) {
143
	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());
144
		CLog::logf(CLog::Log_IO, 3, "ERROR: CFileIO::read() unable to read from file: %s (%s)", _sFilename.c_str(), e.what());
127
		return false;
145
		return false;
128
	}
146
	}
129
 
147
 
130
	if ( bEndChar ) buf[iSize] = '\0';
148
	if (bEndChar) buf[iSize] = '\0';
131
 
149
 
132
	return !m_fId.bad();
150
	return !m_fId.bad();
133
}
151
}
134
 
152
 
135
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
153
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
Line 151... Line 169...
151
	}
169
	}
152
 
170
 
153
	size_t off = 0;
171
	size_t off = 0;
154
	size_t startPos = 0;
172
	size_t startPos = 0;
155
	size_t remainingSize = fullsize;
173
	size_t remainingSize = fullsize;
156
 
174
 
157
	while ( off < numOffset ) {
175
	while ( off < numOffset ) {
158
		startPos = static_cast<size_t>(File.tellg());
176
		startPos = static_cast<size_t>(File.tellg());
159
		size_t offset = offsets[off++];
177
		size_t offset = offsets[off++];
160
		size_t size = offset - startPos;
178
		size_t size = offset - startPos;
161
		try {
179
		try {
Line 196... Line 214...
196
 
214
 
197
	File.close();
215
	File.close();
198
	writeFile.close();
216
	writeFile.close();
199
 
217
 
200
	// now copy to original file
218
	// now copy to original file
201
	std::remove(_fileData());
219
	CFileIO::Remove(_sFilename);
202
	return std::rename(CPackages::tempDirectory() + "/temp.tmp", _fileData()) != 0;
220
	return CFileIO::Rename(CPackages::tempDirectory() + L"/temp.tmp", _sFilename);
203
}
221
}
204
 
222
 
205
 
223
 
206
void CFileIO::setDir(const Utils::WString &dir)
224
void CFileIO::setDir(const Utils::WString &dir)
207
{
225
{
Line 213... Line 231...
213
{
231
{
214
	m_lSize = 0;
232
	m_lSize = 0;
215
 
233
 
216
	std::fstream file(_sFilename, (m_bBinary) ? (std::ios::in | std::ios::binary) : (std::ios::in));
234
	std::fstream file(_sFilename, (m_bBinary) ? (std::ios::in | std::ios::binary) : (std::ios::in));
217
	if ( !file.is_open() ) return;
235
	if ( !file.is_open() ) return;
218
 
236
 
219
	file.seekg(0, std::ios::end);
237
	file.seekg(0, std::ios::end);
220
	m_lSize = static_cast<size_t>(file.tellg());
238
	m_lSize = static_cast<size_t>(file.tellg());
221
	file.close();
239
	file.close();
222
}
240
}
223
 
241
 
Line 237... Line 255...
237
{
255
{
238
	if ( NoFile() ) return FILEERR_NOFILE;
256
	if ( NoFile() ) return FILEERR_NOFILE;
239
 
257
 
240
	if ( !this->startRead() ) return FILEERR_NOOPEN;
258
	if ( !this->startRead() ) return FILEERR_NOOPEN;
241
	if ( (offset + datasize) > m_lSize ) return FILEERR_TOSMALL;
259
	if ( (offset + datasize) > m_lSize ) return FILEERR_TOSMALL;
242
 
260
 
243
	CFileIO File(CPackages::tempDirectory() + "/temp.tmp");
261
	CFileIO File(CPackages::tempDirectory() + "/temp.tmp");
244
	if ( !File.startWrite() ) return FILEERR_NOWRITE;
262
	if ( !File.startWrite() ) return FILEERR_NOWRITE;
245
	File.setAutoDelete(true);
263
	File.setAutoDelete(true);
246
 
264
 
247
	if ( !File.write(*this, offset) ) return false;
265
	if ( !File.write(*this, offset) ) return false;
Line 249... Line 267...
249
	// next fseek after and write
267
	// next fseek after and write
250
	this->seek(datasize);
268
	this->seek(datasize);
251
	size_t size = m_lSize - offset - datasize;
269
	size_t size = m_lSize - offset - datasize;
252
	if ( size > 0 ) {
270
	if ( size > 0 ) {
253
		File.write(*this, size);
271
		File.write(*this, size);
254
	}
272
	}
255
 
273
 
256
	File.close();
274
	File.close();
257
	this->close();
275
	this->close();
258
	File.setAutoDelete(false);
276
	File.setAutoDelete(false);
259
 
277
 
260
	// now copy to original file
278
	// now copy to original file
261
	if ( std::remove(_fileData()) == 0) {
279
	if (CFileIO::Remove(_sFilename)) {
262
		std::rename(CPackages::tempDirectory() + ";/temp.tmp", _fileData());
280
		if (CFileIO::Rename(CPackages::tempDirectory() + L";/temp.tmp", _sFilename))
-
 
281
		{
263
		size_t oldSize = m_lSize;
282
			size_t oldSize = m_lSize;
264
		size_t checkFileSize = m_lSize - datasize;
283
			size_t checkFileSize = m_lSize - datasize;
265
		this->_readFileSize();
284
			this->_readFileSize();
266
		if ( checkFileSize != m_lSize ) {
285
			if (checkFileSize != m_lSize) {
267
			CLog::log(CLog::Log_IO, 3, Utils::String("WARNING: CFileIO::TruncateFile, file size mismatch, ") + (long)checkFileSize + " => " + (long)m_lSize);
286
				CLog::log(CLog::Log_IO, 3, Utils::WString(L"WARNING: CFileIO::TruncateFile, file size mismatch, ") + (long)checkFileSize + L" => " + (long)m_lSize);
-
 
287
			}
-
 
288
			return FILEERR_NONE;
268
		}
289
		}
269
		return FILEERR_NONE;
290
		return FILEERR_NOWRITE;
270
	}
291
	}
271
 
292
 
272
	return FILEERR_NOWRITE;
293
	return FILEERR_NOWRITE;
273
}
294
}
274
 
295
 
275
int CFileIO::_in() const
296
int CFileIO::_in() const
276
{
297
{
277
	if ( m_bBinary ) return std::ios::in | std::ios::binary;
298
	if ( m_bBinary ) return std::ios::in | std::ios::binary;
278
	return std::ios::in;
299
	return std::ios::in;
279
}
300
}
Line 305... Line 326...
305
 
326
 
306
	std::fstream file(_sFilename, _in());
327
	std::fstream file(_sFilename, _in());
307
	if ( !file.is_open() ) return NULL;
328
	if ( !file.is_open() ) return NULL;
308
 
329
 
309
	char *data;
330
	char *data;
310
	try {
331
	try {
311
		data = new char[m_lSize + 1];
332
		data = new char[m_lSize + 1];
312
	}
333
	}
313
	catch (std::exception &e) {
334
	catch (std::exception &e) {
314
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to malloc storage, %d (%s)", m_lSize + 1, e.what());
335
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to malloc storage, %d (%s)", m_lSize + 1, e.what());
315
		file.close();
336
		file.close();
316
		return NULL;
337
		return NULL;
317
	}
338
	}
318
 
339
 
319
	try {
340
	try {
320
		file.read((char *)data, m_lSize);
341
		file.read((char *)data, m_lSize);
321
	}
342
	}
322
	catch (std::exception &e) {
343
	catch (std::exception &e) {
323
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file (%s)", e.what());
344
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file (%s)", e.what());
324
		file.close();
345
		file.close();
325
		return NULL;
346
		return NULL;
326
	}
347
	}
327
 
348
 
328
	*size = m_lSize;
349
	*size = m_lSize;
329
	file.close();
350
	file.close();
330
 
351
 
331
	return data;
352
	return data;
332
}
353
}
333
 
354
 
334
bool CFileIO::WriteData ( const char *data, size_t size )
355
bool CFileIO::WriteData ( const char *data, size_t size )
335
{
356
{
336
	if ( NoFile() )
357
	if ( NoFile() )
337
		return false;
358
		return false;
Line 350... Line 371...
350
 
371
 
351
	File.close();
372
	File.close();
352
 
373
 
353
	if ( ret ) this->_readFileSize();
374
	if ( ret ) this->_readFileSize();
354
	return ret;
375
	return ret;
355
}
376
}
356
 
377
 
357
bool CFileIO::writeString ( const Utils::String &data )
378
bool CFileIO::writeString ( const Utils::String &data )
358
{
379
{
359
	return WriteData ( data.c_str(), data.length() );
380
	return WriteData ( data.c_str(), data.length() );
360
}
381
}
361
 
382
 
362
bool CFileIO::Exists(const Utils::WString &filename)
383
bool CFileIO::Exists(const Utils::WString &filename)
363
{
384
{
364
	std::fstream File(filename, std::ios::in | std::ios::binary);
385
	std::wfstream File(filename, std::ios::in | std::ios::binary);
365
	bool bRet = File.good();
386
	bool bRet = File.good();
366
	File.close();
387
	File.close();
367
	return bRet;
388
	return bRet;
368
}
389
}
369
 
390
 
370
bool CFileIO::ExistsOld () const
391
bool CFileIO::ExistsOld () const
371
{
392
{
372
	return this->exists();
393
	return this->exists();
373
}
394
}
Line 386... Line 407...
386
}
407
}
387
 
408
 
388
bool CFileIO::startWrite()
409
bool CFileIO::startWrite()
389
{
410
{
390
	return _start(_out(), true);
411
	return _start(_out(), true);
391
}
412
}
392
 
413
 
393
bool CFileIO::startModify()
414
bool CFileIO::startModify()
394
{
415
{
395
	return _start(_inout(), true);
416
	return _start(_inout(), true);
396
}
417
}
Line 410... Line 431...
410
	m_bSeekP = bSeekP;
431
	m_bSeekP = bSeekP;
411
	return m_fId.is_open();
432
	return m_fId.is_open();
412
}
433
}
413
 
434
 
414
std::vector<Utils::String>* CFileIO::readLines() const
435
std::vector<Utils::String>* CFileIO::readLines() const
415
{
436
{
416
	if (_sFilename.empty()) return 0;
437
	if (_sFilename.empty()) return 0;
417
 
438
 
418
	std::vector<Utils::String>* file = new std::vector<Utils::String>;
439
	std::vector<Utils::String>* file = new std::vector<Utils::String>;
419
	Utils::String line;
440
	Utils::String line;
420
	file->clear();
441
	file->clear();
421
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
442
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
422
	while (getline(infile, line, '\n')) {
443
	while (getline(infile, line, '\n')) {
423
		file->push_back(Utils::String(line).removeChar((char)0));
444
		file->push_back(Utils::String(line).removeChar((char)0));
-
 
445
	}
-
 
446
 
-
 
447
	infile.close();
-
 
448
 
-
 
449
	return file;
-
 
450
}
-
 
451
size_t CFileIO::readLines(std::vector<Utils::String>& to) const
-
 
452
{
-
 
453
	if (_sFilename.empty()) return 0;
-
 
454
 
-
 
455
	Utils::String line;
-
 
456
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
-
 
457
	while (getline(infile, line, '\n')) {
-
 
458
		to.push_back(Utils::String(line).removeChar((char)0));
424
	}
459
	}
425
 
460
 
426
	infile.close();
461
	infile.close();
427
 
462
 
428
	return file;
463
	return to.size();
429
}
464
}
-
 
465
 
430
size_t CFileIO::readLines(std::vector<Utils::String>& to) const
466
size_t CFileIO::readLines(std::vector<Utils::WString>& to) const
431
{
467
{
432
	if (_sFilename.empty()) return 0;
468
	if (_sFilename.empty()) return 0;
433
 
469
 
434
	Utils::String line;
470
	std::wstring line;
435
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
471
	std::wifstream infile(_sFilename.c_str(), std::ios_base::in);
436
	while (getline(infile, line, '\n')) {
472
	while (getline(infile, line, L'\n')) {
437
		to.push_back(Utils::String(line).removeChar((char)0));
473
		to.push_back(Utils::WString(line).remove((char)0));
438
	}
474
	}
439
 
475
 
440
	infile.close();
476
	infile.close();
441
 
477
 
442
	return to.size();
478
	return to.size();
Line 529... Line 565...
529
bool CFileIO::write(const char *buf, size_t iSize)
565
bool CFileIO::write(const char *buf, size_t iSize)
530
{
566
{
531
	if ( !this->isOpened() ) startWrite();
567
	if ( !this->isOpened() ) startWrite();
532
	if ( !this->isOpened() ) return false;
568
	if ( !this->isOpened() ) return false;
533
	try {
569
	try {
534
		m_fId.write((char *)buf, iSize);
570
		m_fId.write((char*)buf, iSize);
535
	}
571
	}
536
	catch (std::exception &e) {
572
	catch (std::exception &e) {
537
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
573
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
538
		return false;
574
		return false;
539
	}
575
	}
Line 551... Line 587...
551
	va_start(args, buf);
587
	va_start(args, buf);
552
	bool ret = this->_write((char *)buf, args);
588
	bool ret = this->_write((char *)buf, args);
553
	va_end(args);
589
	va_end(args);
554
	return ret;
590
	return ret;
555
}
591
}
556
bool CFileIO::_write(const char *buf, va_list args) 
592
bool CFileIO::_write(const char* buf, va_list args)
557
{
593
{
558
	if ( !this->isOpened() ) return false;
594
	if (!this->isOpened()) return false;
559
 
595
 
560
	char buffer[10000];
596
	char buffer[10000];
561
 
597
 
562
	vsprintf (buffer, buf, args);
598
	vsprintf(buffer, buf, args);
563
	try {
599
	try {
564
		int iLen = strlen(buffer);
600
		int iLen = strlen(buffer);
565
		m_fId.write((char *)buffer, iLen);
601
		m_fId.write((char*)buffer, iLen);
566
		m_lSize += iLen;
602
		m_lSize += iLen;
567
	}
603
	}
568
	catch (std::exception &e) {
604
	catch (std::exception& e) {
569
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
605
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
570
		return false;
606
		return false;
571
	}
607
	}
572
	return !m_fId.bad();
608
	return !m_fId.bad();
573
}
609
}
-
 
610
bool CFileIO::_write(const wchar_t* buf, va_list args)
-
 
611
{
-
 
612
	if (!this->isOpened()) return false;
-
 
613
 
-
 
614
	wchar_t buffer[10000];
-
 
615
 
-
 
616
#ifdef _WIN32
-
 
617
	vswprintf_s(buffer, 10000, buf, args);
-
 
618
#else
-
 
619
	vswprintf(buffer, buf, args);
-
 
620
#endif
-
 
621
	try {
-
 
622
		size_t iLen = wcslen(buffer);
-
 
623
		m_fId.write((char*)buffer, iLen);
-
 
624
		m_lSize += iLen;
-
 
625
	}
-
 
626
	catch (std::exception& e) {
-
 
627
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %ls (%s)", _sFilename.c_str(), e.what());
-
 
628
		return false;
-
 
629
	}
-
 
630
	return !m_fId.bad();
-
 
631
}
574
 
632
 
575
bool CFileIO::write(const char *buf, ...)
633
bool CFileIO::write(const char *buf, ...)
576
{
634
{
577
	va_list args;
635
	va_list args;
578
	va_start(args, buf);
636
	va_start(args, buf);
Line 594... Line 652...
594
{
652
{
595
	_seek(iPos, std::ios::cur);
653
	_seek(iPos, std::ios::cur);
596
}
654
}
597
 
655
 
598
void CFileIO::seekEnd(size_t iPos)
656
void CFileIO::seekEnd(size_t iPos)
599
{
657
{
600
	_seek(iPos, std::ios::end);
658
	_seek(iPos, std::ios::end);
601
}
659
}
602
 
660
 
603
void CFileIO::seekStart(size_t iPos)
661
void CFileIO::seekStart(size_t iPos)
604
{
662
{
Line 606... Line 664...
606
}
664
}
607
 
665
 
608
std::fstream &CFileIO::stream()
666
std::fstream &CFileIO::stream()
609
{
667
{
610
	return m_fId;
668
	return m_fId;
611
}
669
}
612
 
670
 
613
bool CFileIO::isOpened() const
671
bool CFileIO::isOpened() const
614
{
672
{
615
	if ( m_fId.is_open() )
673
	if ( m_fId.is_open() )
616
		return true;
674
		return true;
617
	return false;
675
	return false;
Line 629... Line 687...
629
	if ( this->read(size, 4) ) return (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
687
	if ( this->read(size, 4) ) return (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
630
	return 0;
688
	return 0;
631
}
689
}
632
 
690
 
633
bool CFileIO::atEnd() const
691
bool CFileIO::atEnd() const
634
{
692
{
635
	if ( !this->isOpened() ) return true;
693
	if ( !this->isOpened() ) return true;
636
	if ( m_fId.eof() )		 return true;
694
	if ( m_fId.eof() )		 return true;
637
	return false;
695
	return false;
638
}
696
}
639
 
697
 
Line 700... Line 758...
700
		}
758
		}
701
		pos += read;
759
		pos += read;
702
	}
760
	}
703
 
761
 
704
	File.close();
762
	File.close();
705
	return true;
763
	return true;
706
}
764
}
707
 
765
 
708
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
766
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
709
{
767
{
710
	if ( start > m_lSize ) return false;
768
	if ( start > m_lSize ) return false;
711
	if ( m_lSize <= 0 ) {
769
	if ( m_lSize <= 0 ) {
712
		if ( !this->startWrite() ) return false;
770
		if ( !this->startWrite() ) return false;
Line 718... Line 776...
718
		if ( !this->startModify() ) return false;
776
		if ( !this->startModify() ) return false;
719
		this->seekStart(start);
777
		this->seekStart(start);
720
	}
778
	}
721
 
779
 
722
	return this->write(d, size);
780
	return this->write(d, size);
723
}
781
}
-
 
782
 
-
 
783
Utils::String CFileIO::readEndOfLineStr()
-
 
784
{
-
 
785
	if (!this->isOpened()) {
-
 
786
		if (!startRead())	return "";
-
 
787
	}
724
 
788
 
-
 
789
	std::string str;
-
 
790
	std::getline(m_fId, str, '\n');
-
 
791
	return str;
-
 
792
}
725
Utils::String CFileIO::readEndOfLine()
793
Utils::WString CFileIO::readEndOfLine()
726
{
794
{
727
	if ( !this->isOpened() ) {
795
	if (!this->isOpened()) {
728
		if ( !startRead() )	return "";
796
		if (!startRead())	return L"";
729
	}
797
	}
730
 
798
 
731
	Utils::String str;
799
	std::string str;
732
	std::getline(m_fId, str, '\n');
800
	std::getline(m_fId, str, '\n');
733
	return str;
801
	return str;
734
}
802
}
735
 
803
 
736
Utils::WString CFileIO::baseName() const
804
Utils::WString CFileIO::baseName() const
737
{
805
{
738
	return _sFile.tokens(L".", 1, -2).toString();
806
	return _sFile.tokens(L".", 1, -2);
739
}
807
}
740
 
808
 
741
const Utils::WString& CFileIO::fullFilename() const { return _sFilename; }
809
const Utils::WString& CFileIO::fullFilename() const { return _sFilename; }
742
const Utils::WString& CFileIO::filename() const { return _sFile; }
810
const Utils::WString& CFileIO::filename() const { return _sFile; }
743
 
811
 
Line 746... Line 814...
746
 
814
 
747
Utils::WString CFileIO::extension () const
815
Utils::WString CFileIO::extension () const
748
{
816
{
749
	if ( _sFilename.empty() ) return Utils::WString::Null();
817
	if ( _sFilename.empty() ) return Utils::WString::Null();
750
	return _sFilename.token(L".", -1);
818
	return _sFilename.token(L".", -1);
751
}
819
}
752
 
820
 
753
Utils::WString CFileIO::changeFileExtension(const Utils::WString &ext) const
821
Utils::WString CFileIO::changeFileExtension(const Utils::WString &ext) const
754
{
822
{
755
	if ( _sFilename.empty() )
823
	if ( _sFilename.empty() )
756
		return Utils::WString::Null();
824
		return Utils::WString::Null();
757
	
825
	
758
	return _sFilename.tokens(L".", 1, -2) + L"." + ext;
826
	return _sFilename.tokens(L".", 1, -2) + L"." + ext;
759
}
827
}
760
 
828
 
761
bool CFileIO::Remove(const Utils::WString &rem)
829
bool CFileIO::Remove(const Utils::WString& rem)
762
{
830
{
-
 
831
#ifdef _WIN32
-
 
832
	return DeleteFile(rem.c_str());
-
 
833
#else
763
	//if ( !Exists() ) return false;
834
	//if ( !Exists() ) return false;
764
	return (std::remove(_toFileData(rem)) == 0) ? true : false;
835
	return (std::remove(_toFileData(rem)) == 0) ? true : false;
-
 
836
#endif
-
 
837
}
-
 
838
 
-
 
839
bool CFileIO::Rename(const Utils::WString& from, const Utils::WString &to) 
-
 
840
{
-
 
841
	if (std::rename(_toFileData(from).c_str(), _toFileData(to).c_str()) == 0)
-
 
842
		return true;
-
 
843
	return false;
765
}
844
}
766
 
845
 
767
bool CFileIO::remove()
846
bool CFileIO::remove()
768
{
847
{
769
	if ( this->isOpened() ) this->close();
848
	if ( this->isOpened() ) this->close();
770
	if ( !this->exists() ) return false;
849
	if ( !this->exists() ) return false;
771
	if ( std::remove(_fileData()) == 0) return true;
850
	if ( CFileIO::Remove(_sFilename)) return true;
772
	return false;
851
	return false;
773
}
852
}
774
 
853
 
775
bool CFileIO::writeFileUTF(std::vector<Utils::String> *lines)
854
bool CFileIO::writeFileUTF(std::vector<Utils::String>* lines)
776
{
855
{
777
	if (!lines || _sFilename.empty())
856
	if (!lines || _sFilename.empty())
778
		return false;
857
		return false;
779
 
858
 
780
	// we need to create the directory
859
	// we need to create the directory
Line 784... Line 863...
784
			return false;
863
			return false;
785
	}
864
	}
786
 
865
 
787
#ifdef _WIN32
866
#ifdef _WIN32
788
	TCHAR buf[5000];
867
	TCHAR buf[5000];
789
	wsprintf(buf, L"%hs", _sFilename.c_str());
-
 
790
	FILE *id = _wfopen(buf, L"wt+,ccs=UTF-8");
868
	FILE* id = _wfopen(_sFilename.c_str(), L"wt+,ccs=UTF-8");
791
	if (!id)
869
	if (!id)
792
		return false;
870
		return false;
793
 
871
 
794
	// write the rest
872
	// write the rest
795
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
873
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
796
	{
874
	{
797
		Utils::String l = (*itr);
875
		Utils::String l = (*itr);
798
		if (l.isin('\n'))
876
		if (l.isin('\n'))
799
		{
877
		{
800
			int max;
878
			int max;
801
			Utils::String *strs = l.tokenise("\n", &max);
879
			Utils::String* strs = l.tokenise("\n", &max);
802
			if (strs && max)
880
			if (strs && max)
803
			{
881
			{
804
				for (int i = 0; i < max; i++)
882
				for (int i = 0; i < max; i++)
805
				{
883
				{
806
					Utils::String line = strs[i];
884
					Utils::String line = strs[i];
Line 827... Line 905...
827
	//TODO: write utf8 file writing function
905
	//TODO: write utf8 file writing function
828
	return false;
906
	return false;
829
#endif
907
#endif
830
}
908
}
831
 
909
 
832
bool CFileIO::writeFile(std::vector<Utils::String> *lines)
910
bool CFileIO::writeFileUTF(std::vector<Utils::WString>* lines)
833
{
911
{
834
	if (!lines || _sFilename.empty())
912
	if (!lines || _sFilename.empty())
835
		return false;
913
		return false;
836
 
914
 
837
	// we need to create the directory
915
	// we need to create the directory
838
	if (!_sDirIO.exists())
916
	if (!_sDirIO.exists())
839
	{
917
	{
840
		if (!_sDirIO.create())
918
		if (!_sDirIO.create())
841
			return false;
919
			return false;
-
 
920
	}
-
 
921
 
-
 
922
#ifdef _WIN32
-
 
923
	FILE* id = _wfopen(_sFilename.c_str(), L"wt+,ccs=UTF-8");
-
 
924
	if (!id)
-
 
925
		return false;
-
 
926
 
-
 
927
	// write the rest
-
 
928
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
-
 
929
	{
-
 
930
		Utils::WString l = (*itr);
-
 
931
		if (l.contains('\n'))
-
 
932
		{
-
 
933
			std::vector<Utils::WString> strs;
-
 
934
			if(l.tokenise(L"\n", strs) && !strs.empty())
-
 
935
			{
-
 
936
				for(auto itr = strs.begin(); itr != strs.end(); itr++)
-
 
937
				{
-
 
938
					Utils::WString line = *itr;
-
 
939
					line += L"\n";
-
 
940
					fwrite(l.c_str(), sizeof(TCHAR), wcslen(l.c_str()), id);
-
 
941
				}
-
 
942
			}
-
 
943
		}
-
 
944
		else
-
 
945
		{
-
 
946
			l += L"\n";
-
 
947
			fwrite(l.c_str(), sizeof(TCHAR), wcslen(l.c_str()), id);
-
 
948
		}
-
 
949
	}
-
 
950
 
-
 
951
	fclose(id);
-
 
952
 
-
 
953
	return true;
-
 
954
#else
-
 
955
	//TODO: write utf8 file writing function
-
 
956
	return false;
-
 
957
#endif
-
 
958
}
-
 
959
 
-
 
960
bool CFileIO::writeFile(std::vector<Utils::String> *lines)
-
 
961
{
-
 
962
	if (!lines || _sFilename.empty())
-
 
963
		return false;
-
 
964
 
-
 
965
	// we need to create the directory
-
 
966
	if (!_sDirIO.exists())
-
 
967
	{
-
 
968
		if (!_sDirIO.create())
-
 
969
			return false;
842
	}
970
	}
843
 
971
 
844
 
972
 
845
	std::ofstream out(_sFilename.c_str());
973
	std::ofstream out(_sFilename.c_str());
846
	if (!out)
974
	if (!out)
847
		return false;
975
		return false;
848
 
976
 
849
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
977
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
850
		out << (*itr).c_str() << std::endl;
978
		out << (*itr).c_str() << std::endl;
-
 
979
 
-
 
980
	out.close();
-
 
981
 
-
 
982
	return true;
-
 
983
}
-
 
984
 
-
 
985
bool CFileIO::writeFile(const std::vector<Utils::WString> &lines) const
-
 
986
{
-
 
987
	if (lines.empty() || _sFilename.empty())
-
 
988
		return false;
-
 
989
 
-
 
990
	// we need to create the directory
-
 
991
	if (!_sDirIO.exists())
-
 
992
	{
-
 
993
		if (!_sDirIO.create())
-
 
994
			return false;
-
 
995
	}
-
 
996
 
-
 
997
 
-
 
998
	std::wofstream out(_sFilename.c_str());
-
 
999
	if (!out)
-
 
1000
		return false;
-
 
1001
 
-
 
1002
	for (auto itr = lines.begin(); itr != lines.end(); itr++)
-
 
1003
		out << (*itr).c_str() << std::endl;
851
 
1004
 
852
	out.close();
1005
	out.close();
853
 
1006
 
854
	return true;
1007
	return true;
855
}
1008
}
-
 
1009
 
856
 
1010
 
857
bool CFileIO::writeFile(Utils::WStringList* lines)
1011
bool CFileIO::writeFile(Utils::WStringList* lines)
858
{
1012
{
859
	if (!lines || _sFilename.empty())
1013
	if (!lines || _sFilename.empty())
860
		return false;
1014
		return false;
861
 
1015
 
862
	// we need to create the directory
1016
	// we need to create the directory
863
	if (!_sDirIO.exists())
1017
	if (!_sDirIO.exists())
864
	{
1018
	{
865
		if (!_sDirIO.create())
1019
		if (!_sDirIO.create())
866
			return false;
1020
			return false;
867
	}
1021
	}
868
 
1022
 
869
	std::ofstream out(_sFilename.c_str());
1023
	std::wofstream out(_sFilename.c_str());
870
	if (!out)
1024
	if (!out)
871
		return false;
1025
		return false;
872
 
1026
 
873
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
1027
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
874
		out << (*itr)->str.c_str() << std::endl;
1028
		out << (*itr)->str.c_str() << std::endl;
875
 
1029
 
876
	out.close();
1030
	out.close();
877
 
1031
 
878
	return true;
1032
	return true;
879
}
1033
}
880
bool CFileIO::writeFile(Utils::CStringList* lines)
1034
bool CFileIO::writeFile(Utils::CStringList* lines)
881
{
1035
{
882
	if (!lines || _sFilename.empty())
1036
	if (!lines || _sFilename.empty())
883
		return false;
1037
		return false;
884
 
1038
 
Line 893... Line 1047...
893
	if (!out)
1047
	if (!out)
894
		return false;
1048
		return false;
895
 
1049
 
896
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
1050
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
897
		out << (*itr)->str.c_str() << std::endl;
1051
		out << (*itr)->str.c_str() << std::endl;
898
 
1052
 
899
	out.close();
1053
	out.close();
900
 
1054
 
901
	return true;
1055
	return true;
902
}
1056
}
903
 
1057
 
904
bool CFileIO::Rename(const Utils::WString &toFile)
1058
bool CFileIO::Rename(const Utils::WString &toFile)
905
{
1059
{	
906
	if ( rename(_fileData(), _toFileData(toFile)) == 0)
1060
	return CFileIO::Rename(_sFilename, toFile);
907
		return true;
-
 
908
	return false;
-
 
909
}
1061
}
910
 
1062
 
911
Utils::WString CFileIO::getWindowsFilename() const
1063
Utils::WString CFileIO::getWindowsFilename() const
912
{
1064
{
913
	return _sFilename.findReplace(L"/", L"\\");
1065
	return _sFilename.findReplace(L"/", L"\\");
Line 1014... Line 1166...
1014
{
1166
{
1015
	if (_sFilename.empty()) _sExt = Utils::WString::Null();
1167
	if (_sFilename.empty()) _sExt = Utils::WString::Null();
1016
	_sExt = _sFilename.token(L".", -1);
1168
	_sExt = _sFilename.token(L".", -1);
1017
}
1169
}
1018
 
1170
 
1019
const char* CFileIO::_fileData() const
1171
std::string CFileIO::_fileData() const
1020
{
1172
{
1021
	return std::string(_sFilename.begin(), _sFilename.end()).c_str();
1173
	return _toFileData(_sFilename);
1022
}
1174
}
1023
const char* CFileIO::_toFileData(const Utils::WString &str)
1175
std::string CFileIO::_toFileData(const Utils::WString &str)
1024
{
1176
{
1025
	return std::string(str.begin(), str.end()).c_str();
1177
	return std::string(str.begin(), str.end());
1026
}
1178
}