Advertisement
jacknpoe

Buckwheat (some types and fromBinary)

Nov 8th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1. // Nodes( index) is a macro to protect the access to a array of struct not published
  2.  
  3. ( . . . )
  4.  
  5.     typedef enum {
  6.         SNT_NONE,
  7.         SNT_PT_CHAR, SNT_PT_WCHAR, SNT_STRUCT,      // sized
  8.         SNT_STRING, SNT_WSTRING,        // sized
  9.         SNT_CHAR, SNT_WCHAR, SNT_SHORT, SNT_LONG,
  10.         SNT_LONG_LONG, SNT_FLOAT, SNT_DOUBLE, SNT_LONG_DOUBLE,
  11.         SNT_PT_CHAR16, SNT_PT_CHAR32,   // sized  ]
  12.         SNT_U16STRING, SNT_U32STRING,   // sized  |-- enum types don't have version restraints.
  13.         SNT_CHAR16, SNT_CHAR32,         //        ]
  14.         SNT_MAX = SNT_CHAR32
  15.     } SerializableNodeType;
  16.  
  17. ( . . . )
  18.  
  19.     typedef enum {
  20.         SNST_NONE,
  21.         SNST_POINTER,
  22.         SNST_CHAR_POINTER,
  23.         SNST_CLASS,
  24.         SNST_PRIMITIVE
  25.     } SerializableNodeSuperType;
  26.  
  27. ( . . . )
  28.  
  29.     SerializableNodeSuperType SNSTofNodes[] = {
  30.         SNST_NONE,
  31.         SNST_CHAR_POINTER, SNST_CHAR_POINTER, SNST_POINTER,
  32.         SNST_CLASS, SNST_CLASS,
  33.         SNST_PRIMITIVE, SNST_PRIMITIVE, SNST_PRIMITIVE, SNST_PRIMITIVE,
  34.         SNST_PRIMITIVE, SNST_PRIMITIVE, SNST_PRIMITIVE, SNST_PRIMITIVE,
  35.         SNST_CHAR_POINTER, SNST_CHAR_POINTER,
  36.         SNST_CLASS, SNST_CLASS,
  37.         SNST_PRIMITIVE, SNST_PRIMITIVE
  38.     };
  39.  
  40. //--------------------------- Size of primitive types of serializable data (Buckwheat)
  41.     char SizeofNodes[] = {
  42.         0, 0, 0, 0, 0, 0,
  43.         sizeof( char), sizeof( wchar_t), sizeof( short), sizeof( long),
  44.         sizeof( JACKNPOE_LONGLONG), sizeof( float), sizeof( double), sizeof( long double),
  45.         0, 0, 0, 0,
  46.         sizeof( char16_t), sizeof( char32_t)
  47.     };
  48.  
  49. //--------------------------- Extra size of pointer types of serializable data (Buckwheat)
  50.     char ExtraSizeofPointers[] = {
  51.         0, sizeof( char), sizeof( wchar_t), 0, 0, 0,
  52.         0, 0, 0, 0,
  53.         0, 0, 0, 0,
  54.         sizeof( char16_t), sizeof( char32_t), 0, 0,
  55.         0, 0
  56.     };
  57.  
  58. ( . . . )
  59.  
  60.     bool Serializable::allocPointer( JS_QuantNodesType index) {
  61.         JS_NodeSizeType ExtraBytes; char *ptemp;
  62.         ExtraBytes = ExtraSizeofPointers[ Nodes( index).type];
  63.         Nodes( index).data.Struct = ( void *) std::malloc( Nodes( index).size + ExtraBytes);        // try to alloc
  64.         if( Nodes( index).data.Struct == NULL) {        // failed to alloc
  65.             Nodes( index).type = SNT_NONE;
  66.             Error = ERROR_ALLOC; Errors++; return false;
  67.         }
  68.         ptemp = (char *) Nodes( index).data.Struct;     // ptemp pointer simplify the inclusion of terminate zeros
  69.         for( JS_NodeSizeType i = 0; i < ExtraBytes; i++) ptemp[ Nodes( index).size + i] = 0;    // terminate zeros
  70.         return true;
  71.     }
  72.  
  73.     bool Serializable::fromBinary( void* data, JS_BinarySizeType size) {
  74.         JS_NodeTypeType nodeType;
  75.         SerializableNodeSuperType SNST; void *ptemp;
  76.         JS_BinarySizeType offset = 0;
  77.  
  78.         for( JS_QuantNodesType index = 0; index < Quant; index++) {     // iterate the nodes
  79.             if( offset + sizeof( JS_NodeTypeType) > size ) { Error = ERROR_MEMORY_OVERFLOW; Errors++; return false; }
  80.             memcpy( &nodeType, &((char*)data)[ offset], sizeof( JS_NodeTypeType));      // get the type
  81.             if( nodeType > SNT_MAX) { Error = ERROR_VERSION; Errors++; return false; }
  82.             Nodes( index).type = (SerializableNodeType) nodeType;
  83.             SNST = SNSTofNodes[ nodeType];
  84.             offset += sizeof( JS_NodeTypeType);
  85.  
  86.             if( SNST == SNST_PRIMITIVE or SNST == SNST_NONE) {      // primitive nodes don't need they sizes stored in binary
  87.                 Nodes( index).size = SizeofNodes[ nodeType];        // but in SizeofNodes array
  88.                 if( offset + Nodes( index).size > size ) { Error = ERROR_MEMORY_OVERFLOW; Errors++; return false; }
  89.                 memcpy( &(Nodes( index)).data.Char, &((char*)data)[ offset], Nodes( index).size);
  90.                 offset += Nodes( index).size;
  91.             } else {
  92.                 if( offset + sizeof( JS_NodeSizeType) > size ) { Error = ERROR_MEMORY_OVERFLOW; Errors++; return false; }
  93.                 memcpy( &(Nodes( index)).size, &((char*)data)[ offset], sizeof( JS_NodeSizeType));
  94.                 offset += sizeof( JS_NodeSizeType);
  95.                 if( offset + Nodes( index).size > size ) { Error = ERROR_MEMORY_OVERFLOW; Errors++; return false; }
  96.  
  97.                 if( ( SNST == SNST_CHAR_POINTER ) or ( SNST == SNST_POINTER) )
  98.                 {       // pointer types only need their content copied from binary
  99.                     if( ! allocPointer( index)) return false;   // may add ExtraSize to terminate zeros
  100.                     memcpy( Nodes( index).data.Struct, &((char*)data)[ offset], Nodes( index).size);
  101.                 } else {
  102.                     switch( nodeType) {     // "strange code" to create and assign class values
  103.                         case SNT_STRING:
  104.                             Nodes( index).data.String = new std::string( (long) Nodes( index).size / sizeof( char), ' ' );
  105.                             ptemp = (void *) Nodes( index).data.String->data(); break;
  106.                         case SNT_WSTRING:
  107.                             Nodes( index).data.WString = new std::wstring( (long) Nodes( index).size / sizeof( wchar_t), L' ' );
  108.                             ptemp = (void *) Nodes( index).data.WString->data(); break;
  109. #ifdef JACKNPOE_CPP11
  110.                         case SNT_U16STRING:
  111.                             Nodes( index).data.U16String = new std::u16string( (long) Nodes( index).size / sizeof( char16_t), L' ');
  112.                             ptemp = (void *) Nodes( index).data.U16String->data(); break;
  113.                         case SNT_U32STRING:
  114.                             Nodes( index).data.U32String = new std::u32string( (long) Nodes( index).size / sizeof( char32_t), L' ');
  115.                             ptemp = (void *) Nodes( index).data.U32String->data(); break;
  116. #endif
  117.                         default:
  118.                             Error = ERROR_VERSION; Errors++; return false;
  119.                     }       // switch
  120.                     memcpy( ptemp, &((char*)data)[ offset], Nodes( index).size);
  121.                 }       // non-pointer and non-char-pointer
  122.                 offset += Nodes( index).size;       // generic pointer arithmetic (size is abstract)
  123.             }       // non-primitive
  124.         }       // iterate nodes
  125.         return true;        // return success
  126.     }       // function fromBinary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement