From 772e77ffe3e44194b63c0520e65b0af49d702872 Mon Sep 17 00:00:00 2001 From: Wanne Date: Wed, 10 Jun 2009 21:12:37 +0000 Subject: [PATCH] - Added missing VFS\Tools files and folder. git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@3000 3b4a5df2-a311-0410-b5c6-a8a6f20db521 --- VFS/Tools/Log.cpp | 217 +++++++++++++++++ VFS/Tools/Log.h | 76 ++++++ VFS/Tools/ParserTools.cpp | 473 ++++++++++++++++++++++++++++++++++++++ VFS/Tools/ParserTools.h | 122 ++++++++++ VFS/Tools/Tools.cpp | 22 ++ VFS/Tools/Tools.h | 54 +++++ 6 files changed, 964 insertions(+) create mode 100644 VFS/Tools/Log.cpp create mode 100644 VFS/Tools/Log.h create mode 100644 VFS/Tools/ParserTools.cpp create mode 100644 VFS/Tools/ParserTools.h create mode 100644 VFS/Tools/Tools.cpp create mode 100644 VFS/Tools/Tools.h diff --git a/VFS/Tools/Log.cpp b/VFS/Tools/Log.cpp new file mode 100644 index 000000000..c09d5896f --- /dev/null +++ b/VFS/Tools/Log.cpp @@ -0,0 +1,217 @@ +#include "Log.h" + +#ifdef WIN32 +const char CLog::endl[] = "\r\n"; +#else +const char CLog::endl[] = "\n"; +#endif + +std::list& CLog::_logs() +{ + static std::list logs; + return logs; +} + +CLog* CLog::Create(vfs::Path fileName, bool append, EFlushMode flushMode) +{ + _logs().push_back(new CLog(fileName, append, flushMode)); + return _logs().back(); +} +void CLog::FlushFinally() +{ + std::list::iterator it = _logs().begin(); + for(; it != _logs().end(); ++it) + { + delete *it; + } + _logs().clear(); +} + +void CLog::FlushAll() +{ + std::list::iterator it = _logs().begin(); + for(; it != _logs().end(); ++it) + { + (*it)->Flush(); + (*it)->_file = NULL; + } +} + + +CLog::CLog(vfs::Path fileName, bool append, EFlushMode flushMode) +: _filename(fileName), _file(NULL), _own_file(false), + _first_write(true), _flush_mode(flushMode), _append(append), + _buffer_size(0), _buffer_test_size(512) +{}; + +CLog::~CLog() +{ + _test_flush(true); + if(_file && _own_file) + { + delete _file; + } +} + +CLog& CLog::operator<<(unsigned int const& t) +{ + return PushNumber(t); +} +CLog& CLog::operator<<(unsigned short const& t) +{ + return PushNumber(t); +} +CLog& CLog::operator<<(unsigned char const& t) +{ + return PushNumber(t); +} +CLog& CLog::operator<<(int const& t) +{ + return PushNumber(t); +} +CLog& CLog::operator<<(short const& t) +{ + return PushNumber(t); +} +CLog& CLog::operator<<(char const& t) +{ + return PushNumber(t); +} +CLog& CLog::operator<<(float const& t) +{ + return PushNumber(t); +} +CLog& CLog::operator<<(double const& t) +{ + return PushNumber(t); +} + +CLog& CLog::operator<<(const char* t) +{ + _buffer << t; + _buffer_size += strlen(t); + _test_flush(); + return *this; +} +CLog& CLog::operator<<(const wchar_t* t) +{ + std::string s = utf8string::as_utf8(t); + _buffer << s; + _buffer_size += s.length(); + _test_flush(); + return *this; +} +CLog& CLog::operator<<(std::string const& t) +{ + _buffer << t; + _buffer_size += t.length(); + _test_flush(); + return *this; +} +CLog& CLog::operator<<(std::wstring const& t) +{ + std::string s = utf8string::as_utf8(t); + _buffer << s; + _buffer_size += s.length(); + _test_flush(); + return *this; +} +CLog& CLog::operator<<(utf8string const& t) +{ + std::string s = t.utf8(); + _buffer << s; + _buffer_size += s.length(); + _test_flush(); + return *this; +} + +CLog& CLog::Endl() +{ + _buffer << CLog::endl; + _buffer_size += sizeof(CLog::endl)-1; + _test_flush(); + return *this; +} + +void CLog::SetAppend(bool append) +{ + _append = append; +} + +void CLog::SetBufferSize(vfs::UInt32 bufferSize) +{ + _buffer_test_size = bufferSize; +} + +void CLog::_test_flush(bool force) +{ + if( (_flush_mode == FLUSH_IMMEDIATELY) || + (_flush_mode == FLUSH_BUFFER && _buffer_size > _buffer_test_size) || + (/*_flush_mode == FLUSH_ON_DELETE &&*/ force == true) ) + { + Flush(); + } +} + +#include +void CLog::Flush() +{ + vfs::UInt32 buflen = _buffer.str().length(); + if(buflen == 0) + { + return; + } + + if(!_file) + { + try + { + vfs::COpenWriteFile file_raii(_filename,true,!_append); + _file = &file_raii.file(); + file_raii.release(); + } + catch(CBasicException& ex) + { + LogException(ex); + // write file anyway + _file = vfs::tWriteableFile::Cast(new vfs::CFile(_filename)); + _file->OpenWrite(true,!_append); + _own_file = true; + } + } + + vfs::COpenWriteFile wfile(_file); + if(_append) + { + wfile.file().SetWriteLocation(0,vfs::IBaseFile::SD_END); + } + + vfs::UInt32 io; + if(_first_write) + { + time_t rawtime; + time ( &rawtime ); + std::string datetime(ctime(&rawtime)); + std::string s_out; + + vfs::UInt32 wloc = wfile.file().GetWriteLocation(); + if(wloc > 0) + { + s_out = CLog::endl; + } + s_out += " *** "; + s_out += datetime.substr(0,datetime.length()-1); + s_out += " *** "; + s_out += CLog::endl; + s_out += CLog::endl; + wfile.file().Write(s_out.c_str(), s_out.length(), io); + _first_write = false; + } + + wfile.file().Write(_buffer.str().c_str(), buflen, io); + _buffer.str(""); + _buffer.clear(); + _buffer_size = 0; + + _append = true; +} diff --git a/VFS/Tools/Log.h b/VFS/Tools/Log.h new file mode 100644 index 000000000..77ccd1a0a --- /dev/null +++ b/VFS/Tools/Log.h @@ -0,0 +1,76 @@ +#ifndef _LOG_H_ +#define _LOG_H_ + +#include "Tools.h" +#include "../vfs_types.h" +#include "../Interface/vfs_file_interface.h" +#include "../File/vfs_file.h" +#include "../vfs_file_raii.h" + +//class CMemoryFile; +class CLog +{ +public: + enum EFlushMode + { + FLUSH_ON_DELETE, + FLUSH_BUFFER, + FLUSH_IMMEDIATELY, + }; +public: + + CLog(vfs::Path fileName, bool append = false, EFlushMode flushMode = FLUSH_ON_DELETE); + ~CLog(); + + static CLog* Create(vfs::Path fileName, bool append = false, EFlushMode flushMode = FLUSH_ON_DELETE); + static void FlushAll(); + static void FlushFinally(); + + CLog& operator<<(unsigned int const& t); + CLog& operator<<(unsigned short const& t); + CLog& operator<<(unsigned char const& t); + CLog& operator<<(int const& t); + CLog& operator<<(short const& t); + CLog& operator<<(char const& t); + CLog& operator<<(float const& t); + CLog& operator<<(double const& t); + + CLog& operator<<(const char* t); + CLog& operator<<(const wchar_t* t); + CLog& operator<<(std::string const& t); + CLog& operator<<(std::wstring const& t); + CLog& operator<<(utf8string const& t); + + CLog& Endl(); + static const char endl[]; + + void SetAppend(bool append = true); + void SetBufferSize(vfs::UInt32 bufferSize); + + void Flush(); +private: + void _test_flush(bool force=false); + + template + CLog& PushNumber(T_ const& t) + { + _buffer << ToString(t); + _buffer_size += sizeof(T_); + _test_flush(); + return *this; + } +private: + vfs::Path _filename; + vfs::tWriteableFile* _file; + bool _own_file; + bool _first_write; + EFlushMode _flush_mode; + bool _append; + vfs::UInt32 _buffer_size, _buffer_test_size; + std::stringstream _buffer; +private: + static std::list& _logs(); +}; + + +#endif // _LOG_H_ diff --git a/VFS/Tools/ParserTools.cpp b/VFS/Tools/ParserTools.cpp new file mode 100644 index 000000000..86fbc847d --- /dev/null +++ b/VFS/Tools/ParserTools.cpp @@ -0,0 +1,473 @@ +#include "ParserTools.h" +#include "Tools.h" +#include "../vfs.h" +#include "../File/vfs_file.h" +#include "../vfs_file_raii.h" + +/*************************************************************************************/ +/*************************************************************************************/ + +CReadLine::CReadLine(vfs::tReadableFile& rFile) +: _file(rFile), _buffer_pos(0), _eof(false) +{ + memset(_buffer,0,sizeof(_buffer)); + FillBuffer(); + vfs::UByte utf8bom[3] = {0xef,0xbb,0xbf}; + if(memcmp(utf8bom, &_buffer[0],3) == 0) + { + _buffer_pos += 3; + } +}; + +bool CReadLine::FillBuffer() +{ + if(!_eof) + { + // reading can be done byte-wise or line-wise. + // we have no control over this here, so we have to account for both cases. + + vfs::UInt32 read = 0; + // fill the buffer from the start, BUFFER_SIZE charactes at max (_buffer has BUFFER_SIZE+1 elements) + _eof = !_file.Read(&_buffer[0],BUFFER_SIZE,read); + // if read() fails, it means that we are at the end of the file, + // but we probably have read a couple of valid bytes, so we need to do one last 'correct' run + + // bite-wise read files usually terminate a line with \n (or \r\n on WIN32) + // line-wise read files just returns 0-terminated string + + // always terminate the string with 0 + _buffer[read] = 0; + + _buffer_pos = 0; + _buffer_last = read; + return true; + } + return false; +} + +bool CReadLine::FromBuffer(std::string& line) +{ + bool done = false; + while(!done) + { + if(_buffer_pos < _buffer_last) + { + // start where we left last time + vfs::Byte *temp = &_buffer[_buffer_pos]; + vfs::UInt32 start_pos = _buffer_pos; + // go until we hit 0. since our buffer is always 0 terminated, the second test should be redundant. + while(*temp && (_buffer_pos < _buffer_last)) + { + // stop when we find a line terminator + if(*temp == '\n' || *temp == '\r' /* || *temp == '\0' */) + { + break; + } + temp++; + _buffer_pos++; + } + // need to append substring, as we might have refilles the buffer (because there was no \n or \r\n terminator) + line.append( (char*)&_buffer[start_pos], _buffer_pos - start_pos ); + + // if we reach the (real) end of the buffer (that always terminate with 0), this means + // that there was no line terminator and that we have to refill the buffer. + if( _buffer_pos < BUFFER_SIZE && (*temp == '\n' || *temp == '\r' || *temp == 0) ) + { + // found the line terminator + if(*temp == '\r') + { + // the \r is most probably followed by \n. 'swallow' both characters + *temp++; + _buffer_pos++; + if( (_buffer_pos < BUFFER_SIZE) && (*temp == '\n' || *temp == 0) ) + { + // increase buffer position, so that we can start with a valid character in the next run + _buffer_pos++; + return true; + } + else + { + done = !FillBuffer(); + } + } + else if(*temp == '\n' || *temp == 0) + { + // increase buffer position, so that we can start with a valid character in the next run + _buffer_pos++; + return true; + } + } + else + { + done = !FillBuffer(); + } + } + else + { + done = !FillBuffer(); + } + } + return false; +} + +bool CReadLine::GetLine(std::string& line) +{ + line.clear(); + return FromBuffer(line); +} + +/*************************************************************************************/ +/*************************************************************************************/ + +CSplitStringList::CSplitStringList(utf8string const& sList) +: m_sList(sList), iCurrent(0),iNext(0) +{}; + +CSplitStringList::~CSplitStringList() +{}; + +bool CSplitStringList::NextListEntry(utf8string &sEntry) +{ +if(iNext >= 0) + { + iNext = m_sList.c_wcs().find_first_of(L",",iCurrent+1); + if(iNext > iCurrent) + { + sEntry.r_wcs().assign(vfs::TrimString(m_sList,iCurrent,iNext-1).c_wcs()); + iCurrent = iNext+1; + } + else + { + // last or only entry + sEntry.r_wcs().assign(vfs::TrimString(m_sList,iCurrent,m_sList.length()).c_wcs()); + } + return true; + } + return false; +} + +/*************************************************************************************/ +/*************************************************************************************/ + +CTransferRules::CTransferRules() +: m_eDefaultAction(ACCEPT) +{}; + + +bool CTransferRules::InitFromTxtFile(vfs::Path const& sPath) +{ + // try to open via VirtualFileSystem + if(GetVFS()->FileExists(sPath)) + { + return InitFromTxtFile(GetVFS()->GetRFile(sPath)); + } + else + { + // file doesn't exist or VFS not initialized yet + vfs::IBaseFile* pFile = new vfs::CTextFile(sPath); + if(pFile) + { + bool success = InitFromTxtFile(vfs::tReadableFile::Cast(pFile)); + delete pFile; + return success; + } + } + return false; +} + +bool CTransferRules::InitFromTxtFile(vfs::tReadableFile* pFile) +{ + if(pFile && pFile->OpenRead()) + { + vfs::COpenReadFile rfile(pFile); + std::string sBuffer; + vfs::UInt32 line_counter = 0; + CReadLine rl(*pFile); + while(rl.GetLine(sBuffer)) + { + line_counter++; + // very simple parsing : key = value + if(!sBuffer.empty()) + { + // remove leading white spaces + int iStart = sBuffer.find_first_not_of(" \t",0); + char first = sBuffer.at(iStart); + switch(first) + { + case '!': + case ';': + case '#': + // comment -> do nothing + break; + default: + int iEnd = sBuffer.find_first_of(" \t", iStart); + if(iEnd != std::string::npos) + { + SRule rule; + std::string action = sBuffer.substr(iStart, iEnd - iStart); + if( StrCmp::Equal(action, "deny") ) + { + rule.action = CTransferRules::DENY; + } + else if( StrCmp::Equal(action, "accept") ) + { + rule.action = CTransferRules::ACCEPT; + } + else + { + std::wstring trybuffer = L"Invalid UTF-8 character in string"; + IGNOREEXCEPTION( trybuffer = utf8string(sBuffer).c_wcs() ); /* just make sure we don't break off when string conversion fails */ + std::wstringstream wss; + wss << L"Unknown action in file \"" << pFile->GetFullPath()().c_wcs() + << L", line " << line_counter << " : " << utf8string(sBuffer).c_wcs(); + THROWEXCEPTION(wss.str().c_str()); + } + try + { + rule.pattern = vfs::Path(vfs::TrimString(sBuffer, iEnd, sBuffer.length())); + } + catch(CBasicException& ex) + { + std::wstringstream wss; + wss << L"Could not convert string, invalid utf8 encoding in file \"" << pFile->GetFullPath()().c_wcs() + << L"\", line " << line_counter; + RETHROWEXCEPTION(wss.str().c_str(),&ex); + } + m_listRules.push_back(rule); + } + break; + }; // end switch + } // end if (empty) + } // end while(!eof) + return true; + } + return false; +} + +void CTransferRules::SetDefaultAction(CTransferRules::EAction act) +{ + m_eDefaultAction = act; +} + +CTransferRules::EAction CTransferRules::GetDefaultAction() +{ + return m_eDefaultAction; +} + +CTransferRules::EAction CTransferRules::ApplyRule(utf8string const& sStr) +{ + tPatternList::iterator sit = m_listRules.begin(); + for(; sit != m_listRules.end(); ++sit) + { + if(MatchPattern(sit->pattern(), sStr)) + { + return sit->action; + } + } + return m_eDefaultAction; +} + +/*************************************************************************************/ +/*************************************************************************************/ +typedef vfs::UInt16 UINT16; +extern UINT16 gsKeyTranslationTable[1024]; + +CodePageReader::EMode CodePageReader::ExtractMode(std::string const &readStr, size_t startPos) +{ + vfs::Int32 iEqual = readStr.find_first_of("=",startPos); + if(iEqual < 0) + { + return Error; + } + // extract keyword + std::string key = vfs::TrimString(readStr,0,iEqual-1); + if(!StrCmp::Equal(key, "mode")) + { + return Error; + } + // extract mode + EMode mode = Error; + std::string sMode = vfs::TrimString(readStr,iEqual+1,readStr.size()); + if(StrCmp::Equal(sMode, "normal")) + { + mode = Normal; + } + else if(StrCmp::Equal(sMode, "shift")) + { + mode = Shift; + } + else if(StrCmp::Equal(sMode, "ctrl")) + { + mode = Ctrl; + } + else if(StrCmp::Equal(sMode, "alt")) + { + mode = Alt; + } + return mode; +} + +bool CodePageReader::ExtractCode(std::string const& str, int iStart, vfs::UInt32& rInsertPoint, utf8string::str_t& u8s) +{ + vfs::Int32 iEqual = str.find_first_of("=",iStart); + if(iEqual < 0) + { + return false; + } + // extract insert point + std::string sip = vfs::TrimString(str,0,iEqual-1); + if(!ConvertTo(sip, rInsertPoint)) + { + return false; + } + utf8string::str_t u8temp; + IGNOREEXCEPTION(utf8string::as_utf16(str.substr(iEqual, str.length()-iEqual), u8temp)); + + vfs::Int32 iCodeStart, iCodeEnd; + iCodeStart = u8temp.find_first_of(L"{", 0); + if(iCodeStart < 0) + { + return false; + } + iCodeEnd = u8temp.find_last_of(L"}"); + if(iCodeEnd < 0) + { + return false; + } + u8s = u8temp.substr(iCodeStart+1, iCodeEnd-iCodeStart-1); + return true; +} + +void CodePageReader::ReadCodePage(vfs::Path const& codepagefile) +{ + if(!GetVFS()->FileExists(codepagefile)) + { + return; + } + vfs::COpenReadFile rfile(codepagefile); + CReadLine rline(rfile.file()); + std::string sBuffer; + vfs::UInt32 line_counter = 0; + EMode mode = Normal; + while(rline.GetLine(sBuffer)) + { + line_counter++; + // very simple parsing : key = value + if(!sBuffer.empty()) + { + // remove leading white spaces + int iStart = sBuffer.find_first_not_of(" \t",0); + char first = sBuffer.at(iStart); + switch(first) + { + case '!': + case ';': + case '#': + // comment -> do nothing + break; + case 'm': + mode = ExtractMode(sBuffer,iStart); + if(mode == Error) + { + utf8string::str_t u8s; + IGNOREEXCEPTION(utf8string::as_utf16(sBuffer, u8s)); + std::wstringstream wss; + wss << L"Could not determine mode from line [" << line_counter << L"] : " << u8s; + THROWEXCEPTION(wss.str().c_str()); + } + break; + default: + utf8string::str_t u8s; + vfs::UInt32 uiInsertPoint; + if(!ExtractCode(sBuffer, iStart,uiInsertPoint,u8s)) + { + continue; + } + if(mode == Shift) uiInsertPoint += 256; + else if(mode == Ctrl) uiInsertPoint += 512; + else if(mode == Alt) uiInsertPoint += 768; + + if(uiInsertPoint >= 1023) + { + continue; + } + for(unsigned int i=0; i < u8s.length(); ++i) + { + if(uiInsertPoint+i < 1024) + { + gsKeyTranslationTable[uiInsertPoint+i] = u8s.at(i); + } + } + } + } + } +} + +/*************************************************************************************/ +/*************************************************************************************/ + +#include + +namespace charSet +{ + static std::map > _sets; +}; + +inline bool TestSet(int char_set, charSet::ECharSet es, wchar_t wc) +{ + if(char_set & es) + { + std::set& wcset = charSet::_sets[es]; + return wcset.find(wc) != wcset.end(); + } + return false; +} + +bool charSet::IsFromSet(char wc, unsigned int char_set) +{ + return charSet::IsFromSet((wchar_t)wc, char_set); +} +bool charSet::IsFromSet(int wc, unsigned int char_set) +{ + return charSet::IsFromSet((wchar_t)wc, char_set); +} +bool charSet::IsFromSet(unsigned int wc, unsigned int char_set) +{ + return charSet::IsFromSet((wchar_t)wc, char_set); +} + +bool charSet::IsFromSet(wchar_t wc, unsigned int char_set) +{ + bool inSet = false; + if( inSet |= TestSet(char_set, charSet::CS_SPACE, wc)) return true; + if( inSet |= TestSet(char_set, charSet::CS_ALPHA_LC, wc)) return true; + if( inSet |= TestSet(char_set, charSet::CS_ALPHA_UC, wc)) return true; + if( inSet |= TestSet(char_set, charSet::CS_NUMERIC, wc)) return true; + if( inSet |= TestSet(char_set, charSet::CS_SPECIAL_ALPHA_LC, wc)) return true; + if( inSet |= TestSet(char_set, charSet::CS_SPECIAL_ALPHA_UC, wc)) return true; + if( inSet |= TestSet(char_set, charSet::CS_SPECIAL_NON_CHAR, wc)) return true; + return false; +} + +void charSet::AddToCharSet(ECharSet eset, std::wstring cset) +{ + std::set& wcset = charSet::_sets[eset]; + for(unsigned int i = 0; i < cset.length(); ++i) + { + wcset.insert(cset.at(i)); + } +} + +void charSet::InitializeCharSets() +{ + charSet::AddToCharSet(charSet::CS_SPACE, L" "); + charSet::AddToCharSet(charSet::CS_ALPHA_LC, L"abcdefghijklmnopqrstuvwxyz"); + charSet::AddToCharSet(charSet::CS_ALPHA_UC, L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + charSet::AddToCharSet(charSet::CS_NUMERIC, L"0123456789"); + charSet::AddToCharSet(charSet::CS_SPECIAL_ALPHA_LC, L"äöüßáàâéèêóòôúùô"); + charSet::AddToCharSet(charSet::CS_SPECIAL_ALPHA_UC, L"ÄÖÜÁÀÂÉÈÊÓÒÔÚÙÛ"); + charSet::AddToCharSet(charSet::CS_SPECIAL_NON_CHAR, L"~*+-_.,:;'´`#°^!\"§$%&/()=?\\{}[]"); +} + +/*************************************************************************************/ +/*************************************************************************************/ diff --git a/VFS/Tools/ParserTools.h b/VFS/Tools/ParserTools.h new file mode 100644 index 000000000..87a02d23b --- /dev/null +++ b/VFS/Tools/ParserTools.h @@ -0,0 +1,122 @@ +#ifndef _PARSER_TOOLS_H_ +#define _PARSER_TOOLS_H_ + +#include "../vfs_types.h" +#include "../Interface/vfs_file_interface.h" + +#include + +/**************************************************************/ +/**************************************************************/ +const vfs::UInt32 BUFFER_SIZE = 1024; + +class CReadLine +{ +public: + CReadLine(vfs::tReadableFile& rFile); + + bool FillBuffer(); + bool FromBuffer(std::string& line); + bool GetLine(std::string& line); +private: + vfs::Byte _buffer[BUFFER_SIZE+1]; + vfs::tReadableFile& _file; + vfs::UInt32 _buffer_pos; + vfs::UInt32 _buffer_last; + bool _eof; +}; + +/**************************************************************/ +/**************************************************************/ + +class CSplitStringList +{ +public: + CSplitStringList(utf8string const& sList); + ~CSplitStringList(); + + bool NextListEntry(utf8string &sEntry); +private: + const utf8string m_sList; + int iCurrent,iNext; +}; + + +/**************************************************************/ +/**************************************************************/ + +class CTransferRules +{ +public: + enum EAction + { + DENY, + ACCEPT, + }; +public: + CTransferRules(); + + bool InitFromTxtFile(vfs::Path const& sPath); + bool InitFromTxtFile(vfs::tReadableFile* pFile); + + void SetDefaultAction(EAction act); + EAction GetDefaultAction(); + + EAction ApplyRule(utf8string const& sStr); +private: + struct SRule + { + EAction action; + vfs::Path pattern; + }; + typedef std::list tPatternList; + tPatternList m_listRules; + EAction m_eDefaultAction; +}; + +/**************************************************************/ +/**************************************************************/ + +#define USE_CODE_PAGE + +class CodePageReader +{ +public: + enum EMode + { + Error, Normal, Shift, Ctrl, Alt + }; + void ReadCodePage(vfs::Path const& codepagefile); +private: + CodePageReader::EMode ExtractMode(std::string const &readStr, size_t startPos); + bool ExtractCode(std::string const& str, int iStart, vfs::UInt32& rInsertPoint, utf8string::str_t& u8s); +}; + +namespace charSet +{ + enum ECharSet + { + CS_SPACE = 1, + CS_ALPHA_LC = 2, + CS_ALPHA_UC = 4, + CS_ALPHA = CS_ALPHA_LC + CS_ALPHA_UC, + CS_NUMERIC = 8, + CS_ALPHA_NUM = CS_ALPHA + CS_NUMERIC, + CS_SPECIAL_ALPHA_LC = 16, + CS_SPECIAL_ALPHA_UC = 32, + CS_SPECIAL_ALPHA = CS_SPECIAL_ALPHA_LC + CS_SPECIAL_ALPHA_UC, + CS_SPECIAL_NON_CHAR = 65, + }; + + bool IsFromSet(char wc, unsigned int char_set); + bool IsFromSet(int wc, unsigned int char_set); + bool IsFromSet(unsigned int wc, unsigned int char_set); + bool IsFromSet(wchar_t wc, unsigned int char_set); + + void AddToCharSet(ECharSet eset, std::wstring cset); + + void InitializeCharSets(); +}; + +#endif // _PARSER_TOOLS_H_ + diff --git a/VFS/Tools/Tools.cpp b/VFS/Tools/Tools.cpp new file mode 100644 index 000000000..5e78fb50f --- /dev/null +++ b/VFS/Tools/Tools.cpp @@ -0,0 +1,22 @@ +#include "Tools.h" + +template<> +utf8string ToStringList(std::list const& rValList) +{ + std::wstringstream ss; + std::list::const_iterator cit = rValList.begin(); + if(cit != rValList.end()) + { + ss << (*cit); + cit++; + for(;cit != rValList.end(); ++cit) + { + ss << L" , " << (*cit); + } + } + if(!ss) + { + return L""; + } + return ss.str(); +} diff --git a/VFS/Tools/Tools.h b/VFS/Tools/Tools.h new file mode 100644 index 000000000..064fe5c79 --- /dev/null +++ b/VFS/Tools/Tools.h @@ -0,0 +1,54 @@ +#ifndef _TOOLS_H_ +#define _TOOLS_H_ + +#include "../utf8string.h" +#include + +template +std::basic_string ToString(ValueType const& rVal) +{ + std::basic_stringstream tss; + if( !(tss << rVal)) + { + return std::basic_string(); + } + return tss.str(); +} + +template +bool ConvertTo(utf8string const& sStr, T_ &rVal) +{ + std::wstringstream ss; + ss.str(sStr.c_wcs()); + if(!(ss >> rVal)) + { + return false; + } + return true; +} + +template +utf8string ToStringList(std::list const& rValList) +{ + std::wstringstream ss; + typename std::list::const_iterator cit = rValList.begin(); + if(cit != rValList.end()) + { + ss << *cit; + cit++; + for(;cit != rValList.end(); ++cit) + { + ss << L" , " << *cit; + } + } + if(!ss) + { + return L""; + } + return ss.str(); +} + +template<> +utf8string ToStringList(std::list const& rValList); + +#endif // _TOOLS_H_