2002-06-21 13:16:28 +02:00
|
|
|
#ifndef STRINGADAPTOR_H
|
|
|
|
#define STRINGADAPTOR_H
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <Utils/convertstream.h>
|
|
|
|
#include <Utils/utf16utf8_codecvt.h>
|
|
|
|
|
|
|
|
namespace SAX
|
|
|
|
{
|
|
|
|
|
|
|
|
template<class string_type>
|
|
|
|
class default_string_adaptor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef string_type stringT;
|
|
|
|
typedef typename stringT::value_type value_type;
|
|
|
|
|
|
|
|
value_type makeValueT(char c) const;
|
|
|
|
stringT makeStringT(const char* str) const;
|
|
|
|
stringT makeStringT(const char* str, int length) const;
|
|
|
|
stringT makeStringT(const wchar_t* str) const;
|
|
|
|
stringT makeStringT(const wchar_t* str, int length) const;
|
|
|
|
|
|
|
|
// only used to constuct error strings - don't have to be highly efficient!
|
|
|
|
std::string asStdString(const stringT& str) const;
|
2002-10-08 05:09:56 +02:00
|
|
|
#if !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
std::wstring asStdWString(const stringT& str) const;
|
2002-10-08 05:09:56 +02:00
|
|
|
#endif // !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class default_string_maker
|
|
|
|
|
|
|
|
// specialize for std::string and std::wstring
|
|
|
|
template<>
|
|
|
|
class default_string_adaptor<std::string>
|
|
|
|
{
|
2002-10-08 05:09:56 +02:00
|
|
|
#if !(__GNUG__ && (__GNUC__ < 3))
|
|
|
|
private:
|
2002-06-21 13:16:28 +02:00
|
|
|
typedef basic_iconvertstream<wchar_t, std::char_traits<wchar_t>,
|
|
|
|
char, std::char_traits<char> > widener;
|
|
|
|
typedef basic_oconvertstream<wchar_t, std::char_traits<wchar_t>,
|
|
|
|
char, std::char_traits<char> > narrower;
|
2002-10-08 05:09:56 +02:00
|
|
|
#endif // !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
public:
|
|
|
|
char makeValueT(char c) const { return c; }
|
|
|
|
|
|
|
|
std::string makeStringT(const char* str) const
|
|
|
|
{
|
|
|
|
return str ? std::string(str) : std::string();
|
|
|
|
} // makeStringT
|
|
|
|
std::string makeStringT(const char* str, int length) const
|
|
|
|
{
|
|
|
|
return std::string(str, length);
|
|
|
|
} // makeStringT
|
2002-10-08 05:09:56 +02:00
|
|
|
#if !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
std::string makeStringT(const wchar_t* str) const
|
|
|
|
{
|
|
|
|
std::wstring s;
|
|
|
|
if(str)
|
|
|
|
s = str;
|
|
|
|
n_.str(s);
|
|
|
|
return n_.str();
|
|
|
|
} // makeStringT
|
|
|
|
std::string makeStringT(const wchar_t* str, int length) const
|
|
|
|
{
|
|
|
|
n_.str(std::wstring(str, length));
|
|
|
|
return n_.str();
|
|
|
|
} // makeStringT
|
2002-10-08 05:09:56 +02:00
|
|
|
#endif // !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
const std::string& asStdString(const std::string& str) const
|
|
|
|
{
|
|
|
|
return str;
|
|
|
|
} // toStdString
|
2002-10-08 05:09:56 +02:00
|
|
|
#if !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
std::wstring asStdWString(const std::string& str) const
|
|
|
|
{
|
|
|
|
w_.str(str);
|
|
|
|
return w_.str();
|
|
|
|
} // toStdWString
|
2002-10-08 05:09:56 +02:00
|
|
|
#endif // !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2002-10-08 05:09:56 +02:00
|
|
|
default_string_adaptor()
|
|
|
|
#if !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
#ifndef _MSC_VER
|
2002-10-08 05:09:56 +02:00
|
|
|
: loc_(std::locale(), new utf16utf8_codecvt()),
|
2002-06-21 13:16:28 +02:00
|
|
|
#else
|
2002-10-08 05:09:56 +02:00
|
|
|
: loc_(std::_Addfac(std::locale(), new utf16utf8_codecvt)),
|
2002-06-21 13:16:28 +02:00
|
|
|
#endif
|
|
|
|
n_(),
|
|
|
|
w_()
|
2002-10-08 05:09:56 +02:00
|
|
|
#endif // !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-10-08 05:09:56 +02:00
|
|
|
#if !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
n_.imbue(loc_);
|
|
|
|
w_.imbue(loc_);
|
2002-10-08 05:09:56 +02:00
|
|
|
#endif // !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
} // default_string_adaptor
|
|
|
|
|
|
|
|
private:
|
2002-10-08 05:09:56 +02:00
|
|
|
#if !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
std::locale loc_;
|
|
|
|
mutable narrower n_;
|
|
|
|
mutable widener w_;
|
2002-10-08 05:09:56 +02:00
|
|
|
#endif // !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class default_string_adaptor
|
|
|
|
|
2002-10-08 05:09:56 +02:00
|
|
|
#if !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
template<>
|
|
|
|
class default_string_adaptor<std::wstring>
|
|
|
|
{
|
2002-10-08 05:09:56 +02:00
|
|
|
private:
|
2002-06-21 13:16:28 +02:00
|
|
|
typedef basic_iconvertstream<wchar_t, std::char_traits<wchar_t>,
|
|
|
|
char, std::char_traits<char> > widener;
|
|
|
|
typedef basic_oconvertstream<wchar_t, std::char_traits<wchar_t>,
|
|
|
|
char, std::char_traits<char> > narrower;
|
|
|
|
public:
|
|
|
|
wchar_t makeValueT(char c) const
|
|
|
|
{
|
|
|
|
return static_cast<wchar_t>(c);
|
|
|
|
} // makeValueT
|
|
|
|
|
|
|
|
std::wstring makeStringT(const char* str) const
|
|
|
|
{
|
|
|
|
if(str)
|
|
|
|
w_.str(str);
|
|
|
|
else
|
|
|
|
w_.str("");
|
|
|
|
return w_.str();
|
|
|
|
} // makeStringT
|
|
|
|
std::wstring makeStringT(const char* str, int length) const
|
|
|
|
{
|
|
|
|
w_.str(std::string(str, length));
|
|
|
|
return w_.str();
|
|
|
|
} // makeStringT
|
|
|
|
std::wstring makeStringT(const wchar_t* str) const
|
|
|
|
{
|
|
|
|
return str ? std::wstring(str) : std::wstring();
|
|
|
|
} // makeStringT
|
|
|
|
std::wstring makeStringT(const wchar_t* str, int length) const
|
|
|
|
{
|
|
|
|
return std::wstring(str, length);
|
|
|
|
} // makeStringT
|
|
|
|
|
|
|
|
std::string asStdString(const std::wstring& str) const
|
|
|
|
{
|
|
|
|
n_.str(str);
|
|
|
|
return n_.str();
|
|
|
|
} // toStdString
|
|
|
|
const std::wstring& asStdWString(const std::wstring& str) const
|
|
|
|
{
|
|
|
|
return str;
|
|
|
|
} // toStdWString
|
|
|
|
|
|
|
|
default_string_adaptor() :
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
loc_(std::locale(), new utf16utf8_codecvt()),
|
|
|
|
#else
|
|
|
|
loc_(std::_Addfac(std::locale(), new utf16utf8_codecvt)),
|
|
|
|
#endif
|
|
|
|
n_(),
|
|
|
|
w_()
|
|
|
|
{
|
|
|
|
n_.imbue(loc_);
|
|
|
|
w_.imbue(loc_);
|
|
|
|
} // default_string_adaptor
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::locale loc_;
|
|
|
|
mutable narrower n_;
|
|
|
|
mutable widener w_;
|
|
|
|
}; // class default_string_adaptor
|
2002-10-08 05:09:56 +02:00
|
|
|
#endif // !(__GNUG__ && (__GNUC__ < 3))
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
} // namespace SAX
|
|
|
|
|
|
|
|
#endif
|
|
|
|
// end of file
|