From 7c8e31fc057859d08610f17f669fdfcaf2fc2956 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 12 Dec 2008 08:13:34 +0000 Subject: All of those changes I thought I'd already committed. The taf writer handles binary data much better, actually escaping it properly and not stopping on null. Bu::FString has an iterator, it's actually just a raw datatype, but it may have more function later, so careful assuming that it's a char and using it in any non-iterator like way. Also augmented the taf unit test, and added the Bu::CacheCalc base class, the rest of the simple develpment cycle will happen between here and project hhp. --- src/cachecalc.cpp | 1 + src/cachecalc.h | 22 ++++++++++++++++++++++ src/fstring.h | 25 +++++++++++++++++++++++++ src/tafwriter.cpp | 8 +++++++- src/unit/taf.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/cachecalc.cpp create mode 100644 src/cachecalc.h diff --git a/src/cachecalc.cpp b/src/cachecalc.cpp new file mode 100644 index 0000000..0080735 --- /dev/null +++ b/src/cachecalc.cpp @@ -0,0 +1 @@ +#include "bu/cachecalc.h" diff --git a/src/cachecalc.h b/src/cachecalc.h new file mode 100644 index 0000000..9e83b06 --- /dev/null +++ b/src/cachecalc.h @@ -0,0 +1,22 @@ +#ifndef BU_CACHE_CALC_H +#define BU_CACHE_CALC_H + +namespace Bu +{ + template + class CacheCalc + { + public: + CacheCalc() + { + } + + virtual ~CacheCalc() + { + } + + private: + }; +}; + +#endif diff --git a/src/fstring.h b/src/fstring.h index 167c055..91251bc 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -916,6 +916,31 @@ namespace Bu } } + typedef chr *iterator; + typedef const chr *const_iterator; + + iterator begin() + { + flatten(); + return pFirst->pData; + } + + const_iterator begin() const + { + flatten(); + return pFirst->pData; + } + + iterator end() + { + return pFirst->pData+pFirst->nLength; + } + + const_iterator end() const + { + return pFirst->pData+pFirst->nLength; + } + private: void flatten() const { diff --git a/src/tafwriter.cpp b/src/tafwriter.cpp index c30bc67..8e5fcdc 100644 --- a/src/tafwriter.cpp +++ b/src/tafwriter.cpp @@ -93,12 +93,18 @@ void Bu::TafWriter::writeString( const Bu::FString &str ) if( str.getStr() == NULL ) return; sOut.write("\"", 1 ); - for( const char *s = str.getStr(); *s; s++ ) + for( Bu::FString::const_iterator s = str.begin(); s != str.end(); s++ ) { if( *s == '\"' ) sOut.write("\\\"", 2 ); else if( *s == '\\' ) sOut.write("\\\\", 2 ); + else if( *s < 32 || *s > 126 ) + { + char buf[5]; + sprintf( buf, "\\x%02X", (unsigned char)*s ); + sOut.write(buf, 4 ); + } else sOut.write( s, 1 ); } diff --git a/src/unit/taf.cpp b/src/unit/taf.cpp index 71bd867..0813444 100644 --- a/src/unit/taf.cpp +++ b/src/unit/taf.cpp @@ -8,6 +8,8 @@ #include "bu/unitsuite.h" #include "bu/file.h" #include "bu/tafreader.h" +#include "bu/tafwriter.h" +#include "bu/membuf.h" #include #include @@ -19,6 +21,7 @@ public: { setName("taf"); addTest( Unit::read1 ); + addTest( Unit::encode ); } virtual ~Unit() @@ -46,6 +49,50 @@ public: unlink(sFnTmp.getStr()); #undef FN_TMP } + + void encode() + { + Bu::MemBuf mb; + Bu::TafWriter tw( mb ); + + Bu::TafGroup g("Test data"); + Bu::FString sData( 256 ); + for( int j = 0; j < 256; j++ ) + sData[j] = (unsigned char)j; + g.addChild( new Bu::TafProperty("Encoded", sData) ); + tw.writeGroup( &g ); + + static const char *cmpdata = "{\"Test data\":\n \"Encoded\"=\"" + "\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07" + "\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F" + "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17" + "\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F" + " !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCD" + "EFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghi" + "jklmnopqrstuvwxyz{|}~\\x7F" + "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87" + "\\x88\\x89\\x8A\\x8B\\x8C\\x8D\\x8E\\x8F" + "\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97" + "\\x98\\x99\\x9A\\x9B\\x9C\\x9D\\x9E\\x9F" + "\\xA0\\xA1\\xA2\\xA3\\xA4\\xA5\\xA6\\xA7" + "\\xA8\\xA9\\xAA\\xAB\\xAC\\xAD\\xAE\\xAF" + "\\xB0\\xB1\\xB2\\xB3\\xB4\\xB5\\xB6\\xB7" + "\\xB8\\xB9\\xBA\\xBB\\xBC\\xBD\\xBE\\xBF" + "\\xC0\\xC1\\xC2\\xC3\\xC4\\xC5\\xC6\\xC7" + "\\xC8\\xC9\\xCA\\xCB\\xCC\\xCD\\xCE\\xCF" + "\\xD0\\xD1\\xD2\\xD3\\xD4\\xD5\\xD6\\xD7" + "\\xD8\\xD9\\xDA\\xDB\\xDC\\xDD\\xDE\\xDF" + "\\xE0\\xE1\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7" + "\\xE8\\xE9\\xEA\\xEB\\xEC\\xED\\xEE\\xEF" + "\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5\\xF6\\xF7" + "\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF\"\n}\n"; + unitTest( mb.getString() == cmpdata ); + mb.setPos( 0 ); + Bu::TafReader tr( mb ); + Bu::TafGroup *rg = tr.readGroup(); + unitTest( rg->getProperty("Encoded") == sData ); + delete rg; + } }; int main( int argc, char *argv[] ) -- cgit v1.2.3