Add utility functions to convert between string/wstring
This commit is contained in:
parent
3cb4cf6fe3
commit
c1e90feb0a
|
@ -24,6 +24,8 @@
|
|||
#include "PreCompiled.h"
|
||||
#ifndef _PreComp_
|
||||
# include <sstream>
|
||||
# include <locale>
|
||||
# include <iostream>
|
||||
#endif
|
||||
|
||||
# include <QTime>
|
||||
|
@ -122,6 +124,24 @@ std::string Base::Tools::getIdentifier(const std::string& name)
|
|||
return CleanName;
|
||||
}
|
||||
|
||||
std::wstring Base::Tools::widen(const std::string& str)
|
||||
{
|
||||
std::wostringstream wstm;
|
||||
const std::ctype<wchar_t>& ctfacet = std::use_facet< std::ctype<wchar_t> >(wstm.getloc());
|
||||
for (size_t i=0; i<str.size(); ++i)
|
||||
wstm << ctfacet.widen(str[i]);
|
||||
return wstm.str();
|
||||
}
|
||||
|
||||
std::string Base::Tools::narrow(const std::wstring& str)
|
||||
{
|
||||
std::ostringstream stm;
|
||||
const std::ctype<char>& ctfacet = std::use_facet< std::ctype<char> >(stm.getloc());
|
||||
for (size_t i=0; i<str.size(); ++i)
|
||||
stm << ctfacet.narrow(str[i], 0);
|
||||
return stm.str();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
using namespace Base;
|
||||
|
|
|
@ -151,6 +151,8 @@ struct BaseExport Tools
|
|||
static std::string getUniqueName(const std::string&, const std::vector<std::string>&,int d=0);
|
||||
static std::string addNumber(const std::string&, unsigned int, int d=0);
|
||||
static std::string getIdentifier(const std::string&);
|
||||
static std::wstring widen(const std::string& str);
|
||||
static std::string narrow(const std::wstring& str);
|
||||
};
|
||||
|
||||
} // namespace Base
|
||||
|
|
Loading…
Reference in New Issue
Block a user