ostream like interface to a string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • __PPS__

    #1

    ostream like interface to a string?

    Hello,
    I was thinking about one solution, but no luck.

    suppose I have an std::string object.
    I want to be able to use this string like std::ostringstr eam.
    something like:

    string str;
    ostringstream out(str);

    out << "Hello world." << 123;

    and then str should contain "Hello world.123". Basicly, I heed a
    specified string object be a underlying buffer for a ostringstream
    object. How can I do that?

    Should I write another class derived from streambuf or something???


    Thanks for any ideas.
  • White Wolf

    #2
    Re: ostream like interface to a string?

    __PPS__ wrote:[color=blue]
    > Hello,
    > I was thinking about one solution, but no luck.
    >
    > suppose I have an std::string object.
    > I want to be able to use this string like std::ostringstr eam.
    > something like:
    >
    > string str;
    > ostringstream out(str);
    >
    > out << "Hello world." << 123;
    >
    > and then str should contain "Hello world.123". Basicly, I heed a
    > specified string object be a underlying buffer for a ostringstream
    > object. How can I do that?
    >
    > Should I write another class derived from streambuf or something???[/color]

    Yes, something like that. The least "expensive" (from manhours POV) is to
    use ostringstream. I have found that in most cases when I would need that
    spec. string looks exactly like the above case of yours, which is
    equvivalent to this:


    ostringstream ostr();

    out << "Hello world." << 123;
    string str(ostr.str();

    Give a library using COW strings, this is just as efficient (or inefficient
    in MT :-)) as using that special wrapper-thing.

    --
    WW aka Attila
    :::
    Change is inevitable, except from a vending machine.


    Comment

    • __PPS__

      #3
      Re: ostream like interface to a string?

      > Yes, something like that. The least "expensive" (from manhours POV) is to[color=blue]
      > use ostringstream. I have found that in most cases when I would need that
      > spec. string looks exactly like the above case of yours, which is
      > equvivalent to this:
      >
      >
      > ostringstream ostr();
      >
      > out << "Hello world." << 123;
      > string str(ostr.str();
      >
      > Give a library using COW strings, this is just as efficient (or inefficient
      > in MT :-)) as using that special wrapper-thing.[/color]

      What are the COW strings??
      I solved it this way: (but it's not what I needed/wanted)

      /////////////
      #include <iostream>
      #include <sstream>
      #include <string>

      class xostream : public std::ostringstr eam {
      protected:
      std::string & str_;
      public:
      xostream (std::string & s) :
      str_(s),
      std::ostringstr eam(s,std::ios: :ate|std::ios:: app)
      { }
      ~xostream ()
      { str_ = str(); }
      };

      // and then:
      using namespace std;

      int main(int,char*[]){
      string data = "Hello1";
      {
      xostream file(data);
      file << "\nHello2"
      << "\nInt: " << 12
      << "\nFloat: " << 123.3456 << std::endl;
      }
      std::cout << data << std::endl; //produces unbelievable result !!
      return 0;
      }
      ////////

      But there seems to be a bug in libraries - it produces different
      output when compiled by different compilers:

      Microsoft Visual C++ .NET 2002 / VC 6.0 (Incorrect):

      Hello2
      Int: 12
      Float: 123.346

      Microsoft Visual C++ 2005 (Correct):
      Hello1
      Hello2
      Int: 12
      Float: 123.346

      Looks like there are problems with ostringstream in older versions.
      to avoid problem:
      xostream::xostr eam (std::string & s) : str_(s),
      std::ostringstr eam(std::ios::a te|std::ios::ap p)
      { *this << s; }
      /**/


      as to the "The least "expensive" (from manhours POV)" - It's not work
      or anything - it's my own idea (unrelated to job). So, I'm not looking
      for easy ways :)
      Originally it was to create a virtual filesystem for a small my
      private project.
      It stored "files" in a std::map<string ,string> and there was also an
      ebedded Javascript interpreter to run scripts that would have access
      to these files. I wanted to provide standart fstream interface to
      these virtual files (stored in std::strings) so that they would be
      manipulated directly without any unnecessary copying or buffering (and
      using this interface, manipulate these files from JS, or from external
      modules). So the interface should be based on standard classes...
      That's why I posted my question here - someone might have had
      implemented something like this and will share ideas/info.


      In some articles/tutorials I saw examples using input_iterator , maybe
      it could help me??


      Thanks.

      Comment

      • White Wolf

        #4
        Re: ostream like interface to a string?

        __PPS__ wrote:[color=blue][color=green]
        >> Yes, something like that. The least "expensive" (from manhours POV) is
        >> to use ostringstream. I have found that in most cases when I would
        >> need that spec. string looks exactly like the above case of yours,
        >> which is equvivalent to this:
        >>
        >>
        >> ostringstream ostr();
        >>
        >> out << "Hello world." << 123;
        >> string str(ostr.str();
        >>
        >> Give a library using COW strings, this is just as efficient (or
        >> inefficient in MT :-)) as using that special wrapper-thing.[/color]
        >
        > What are the COW strings??
        > I solved it this way: (but it's not what I needed/wanted)[/color]



        --
        WW aka Attila
        :::
        Water which is too pure has no fish.


        Comment

        Working...