streams implementation - byte buffer

Discussion in 'C++' started by ma740988@pegasus.cc.ucf.edu, Apr 25, 2005.

  1. Guest

    A few days ago I recieved yet again advice on implementing a buffer of
    bytes. At issue: (part 1) how do I take the contents of a struct,
    then dump (used sparingly) it into a byte buffer. Similarily, take the
    contents of a (part 2) buffer and dump (used sparingly) it into a
    struct.

    So I found an implementation that handles part 1. Well partially.

    #include <ostream>
    namespace jngcomp { namespace utils
    {
    // Output string buffer class
    class stringbuf : public std::streambuf
    {
    public:
    stringbuf(char* buffer, int buflen)
    : m_buffer(buffer), m_buflen(buflen)
    {
    setp(m_buffer, m_buffer + m_buflen);
    }
    char* getpptr()
    {
    return pptr();
    }
    void setpptr(char* p)
    {
    setp(p, m_buffer + m_buflen);
    }
    protected:
    virtual int_type overflow(int_type c)
    {
    return EOF;
    }
    char* m_buffer;
    int m_buflen;
    };

    // Output string stream class which is suppled with a buffer to
    write to.
    // Ie, stream equivalent of sprintf.
    class ostringstream : public std::eek:stream
    {
    public:
    ostringstream(char* buffer, int buflen)
    : m_buf(buffer, buflen), std::eek:stream(&m_buf)
    {
    }
    char* getpptr()
    {
    return m_buf.getpptr();
    }
    void setpptr(char* p)
    {
    m_buf.setpptr(p);
    }
    protected:
    stringbuf m_buf;
    };
    }}

    struct test {
    unsigned int idx : 16;
    unsigned int jdx : 32;
    bool kdx : 1;
    };

    int main()
    {
    test t_;
    t_.idx = 15;
    t_.jdx = 25;

    char buffer[10];
    jngcomp::utils::eek:stringstream strm(buffer, sizeof(buffer));
    strm << t_.idx ;
    std::cout << buffer[0] << std::endl;
    std::cout << buffer[1] << std::endl;
    }

    What's interesting to me is the output is
    1
    5

    With this approach how does one get 15 to spread across 4 bytes
    withouth having to do
    strm << 0 << 0 << t_.idx; ?

    Tips/hints on part 2?
     
    , Apr 25, 2005
    #1
    1. Advertising

  2. Guest

    seems like what you posted has some BIG mistakes...
    for simple structs as yours I think it's enough to copy memory byte by
    byte:

    test the_struct;
    unsigned char buf[128]; //assume it
    //has enough space to hold the_struct

    //to buffer:
    memcpy(buf,&the_struct,sizeof(the_struct));

    //from buffer:
    memcpy(&the_struct,buf,sizeof(the_struct));


    this should answer part1, part2... but the need for this itself is very
    questionable...
     
    , Apr 25, 2005
    #2
    1. Advertising

  3. Guest

    Unless I'm missing something your solution assumes that padding etc
    with regard to compiler a and b on two different platforms are the
    same. Wont work well for tranmitting data between different platfoms.
    The struct shown was for 'demonstration' purposes. The 'real' struct
    is quite large.
     
    , Apr 25, 2005
    #3
  4. Guest

    > dump (used sparingly) it into a byte buffer
    means to copy to a byte buffer and nothing else.

    I didn't look inside that jngcomp::utils::eek:stringstream and probably
    all it does is the same as printing to a stream, e.g. it converts 15
    into "15" and nothing else, that's why you get 1 and 5. From the first
    glance I thought there was something wrong with the code because you
    put in integers into buffer and access then as bytes.

    If you are interested how to make it work yourself, you may provide
    friend operators for >> and << and print/read into your strutcs to/from
    a stream. Example:
    struct test {
    unsigned int idx : 16;
    unsigned int jdx : 32;
    bool kdx : 1;
    friend ostream& operator<<(ostream& s, const test& t){
    return s<<t.idx<<'\n'<<t.jdx<<'\n'<<t.kde<<'\n';
    }
    ... similar for operator>>
    };

    but I'd better use something that's already developed and tested by
    others, for example boost.serialization
     
    , Apr 25, 2005
    #4
  5. Guest

    >>> serialization

    Somehow I overlooked the boost library (once again). Adimittidely,
    this is pretty swanky and quite sophisticated for my level but I'm
    almost convinced the solution lies here in serialization.
     
    , Apr 26, 2005
    #5
    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. Ike
    Replies:
    1
    Views:
    1,966
  2. Raja
    Replies:
    12
    Views:
    24,522
    John Harrison
    Jun 21, 2004
  3. Replies:
    2
    Views:
    611
    sergejusz
    Mar 26, 2007
  4. Jordi
    Replies:
    4
    Views:
    2,040
    Jordi
    Apr 23, 2007
  5. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    419
    Neal Becker
    Jan 8, 2009
Loading...

Share This Page