home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / SERIALZE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.4 KB  |  145 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.6  $
  6. //
  7. // Implementation of classes TSerializer and TSerializeReceiver
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_SERIALZE_H)
  11. # include <owl/serialze.h>
  12. #endif
  13. #if !defined(OWL_WINDOWEV_H)
  14. # include <owl/windowev.h>
  15. #endif
  16. #include <stdio.h>
  17.  
  18. OWL_DIAGINFO;
  19.  
  20. //
  21. // Breaks down the data into blocks and sends each block to the window via
  22. // SendMessage.
  23. // wParam of the SendMessage is of type TBlock which signifies what
  24. // lParam contains.
  25. //
  26. TSerializer::TSerializer(HWND hwndTarget, uint32 length, void far* data)
  27. {
  28.   if (!::IsWindow(hwndTarget) || length == 0)
  29.     return;
  30.   uint msg = ::RegisterWindowMessage(SerializerMessage);
  31.  
  32.   ::SendMessage(hwndTarget, msg, Begin, length);
  33.  
  34.   // Send blocks 4 at a time
  35.   //
  36.   uint32* dataBlocks = (uint32*)data;
  37.   while (length > 3) {
  38.     ::SendMessage(hwndTarget, msg, Data4, *dataBlocks);
  39.     dataBlocks += 1;
  40.     length -= 4;
  41.   }
  42.  
  43.   // Block was an even multiple of 4
  44.   //
  45.   if (length == 0) {
  46.     ::SendMessage(hwndTarget, msg, End, 0);
  47.     return;
  48.   }
  49.  
  50.   // Length must be either 1, 2, or 3
  51.   //
  52.   uchar* dataBytes = (uchar*)dataBlocks;
  53.   uint32 finalBlock = 0;
  54.  
  55.   if (length == 3)
  56.     finalBlock += 0x100L * dataBytes[2];
  57.  
  58.   if (length >= 2)
  59.     finalBlock += 0x10000L * dataBytes[1];
  60.  
  61.   finalBlock += 0x1000000L * dataBytes[0];
  62.  
  63.   ::SendMessage(hwndTarget, msg, int(length), finalBlock);
  64.   ::SendMessage(hwndTarget, msg, End, 0);
  65. }
  66.  
  67. //----------------------------------------------------------------------------
  68.  
  69.  
  70. DEFINE_RESPONSE_TABLE1(TSerializeReceiver, TEventHandler)
  71.   EV_REGISTERED(SerializerMessage, BlockReceived),
  72. END_RESPONSE_TABLE;
  73.  
  74. //
  75. // Constructor
  76. //
  77. TSerializeReceiver::TSerializeReceiver()
  78. :
  79.   TEventHandler(), Length(0), Data(0), CurPtr(0)
  80. {
  81. }
  82.  
  83. //
  84. // Automatically put the data blocks back together.
  85. //
  86. int32
  87. TSerializeReceiver::BlockReceived(TParam1 param1, TParam2 param2)
  88. {
  89.   switch (param1) {
  90.     case TSerializer::Begin: {
  91.       Length = param2;
  92.       Data = new HUGE char[Length];
  93.       CurPtr = Data;
  94.       return 0;
  95.     }
  96.  
  97.     case TSerializer::End: {
  98.       DataReceived(Length, Data);
  99.       delete[] Data;
  100.       Data = 0;
  101.       return 0;
  102.     }
  103.  
  104.     case TSerializer::Data4: {
  105.       uint32* ptr = (uint32*)CurPtr;
  106.       *ptr = param2;
  107.       CurPtr += 4;
  108.       return 0;
  109.     }
  110.  
  111.     case TSerializer::Data1:
  112.     case TSerializer::Data2:
  113.     case TSerializer::Data3:
  114.       // Fall through
  115.       //
  116.       break;
  117.  
  118.     default: // ignored, unknown block type
  119.       return 0;
  120.   }
  121.  
  122.   // Unpack the last remaining bytes
  123.   //
  124.   *CurPtr++ = (char)HiUint8(HiUint16(param2));
  125.  
  126.   if (param1 >= TSerializer::Data2)
  127.     *CurPtr++ = (char)LoUint8(HiUint16(param2));
  128.  
  129.   if (param1 == TSerializer::Data3)
  130.     *CurPtr++ = (char)HiUint8(LoUint16(param2));
  131.  
  132.   return 0;
  133. }
  134.  
  135. //
  136. // This virtual function will be called whenever the data has been
  137. // reconstructed.
  138. // Derived classes should override this function to copy the data because it
  139. // will be deleted when this function returns.
  140. //
  141. void
  142. TSerializeReceiver::DataReceived(uint32 /*length*/, void far* /*data*/)
  143. {
  144. }
  145.