user-defined iomanip w/ parameters?

M

Mr. K.V.B.L.

I'm scouring the net trying to find this answer but coming up empty.
Everything I've tried is not compiling. I'm trying to define a
iomanipulator that will convert some text to another character set
then output. So, something like:

cout << ascii(ebcdicTextString) << endl;

Is it not standard from one platform to another? In this case the
compiler is saying that 'ascii' is unexpected. I'm including
<iostream> and <iomanip>. Thanks a bunch!

ostream& convert2ascii(ostream &out, string str)
{
string asciiResponse;
char *target = new char[str.length() + 1];
UErrorCode status = U_ZERO_ERROR;
int32_t i = ucnv_convert("iso-8859-1", "ibm-37", target, str.length
() + 1, str.c_str(), str.length(), &status);

asciiResponse = target;
delete[] target;
return out << asciiResponse;
}


OMANIP(string) ascii(string str)
{
return OMANIP(string) (convert2ascii,str);
}
 
V

Victor Bazarov

Mr. K.V.B.L. said:
I'm scouring the net trying to find this answer but coming up empty.
Everything I've tried is not compiling. I'm trying to define a
iomanipulator that will convert some text to another character set
then output. So, something like:

cout << ascii(ebcdicTextString) << endl;

Is it not standard from one platform to another? In this case the
compiler is saying that 'ascii' is unexpected. I'm including
<iostream> and <iomanip>. Thanks a bunch!

ostream& convert2ascii(ostream &out, string str)
{
string asciiResponse;
char *target = new char[str.length() + 1];
UErrorCode status = U_ZERO_ERROR;
int32_t i = ucnv_convert("iso-8859-1", "ibm-37", target, str.length
() + 1, str.c_str(), str.length(), &status);

asciiResponse = target;
delete[] target;
return out << asciiResponse;
}


OMANIP(string) ascii(string str)
{
return OMANIP(string) (convert2ascii,str);
}

Drop the stream manipulator. You just need a function:

std::string ascii(std::string const& ebcdic)
{
std::string asciiResponse;
// take the source string, convert
return asciiResponse;
}

V
 
J

James Kanze

Mr. K.V.B.L. said:
I'm scouring the net trying to find this answer but coming
up empty. Everything I've tried is not compiling. I'm
trying to define a iomanipulator that will convert some text
to another character set then output. So, something like:
cout << ascii(ebcdicTextString) << endl;
Is it not standard from one platform to another? In this
case the compiler is saying that 'ascii' is unexpected. I'm
including <iostream> and <iomanip>. Thanks a bunch!
ostream& convert2ascii(ostream &out, string str)
{
string asciiResponse;
char *target = new char[str.length() + 1];
UErrorCode status = U_ZERO_ERROR;
int32_t i = ucnv_convert("iso-8859-1", "ibm-37", target, str.length
() + 1, str.c_str(), str.length(), &status);
asciiResponse = target;
delete[] target;
return out << asciiResponse;
}
OMANIP(string) ascii(string str)
{
return OMANIP(string) (convert2ascii,str);
}
Drop the stream manipulator. You just need a function:
std::string ascii(std::string const& ebcdic)
{
std::string asciiResponse;
// take the source string, convert
return asciiResponse;
}

He could also use a decorator object, to do the conversion on
the fly and avoid the extra string:

class ascii
{
public:
explicit ascii(
std::string const& text )
: myText( text )
{
}

friend std::eek:stream&operator<<(
std:eek:stream& dest,
ascii const& object )
{
// ...
return dest ;
}

private:
std::string const& myText ;
} ;

(Note that such classes should only be used as temporary
decorators, since they do not make a copy of the data they
output, but count on the initializing object having sufficient
lifetime. Which is guaranteed if they are a temporary object,
but not necessarily in other cases.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top