Add utility functions to convert between string/wstring

This commit is contained in:
wmayer
2013-04-26 17:07:18 +02:00
parent 3cb4cf6fe3
commit c1e90feb0a
2 changed files with 22 additions and 0 deletions

View File

@@ -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;