#define XERR "memorybuf"
#include "memorybuf.ih"

    // called when using e.g., file << memorystreamobj.rdbuf()

// overrides
int MemoryBuf::underflow()
{
    if (
        not (d_mode & ios::in)              // reading not OK or 
        or                                  // beyond the last written offset?
        getOffset() >= d_bridge.writtenUntil()
    )  
        return EOF;                         // then quit

    d_bridge.load();                        // load offset's segment
    setg(d_bridge.beginPtr(), d_bridge.offsetPtr(), d_bridge.endReadPtr());

//xerr("offset " << d_bridge.offset() << " points to " << 
//(void *)d_bridge.beginPtr() << ", step = " << 
//(d_bridge.offsetPtr() - d_bridge.beginPtr()) << ", ch = " << 
//*d_bridge.beginPtr() << " and " << *d_bridge.offsetPtr() );

    d_last = READ;
    return static_cast<unsigned char>(*gptr());
}


//xerr("CALLED from offset " << d_bridge.offset());

//xerr("  data: " << (void *)d_bridge.data());
//xerr("  begin: " << d_bridge.begin());
//xerr("  end: " << d_bridge.end());
//xerr("  size: " << d_bridge.size());

//xerr("  ptr: " << (void *)d_bridge.ptr());
//xerr("  beyond: " << (void *)d_bridge.beyond());
//xerr("  writing " << (d_bridge.beyond() - d_bridge.ptr()) <<
//    " bytes from " << d_bridge.offset());

//xerr("Next offset: " << d_bridge.offset());

//
////xerr("available: " << (d_bridge.beyond() - d_bridge.next()) << " chars");
//
//    return static_cast<unsigned char>(*d_bridge.next());
