Здравствуйте, Аноним, Вы писали:
А>подскажите пожалуйста, как преобразовать std::string в std::wstring и обратно, желательно средствами stl или boost (без прямого вызова MultiByteToWideChar)?
посмотри этот файлик, правда я не тестировал...
// boost/string_conversion.hpp
// <!!----------------------------------------------------------------------->
// <!! Copyright © 2001 Dietmar Kuehl, All Rights Reserved >
// <!! >
// <!! Permission to use, copy, modify, distribute and sell this >
// <!! software for any purpose is hereby granted without fee, provided >
// <!! that the above copyright notice appears in all copies and that >
// <!! both that copyright notice and this permission notice appear in >
// <!! supporting documentation. Dietmar Kuehl makes no representations about>
// <!! the suitability of this software for any purpose. It is provided >
// <!! "as is" without express or implied warranty. >
// <!!---------------------------------------------------------------------- >
// Author: Dietmar Kuehl (dietmar_kuehl@yahoo.com)
// Revised by Jan Langer (jan@langernetz.de)
// ---------------------------------------------------------------------------
#ifndef BOOST_STRING_CONVERSION_HPP
#define BOOST_STRING_CONVERSION_HPP
#include "boost/config.hpp"
// --------------------------------------------------------
// if no locale is available
// provide a simple implementation
#ifndef BOOST_NO_STD_LOCALE
#include <locale>
using std::locale;
using std::use_facet;
using std::ctype;
#else
namespace
{
struct locale {};
template <class T> T use_facet(locale const&)
{
return T();
}
template <class CharT>
struct ctype
{
char narrow(CharT c, char) const
{
return char (c);
}
CharT widen (char c) const
{
return CharT (c);
}
};
}
#endif
#include <string>
namespace boost
{
namespace detail
{
// --------------------------------------------------
// char conversion by means of ctype's widen and narrow
// uses widen if cFrom=char
// uses narrow if cTo=char
// returns just the argument in all other cases
// --------------------------------------------------
// general case
template <class cTo, class cFrom>
struct char_conversion
{
static cTo
convert (cFrom c, locale const & = locale ())
{
return c;
}
};
// conversion from cFrom to char with narrow
template <class cFrom>
struct char_conversion <char, cFrom>
{
static char
convert (cFrom c, locale const &loc = locale ())
{
ctype <cFrom> const &ct = use_facet <ctype <cFrom> > (loc);
return ct.narrow (c, '.');
}
};
// conversion from char to cTo with widen
template <class cTo>
struct char_conversion <cTo, char>
{
static cTo
convert (char c, locale const &loc = locale ())
{
ctype <cTo> const &ct = use_facet <ctype <cTo> > (loc);
return ct.widen (c);
}
};
// conversion from char to char
// general case be fit, but is less specialized than the other
// specializations
template <>
struct char_conversion <char, char>
{
static char
convert (char c, locale const & = locale ())
{
return c;
}
};
// --------------------------------------------------
// string conversion with the help of char_conversion
// just returns if cTo=cFrom
// --------------------------------------------------
// general case
template <class cTo, class cFrom>
struct string_conversion
{
static std::basic_string <cTo>
convert (std::basic_string <cFrom> const &s,
locale const &loc = locale ())
{
std::basic_string <cTo> rc (s.size (), cTo ());
for (typename std::basic_string <cFrom>::size_type i = 0; i < s.size (); ++i)
{
rc [i] = char_conversion <cTo, cFrom>::convert (s [i], loc);
}
return rc;
}
};
// if cTo==cFrom
template <class cTo>
struct string_conversion <cTo, cTo>
{
static std::basic_string <cTo>
convert (std::basic_string <cTo> const &s,
locale const & = locale ())
{
return s;
}
};
} // namespace detail
// ----------------------------------------------------
// string_cast
template <class cTo, class cFrom>
std::basic_string <cTo> string_cast (
std::basic_string <cFrom> const &str,
locale const &loc = locale ())
{
return detail::string_conversion <cTo, cFrom>::convert(str, loc);
}
template <class cTo, class cFrom>
std::basic_string <cTo> string_cast (
cFrom const *s,
locale const &loc = locale ())
{
return string_cast <cTo, cFrom> (s, loc);
}
// ----------------------------------------------------
// char_cast
template <class cTo, class cFrom>
cTo char_cast(cFrom c, locale const& loc = locale ())
{
return detail::char_conversion <cTo, cFrom>::convert (c, loc);
}
// ----------------------------------------------------
} // namespace boost
#endif // BOOST_STRING_CONVERSION_HPP