user-defined iomanip w/ parameters?

Discussion in 'C++' started by Mr. K.V.B.L., Sep 10, 2009.

  1. Mr. K.V.B.L.

    Mr. K.V.B.L. Guest

    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);
    }
     
    Mr. K.V.B.L., Sep 10, 2009
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Sep 10, 2009
    #2
    1. Advertising

  3. Mr. K.V.B.L.

    James Kanze Guest

    On Sep 10, 10:53 pm, Victor Bazarov <> 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::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.)

    --
    James Kanze
     
    James Kanze, Sep 10, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher

    iomanip

    Christopher, Nov 9, 2003, in forum: C++
    Replies:
    2
    Views:
    774
    Jerry Coffin
    Nov 9, 2003
  2. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  3. Vaxius
    Replies:
    3
    Views:
    3,461
  4. alternativa
    Replies:
    4
    Views:
    597
    Alan Woodland
    May 17, 2006
  5. rupert

    <iomanip> precision value

    rupert, Jul 29, 2006, in forum: C++
    Replies:
    2
    Views:
    2,689
    Mike Wahler
    Jul 29, 2006
Loading...

Share This Page