Subversion Repositories spk

Rev

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

Rev 222 Rev 223
Line 430... Line 430...
430
	}
430
	}
431
	m_bSeekP = bSeekP;
431
	m_bSeekP = bSeekP;
432
	return m_fId.is_open();
432
	return m_fId.is_open();
433
}
433
}
434
 
434
 
435
std::vector<Utils::String>* CFileIO::readLines() const
-
 
436
{
-
 
437
	if (_sFilename.empty()) return 0;
-
 
438
 
-
 
439
	std::vector<Utils::String>* file = new std::vector<Utils::String>;
-
 
440
	Utils::String line;
-
 
441
	file->clear();
-
 
442
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
-
 
443
	while (getline(infile, line, '\n')) {
-
 
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
435
size_t CFileIO::readLines(std::vector<Utils::String>& to) const
452
{
436
{
453
	if (_sFilename.empty()) return 0;
437
	if (_sFilename.empty()) return 0;
454
 
438
 
455
	Utils::String line;
439
	Utils::String line;
456
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
440
	std::ifstream infile(_sFilename.c_str(), std::ios_base::in);
457
	while (getline(infile, line, '\n')) {
441
	while (getline(infile, line, '\n')) {
458
		to.push_back(Utils::String(line).removeChar((char)0));
442
		to.push_back(Utils::String(line).removeChar((char)0));
459
	}
443
	}
460
 
444
 
461
	infile.close();
445
	infile.close();
462
 
446
 
463
	return to.size();
447
	return to.size();
464
}
448
}
465
 
449
 
466
size_t CFileIO::readLines(std::vector<Utils::WString>& to) const
450
size_t CFileIO::readLines(std::vector<Utils::WString>& to) const
467
{
451
{
468
	if (_sFilename.empty()) return 0;
452
	if (_sFilename.empty()) return 0;
469
 
453
 
470
	std::wstring line;
454
	std::wstring line;
471
	std::wifstream infile(_sFilename.c_str(), std::ios_base::in);
455
	std::wifstream infile(_sFilename.c_str(), std::ios_base::in);
472
	while (getline(infile, line, L'\n')) {
456
	while (getline(infile, line, L'\n')) {
473
		to.push_back(Utils::WString(line).remove((char)0));
457
		to.push_back(Utils::WString(line).remove((char)0));
474
	}
458
	}
475
 
459
 
476
	infile.close();
460
	infile.close();
477
 
461
 
478
	return to.size();
462
	return to.size();
479
}
463
}
480
 
464
 
481
size_t CFileIO::readLines(Utils::WStringList& to) const
465
size_t CFileIO::readLines(Utils::WStringList& to) const
482
{
466
{
483
	if (_sFilename.empty()) return 0;
467
	if (_sFilename.empty()) return 0;
484
 
468
 
485
	std::wstring line;
469
	std::wstring line;
Line 487... Line 471...
487
	while (getline(infile, line, L'\n')) {
471
	while (getline(infile, line, L'\n')) {
488
		to.pushBack(Utils::WString(line).remove((char)0));
472
		to.pushBack(Utils::WString(line).remove((char)0));
489
	}
473
	}
490
 
474
 
491
	infile.close();
475
	infile.close();
492
 
476
 
493
	return to.size();
477
	return to.size();
494
}
478
}
495
 
479
 
496
bool CFileIO::put(const unsigned char c)
480
bool CFileIO::put(const unsigned char c)
497
{
481
{
498
	if ( !this->isOpened() ) return false;
482
	if ( !this->isOpened() ) return false;
499
 
483
 
500
	m_fId.put(c);
484
	m_fId.put(c);
501
	return !m_fId.bad();
485
	return !m_fId.bad();
502
}
486
}
503
 
487
 
Line 541... Line 525...
541
	}
525
	}
542
 
526
 
543
	delete data;
527
	delete data;
544
 
528
 
545
	return bSuccess;
529
	return bSuccess;
546
}
530
}
547
 
531
 
548
bool CFileIO::write(const char *buf, size_t iSize)
532
bool CFileIO::write(const char *buf, size_t iSize)
549
{
533
{
550
	if ( !this->isOpened() ) startWrite();
534
	if ( !this->isOpened() ) startWrite();
551
	if ( !this->isOpened() ) return false;
535
	if ( !this->isOpened() ) return false;
Line 684... Line 668...
684
	if ( !fromFile.is_open() ) return false;
668
	if ( !fromFile.is_open() ) return false;
685
 
669
 
686
	std::fstream toFile(_sFilename, _append());
670
	std::fstream toFile(_sFilename, _append());
687
	if ( !toFile.is_open() ) {
671
	if ( !toFile.is_open() ) {
688
		fromFile.close();
672
		fromFile.close();
689
		return false;
673
		return false;
690
	}
674
	}
691
 
675
 
692
	// move to the end of the file
676
	// move to the end of the file
693
	toFile.seekg(0, std::ios::end);
677
	toFile.seekg(0, std::ios::end);
694
 
678
 
Line 789... Line 773...
789
	return _sFile.tokens(L".", 1, -2);
773
	return _sFile.tokens(L".", 1, -2);
790
}
774
}
791
 
775
 
792
const Utils::WString& CFileIO::fullFilename() const { return _sFilename; }
776
const Utils::WString& CFileIO::fullFilename() const { return _sFilename; }
793
const Utils::WString& CFileIO::filename() const { return _sFile; }
777
const Utils::WString& CFileIO::filename() const { return _sFile; }
794
 
-
 
795
const Utils::String CFileIO::fullFilenameStr() const { return _sFilename.toString(); }
-
 
796
const Utils::String CFileIO::filenameStr() const { return _sFile.toString(); }
-
 
797
 
778
 
798
Utils::WString CFileIO::extension () const
779
Utils::WString CFileIO::extension () const
799
{
780
{
800
	if ( _sFilename.empty() ) return Utils::WString::Null();
781
	if ( _sFilename.empty() ) return Utils::WString::Null();
801
	return _sFilename.token(L".", -1);
782
	return _sFilename.token(L".", -1);
802
}
783
}
803
 
784
 
804
Utils::WString CFileIO::changeFileExtension(const Utils::WString &ext) const
785
Utils::WString CFileIO::changeFileExtension(const Utils::WString &ext) const
805
{
786
{
806
	if ( _sFilename.empty() )
787
	if ( _sFilename.empty() )
807
		return Utils::WString::Null();
788
		return Utils::WString::Null();
808
	
789
	
809
	return _sFilename.tokens(L".", 1, -2) + L"." + ext;
790
	return _sFilename.tokens(L".", 1, -2) + L"." + ext;
810
}
791
}
811
 
792
 
812
bool CFileIO::Remove(const Utils::WString& rem)
793
bool CFileIO::Remove(const Utils::WString& rem)
813
{
794
{
814
#ifdef _WIN32
795
#ifdef _WIN32
815
	return DeleteFile(rem.c_str());
796
	return DeleteFile(rem.c_str());
816
#else
797
#else
817
	//if ( !Exists() ) return false;
798
	//if ( !Exists() ) return false;
818
	return (std::remove(_toFileData(rem)) == 0) ? true : false;
799
	return (std::remove(_toFileData(rem)) == 0) ? true : false;
819
#endif
800
#endif
820
}
801
}
821
 
802
 
822
bool CFileIO::Rename(const Utils::WString& from, const Utils::WString &to) 
803
bool CFileIO::Rename(const Utils::WString& from, const Utils::WString &to) 
823
{
804
{
824
	if (std::rename(_toFileData(from).c_str(), _toFileData(to).c_str()) == 0)
805
	if (std::rename(_toFileData(from).c_str(), _toFileData(to).c_str()) == 0)
825
		return true;
806
		return true;
826
	return false;
807
	return false;
827
}
808
}
828
 
809
 
829
bool CFileIO::remove()
810
bool CFileIO::remove()
830
{
811
{
831
	if ( this->isOpened() ) this->close();
812
	if ( this->isOpened() ) this->close();
832
	if ( !this->exists() ) return false;
813
	if ( !this->exists() ) return false;
833
	if ( CFileIO::Remove(_sFilename)) return true;
814
	if ( CFileIO::Remove(_sFilename)) return true;
834
	return false;
815
	return false;
835
}
-
 
836
 
-
 
837
bool CFileIO::writeFileUTF(std::vector<Utils::String>* lines)
-
 
838
{
-
 
839
	if (!lines || _sFilename.empty())
-
 
840
		return false;
-
 
841
 
-
 
842
	// we need to create the directory
-
 
843
	if (!_sDirIO.exists())
-
 
844
	{
-
 
845
		if (!_sDirIO.create())
-
 
846
			return false;
-
 
847
	}
-
 
848
 
-
 
849
#ifdef _WIN32
-
 
850
	TCHAR buf[5000];
-
 
851
	FILE* id = _wfopen(_sFilename.c_str(), L"wt+,ccs=UTF-8");
-
 
852
	if (!id)
-
 
853
		return false;
-
 
854
 
-
 
855
	// write the rest
-
 
856
	for (auto itr = lines->begin(); itr != lines->end(); itr++)
-
 
857
	{
-
 
858
		Utils::String l = (*itr);
-
 
859
		if (l.isin('\n'))
-
 
860
		{
-
 
861
			int max;
-
 
862
			Utils::String* strs = l.tokenise("\n", &max);
-
 
863
			if (strs && max)
-
 
864
			{
-
 
865
				for (int i = 0; i < max; i++)
-
 
866
				{
-
 
867
					Utils::String line = strs[i];
-
 
868
					line += "\n";
-
 
869
					int size = wsprintf(buf, L"%hs", line.c_str());
-
 
870
					fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
-
 
871
				}
-
 
872
 
-
 
873
				CLEANSPLIT(strs, max);
-
 
874
			}
-
 
875
		}
-
 
876
		else
-
 
877
		{
-
 
878
			l += "\n";
-
 
879
			int size = wsprintf(buf, L"%hs", l.c_str());
-
 
880
			fwrite(buf, sizeof(TCHAR), wcslen(buf), id);
-
 
881
		}
-
 
882
	}
-
 
883
 
-
 
884
	fclose(id);
-
 
885
 
-
 
886
	return true;
-
 
887
#else
-
 
888
	//TODO: write utf8 file writing function
-
 
889
	return false;
-
 
890
#endif
-
 
891
}
816
}
892
 
817
 
893
bool CFileIO::writeFileUTF(std::vector<Utils::WString>* lines)
818
bool CFileIO::writeFileUTF(std::vector<Utils::WString>* lines)
894
{
819
{
895
	if (!lines || _sFilename.empty())
820
	if (!lines || _sFilename.empty())