Re: Streambuf with code conversion
* Arcturus:
I have been following Langers book for some time and am trying a simple
code conversion example without much luck. Here is the code.
Don't post HTML please.
#include <iostream>
#include <ostream>
#include <streambuf>
template <class charT, class traits>
class Fcgibuf: public std::basic_streambuf<charT, traits>
{
public:
Fcgibuf() { setp(buffer, buffer+buffSize); }
virtual ~Fcgibuf() { sync(); }
protected:
typedef typename std::basic_streambuf<charT, traits>::int_type int_type;
typedef typename std::basic_streambuf<charT, traits>::char_type char_type;
typedef typename std::basic_streambuf<charT, traits>::traits_type traits_type;
int_type overflow(int_type c = traits_type::eof())
{
if(emptyBuffer() < 0)
return traits_type::eof();
if(!traits_type::eq_int_type(c, traits_type::eof()))
return sputc(c);
else
return traits_type::not_eof(c);
}
int sync() { return emptyBuffer(); }
int emptyBuffer();
static const int buffSize = 8192;
char_type buffer[buffSize];
};
template <class charT, class traits>
int Fcgibuf<charT, traits>::emptyBuffer()
{
char outBuffer[buffSize];
mbstate_t conversionState;
char_type* pStreamPos;
char* toNext;
std::use_facet<std::codecvt<char_type, char, mbstate_t> >(this->getloc()).out(conversionState, this->pptr(), this->pbase(), pStreamPos, (char*)outBuffer, outBuffer+buffSize, toNext);
*toNext='\0';
std::cerr << outBuffer;
pbump(-(this->pptr()-this->pbase()));
return 0;
}
int main()
{
Fcgibuf<wchar_t, std::char_traits<wchar_t> > theBuffer;
std::basic_ostream<wchar_t> theStream(&theBuffer);
theStream << L"This is a test";
return 0;
}
Upon compiling g++ gives me the following error:
tmp.cpp: In member function ?int Fcgibuf<charT,
traits>::emptyBuffer() [with charT = wchar_t, traits =
std::char_traits<wchar_t>]?:
tmp.cpp:25: instantiated from ?int Fcgibuf<charT, traits>::sync()
[with charT = wchar_t, traits = std::char_traits<wchar_t>]?
tmp.cpp:10: instantiated from ?Fcgibuf<charT, traits>::~Fcgibuf()
[with charT = wchar_t, traits = std::char_traits<wchar_t>]?
tmp.cpp:51: instantiated from here
tmp.cpp:40: error: no matching function for call to
?std::codecvt<wchar_t, char, __mbstate_t>::out(mbstate_t&, wchar_t*,
wchar_t*, wchar_t*&, char*, char*, char*&) const?
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/bits/codecvt.h:121:
note: candidates are: std::codecvt_base::result
std::__codecvt_abstract_base<_InternT, _ExternT,
_StateT>::out(_StateT&, const _InternT*, const _InternT*, const
_InternT*&, _ExternT*, _ExternT*, _ExternT*&) const [with _InternT =
wchar_t, _ExternT = char, _StateT = __mbstate_t]
This error message doesn't make very much sense to me as the candidate
seems identical to the actual function call. Can anybody shed some light
on this?
Hm.
<example>
V:\test> gnuc x.cpp
x.cpp: In member function `int Fcgibuf<charT, traits>::emptyBuffer() [with charT
= wchar_t, traits = std::char_traits<wchar_t>]':
x.cpp:25: instantiated from `int Fcgibuf<charT, traits>::sync() [with charT =
wchar_t, traits = std::char_traits<wchar_t>]'
x.cpp:10: instantiated from `Fcgibuf<charT, traits>::~Fcgibuf() [with charT =
wchar_t, traits = std::char_traits<wchar_t>]'
x.cpp:51: instantiated from here
x.cpp:40: error: no matching function for call to `std::codecvt<wchar_t, char,
mbstate_t>::out(mbstate_t&, wchar_t*, wchar_t*, wchar_t*&, char*, char*, char*&)
const'
c:/program
files/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/codecvt.h:125:
note: candidates are: std::codecvt_base::result
std::__codecvt_abstract_base<_InternT, _ExternT, _StateT>::out(_StateT&, const
_InternT*,
const _InternT*, const _InternT*&, _ExternT*, _ExternT*, _ExternT*&) const [with
_InternT = wchar_t, _ExternT = char, _StateT = mbstate_t]
V:\test> msvc x.cpp
x.cpp
V:\test> _
</example>
Dunno, but I suspect it's the same reason or related reason that
#include <iostream>
int main()
{
std::wcout << L"Bah" << std::endl;
}
fails with MinGW g++ for Windows.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?