Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > user-defined iomanip w/ parameters?

Reply
Thread Tools

user-defined iomanip w/ parameters?

 
 
Mr. K.V.B.L.
Guest
Posts: n/a
 
      09-10-2009
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);
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-10-2009
Mr. K.V.B.L. wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      09-10-2009
On Sep 10, 10:53 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> Mr. K.V.B.L. wrote:
> > 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:stream&operator<<(
stdstream& 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.)

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scientific & setprecision (iomanip) PengYu.UT@gmail.com C++ 3 09-03-2007 06:14 PM
<iomanip> precision value rupert C++ 2 07-29-2006 02:41 PM
problem with setw(X) and library <iomanip.h> alternativa C++ 4 05-17-2006 07:03 PM
iomanip: setwidth was not declared in this scope Vaxius C++ 3 04-18-2006 07:31 AM
iomanip Christopher C++ 2 11-09-2003 10:25 PM



Advertisments