[Assorted-commits] SF.net SVN: assorted:[1252] cpp-commons/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-03-05 23:14:58
|
Revision: 1252 http://assorted.svn.sourceforge.net/assorted/?rev=1252&view=rev Author: yangzhang Date: 2009-03-05 23:14:45 +0000 (Thu, 05 Mar 2009) Log Message: ----------- - accum returns residual count instead of bool - added anchor, reset Modified Paths: -------------- cpp-commons/trunk/src/commons/streamreader.h cpp-commons/trunk/src/test/streamreader.cc Modified: cpp-commons/trunk/src/commons/streamreader.h =================================================================== --- cpp-commons/trunk/src/commons/streamreader.h 2009-03-04 23:31:58 UTC (rev 1251) +++ cpp-commons/trunk/src/commons/streamreader.h 2009-03-05 23:14:45 UTC (rev 1252) @@ -48,7 +48,8 @@ full_reader_(repeat_reader(reader_)), buf_(buf, bufsize), start_(buf), - end_(buf) + end_(buf), + anchor_(buf) {} stream_reader(boost::function<size_t(char*, size_t)> reader, @@ -58,7 +59,8 @@ full_reader_(full_reader), buf_(buf, bufsize), start_(buf), - end_(buf) + end_(buf), + anchor_(buf) {} /** @@ -80,33 +82,47 @@ * Manually update/adjust the pointers; useful after changing buf_. */ void reset_range(char *start, char *end) { - start_ = start; + anchor_ = start_ = start; end_ = end; } + void set_anchor() { anchor_ = start_; } + + /** + * Reset the pointers, emptying the buffer. + */ + void reset() { + start_ = end_ = anchor_ = buf_.get(); + } + char *start() { return start_; } char *end() { return end_; } + char *anchor() { return anchor_; } /** - * Accumulate (but don't read/copy) the requested number of bytes. + * Accumulate (treat as read, but don't discard, and don't actually + * copy for reading) the requested number of bytes. Returns the + * remaining number of requested bytes that couldn't be accumulated + * because we ran out of buffer space. */ - bool accum(size_t req) { + size_t accum(size_t req) { while (unread() < req && rem() > 0) { size_t res = reader_(end_, rem()); if (res == 0) throw eof_exception(); end_ += res; } if (unread() < req) { + size_t ret = req - unread(); start_ = end_; - return false; + return ret; } else { start_ += req; - return true; + return 0; } } /** - * Discard the requested number of bytes. + * Discard the requested number of bytes. Disregards anchor. */ void skip(size_t req) { if (unread() >= req) { @@ -119,7 +135,7 @@ // reading. Skip over bytes that are immediately available... req -= unread(); // ...and reset pointers to discard current buffer. - start_ = end_ = buf_.get(); + reset(); // Keep reading until we have enough. while (true) { @@ -153,7 +169,7 @@ managed_array<char> p(new char[req], true); memcpy(p.get(), start_, unread()); full_reader_(p + unread(), req - unread()); - start_ = end_ = buf_.get(); + reset(); return p; } @@ -240,6 +256,11 @@ * The end of the unconsumed range of bytes. */ char *end_; + + /** + * Do not discard bytes >= this. + */ + char *anchor_; }; } Modified: cpp-commons/trunk/src/test/streamreader.cc =================================================================== --- cpp-commons/trunk/src/test/streamreader.cc 2009-03-04 23:31:58 UTC (rev 1251) +++ cpp-commons/trunk/src/test/streamreader.cc 2009-03-05 23:14:45 UTC (rev 1252) @@ -52,13 +52,13 @@ { array<char> rbuf(9); stream_reader r(rfn(chunklen, src), rbuf.get(), rbuf.size()); - EXPECT_TRUE(r.accum(sizeof(int))); - EXPECT_TRUE(r.accum(sizeof(int))); - EXPECT_FALSE(r.accum(sizeof(int))); + EXPECT_EQ(0, r.accum(sizeof(int))); + EXPECT_EQ(0, r.accum(sizeof(int))); + EXPECT_EQ(3, r.accum(sizeof(int))); r.shift(); - EXPECT_TRUE(r.accum(sizeof(int))); - EXPECT_TRUE(r.accum(sizeof(int))); - EXPECT_FALSE(r.accum(sizeof(int))); + EXPECT_EQ(0, r.accum(sizeof(int))); + EXPECT_EQ(0, r.accum(sizeof(int))); + EXPECT_EQ(3, r.accum(sizeof(int))); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |