/[svn]/libgig/trunk/src/Serialization.cpp
ViewVC logotype

Diff of /libgig/trunk/src/Serialization.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3153 by schoenebeck, Sat May 6 13:43:43 2017 UTC revision 3168 by schoenebeck, Tue May 9 19:12:32 2017 UTC
# Line 30  Line 30 
30    
31  #include "helper.h"  #include "helper.h"
32    
33    #define LIBGIG_EPOCH_TIME ((time_t)0)
34    
35  namespace Serialization {  namespace Serialization {
36    
37      // *************** DataType ***************      // *************** DataType ***************
38      // *      // *
39    
40      static UID _createNullUID() {      static UID _createNullUID() {
41          return (UID) { NULL, 0 };          const UID uid = { NULL, 0 };
42            return uid;
43      }      }
44    
45      const UID NO_UID = _createNullUID();      const UID NO_UID = _createNullUID();
# Line 277  namespace Serialization { Line 280  namespace Serialization {
280          m_operation = OPERATION_NONE;          m_operation = OPERATION_NONE;
281          m_root = NO_UID;          m_root = NO_UID;
282          m_isModified = false;          m_isModified = false;
283            m_timeCreated = m_timeModified = LIBGIG_EPOCH_TIME;
284      }      }
285    
286      Archive::Archive(const RawData& data) {      Archive::Archive(const RawData& data) {
287          m_operation = OPERATION_NONE;          m_operation = OPERATION_NONE;
288          m_root = NO_UID;          m_root = NO_UID;
289          m_isModified = false;          m_isModified = false;
290            m_timeCreated = m_timeModified = LIBGIG_EPOCH_TIME;
291          decode(m_rawData);          decode(m_rawData);
292      }      }
293    
# Line 290  namespace Serialization { Line 295  namespace Serialization {
295          m_operation = OPERATION_NONE;          m_operation = OPERATION_NONE;
296          m_root = NO_UID;          m_root = NO_UID;
297          m_isModified = false;          m_isModified = false;
298            m_timeCreated = m_timeModified = LIBGIG_EPOCH_TIME;
299          decode(data, size);          decode(data, size);
300      }      }
301    
# Line 311  namespace Serialization { Line 317  namespace Serialization {
317          return _encodeBlob(s);          return _encodeBlob(s);
318      }      }
319    
320        static String _encode(const time_t& time) {
321            return _encodeBlob(ToString(time));
322        }
323    
324      static String _encode(const DataType& type) {      static String _encode(const DataType& type) {
325          String s;          String s;
326          s += _encodeBlob(type.baseTypeName());          s += _encodeBlob(type.baseTypeName());
# Line 426  namespace Serialization { Line 436  namespace Serialization {
436          s += _encodeBlob(ToString(ENCODING_FORMAT_MINOR_VERSION));          s += _encodeBlob(ToString(ENCODING_FORMAT_MINOR_VERSION));
437          s += _encode(m_root);          s += _encode(m_root);
438          s += _encode(m_allObjects);          s += _encode(m_allObjects);
439            s += _encodeBlob(m_name);
440            s += _encodeBlob(m_comment);
441            s += _encode(m_timeCreated);
442            s += _encode(m_timeModified);
443          return _encodeBlob(s);          return _encodeBlob(s);
444      }      }
445    
446      void Archive::encode() {      void Archive::encode() {
447          m_rawData.clear();          m_rawData.clear();
448          String s = MAGIC_START;          String s = MAGIC_START;
449            m_timeModified = time(NULL);
450            if (m_timeCreated == LIBGIG_EPOCH_TIME)
451                m_timeCreated = m_timeModified;
452          s += _encodeRootBlob();          s += _encodeRootBlob();
453          m_rawData.resize(s.length() + 1);          m_rawData.resize(s.length() + 1);
454          memcpy(&m_rawData[0], &s[0], s.length() + 1);          memcpy(&m_rawData[0], &s[0], s.length() + 1);
# Line 444  namespace Serialization { Line 461  namespace Serialization {
461      };      };
462    
463      static _Blob _decodeBlob(const char* p, const char* end, bool bThrow = true) {      static _Blob _decodeBlob(const char* p, const char* end, bool bThrow = true) {
464          if (!bThrow && p >= end)          if (!bThrow && p >= end) {
465              return (_Blob) { p, end };              const _Blob blob =  { p, end };
466                return blob;
467            }
468          size_t sz = 0;          size_t sz = 0;
469          for (; true; ++p) {          for (; true; ++p) {
470              if (p >= end)              if (p >= end)
# Line 460  namespace Serialization { Line 479  namespace Serialization {
479          ++p;          ++p;
480          if (p + sz > end)          if (p + sz > end)
481              throw Exception("Decode Error: Premature end of blob");              throw Exception("Decode Error: Premature end of blob");
482          return (_Blob) { p, p + sz };          const _Blob blob = { p, p + sz };
483            return blob;
484      }      }
485    
486      template<typename T_int>      template<typename T_int>
# Line 535  namespace Serialization { Line 555  namespace Serialization {
555          return s;          return s;
556      }      }
557    
558        static time_t _popTimeBlob(const char*& p, const char* end) {
559            const uint64_t i = _popIntBlob<uint64_t>(p, end);
560            return (time_t) i;
561        }
562    
563      DataType _popDataTypeBlob(const char*& p, const char* end) {      DataType _popDataTypeBlob(const char*& p, const char* end) {
564          _Blob blob = _decodeBlob(p, end);          _Blob blob = _decodeBlob(p, end);
565          p   = blob.p;          p   = blob.p;
# Line 559  namespace Serialization { Line 584  namespace Serialization {
584          const ID id = (ID) _popIntBlob<size_t>(p, end);          const ID id = (ID) _popIntBlob<size_t>(p, end);
585          const size_t size = _popIntBlob<size_t>(p, end);          const size_t size = _popIntBlob<size_t>(p, end);
586    
587          return (UID) { id, size };          const UID uid = { id, size };
588            return uid;
589      }      }
590    
591      static UIDChain _popUIDChainBlob(const char*& p, const char* end) {      static UIDChain _popUIDChainBlob(const char*& p, const char* end) {
# Line 711  namespace Serialization { Line 737  namespace Serialization {
737          _popObjectsBlob(p, end);          _popObjectsBlob(p, end);
738          if (!m_allObjects[m_root])          if (!m_allObjects[m_root])
739              throw Exception("Decode Error: Missing declared root object");              throw Exception("Decode Error: Missing declared root object");
740    
741            m_name = _popStringBlob(p, end);
742            m_comment = _popStringBlob(p, end);
743            m_timeCreated = _popTimeBlob(p, end);
744            m_timeModified = _popTimeBlob(p, end);
745      }      }
746    
747      void Archive::decode(const RawData& data) {      void Archive::decode(const RawData& data) {
748          m_rawData = data;          m_rawData = data;
749          m_allObjects.clear();          m_allObjects.clear();
750          m_isModified = false;          m_isModified = false;
751            m_timeCreated = m_timeModified = LIBGIG_EPOCH_TIME;
752          const char* p   = (const char*) &data[0];          const char* p   = (const char*) &data[0];
753          const char* end = p + data.size();          const char* end = p + data.size();
754          if (memcmp(p, MAGIC_START, std::min(strlen(MAGIC_START), data.size())))          if (memcmp(p, MAGIC_START, std::min(strlen(MAGIC_START), data.size())))
# Line 751  namespace Serialization { Line 783  namespace Serialization {
783          m_root = NO_UID;          m_root = NO_UID;
784          m_rawData.clear();          m_rawData.clear();
785          m_isModified = false;          m_isModified = false;
786            m_timeCreated = m_timeModified = LIBGIG_EPOCH_TIME;
787        }
788    
789        String Archive::name() const {
790            return m_name;
791        }
792    
793        void Archive::setName(String name) {
794            if (m_name == name) return;
795            m_name = name;
796            m_isModified = true;
797        }
798    
799        String Archive::comment() const {
800            return m_comment;
801        }
802    
803        void Archive::setComment(String comment) {
804            if (m_comment == comment) return;
805            m_comment = comment;
806            m_isModified = true;
807        }
808    
809        static tm _convertTimeStamp(const time_t& time, time_base_t base) {
810            tm* pTm;
811            switch (base) {
812                case LOCAL_TIME:
813                    pTm = localtime(&time);
814                    break;
815                case UTC_TIME:
816                    pTm = gmtime(&time);
817                    break;
818                default:
819                    throw Exception("Time stamp with unknown time base (" + ToString((int64_t)base) + ") requested");
820            }
821            if (!pTm)
822                throw Exception("Failed assembling time stamp structure");
823            return *pTm;
824        }
825    
826        time_t Archive::timeStampCreated() const {
827            return m_timeCreated;
828        }
829    
830        time_t Archive::timeStampModified() const {
831            return m_timeModified;
832        }
833    
834        tm Archive::dateTimeCreated(time_base_t base) const {
835            return _convertTimeStamp(m_timeCreated, base);
836        }
837    
838        tm Archive::dateTimeModified(time_base_t base) const {
839            return _convertTimeStamp(m_timeModified, base);
840      }      }
841    
842      void Archive::removeMember(Object& parent, const Member& member) {      void Archive::removeMember(Object& parent, const Member& member) {

Legend:
Removed from v.3153  
changed lines
  Added in v.3168

  ViewVC Help
Powered by ViewVC