arabica/include/io/convertstream.hpp

285 lines
8 KiB
C++
Raw Permalink Normal View History

2003-09-11 12:26:53 +02:00
#ifndef ARABICA_CONVERT_STREAM_H
#define ARABICA_CONVERT_STREAM_H
2002-06-21 13:16:28 +02:00
//////////////////////////////////////////////////////
//
// $Id$
//
//////////////////////////////////////////////////////
//
// basic_iconvertstream, basic_oconvertstream
//
2003-08-28 14:36:33 +02:00
// Written by Jez Higgins <jez@jezuk.co.uk>
// Copyright 1999-2005 Jez UK Ltd, http://www.jezuk.co.uk/
2003-08-28 14:36:33 +02:00
//
2002-06-21 13:16:28 +02:00
// Normal basic_stringstream do not apply the codecvt facet
// of their locale. These two streams act exactly like
// basic_stringstream except that they do apply the imbued codecvt
// facet to their input (in the case of basic_iconvertstream)
// or output (int the case of basic_oconvertstream).
//
// This means you can to cool things like this
//
// std::locale loc(std::_Addfac(std::locale(), new base64_codecvt));
// converting_ostringstream os;
// os.imbue(loc);
//
// os << "stuff";
// ... lots more stuff streamed into os
//
// std::cout << os.str() << std::endl;
// os.str() contains the Base64 encoded byte sequence
//
// Decoding is just as simple.
//
// std::locale loc(std::_Addfac(std::locale(), new base64_codecvt));
//
// converting_istringstream is;
// is.imbue(loc);
// is.str(a_base64_byte_stream);
// std::cout << is.str();
// ... is.str() is the decode byte stream, which can also be extracted
// ... using >> operators (is >> byte; etc)
//
////////////////////////////////////////////////////////////
2007-09-05 00:55:47 +02:00
#include <SAX/ArabicaConfig.hpp>
2002-06-21 13:16:28 +02:00
#include <locale>
#include <sstream>
#include <algorithm>
2006-10-06 23:15:22 +02:00
#include <iterator>
2002-06-21 13:16:28 +02:00
2003-09-11 16:05:18 +02:00
namespace Arabica
{
namespace io
2003-09-11 16:05:18 +02:00
{
template<typename charT, typename traitsT>
class convertstreambuf_init
{
public:
typedef std::basic_stringbuf<charT, traitsT> stringbufT;
convertstreambuf_init(std::ios_base::openmode mode) :
buf_(mode)
{
} // convertstreambuf_init
stringbufT const* buf() const
{
return &buf_;
} // buf()
stringbufT* buf()
{
return &buf_;
} // buf()
private:
stringbufT buf_;
}; // class convertstreambuf_init
2002-06-21 13:16:28 +02:00
template<typename charT,
typename traitsT = std::char_traits<charT>,
typename fromCharT = charT,
typename fromTraitsT = std::char_traits<fromCharT> >
class basic_iconvertstream :
private virtual convertstreambuf_init<charT, traitsT>,
public std::basic_istream<charT, traitsT>
2002-06-21 13:16:28 +02:00
{
typedef convertstreambuf_init<charT, traitsT> convertstreambuf_initT;
2002-06-21 13:16:28 +02:00
public:
typedef std::basic_istream<charT, traitsT> istreamT;
typedef typename convertstreambuf_initT::stringbufT stringbufT;
2002-06-21 13:16:28 +02:00
typedef std::basic_string<charT, traitsT> stringT;
typedef std::basic_string<fromCharT, fromTraitsT> fromStringT;
2004-09-11 12:11:03 +02:00
explicit basic_iconvertstream(std::ios_base::openmode mode = std::ios_base::in) :
convertstreambuf_initT(mode | std::ios_base::in),
std::basic_istream<charT, traitsT>(convertstreambuf_initT::buf())
2020-02-16 03:07:06 +01:00
{
std::fill_n(to_, toSize_, 0);
2004-09-11 12:11:03 +02:00
} // basic_iconvertstream
explicit basic_iconvertstream(const stringT& str, std::ios_base::openmode mode = std::ios_base::in) :
convertstreambuf_initT(mode | std::ios_base::in),
std::basic_istream<charT, traitsT>(convertstreambuf_initT::buf())
2004-09-11 12:11:03 +02:00
{
str(str);
} // basic_iconvertstream
2002-06-21 13:16:28 +02:00
virtual ~basic_iconvertstream()
{}
stringbufT* rdbuf() const
{
return const_cast<stringbufT*>(convertstreambuf_initT::buf());
2002-06-21 13:16:28 +02:00
} // rdbuf
stringT str() const
{
return convertstreambuf_initT::buf()->str();
2002-06-21 13:16:28 +02:00
} // str
void str(const fromStringT& str)
{
// do conversion
const std::codecvt<charT, fromCharT, typename traitsT::state_type>& cvt =
std::use_facet<std::codecvt<charT, fromCharT, typename traitsT::state_type> >(this->getloc());
if(cvt.always_noconv())
{
convertstreambuf_initT::buf()->str(no_conversion(str));
2002-06-21 13:16:28 +02:00
return;
}
// we must do code conversion
stringT converted;
const fromCharT* from_next = str.data();
typename std::codecvt_base::result r;
typename traitsT::state_type state;
do
{
charT* to_next;
r = cvt.in(state, from_next, str.data() + str.length(), from_next,
to_, to_ + toSize_, to_next);
2002-06-21 13:16:28 +02:00
if(r == std::codecvt_base::noconv)
{
converted.append(no_conversion(str));
break;
}
converted.append(to_, (to_next - to_));
2002-06-21 13:16:28 +02:00
}
while(r == std::codecvt_base::partial);
// naughty! ignore (r == std::codecvt_base::error)
convertstreambuf_initT::buf()->str(converted);
2002-06-21 13:16:28 +02:00
} // str
private:
stringT no_conversion(const fromStringT& str)
{
stringT dest;
std::back_insert_iterator<stringT> id(dest);
for(typename fromStringT::const_iterator i = str.begin(); i != str.end(); ++i, ++id)
*id = static_cast<charT>(*i);
2002-06-21 13:16:28 +02:00
return dest;
} // no_conversion
static const int toSize_ = 4096;
charT to_[toSize_];
2002-06-21 13:16:28 +02:00
}; // basic_iconvertstream
template<typename charT,
typename traitsT = std::char_traits<charT>,
typename toCharT = charT,
typename toTraitsT = std::char_traits<toCharT> >
class basic_oconvertstream :
private virtual convertstreambuf_init<charT, traitsT>,
public std::basic_ostream<charT, traitsT>
2002-06-21 13:16:28 +02:00
{
typedef convertstreambuf_init<charT, traitsT> convertstreambuf_initT;
2002-06-21 13:16:28 +02:00
public:
typedef std::basic_ostream<charT, traitsT> ostreamT;
typedef typename convertstreambuf_initT::stringbufT stringbufT;
2002-06-21 13:16:28 +02:00
typedef std::basic_string<charT, traitsT> stringT;
typedef std::basic_string<toCharT, toTraitsT> toStringT;
2004-09-11 12:11:03 +02:00
explicit basic_oconvertstream(std::ios_base::openmode mode = std::ios_base::out) :
convertstreambuf_initT(mode | std::ios_base::out),
std::basic_ostream<charT, traitsT>(convertstreambuf_initT::buf())
2004-09-11 12:11:03 +02:00
{
2020-02-16 03:07:06 +01:00
std::fill_n(to_, toSize_, 0);
2004-09-11 12:11:03 +02:00
} // basic_oconvertstream
explicit basic_oconvertstream(const stringT& str, std::ios_base::openmode mode = std::ios_base::out) :
convertstreambuf_initT(mode | std::ios_base::out),
std::basic_ostream<charT, traitsT>(convertstreambuf_initT::buf())
2004-09-11 12:11:03 +02:00
{
2005-11-25 14:47:27 +01:00
convertstreambuf_initT::buf()->str(str);
2004-09-11 12:11:03 +02:00
} // basic_oconvertstream
2002-11-23 21:03:54 +01:00
2002-06-21 13:16:28 +02:00
virtual ~basic_oconvertstream()
2004-09-11 12:11:03 +02:00
{}
2002-06-21 13:16:28 +02:00
stringbufT* rdbuf() const
{
2005-11-25 14:47:27 +01:00
return const_cast<stringbufT*>(convertstreambuf_initT::buf());
2002-06-21 13:16:28 +02:00
} // rdbuf
toStringT str()
{
toStringT out;
stringT newstuff(convertstreambuf_initT::buf()->str());
2002-06-21 13:16:28 +02:00
if(newstuff.length() == 0)
return out;
// convert it here
const std::codecvt<charT, toCharT, typename traitsT::state_type>& cvt =
std::use_facet<std::codecvt<charT, toCharT, typename traitsT::state_type> >(this->getloc());
if(cvt.always_noconv())
out.append(no_conversion(newstuff));
else
{
// we must do code conversion
const charT* from_next = newstuff.data();
typename std::codecvt_base::result r;
typename traitsT::state_type state;
do
{
toCharT* to_next;
r = cvt.out(state, from_next, newstuff.data() + newstuff.length(), from_next,
to_, to_ + toSize_, to_next);
2002-06-21 13:16:28 +02:00
if(r == std::codecvt_base::noconv)
{
out.append(no_conversion(newstuff));
break;
}
out.append(to_, (to_next - to_));
2002-06-21 13:16:28 +02:00
}
while(r == std::codecvt_base::partial);
// naughty! ignore (r == std::codecvt_base::error)
} // if(cvt.always_noconv())
convertstreambuf_initT::buf()->str(stringT());
2002-06-21 13:16:28 +02:00
return out;
} // str
void str(const stringT& str)
{
convertstreambuf_initT::buf()->str(str);
2002-06-21 13:16:28 +02:00
} // str
private:
toStringT no_conversion(const stringT& str)
{
2002-09-20 12:01:18 +02:00
toStringT dest;
std::back_insert_iterator<toStringT> id(dest);
for(typename stringT::const_iterator i = str.begin(); i != str.end(); ++i, ++id)
*id = static_cast<toCharT>(*i);
2002-06-21 13:16:28 +02:00
return dest;
} // no_conversion
static const int toSize_ = 4096;
toCharT to_[toSize_];
2002-06-21 13:16:28 +02:00
}; // basic_oconvertstream
typedef basic_iconvertstream<char> converting_istringstream;
typedef basic_oconvertstream<char> converting_ostringstream;
2003-09-13 01:15:14 +02:00
#ifndef ARABICA_NO_WSTRING_T
2003-09-09 15:09:48 +02:00
typedef basic_iconvertstream<wchar_t> converting_iwstringstream;
2002-06-21 13:16:28 +02:00
typedef basic_oconvertstream<wchar_t> converting_owstringstream;
2003-09-09 15:09:48 +02:00
#endif
2002-06-21 13:16:28 +02:00
} // namespace io
2003-09-11 16:05:18 +02:00
} // namespace Arabica
2002-06-21 13:16:28 +02:00
#endif