From 00cef0e9a21e1ee07d622bb05c3a7e4e56425723 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 25 May 2019 00:19:04 -0700 Subject: Started Bu::Text and Bu::Blob. These will someday replace Bu::String and Bu::UtfString. The plan is to support more encodings in the Text class than just Unicode, Blob and Text will not be shared core, but the Builders will be, making Text and Blob objects much safer in multi-threaded programs. --- src/stable/exceptioninvaliditerator.cpp | 14 ++ src/stable/exceptioninvaliditerator.h | 18 ++ src/unstable/blob.cpp | 403 ++++++++++++++++++++++++++++++++ src/unstable/blob.h | 125 ++++++++++ src/unstable/blobbuilder.cpp | 8 + src/unstable/blobbuilder.h | 8 + src/unstable/text.cpp | 8 + src/unstable/text.h | 0 src/unstable/textbuilder.cpp | 8 + src/unstable/textbuilder.h | 8 + 10 files changed, 600 insertions(+) create mode 100644 src/stable/exceptioninvaliditerator.cpp create mode 100644 src/stable/exceptioninvaliditerator.h create mode 100644 src/unstable/blob.cpp create mode 100644 src/unstable/blob.h create mode 100644 src/unstable/blobbuilder.cpp create mode 100644 src/unstable/blobbuilder.h create mode 100644 src/unstable/text.cpp create mode 100644 src/unstable/text.h create mode 100644 src/unstable/textbuilder.cpp create mode 100644 src/unstable/textbuilder.h diff --git a/src/stable/exceptioninvaliditerator.cpp b/src/stable/exceptioninvaliditerator.cpp new file mode 100644 index 0000000..8cd9061 --- /dev/null +++ b/src/stable/exceptioninvaliditerator.cpp @@ -0,0 +1,14 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/exceptioninvaliditerator.h" + +namespace Bu +{ + subExceptionDef( ExceptionInvalidIterator ); +} + diff --git a/src/stable/exceptioninvaliditerator.h b/src/stable/exceptioninvaliditerator.h new file mode 100644 index 0000000..a157553 --- /dev/null +++ b/src/stable/exceptioninvaliditerator.h @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_EXCEPTION_INVALID_ITERATOR_H +#define BU_EXCEPTION_INVALID_ITERATOR_H + +#include "exceptionbase.h" + +namespace Bu +{ + subExceptionDecl( ExceptionInvalidIterator ); +} + +#endif diff --git a/src/unstable/blob.cpp b/src/unstable/blob.cpp new file mode 100644 index 0000000..51a3b93 --- /dev/null +++ b/src/unstable/blob.cpp @@ -0,0 +1,403 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/blob.h" +#include "bu/exceptioninvaliditerator.h" + +#include + +Bu::Blob::Blob() : + pData( 0 ), + iSize( -1 ) +{ +} + +Bu::Blob::Blob( const Bu::Blob &rSrc ) : + pData( 0 ), + iSize( rSrc.iSize ) +{ + pData = new char[rSrc.iSize+1]; + memcpy( pData, rSrc.pData, iSize+1 ); +} + +Bu::Blob::Blob( const char *pSrc ) : + pData( 0 ), + iSize( 0 ) +{ + for(; iSize < __INT_MAX__ && pSrc[iSize]; iSize++ ) { } + pData = new char[iSize+1]; + memcpy( pData, pSrc, iSize+1 ); +} + +Bu::Blob::Blob( const void *pSrc, int32_t iSize ) +{ + this->iSize = iSize; + pData = new char[iSize+1]; + memcpy( pData, pSrc, iSize ); + pData[iSize] = '\0'; +} + +Bu::Blob::~Blob() +{ + delete[] pData; + pData = 0; + iSize = -1; +} + +int32_t Bu::Blob::getSize() const +{ + return iSize; +} + +char *Bu::Blob::getData() const +{ + return pData; +} + +char *Bu::Blob::c_str() const +{ + return pData; +} + +Bu::Blob &Bu::Blob::operator=( const Bu::Blob &rRhs ) +{ + delete[] pData; + + iSize = rRhs.iSize; + pData = new char[iSize+1]; + memcpy( pData, rRhs.pData, iSize+1 ); + + return *this; +} + +Bu::Blob &Bu::Blob::operator=( const char *pRhs ) +{ + delete[] pData; + + for(iSize = 0; iSize < __INT_MAX__ && pRhs[iSize]; iSize++ ) { } + pData = new char[iSize+1]; + memcpy( pData, pRhs, iSize+1 ); + + return *this; +} + +Bu::Blob::iterator::iterator( Bu::Blob *pBlob, bool bForward ) : + pBlob( pBlob ), + iIndex( bForward?0:pBlob->iSize-1 ), + bForward( bForward ) +{ +} + +Bu::Blob::iterator::iterator() : + pBlob( NULL ), + iIndex( -1 ), + bForward( true ) +{ +} + +Bu::Blob::iterator::iterator( const Bu::Blob::iterator &rSrc ) : + pBlob( rSrc.pBlob ), + iIndex( rSrc.iIndex ), + bForward( rSrc.bForward ) +{ +} + +bool Bu::Blob::iterator::isValid() const +{ + if( !pBlob ) + return false; + + if( iIndex < 0 || iIndex >= pBlob->iSize ) + return false; + + return true; +} + +Bu::Blob::iterator::operator bool() const +{ + return isValid(); +} + +char &Bu::Blob::iterator::operator *() +{ + if( !isValid() ) + throw Bu::ExceptionInvalidIterator(); + + return pBlob->pData[iIndex]; +} + +Bu::Blob::iterator &Bu::Blob::iterator::operator++( int ) +{ + if( bForward ) + ++iIndex; + else + --iIndex; + + return *this; +} + +Bu::Blob::iterator &Bu::Blob::iterator::operator++() +{ + if( bForward ) + ++iIndex; + else + --iIndex; + + return *this; +} + +Bu::Blob::iterator &Bu::Blob::iterator::operator--( int ) +{ + if( bForward ) + --iIndex; + else + ++iIndex; + + return *this; +} + +Bu::Blob::iterator &Bu::Blob::iterator::operator--() +{ + if( bForward ) + --iIndex; + else + ++iIndex; + + return *this; +} + +bool Bu::Blob::iterator::operator==( const Bu::Blob::iterator &rRhs ) +{ + if( isValid() == false && rRhs.isValid() == false ) + return true; + + return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; +} + +bool Bu::Blob::iterator::operator==( const Bu::Blob::const_iterator &rRhs ) +{ + if( isValid() == false && rRhs.isValid() == false ) + return true; + + return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; +} + +bool Bu::Blob::iterator::operator!=( const Bu::Blob::iterator &rRhs ) +{ + if( isValid() != rRhs.isValid() ) + return true; + + return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; +} + +bool Bu::Blob::iterator::operator!=( const Bu::Blob::const_iterator &rRhs ) +{ + if( isValid() != rRhs.isValid() ) + return true; + + return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; +} + +Bu::Blob::iterator &Bu::Blob::iterator::operator=( Bu::Blob::iterator &rRhs ) +{ + pBlob = rRhs.pBlob; + iIndex = rRhs.iIndex; + bForward = rRhs.bForward; + + return *this; +} + + +Bu::Blob::const_iterator::const_iterator( const Blob *pBlob, bool bForward ) : + pBlob( pBlob ), + iIndex( bForward?0:pBlob->iSize-1 ), + bForward( bForward ) +{ +} + +Bu::Blob::const_iterator::const_iterator() : + pBlob( NULL ), + iIndex( -1 ), + bForward( true ) +{ +} + +Bu::Blob::const_iterator::const_iterator( + const Bu::Blob::const_iterator &rSrc ) : + pBlob( rSrc.pBlob ), + iIndex( rSrc.iIndex ), + bForward( rSrc.bForward ) +{ +} + +Bu::Blob::const_iterator::const_iterator( const Bu::Blob::iterator &rSrc ) : + pBlob( rSrc.pBlob ), + iIndex( rSrc.iIndex ), + bForward( rSrc.bForward ) +{ +} + +bool Bu::Blob::const_iterator::isValid() const +{ + if( !pBlob ) + return false; + + if( iIndex < 0 || iIndex >= pBlob->iSize ) + return false; + + return true; +} + +Bu::Blob::const_iterator::operator bool() const +{ + return isValid(); +} + +char Bu::Blob::const_iterator::operator *() const +{ + if( !isValid() ) + throw Bu::ExceptionInvalidIterator(); + + return pBlob->pData[iIndex]; +} + +Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator++( int ) +{ + if( bForward ) + ++iIndex; + else + --iIndex; + + return *this; +} + +Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator++() +{ + if( bForward ) + ++iIndex; + else + --iIndex; + + return *this; +} + +Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator--( int ) +{ + if( bForward ) + ++iIndex; + else + --iIndex; + + return *this; +} + +Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator--() +{ + if( bForward ) + ++iIndex; + else + --iIndex; + + return *this; +} + +bool Bu::Blob::const_iterator::operator==( const Bu::Blob::iterator &rRhs ) +{ + if( isValid() == false && rRhs.isValid() == false ) + return true; + + return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; +} + +bool Bu::Blob::const_iterator::operator==( + const Bu::Blob::const_iterator &rRhs ) +{ + if( isValid() == false && rRhs.isValid() == false ) + return true; + + return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; +} + +bool Bu::Blob::const_iterator::operator!=( const Bu::Blob::iterator &rRhs ) +{ + if( isValid() != rRhs.isValid() ) + return true; + + return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; +} + +bool Bu::Blob::const_iterator::operator!=( + const Bu::Blob::const_iterator &rRhs ) +{ + if( isValid() != rRhs.isValid() ) + return true; + + return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; +} + +Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( + Bu::Blob::iterator &rRhs ) +{ + pBlob = rRhs.pBlob; + iIndex = rRhs.iIndex; + bForward = rRhs.bForward; + + return *this; +} + +Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( + Bu::Blob::const_iterator &rRhs ) +{ + pBlob = rRhs.pBlob; + iIndex = rRhs.iIndex; + bForward = rRhs.bForward; + + return *this; +} + + +Bu::Blob::iterator Bu::Blob::begin() +{ + return iterator( this, true ); +} + +Bu::Blob::const_iterator Bu::Blob::begin() const +{ + return const_iterator( this, true ); +} + +Bu::Blob::iterator Bu::Blob::end() +{ + return iterator(); +} + +Bu::Blob::const_iterator Bu::Blob::end() const +{ + return const_iterator(); +} + +Bu::Blob::iterator Bu::Blob::rbegin() +{ + return iterator( this, false ); +} + +Bu::Blob::const_iterator Bu::Blob::rbegin() const +{ + return const_iterator( this, false ); +} + +Bu::Blob::iterator Bu::Blob::rend() +{ + return iterator(); +} + +Bu::Blob::const_iterator Bu::Blob::rend() const +{ + return const_iterator(); +} + + diff --git a/src/unstable/blob.h b/src/unstable/blob.h new file mode 100644 index 0000000..5b8cabd --- /dev/null +++ b/src/unstable/blob.h @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_BLOB_H +#define BU_BLOB_H + +#include + +namespace Bu +{ + /** + * Blob contans a sequence of binary values. This is basically the + * replacement for the old libbu++ Bu::String, std::string classes, or the + * tradition C char * string. The Blob is static and immutable, and a good + * choice when dealing with any sequence of binary data that you don't + * need to interpret further. + * + * If you're dealing with data that contains language, such as human text, + * then use the Bu::Text class instead. + */ + class Blob + { + public: + Blob(); + Blob( const Blob &rSrc ); + Blob( const char *pSrc ); + Blob( const void *pSrc, int32_t iSize ); + virtual ~Blob(); + + int32_t getSize() const; + char *getData() const; + + char *c_str() const; + + Blob &operator=( const Blob &rRhs ); + Blob &operator=( const char *pRhs ); + + class const_iterator; + class iterator + { + friend class Blob; + friend class const_iterator; + private: + iterator( Blob *pBlob, bool bForward ); + + public: + iterator(); + iterator( const iterator &rSrc ); + + bool isValid() const; + operator bool() const; + char &operator *(); + + iterator &operator++( int ); + iterator &operator++(); + iterator &operator--( int ); + iterator &operator--(); + bool operator==( const iterator &rRhs ); + bool operator==( const const_iterator &rRhs ); + bool operator!=( const iterator &rRhs ); + bool operator!=( const const_iterator &rRhs ); + + iterator &operator=( iterator &rRhs ); + + private: + Blob *pBlob; + int32_t iIndex; + bool bForward; + }; + + class const_iterator + { + friend class Blob; + friend class iterator; + private: + const_iterator( const Blob *pBlob, bool bForward ); + + public: + const_iterator(); + const_iterator( const const_iterator &rSrc ); + const_iterator( const iterator &rSrc ); + + bool isValid() const; + operator bool() const; + char operator *() const; + + const_iterator &operator++( int ); + const_iterator &operator++(); + const_iterator &operator--( int ); + const_iterator &operator--(); + bool operator==( const iterator &rRhs ); + bool operator==( const const_iterator &rRhs ); + bool operator!=( const iterator &rRhs ); + bool operator!=( const const_iterator &rRhs ); + + const_iterator &operator=( iterator &rRhs ); + const_iterator &operator=( const_iterator &rRhs ); + + private: + const Blob *pBlob; + int32_t iIndex; + bool bForward; + }; + + iterator begin(); + const_iterator begin() const; + iterator end(); + const_iterator end() const; + iterator rbegin(); + const_iterator rbegin() const; + iterator rend(); + const_iterator rend() const; + + private: + char *pData; + int32_t iSize; + }; +} + +#endif + diff --git a/src/unstable/blobbuilder.cpp b/src/unstable/blobbuilder.cpp new file mode 100644 index 0000000..73271f8 --- /dev/null +++ b/src/unstable/blobbuilder.cpp @@ -0,0 +1,8 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + + diff --git a/src/unstable/blobbuilder.h b/src/unstable/blobbuilder.h new file mode 100644 index 0000000..73271f8 --- /dev/null +++ b/src/unstable/blobbuilder.h @@ -0,0 +1,8 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + + diff --git a/src/unstable/text.cpp b/src/unstable/text.cpp new file mode 100644 index 0000000..73271f8 --- /dev/null +++ b/src/unstable/text.cpp @@ -0,0 +1,8 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + + diff --git a/src/unstable/text.h b/src/unstable/text.h new file mode 100644 index 0000000..e69de29 diff --git a/src/unstable/textbuilder.cpp b/src/unstable/textbuilder.cpp new file mode 100644 index 0000000..73271f8 --- /dev/null +++ b/src/unstable/textbuilder.cpp @@ -0,0 +1,8 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + + diff --git a/src/unstable/textbuilder.h b/src/unstable/textbuilder.h new file mode 100644 index 0000000..73271f8 --- /dev/null +++ b/src/unstable/textbuilder.h @@ -0,0 +1,8 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + + -- cgit v1.2.3