Revision: 44376 http://brlcad.svn.sourceforge.net/brlcad/?rev=44376&view=rev Author: davidloman Date: 2011-04-14 11:21:45 +0000 (Thu, 14 Apr 2011)
Log Message: ----------- Add getString and putString to ByteBuffer class. ByteBuffer class should now be a complete replacement for DataStream and ByteArray classes. Replacements to follow. Modified Paths: -------------- geomcore/trunk/include/ByteBuffer.h geomcore/trunk/src/utility/ByteBuffer.cxx geomcore/trunk/tests/unit/utility/ByteBufferUTest.cxx geomcore/trunk/tests/unit/utility/ByteBufferUTest.h Modified: geomcore/trunk/include/ByteBuffer.h =================================================================== --- geomcore/trunk/include/ByteBuffer.h 2011-04-14 06:41:58 UTC (rev 44375) +++ geomcore/trunk/include/ByteBuffer.h 2011-04-14 11:21:45 UTC (rev 44376) @@ -301,7 +301,34 @@ void put64bit(uint64_t v); + /** + * Relative <i>get</i> method for reading a standard string value. + * + * <p> Reads the four bytes containing the given string's length followed + * by the string data, and then increments the position by 4 + length of + * the string.</p> + * + * @return The standard string value at the buffer's current position + * + */ + std::string + getString(); + + /** + * Relative <i>put</i> method for writing a standard string value. + * + * <p> Writes the four bytes containing the given string's length into this + * buffer at the current position followed by the string data, and then + * increments the position by 4 + length of the string.</p> + * + * @param str + * The string to be written + */ + void + putString(std::string str); + + /** * <p>Returns the number of bytes between <i>position</i> and * <i>limit</i>.</p> * Modified: geomcore/trunk/src/utility/ByteBuffer.cxx =================================================================== --- geomcore/trunk/src/utility/ByteBuffer.cxx 2011-04-14 06:41:58 UTC (rev 44375) +++ geomcore/trunk/src/utility/ByteBuffer.cxx 2011-04-14 11:21:45 UTC (rev 44376) @@ -246,6 +246,26 @@ bu_vlb_write(&this->vlb, (unsigned char *)&net, 8); } + +void +ByteBuffer::putString(std::string str) +{ + this->put32bit(str.length()); + this->put((char*)str.c_str(), str.length()); +} + +std::string +ByteBuffer::getString() +{ + uint32_t len = this->get32bit(); + + char* ptr = this->array() + this->position(); + this->setPosition(this->position() + len); + std::string out; + out.append(ptr, len); + return out; +} + size_t ByteBuffer::remaining() { Modified: geomcore/trunk/tests/unit/utility/ByteBufferUTest.cxx =================================================================== --- geomcore/trunk/tests/unit/utility/ByteBufferUTest.cxx 2011-04-14 06:41:58 UTC (rev 44375) +++ geomcore/trunk/tests/unit/utility/ByteBufferUTest.cxx 2011-04-14 11:21:45 UTC (rev 44376) @@ -35,6 +35,7 @@ const uint32_t ByteBufferUTest::testVal_32bit_net = 0x5D4C3B2A; const uint64_t ByteBufferUTest::testVal_64bit_host = 0x2A3B4C5D6E7F8A9B; const uint64_t ByteBufferUTest::testVal_64bit_net = 0x9B8A7F6E5D4C3B2A; +const std::string ByteBufferUTest::testVal_StdString = "The quick brown fox jumps over the lazy dog"; void @@ -347,6 +348,15 @@ } void +ByteBufferUTest::testGetSetString() +{ + this->bb->putString(testVal_StdString); + this->bb->flip(); + std::string result = this->bb->getString(); + CPPUNIT_ASSERT(result == testVal_StdString); +} + +void ByteBufferUTest::setUp() { // std::cout << "\nSetUp.\n"; Modified: geomcore/trunk/tests/unit/utility/ByteBufferUTest.h =================================================================== --- geomcore/trunk/tests/unit/utility/ByteBufferUTest.h 2011-04-14 06:41:58 UTC (rev 44375) +++ geomcore/trunk/tests/unit/utility/ByteBufferUTest.h 2011-04-14 11:21:45 UTC (rev 44376) @@ -29,6 +29,7 @@ #include "ByteBuffer.h" #include <cppunit/TestCase.h> #include <cppunit/extensions/HelperMacros.h> +#include <string.h> class ByteBufferUTest : public CPPUNIT_NS::TestFixture { @@ -60,6 +61,7 @@ CPPUNIT_TEST( testReset ); CPPUNIT_TEST( testRewind ); + CPPUNIT_TEST( testGetSetString ); CPPUNIT_TEST_SUITE_END(); @@ -97,6 +99,7 @@ void testSetPosition(); void testReset(); void testRewind(); + void testGetSetString(); private: ByteBuffer* bb; @@ -109,6 +112,7 @@ static const uint32_t testVal_32bit_net; static const uint64_t testVal_64bit_host; static const uint64_t testVal_64bit_net; + static const std::string testVal_StdString; }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Benefiting from Server Virtualization: Beyond Initial Workload Consolidation -- Increasing the use of server virtualization is a top priority.Virtualization can reduce costs, simplify management, and improve application availability and disaster protection. Learn more about boosting the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev _______________________________________________ BRL-CAD Source Commits mailing list brlcad-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/brlcad-commits