arabica/SAX/helpers/StringAdaptor.h

182 lines
4.5 KiB
C
Raw Normal View History

2003-09-11 12:26:53 +02:00
#ifndef ARABICA_STRING_ADAPTOR_H
#define ARABICA_STRING_ADAPTOR_H
2002-06-21 13:16:28 +02:00
/*
* $Id$
*/
2003-09-09 13:14:48 +02:00
#include <SAX/ArabicaConfig.h>
2002-06-21 13:16:28 +02:00
#include <string>
#include <Utils/convertstream.h>
2003-09-02 13:23:52 +02:00
#include <Utils/utf8ucs2codecvt.h>
2002-06-21 13:16:28 +02:00
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;
2003-09-09 15:09:48 +02:00
#ifndef ARABICA_NO_WCHAR_T
2002-06-21 13:16:28 +02:00
stringT makeStringT(const wchar_t* str) const;
stringT makeStringT(const wchar_t* str, int length) const;
2003-09-09 15:09:48 +02:00
#endif
2002-06-21 13:16:28 +02:00
// only used to constuct error strings - don't have to be highly efficient!
std::string asStdString(const stringT& str) const;
2003-09-09 15:09:48 +02:00
#ifndef ARABICA_NO_WCHAR_T
2002-06-21 13:16:28 +02:00
std::wstring asStdWString(const stringT& str) const;
2003-09-09 15:09:48 +02:00
#endif
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>
{
public:
typedef std::string stringT;
typedef std::string::value_type value_type;
2002-06-21 13:16:28 +02:00
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
2003-09-09 15:09:48 +02:00
const std::string& asStdString(const std::string& str) const
{
return str;
} // toStdString
#ifndef ARABICA_NO_WCHAR_T
2003-09-11 16:05:18 +02:00
typedef Arabica::convert::basic_iconvertstream<wchar_t, std::char_traits<wchar_t>,
2003-09-09 15:09:48 +02:00
char, std::char_traits<char> > widener;
2003-09-11 16:05:18 +02:00
typedef Arabica::convert::basic_oconvertstream<wchar_t, std::char_traits<wchar_t>,
2003-09-09 15:09:48 +02:00
char, std::char_traits<char> > narrower;
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
std::wstring asStdWString(const std::string& str) const
{
w_.str(str);
return w_.str();
} // toStdWString
2003-09-09 15:09:48 +02:00
#endif
2002-06-21 13:16:28 +02:00
2003-09-09 15:09:48 +02:00
#ifndef ARABICA_NO_WCHAR_T
default_string_adaptor() :
2003-09-09 15:09:48 +02:00
#ifndef ARABICA_VS6_WORKAROUND
2003-09-11 16:05:18 +02:00
loc_(std::locale(), new Arabica::convert::utf8ucs2codecvt()),
2002-06-21 13:16:28 +02:00
#else
2003-09-11 16:05:18 +02:00
loc_(std::_Addfac(std::locale(), new Arabica::convert::utf8ucs2codecvt)),
2002-06-21 13:16:28 +02:00
#endif
n_(),
w_()
{
n_.imbue(loc_);
w_.imbue(loc_);
} // default_string_adaptor
private:
std::locale loc_;
mutable narrower n_;
mutable widener w_;
2003-09-09 15:09:48 +02:00
#else
default_string_adaptor() { }
#endif
2002-06-21 13:16:28 +02:00
}; // class default_string_adaptor
2003-09-09 15:09:48 +02:00
#ifndef ARABICA_NO_WCHAR_T
2002-06-21 13:16:28 +02:00
template<>
class default_string_adaptor<std::wstring>
{
2003-09-11 16:05:18 +02:00
typedef Arabica::convert::basic_iconvertstream<wchar_t, std::char_traits<wchar_t>,
2002-06-21 13:16:28 +02:00
char, std::char_traits<char> > widener;
2003-09-11 16:05:18 +02:00
typedef Arabica::convert::basic_oconvertstream<wchar_t, std::char_traits<wchar_t>,
2002-06-21 13:16:28 +02:00
char, std::char_traits<char> > narrower;
public:
typedef std::wstring stringT;
typedef std::wstring::value_type value_type;
2002-06-21 13:16:28 +02:00
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() :
2003-09-09 15:09:48 +02:00
#ifndef ARABICA_VS6_WORKAROUND
2003-09-11 16:05:18 +02:00
loc_(std::locale(), new Arabica::convert::utf8ucs2codecvt()),
2002-06-21 13:16:28 +02:00
#else
2003-09-11 16:05:18 +02:00
loc_(std::_Addfac(std::locale(), new Arabica::convert::utf8ucs2codecvt())),
2002-06-21 13:16:28 +02:00
#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
2003-09-09 15:09:48 +02:00
#endif // ARABICA_NO_WCHAR_T
2002-06-21 13:16:28 +02:00
} // namespace SAX
#endif
// end of file