Subversion Repositories spk

Rev

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

Rev 158 Rev 160
Line 42... Line 42...
42
}
42
}
43
 
43
 
44
bool CFileIO::open(const Utils::String &sFilename, bool bBinary)
44
bool CFileIO::open(const Utils::String &sFilename, bool bBinary)
45
{
45
{
46
	m_bBinary = bBinary;
46
	m_bBinary = bBinary;
47
	m_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 ( m_sFilename.isin('/') ) {
50
	if ( _sFilename.isin('/') ) {
51
		m_sDirIO.SetDir(m_sFilename.tokens("/", 1, -2));
51
		_sDirIO.setDir(_sFilename.tokens("/", 1, -2));
52
		m_sFile = m_sFilename.token("/", -1);
52
		_sFile = _sFilename.token("/", -1);
53
	}
53
	}
54
	else
54
	else
55
		m_sFile = m_sFilename;
55
		_sFile = _sFilename;
56
 
56
 
57
	m_sFile.removeFirstSpace();
57
	_sFile.removeFirstSpace();
58
	m_sFile.removeChar(13);
58
	_sFile.removeChar(13);
-
 
59
 
-
 
60
	_updateFilename();
59
 
61
 
60
	this->_readFileSize();
62
	this->_readFileSize();
61
	return true;
63
	return true;
62
}
64
}
63
 
65
 
Line 76... Line 78...
76
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to malloc data, %d (%s)", iAmount + 1, e.what());
78
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to malloc data, %d (%s)", iAmount + 1, e.what());
77
		return NULL;
79
		return NULL;
78
	}
80
	}
79
 
81
 
80
	if ( !read(data, iAmount, true) ) {
82
	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);
83
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to read data from file, %s/%d", _sFilename.c_str(), iAmount);
82
		delete data;
84
		delete data;
83
		return NULL;
85
		return NULL;
84
	}
86
	}
85
 
87
 
86
	return data;
88
	return data;
Line 100... Line 102...
100
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to malloc data, %d (%s)", m_lSize + 1, e.what());
102
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to malloc data, %d (%s)", m_lSize + 1, e.what());
101
		return NULL;
103
		return NULL;
102
	}
104
	}
103
 
105
 
104
	if ( !read(data, m_lSize, true) ) {
106
	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);
107
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to read data from file, %s/%d", _sFilename.c_str(), m_lSize);
106
		delete data;
108
		delete data;
107
		return NULL;
109
		return NULL;
108
	}
110
	}
109
 
111
 
110
	if ( pSize ) (*pSize) = m_lSize;
112
	if ( pSize ) (*pSize) = m_lSize;
Line 119... Line 121...
119
	if ( iSize > m_lSize ) iSize = m_lSize;
121
	if ( iSize > m_lSize ) iSize = m_lSize;
120
	try {
122
	try {
121
		m_fId.read((char *)buf, iSize);
123
		m_fId.read((char *)buf, iSize);
122
	}
124
	}
123
	catch (std::exception &e) {
125
	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());
126
		CLog::logf(CLog::Log_IO, 3, "ERROR: CFileIO::read() unable to read from file: %s (%s)", _sFilename.c_str(), e.what());
125
		return false;
127
		return false;
126
	}
128
	}
127
 
129
 
128
	if ( bEndChar ) buf[iSize] = '\0';
130
	if ( bEndChar ) buf[iSize] = '\0';
129
 
131
 
Line 132... Line 134...
132
 
134
 
133
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
135
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
134
{
136
{
135
	if ( NoFile() )	return false;
137
	if ( NoFile() )	return false;
136
 
138
 
137
	std::fstream File(m_sFilename, _in());
139
	std::fstream File(_sFilename, _in());
138
	if ( !File.is_open() ) return false;
140
	if ( !File.is_open() ) return false;
139
 
141
 
140
	// first find file size
142
	// first find file size
141
	File.seekg(0, std::ios::end);
143
	File.seekg(0, std::ios::end);
142
	size_t fullsize = static_cast<size_t>(File.tellg()), size = fullsize;
144
	size_t fullsize = static_cast<size_t>(File.tellg()), size = fullsize;
Line 161... Line 163...
161
			File.read(data, size);
163
			File.read(data, size);
162
			writeFile.write(data, size);
164
			writeFile.write(data, size);
163
			delete data;
165
			delete data;
164
		}
166
		}
165
		catch(std::exception &e) {
167
		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());
168
			CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", _sFilename.c_str(), e.what());
167
			return false;
169
			return false;
168
		}
170
		}
169
 
171
 
170
		size_t datasize = offsets[off++];
172
		size_t datasize = offsets[off++];
171
		File.seekg(datasize, std::ios::beg);
173
		File.seekg(datasize, std::ios::beg);
Line 181... Line 183...
181
			try {
183
			try {
182
				File.read(data, amountLeft);
184
				File.read(data, amountLeft);
183
				writeFile.write(data, amountLeft);
185
				writeFile.write(data, amountLeft);
184
			}
186
			}
185
			catch(std::exception &e) {
187
			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());
188
				CLog::logf(CLog::Log_IO, 2, "CFileIO::WritePartFilea() unable to read data from file: %s (%s)", _sFilename.c_str(), e.what());
187
				return false;
189
				return false;
188
			}
190
			}
189
 
191
 
190
			remainingSize -= amountLeft;
192
			remainingSize -= amountLeft;
191
			amountLeft = 1000000;
193
			amountLeft = 1000000;
Line 194... Line 196...
194
 
196
 
195
	File.close();
197
	File.close();
196
	writeFile.close();
198
	writeFile.close();
197
 
199
 
198
	// now copy to original file
200
	// now copy to original file
199
	std::remove(m_sFilename);
201
	std::remove(_sFilename);
200
	std::rename(CPackages::tempDirectory() + "/temp.tmp", m_sFilename);
202
	std::rename(CPackages::tempDirectory() + "/temp.tmp", _sFilename);
201
 
203
 
202
	return true;
204
	return true;
203
}
205
}
204
 
206
 
205
 
207
 
206
void CFileIO::setDir(const Utils::String &dir)
208
void CFileIO::setDir(const Utils::String &dir)
207
{
209
{
208
	if ( m_sFile.empty() )	m_sDirIO.SetDir(dir);
210
	if ( _sFile.empty() )	_sDirIO.setDir(dir);
209
	else					open(dir + "/" + m_sFile, m_bBinary);
211
	else					open(dir + "/" + _sFile, m_bBinary);
210
}
-
 
211
 
-
 
212
void CFileIO::SetDir ( CyString dir )
-
 
213
{
-
 
214
	setDir(dir.ToString());
-
 
215
}
212
}
216
 
213
 
217
void CFileIO::_readFileSize ()
214
void CFileIO::_readFileSize ()
218
{
215
{
219
	m_lSize = 0;
216
	m_lSize = 0;
220
 
217
 
221
	std::fstream file(m_sFilename, (m_bBinary) ? (std::ios::in | std::ios::binary) : (std::ios::in));
218
	std::fstream file(_sFilename, (m_bBinary) ? (std::ios::in | std::ios::binary) : (std::ios::in));
222
	if ( !file.is_open() ) return;
219
	if ( !file.is_open() ) return;
223
 
220
 
224
	file.seekg(0, std::ios::end);
221
	file.seekg(0, std::ios::end);
225
	m_lSize = static_cast<size_t>(file.tellg());
222
	m_lSize = static_cast<size_t>(file.tellg());
226
	file.close();
223
	file.close();
Line 228... Line 225...
228
 
225
 
229
bool CFileIO::WipeFile()
226
bool CFileIO::WipeFile()
230
{
227
{
231
	if ( NoFile() )	return false;
228
	if ( NoFile() )	return false;
232
	std::ofstream file;
229
	std::ofstream file;
233
	file.open(m_sFilename, std::ios::out | std::ios::trunc);
230
	file.open(_sFilename, std::ios::out | std::ios::trunc);
234
	bool bDone = file.is_open();
231
	bool bDone = file.is_open();
235
	file.close();
232
	file.close();
236
 
233
 
237
	return bDone;
234
	return bDone;
238
}
235
}
Line 261... Line 258...
261
	File.close();
258
	File.close();
262
	this->close();
259
	this->close();
263
	File.setAutoDelete(false);
260
	File.setAutoDelete(false);
264
 
261
 
265
	// now copy to original file
262
	// now copy to original file
266
	if ( std::remove(m_sFilename.c_str()) == 0 ) {
263
	if ( std::remove(_sFilename.c_str()) == 0 ) {
267
		std::rename(CPackages::tempDirectory() + "/temp.tmp", m_sFilename.c_str());
264
		std::rename(CPackages::tempDirectory() + "/temp.tmp", _sFilename.c_str());
268
		size_t oldSize = m_lSize;
265
		size_t oldSize = m_lSize;
269
		size_t checkFileSize = m_lSize - datasize;
266
		size_t checkFileSize = m_lSize - datasize;
270
		this->_readFileSize();
267
		this->_readFileSize();
271
		if ( checkFileSize != m_lSize ) {
268
		if ( checkFileSize != m_lSize ) {
272
			CLog::log(CLog::Log_IO, 3, Utils::String("WARNING: CFileIO::TruncateFile, file size mismatch, ") + (long)checkFileSize + " => " + (long)m_lSize);
269
			CLog::log(CLog::Log_IO, 3, Utils::String("WARNING: CFileIO::TruncateFile, file size mismatch, ") + (long)checkFileSize + " => " + (long)m_lSize);
Line 306... Line 303...
306
 
303
 
307
	if ( NoFile() ) return NULL;
304
	if ( NoFile() ) return NULL;
308
	if ( !m_lSize )	this->_readFileSize();
305
	if ( !m_lSize )	this->_readFileSize();
309
	if ( !m_lSize ) return NULL;
306
	if ( !m_lSize ) return NULL;
310
 
307
 
311
	std::fstream file(m_sFilename, _in());
308
	std::fstream file(_sFilename, _in());
312
	if ( !file.is_open() ) return NULL;
309
	if ( !file.is_open() ) return NULL;
313
 
310
 
314
	char *data;
311
	char *data;
315
	try {
312
	try {
316
		data = new char[m_lSize + 1];
313
		data = new char[m_lSize + 1];
Line 339... Line 336...
339
bool CFileIO::WriteData ( const char *data, size_t size )
336
bool CFileIO::WriteData ( const char *data, size_t size )
340
{
337
{
341
	if ( NoFile() )
338
	if ( NoFile() )
342
		return false;
339
		return false;
343
 
340
 
344
	std::fstream File(m_sFilename, _out());
341
	std::fstream File(_sFilename, _out());
345
	if ( !File.is_open() ) return false;
342
	if ( !File.is_open() ) return false;
346
 
343
 
347
	bool ret = true;
344
	bool ret = true;
348
	try {
345
	try {
349
		File.write(data, size);
346
		File.write(data, size);
350
	}
347
	}
351
	catch (std::exception &e) {
348
	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());
349
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file: %s (%s)", _sFilename.c_str(), e.what());
353
		ret = false;
350
		ret = false;
354
	}
351
	}
355
 
352
 
356
	File.close();
353
	File.close();
357
 
354
 
Line 377... Line 374...
377
	return this->exists();
374
	return this->exists();
378
}
375
}
379
bool CFileIO::exists () const
376
bool CFileIO::exists () const
380
{
377
{
381
	if ( this->isOpened() ) return true;
378
	if ( this->isOpened() ) return true;
382
	std::fstream File(m_sFilename, _in());
379
	std::fstream File(_sFilename, _in());
383
	bool bRet = File.good();
380
	bool bRet = File.good();
384
	File.close();
381
	File.close();
385
	return bRet;
382
	return bRet;
386
}
383
}
387
 
384
 
Line 405... Line 402...
405
	return _start(_append(), false);
402
	return _start(_append(), false);
406
}
403
}
407
 
404
 
408
bool CFileIO::_start(int iFlags, bool bSeekP)
405
bool CFileIO::_start(int iFlags, bool bSeekP)
409
{
406
{
410
	if ( !m_sFilename.empty() ) {
407
	if ( !_sFilename.empty() ) {
411
		if ( this->isOpened() ) this->close();
408
		if ( this->isOpened() ) this->close();
412
 
409
 
413
		m_fId.open(m_sFilename, iFlags);
410
		m_fId.open(_sFilename, iFlags);
414
	}
411
	}
415
	m_bSeekP = bSeekP;
412
	m_bSeekP = bSeekP;
416
	return m_fId.is_open();
413
	return m_fId.is_open();
417
}
414
}
418
 
415
 
419
std::vector<Utils::String> *CFileIO::readLines()
416
std::vector<Utils::String> *CFileIO::readLines()
420
{
417
{
421
	if ( m_sFilename.empty() ) return 0;
418
	if ( _sFilename.empty() ) return 0;
422
 
419
 
423
	std::vector<Utils::String> *file = new std::vector<Utils::String>;
420
	std::vector<Utils::String> *file = new std::vector<Utils::String>;
424
	Utils::String line;
421
	Utils::String line;
425
	file->clear();
422
	file->clear();
426
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
423
	std::ifstream infile (_sFilename.c_str(), std::ios_base::in);
427
	while (getline(infile, line, '\n')) {
424
	while (getline(infile, line, '\n')) {
428
		file->push_back(Utils::String(line).removeChar((char)0));
425
		file->push_back(Utils::String(line).removeChar((char)0));
429
	}
426
	}
430
 
427
 
431
	infile.close();
428
	infile.close();
432
 
429
 
433
	return file;
430
	return file;
434
}
431
}
435
 
432
 
436
std::vector<CyString> *CFileIO::ReadLines()
-
 
437
{
-
 
438
	if ( m_sFilename.empty() ) return 0;
-
 
439
 
-
 
440
	std::vector<CyString> *file = new std::vector<CyString>;
-
 
441
	Utils::String line;
-
 
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
 
433
 
456
CyStringList *CFileIO::ReadLinesStr()
434
CyStringList *CFileIO::ReadLinesStr()
457
{
435
{
458
	if ( m_sFilename.empty() ) return 0;
436
	if ( _sFilename.empty() ) return 0;
459
 
437
 
460
	CyStringList *file = new CyStringList;
438
	CyStringList *file = new CyStringList;
461
	Utils::String line;
439
	Utils::String line;
462
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
440
	std::ifstream infile (_sFilename.c_str(), std::ios_base::in);
463
	while (getline(infile, line, '\n')) {
441
	while (getline(infile, line, '\n')) {
464
		file->PushBack(CyString(line.removeChar((char)0)));
442
		file->PushBack(CyString(line.removeChar((char)0)));
465
	}
443
	}
466
 
444
 
467
	infile.close();
445
	infile.close();
Line 469... Line 447...
469
	return file;
447
	return file;
470
}
448
}
471
 
449
 
472
Utils::CStringList *CFileIO::readLinesStr()
450
Utils::CStringList *CFileIO::readLinesStr()
473
{
451
{
474
	if ( m_sFilename.empty() ) return 0;
452
	if ( _sFilename.empty() ) return 0;
475
 
453
 
476
	Utils::CStringList *file = new Utils::CStringList();
454
	Utils::CStringList *file = new Utils::CStringList();
477
	Utils::String line;
455
	Utils::String line;
478
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
456
	std::ifstream infile (_sFilename.c_str(), std::ios_base::in);
479
	while (getline(infile, line, '\n'))	{
457
	while (getline(infile, line, '\n'))	{
480
		file->pushBack(line.removeChar((char)0));
458
		file->pushBack(line.removeChar((char)0));
481
	}
459
	}
482
 
460
 
483
	infile.close();
461
	infile.close();
Line 544... Line 522...
544
	if ( !this->isOpened() ) return false;
522
	if ( !this->isOpened() ) return false;
545
	try {
523
	try {
546
		m_fId.write((char *)buf, iSize);
524
		m_fId.write((char *)buf, iSize);
547
	}
525
	}
548
	catch (std::exception &e) {
526
	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());
527
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
550
		return false;
528
		return false;
551
	}
529
	}
552
	m_lSize += iSize;
530
	m_lSize += iSize;
553
	return !m_fId.bad();
531
	return !m_fId.bad();
554
}
532
}
Line 576... Line 554...
576
		int iLen = strlen(buffer);
554
		int iLen = strlen(buffer);
577
		m_fId.write((char *)buffer, iLen);
555
		m_fId.write((char *)buffer, iLen);
578
		m_lSize += iLen;
556
		m_lSize += iLen;
579
	}
557
	}
580
	catch (std::exception &e) {
558
	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());
559
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", _sFilename.c_str(), e.what());
582
		return false;
560
		return false;
583
	}
561
	}
584
	return !m_fId.bad();
562
	return !m_fId.bad();
585
}
563
}
586
 
564
 
Line 652... Line 630...
652
bool CFileIO::appendFile ( const Utils::String &filename )
630
bool CFileIO::appendFile ( const Utils::String &filename )
653
{
631
{
654
	std::fstream fromFile(filename, _in());
632
	std::fstream fromFile(filename, _in());
655
	if ( !fromFile.is_open() ) return false;
633
	if ( !fromFile.is_open() ) return false;
656
 
634
 
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);
-
 
668
	size_t size = static_cast<size_t>(fromFile.tellg());
-
 
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
 
-
 
689
bool CFileIO::AppendFile ( CyString filename )
-
 
690
{
-
 
691
	std::fstream fromFile(filename.ToString().c_str(), _in());
-
 
692
	if ( !fromFile.is_open() ) return false;
-
 
693
 
-
 
694
	std::fstream toFile(m_sFilename, _append());
635
	std::fstream toFile(_sFilename, _append());
695
	if ( !toFile.is_open() ) {
636
	if ( !toFile.is_open() ) {
696
		fromFile.close();
637
		fromFile.close();
697
		return false;
638
		return false;
698
	}
639
	}
699
 
640
 
Line 724... Line 665...
724
}
665
}
725
 
666
 
726
 
667
 
727
bool CFileIO::AppendData ( const char *d, size_t size )
668
bool CFileIO::AppendData ( const char *d, size_t size )
728
{
669
{
729
	std::ofstream File(m_sFilename, _append());
670
	std::ofstream File(_sFilename, _append());
730
	if ( !File.is_open() ) return false;
671
	if ( !File.is_open() ) return false;
731
 
672
 
732
	// move to the end of the file
673
	// move to the end of the file
733
	//File.seekg(0, std::ios::end);
674
	//File.seekg(0, std::ios::end);
734
 
675
 
Line 741... Line 682...
741
 
682
 
742
		try {
683
		try {
743
			File.write(pos, read);
684
			File.write(pos, read);
744
		} 
685
		} 
745
		catch(std::exception &e) {
686
		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());
687
			CLog::logf(CLog::Log_IO, 2, "CFileIO::AppendData() unable to write data to file: %s (%s)", _sFilename.c_str(), e.what());
747
			File.close();
688
			File.close();
748
			return false;
689
			return false;
749
		}
690
		}
750
		pos += read;
691
		pos += read;
751
	}
692
	}
Line 782... Line 723...
782
	return str;
723
	return str;
783
}
724
}
784
 
725
 
785
Utils::String CFileIO::baseName() const
726
Utils::String CFileIO::baseName() const
786
{
727
{
787
	return m_sFile.tokens(".", 1, -2);
728
	return _sFile.tokens(".", 1, -2);
788
}
-
 
789
CyString CFileIO::GetFileExtension ()
-
 
790
{
-
 
791
	if ( m_sFilename.empty() ) return NullString;
-
 
792
	return m_sFilename.token(".", -1);
-
 
793
}
-
 
794
 
-
 
795
Utils::String CFileIO::extension ()
-
 
796
{
-
 
797
	if ( m_sFilename.empty() ) return Utils::String::Null();
-
 
798
	return m_sFilename.token(".", -1);
-
 
799
}
-
 
800
 
-
 
801
CyString CFileIO::ChangeFileExtension(CyString ext) const
-
 
802
{
-
 
803
	return changeFileExtension(ext.ToString());
-
 
804
}
729
}
-
 
730
 
-
 
731
Utils::String CFileIO::extension () const
-
 
732
{
-
 
733
	if ( _sFilename.empty() ) return Utils::String::Null();
-
 
734
	return _sFilename.token(".", -1);
-
 
735
}
-
 
736
 
805
Utils::String CFileIO::changeFileExtension(const Utils::String &ext) const
737
Utils::String CFileIO::changeFileExtension(const Utils::String &ext) const
806
{
738
{
807
	if ( m_sFilename.empty() )
739
	if ( _sFilename.empty() )
808
		return Utils::String::Null();
740
		return Utils::String::Null();
809
	
741
	
810
	return m_sFilename.tokens(".", 1, -2) + "." + ext;
742
	return _sFilename.tokens(".", 1, -2) + "." + ext;
811
}
743
}
812
 
744
 
813
bool CFileIO::Remove(const Utils::String &rem)
745
bool CFileIO::Remove(const Utils::String &rem)
814
{
746
{
815
	//if ( !Exists() ) return false;
747
	//if ( !Exists() ) return false;
816
	return (std::remove(rem) == 0) ? true : false;
748
	return (std::remove(rem) == 0) ? true : false;
817
}
749
}
818
 
750
 
819
bool CFileIO::remove()
751
bool CFileIO::remove()
820
{
752
{
821
	if ( this->isOpened() ) this->close();
753
	if ( this->isOpened() ) this->close();
822
	if ( !this->exists() ) return false;
754
	if ( !this->exists() ) return false;
823
	if ( std::remove(m_sFilename.c_str()) == 0 ) return true;
755
	if ( std::remove(_sFilename.c_str()) == 0 ) return true;
824
	return false;
756
	return false;
825
}
757
}
826
 
758
 
827
bool CFileIO::WriteFileUTF(std::vector<CyString> *lines)
759
bool CFileIO::writeFileUTF(std::vector<Utils::String> *lines)
828
{
760
{
829
	if ( !lines || m_sFilename.empty() )
761
	if (!lines || _sFilename.empty())
830
		return false;
762
		return false;
831
 
763
 
832
	// we need to create the directory
764
	// we need to create the directory
833
	if ( !m_sDirIO.exists() )
765
	if (!_sDirIO.exists())
834
	{
766
	{
835
		if ( !m_sDirIO.Create() )
-
 
836
			return false;
-
 
837
	}
-
 
838
 
-
 
839
#ifdef _WIN32
-
 
840
	TCHAR buf[5000];
-
 
841
	wsprintf(buf, L"%hs", m_sFilename.c_str());
-
 
842
	FILE *id = _wfopen(buf, L"wt+,ccs=UTF-8");
-
 
843
	if ( !id )
-
 
844
		return false;
-
 
845
 
-
 
846
	// write the rest
-
 
847
	for ( int i = 0; i < (int)lines->size(); i++ )
-
 
848
	{
-
 
849
		CyString l = lines->at(i);
-
 
850
		if ( l.IsIn('\n') )
-
 
851
		{
-
 
852
			int max;
-
 
853
			CyString *strs = l.SplitToken("\n", &max);
-
 
854
			if ( strs && max )
-
 
855
			{
-
 
856
				for ( int i = 0; i < max; i++ )
-
 
857
				{
-
 
858
					CyString line = strs[i];
-
 
859
					line += "\n";
-
 
860
					int size = wsprintf(buf, L"%hs", line.c_str());
-
 
861
					fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
-
 
862
				}
-
 
863
 
-
 
864
				CLEANSPLIT(strs, max);
-
 
865
			}
-
 
866
		}
-
 
867
		else
-
 
868
		{
-
 
869
			l += "\n";
-
 
870
			int size = wsprintf(buf, L"%hs", l.c_str());
-
 
871
			fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
-
 
872
		}
-
 
873
	}
-
 
874
 
-
 
875
	fclose(id);
-
 
876
 
-
 
877
	return true;
-
 
878
#else
-
 
879
    //TODO: write utf8 file writing function
-
 
880
    return false;
-
 
881
#endif
-
 
882
}
-
 
883
 
-
 
884
bool CFileIO::writeFileUTF(std::vector<Utils::String> *lines)
-
 
885
{
-
 
886
	if (!lines || m_sFilename.empty())
-
 
887
		return false;
-
 
888
 
-
 
889
	// we need to create the directory
-
 
890
	if (!m_sDirIO.exists())
-
 
891
	{
-
 
892
		if (!m_sDirIO.Create())
767
		if (!_sDirIO.create())
893
			return false;
768
			return false;
894
	}
769
	}
895
 
770
 
896
#ifdef _WIN32
771
#ifdef _WIN32
897
	TCHAR buf[5000];
772
	TCHAR buf[5000];
898
	wsprintf(buf, L"%hs", m_sFilename.c_str());
773
	wsprintf(buf, L"%hs", _sFilename.c_str());
899
	FILE *id = _wfopen(buf, L"wt+,ccs=UTF-8");
774
	FILE *id = _wfopen(buf, L"wt+,ccs=UTF-8");
900
	if (!id)
775
	if (!id)
901
		return false;
776
		return false;
902
 
777
 
903
	// write the rest
778
	// write the rest
Line 936... Line 811...
936
	//TODO: write utf8 file writing function
811
	//TODO: write utf8 file writing function
937
	return false;
812
	return false;
938
#endif
813
#endif
939
}
814
}
940
 
815
 
941
bool CFileIO::WriteFile(std::vector<CyString> *lines)
816
bool CFileIO::WriteFile(CyStringList *lines)
942
{
817
{
943
	if ( !lines || m_sFilename.empty() )
818
	if ( !lines || _sFilename.empty() )
944
		return false;
819
		return false;
945
 
820
 
946
	// we need to create the directory
821
	// we need to create the directory
947
	if ( !m_sDirIO.exists() )
822
	if ( !_sDirIO.exists() )
948
	{
823
	{
949
		if ( !m_sDirIO.Create() )
824
		if ( !_sDirIO.create() )
950
			return false;
825
			return false;
951
	}
826
	}
952
 
-
 
953
 
-
 
954
	std::ofstream out(m_sFilename.c_str());
-
 
955
	if ( !out )
-
 
956
		return false;
-
 
957
 
-
 
958
	for ( int i = 0; i < (int)lines->size(); i++ )
-
 
959
	{
-
 
960
		CyString l = lines->at(i);
-
 
961
		out << l.c_str() << std::endl;
-
 
962
	}
-
 
963
 
-
 
964
	out.close();
-
 
965
 
-
 
966
	return true;
-
 
967
}
-
 
968
 
-
 
969
bool CFileIO::WriteFile(CyStringList *lines)
-
 
970
{
-
 
971
	if ( !lines || m_sFilename.empty() )
-
 
972
		return false;
-
 
973
 
-
 
974
	// we need to create the directory
-
 
975
	if ( !m_sDirIO.exists() )
-
 
976
	{
-
 
977
		if ( !m_sDirIO.Create() )
-
 
978
			return false;
-
 
979
	}
-
 
980
 
827
 
981
	std::ofstream out(m_sFilename.c_str());
828
	std::ofstream out(_sFilename.c_str());
982
	if ( !out )
829
	if ( !out )
983
		return false;
830
		return false;
984
 
831
 
985
	/*
832
	/*
986
	if ( utf )
833
	if ( utf )
Line 993... Line 840...
993
 
840
 
994
		out << smarker;
841
		out << smarker;
995
	}
842
	}
996
*/
843
*/
997
	for ( int i = 0; i < (int)lines->Count(); i++ )
844
	for ( int i = 0; i < (int)lines->Count(); i++ )
998
	{
845
	{
999
		CyString l = lines->StringAt(i);
846
		CyString l = lines->StringAt(i);
1000
		out << l.c_str() << std::endl;
847
		out << l.c_str() << std::endl;
1001
	}
848
	}
1002
 
849
 
1003
	out.close();
850
	out.close();
1004
 
851
 
1005
	return true;
852
	return true;
1006
}
853
}
1007
 
854
 
1008
bool CFileIO::writeFile(std::vector<Utils::String> *lines)
855
bool CFileIO::writeFile(std::vector<Utils::String> *lines)
1009
{
856
{
1010
	if (!lines || m_sFilename.empty())
857
	if (!lines || _sFilename.empty())
1011
		return false;
858
		return false;
1012
 
859
 
1013
	// we need to create the directory
860
	// we need to create the directory
1014
	if (!m_sDirIO.exists())
861
	if (!_sDirIO.exists())
1015
	{
862
	{
1016
		if (!m_sDirIO.Create())
863
		if (!_sDirIO.create())
1017
			return false;
864
			return false;
1018
	}
865
	}
1019
 
866
 
1020
 
867
 
1021
	std::ofstream out(m_sFilename.c_str());
868
	std::ofstream out(_sFilename.c_str());
1022
	if (!out)
869
	if (!out)
1023
		return false;
870
		return false;
1024
 
871
 
1025
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
872
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
1026
		out << (*itr).c_str() << std::endl;
873
		out << (*itr).c_str() << std::endl;
Line 1030... Line 877...
1030
	return true;
877
	return true;
1031
}
878
}
1032
 
879
 
1033
bool CFileIO::writeFile(Utils::CStringList *lines)
880
bool CFileIO::writeFile(Utils::CStringList *lines)
1034
{
881
{
1035
	if (!lines || m_sFilename.empty())
882
	if (!lines || _sFilename.empty())
1036
		return false;
883
		return false;
1037
 
884
 
1038
	// we need to create the directory
885
	// we need to create the directory
1039
	if (!m_sDirIO.exists())
886
	if (!_sDirIO.exists())
1040
	{
887
	{
1041
		if (!m_sDirIO.Create())
888
		if (!_sDirIO.create())
1042
			return false;
889
			return false;
1043
	}
890
	}
1044
 
891
 
1045
	std::ofstream out(m_sFilename.c_str());
892
	std::ofstream out(_sFilename.c_str());
1046
	if (!out)
893
	if (!out)
1047
		return false;
894
		return false;
1048
 
895
 
1049
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
896
	for(auto itr = lines->begin(); itr != lines->end(); itr++)
1050
		out << (*itr)->str.c_str() << std::endl;
897
		out << (*itr)->str.c_str() << std::endl;
1051
 
898
 
1052
	out.close();
899
	out.close();
1053
 
900
 
1054
	return true;
901
	return true;
1055
}
902
}
1056
 
903
 
1057
bool CFileIO::Rename(const Utils::String &toFile)
904
bool CFileIO::Rename(const Utils::String &toFile)
1058
{
905
{
1059
	if ( rename(m_sFilename.c_str(), toFile.c_str()) == 0 )
906
	if ( rename(_sFilename.c_str(), toFile.c_str()) == 0 )
1060
		return true;
907
		return true;
1061
	return false;
908
	return false;
1062
}
909
}
1063
 
910
 
1064
CyString CFileIO::GetWindowsFilename()
911
Utils::String CFileIO::getWindowsFilename() const
1065
{
912
{
1066
	CyString returnString = m_sFilename;
913
	Utils::String returnString = _sFilename;
1067
	returnString = returnString.FindReplace("/", "\\");
914
	returnString = returnString.findReplace("/", "\\");
1068
	return returnString;
915
	return returnString;
1069
}
916
}
1070
 
917
 
1071
void CFileIO::SetCreationTime(time_t time)
918
void CFileIO::setCreationTime(time_t time)
1072
{
919
{
1073
#ifdef _WIN32
920
#ifdef _WIN32
1074
	// Note that LONGLONG is a 64-bit value
921
	// Note that LONGLONG is a 64-bit value
1075
    LONGLONG ll;
922
    LONGLONG ll;
1076
 
923
 
1077
	FILETIME ft;
924
	FILETIME ft;
1078
    ll = Int32x32To64(time, 10000000) + 116444736000000000;
925
    ll = Int32x32To64(time, 10000000) + 116444736000000000;
1079
    ft.dwLowDateTime = (DWORD)ll;
926
    ft.dwLowDateTime = (DWORD)ll;
1080
    ft.dwHighDateTime = ll >> 32;
927
    ft.dwHighDateTime = ll >> 32;
-
 
928
 
-
 
929
	Utils::String windowsFilename = getWindowsFilename();
1081
 
930
 
1082
	WCHAR    str[MAX_PATH];
931
	WCHAR    str[MAX_PATH];
1083
	MultiByteToWideChar(CP_ACP, NULL, GetWindowsFilename().c_str(), -1, str, GetWindowsFilename().Length() + 1);
932
	MultiByteToWideChar(CP_ACP, NULL, windowsFilename.c_str(), -1, str, windowsFilename.length() + 1);
1084
	HANDLE filename = CreateFile(str, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
933
	HANDLE filename = CreateFile(str, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1085
	// Set the file time on the file
934
	// Set the file time on the file
1086
	SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&ft);
935
	SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&ft);
1087
	// Close our handle.
936
	// Close our handle.
1088
	CloseHandle(filename);
937
	CloseHandle(filename);
1089
 
938
 
1090
#endif
939
#endif
1091
}
940
}
1092
 
941
 
1093
time_t CFileIO::GetCreationTime()
942
time_t CFileIO::creationTime() const
1094
{
943
{
1095
	return modifiedTime();
944
	return modifiedTime();
1096
}
945
}
1097
 
946
 
1098
time_t CFileIO::modifiedTime()
947
time_t CFileIO::modifiedTime() const
1099
{
948
{
1100
#ifdef _WIN32
949
#ifdef _WIN32
1101
	WCHAR    str[MAX_PATH];
950
	WCHAR    str[MAX_PATH];
-
 
951
	Utils::String windowsFilename = getWindowsFilename();
1102
	MultiByteToWideChar(CP_ACP, NULL, GetWindowsFilename().c_str(), -1, str, GetWindowsFilename().Length() + 1);
952
	MultiByteToWideChar(CP_ACP, NULL, windowsFilename.c_str(), windowsFilename.length() + 1, str, MAX_PATH);
1103
 
953
 
1104
	WIN32_FILE_ATTRIBUTE_DATA wfad;
954
	WIN32_FILE_ATTRIBUTE_DATA wfad;
1105
	GetFileAttributesEx(str, GetFileExInfoStandard, &wfad);
955
	GetFileAttributesEx(str, GetFileExInfoStandard, &wfad);
1106
 
956
 
1107
	LARGE_INTEGER date, adjust;
957
	LARGE_INTEGER date, adjust;
Line 1134... Line 984...
1134
 *
984
 *
1135
 * Reads and writes the files in block
985
 * Reads and writes the files in block
1136
 */
986
 */
1137
bool CFileIO::copy(const Utils::String &toFile, bool keepTime)
987
bool CFileIO::copy(const Utils::String &toFile, bool keepTime)
1138
{
988
{
1139
	time_t time = GetCreationTime();
989
	time_t time = creationTime();
1140
 
990
 
1141
	CFileIO File(toFile);
991
	CFileIO File(toFile);
1142
	if ( File.write(*this, m_lSize) ) {
992
	if ( File.write(*this, m_lSize) ) {
1143
		this->close();
993
		this->close();
1144
		File.close();
994
		File.close();
1145
		if ( keepTime )	File.SetCreationTime(time);
995
		if ( keepTime )	File.setCreationTime(time);
1146
		return true;
996
		return true;
1147
	}
997
	}
1148
/*
998
/*
1149
	std::fstream f(GetWindowsFilename().c_str(), std::fstream::in | std::fstream::binary);
999
	std::fstream f(GetWindowsFilename().c_str(), std::fstream::in | std::fstream::binary);
1150
	f << std::noskipws;
1000
	f << std::noskipws;
Line 1161... Line 1011...
1161
}
1011
}
1162
 
1012
 
1163
size_t CFileIO::fileSize() const
1013
size_t CFileIO::fileSize() const
1164
{
1014
{
1165
	return m_lSize;
1015
	return m_lSize;
-
 
1016
}
-
 
1017
 
-
 
1018
 
-
 
1019
void CFileIO::_updateFilename()
-
 
1020
{
-
 
1021
	if (_sFilename.empty()) _sExt = Utils::String::Null();
-
 
1022
	_sExt = _sFilename.token(".", -1);
1166
}
1023
}