Update build.
[asdcplib.git] / src / KM_util.h
index d210bb91560dd3cc4ae9c9e1135a878858159098..6714c368c6b48f8440a0c30b84bfa6537f85629a 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2006, John Hurst
+Copyright (c) 2005-2008, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -36,6 +36,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <KM_error.h>
 #include <string.h>
 #include <string>
+#include <list>
 
 namespace Kumu
 {
@@ -117,7 +118,7 @@ namespace Kumu
   i32_t       hex2bin(const char* str, byte_t* buf, ui32_t buf_len, ui32_t* char_count);
 
   // Convert a binary string to NULL-terminated UTF-8 hexadecimal, returns the buffer
-  // if the binary buffer was large enough to hold the result. If the output buffer
+  // if the output buffer was large enough to hold the result. If the output buffer
   // is too small or any of the pointer arguments are NULL, the subroutine will
   // return 0.
   //
@@ -169,11 +170,59 @@ namespace Kumu
     {
     public:
       virtual ~IArchive(){}
-      virtual bool HasValue() const = 0;
-      virtual bool Archive(MemIOWriter* Writer) const = 0;
-      virtual bool Unarchive(MemIOReader* Reader) = 0;
+      virtual bool   HasValue() const = 0;
+      virtual ui32_t ArchiveLength() const = 0;
+      virtual bool   Archive(MemIOWriter* Writer) const = 0;
+      virtual bool   Unarchive(MemIOReader* Reader) = 0;
     };
 
+  //
+  template <class T>
+  class ArchivableList : public std::list<T>, public IArchive
+    {
+    public:
+      ArchivableList() {}
+      virtual ~ArchivableList() {}
+
+      bool HasValue() const { return ! this->empty(); }
+
+      ui32_t ArchiveLength() const
+      {
+       ui32_t arch_size = sizeof(ui32_t);
+
+       typename ArchivableList<T>::const_iterator i = this->begin();
+       for ( ; i != this->end(); i++ )
+         arch_size += i->ArchiveLength();
+
+       return arch_size;
+      }
+
+      bool Unarchive(Kumu::MemIOReader* Reader)
+       {
+         if ( Reader == 0 ) return false;
+         ui32_t read_size = 0;
+         if ( ! Reader->ReadUi32BE(&read_size) ) return false;
+         for ( ui32_t i = 0; i < read_size; i++ )
+           {
+             T TmpTP;
+             if ( ! TmpTP.Unarchive(Reader) ) return false;
+             this->push_back(TmpTP);
+           }
+
+         return true;
+       }
+
+      bool Archive(Kumu::MemIOWriter* Writer) const
+       {
+         if ( Writer == 0 ) return false;
+         if ( ! Writer->WriteUi32BE(this->size()) ) return false;
+         typename ArchivableList<T>::const_iterator i = this->begin();
+         for ( ; i != this->end(); i++ )
+           if ( ! i->Archive(Writer) ) return false;
+
+         return true;
+       }
+    };
 
   //
   // the base of all identifier classes, Identifier is not usually used directly
@@ -203,6 +252,7 @@ namespace Kumu
       }
 
       inline void Set(const byte_t* value) { m_HasValue = true; memcpy(m_Value, value, SIZE); }
+      inline void Reset() { m_HasValue = false; memset(m_Value, 0, SIZE); }
       inline const byte_t* Value() const { return m_Value; }
       inline ui32_t Size() const { return SIZE; }
 
@@ -254,6 +304,8 @@ namespace Kumu
 
       inline bool HasValue() const { return m_HasValue; }
 
+      inline ui32_t ArchiveLength() const { return SIZE; }
+
       inline bool Unarchive(Kumu::MemIOReader* Reader) {
        m_HasValue = Reader->ReadRaw(m_Value, SIZE);
        return m_HasValue;
@@ -264,7 +316,7 @@ namespace Kumu
       }
     };
 
-  
+
   // UUID
   //
   const ui32_t UUID_Length = 16;
@@ -276,6 +328,10 @@ namespace Kumu
       UUID(const UUID& rhs) : Identifier<UUID_Length>(rhs) {}
       virtual ~UUID() {}
 
+      inline const char* EncodeString(char* buf, ui32_t buf_len) const {
+       return bin2UUIDhex(m_Value, Size(), buf, buf_len);
+      }
+
       inline const char* EncodeHex(char* buf, ui32_t buf_len) const {
        return bin2UUIDhex(m_Value, Size(), buf, buf_len);
       }
@@ -325,6 +381,7 @@ namespace Kumu
 
       const Timestamp& operator=(const Timestamp& rhs);
       bool operator<(const Timestamp& rhs) const;
+      bool operator>(const Timestamp& rhs) const;
       bool operator==(const Timestamp& rhs) const;
       bool operator!=(const Timestamp& rhs) const;
 
@@ -345,9 +402,10 @@ namespace Kumu
       // | 16 bits int, big-endian |    8 bits   |   8 bits  |   8 bits   |    8 bits    |    8 bits    |
       // |        Year A.D         | Month(1-12) | Day(1-31) | Hour(0-23) | Minute(0-59) | Second(0-59) |
       //
-      virtual bool HasValue() const;
-      virtual bool Archive(MemIOWriter* Writer) const;
-      virtual bool Unarchive(MemIOReader* Reader);
+      virtual bool   HasValue() const;
+      virtual ui32_t ArchiveLength() const { return 8L; }
+      virtual bool   Archive(MemIOWriter* Writer) const;
+      virtual bool   Unarchive(MemIOReader* Reader);
     };
 
   //
@@ -365,8 +423,7 @@ namespace Kumu
       ByteString(ui32_t cap);
       virtual ~ByteString();
 
-      // Sets the size of the internally allocated buffer.
-      // Resets content Size to zero.
+      // Sets or resets the size of the internally allocated buffer.
       Result_t Capacity(ui32_t cap);
 
       Result_t Append(const ByteString&);
@@ -376,10 +433,10 @@ namespace Kumu
       inline ui32_t  Capacity() const { return m_Capacity; }
 
       // returns a const pointer to the essence data
-      inline const byte_t* RoData() const { return m_Data; }
+      inline const byte_t* RoData() const { assert(m_Data); return m_Data; }
        
       // returns a non-const pointer to the essence data
-      inline byte_t* Data() { return m_Data; }
+      inline byte_t* Data() { assert(m_Data); return m_Data; }
        
       // set the length of the buffer's contents
       inline ui32_t  Length(ui32_t l) { return m_Length = l; }
@@ -390,9 +447,12 @@ namespace Kumu
       // copy the given data into the ByteString, set Length value.
       // Returns error if the ByteString is too small.
       Result_t Set(const byte_t* buf, ui32_t buf_len);
+      Result_t Set(const ByteString& Buf);
 
       inline virtual bool HasValue() const { return m_Length > 0; }
 
+      inline virtual ui32_t ArchiveLength() const { return m_Length; }
+
       inline virtual bool Archive(MemIOWriter* Writer) const {
        assert(Writer);
        if ( ! Writer->WriteUi32BE(m_Length) ) return false;
@@ -402,9 +462,11 @@ namespace Kumu
 
       inline virtual bool Unarchive(MemIOReader* Reader) {
        assert(Reader);
-       if ( ! Reader->ReadUi32BE(&m_Length) ) return false;
-       if ( KM_FAILURE(Capacity(m_Length)) ) return false;
-       if ( ! Reader->ReadRaw(m_Data, m_Length) ) return false;
+       ui32_t tmp_len;
+       if ( ! Reader->ReadUi32BE(&tmp_len) ) return false;
+       if ( KM_FAILURE(Capacity(tmp_len)) ) return false;
+       if ( ! Reader->ReadRaw(m_Data, tmp_len) ) return false;
+       m_Length = tmp_len;
        return true;
       }
     };