home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / StreamTools.cp < prev    next >
Encoding:
Text File  |  1994-02-20  |  4.3 KB  |  161 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // StreamTools.cp
  3.  
  4. #include "StreamTools.h"
  5. #include <ErrorGlobals.h>
  6.  
  7. #pragma segment MyTools
  8.  
  9. //----------------------------------------------------------------
  10. #if qDebug
  11. void MyStreamCheckVersion(long theVersion, long minVersion, long maxVersion, const char *debugName)
  12. #else
  13. void MyStreamCheckVersion(long theVersion, long minVersion, long maxVersion, const char *)
  14. #endif
  15. {
  16.     if (theVersion < minVersion || theVersion > maxVersion)
  17.     {
  18. #if qDebug
  19.         fprintf(stderr, "**** Incompatible %s disk version = %ld, min = %ld, cur = %ld. Aborts read of file.\n", debugName, theVersion, minVersion, maxVersion);
  20. #endif
  21.         if (theVersion < minVersion)
  22.             FailOSErr(errTooOldFileFormat);
  23.         else
  24.             FailOSErr(errTooNewFileFormat);
  25.     }
  26. #if qDebug
  27.     if (theVersion < maxVersion)
  28.         fprintf(stderr, "Reads old version %s: version = %ld, min = %ld, cur = %ld.\n", debugName, theVersion, minVersion, maxVersion);
  29. #endif
  30. }
  31.  
  32. #if qDebug
  33. Boolean MyCheckVersion(long theVersion, long minVersion, long maxVersion, const char *debugName)
  34. #else
  35. Boolean MyCheckVersion(long theVersion, long minVersion, long maxVersion, const char *)
  36. #endif
  37. {
  38.     if (theVersion < minVersion)
  39.     {
  40. #if qDebug
  41.         fprintf(stderr, "**** Incompatible %s disk version = %ld, min = %ld, cur = %ld. Doesn't read file - but no abort.\n", debugName, theVersion, minVersion, maxVersion);
  42. #endif
  43.         return false;
  44.     }
  45.     if (theVersion > maxVersion)
  46.     {
  47. #if qDebug
  48.         fprintf(stderr, "**** Incompatible %s disk version = %ld, min = %ld, cur = %ld. Aborts read of file.\n", debugName, theVersion, minVersion, maxVersion);
  49. #endif
  50.         FailOSErr(errTooNewFileFormat);
  51.     }
  52. #if qDebug
  53.     if (theVersion < maxVersion)
  54.         fprintf(stderr, "Reads old version %s: version = %ld, min = %ld, cur = %ld.\n", debugName, theVersion, minVersion, maxVersion);
  55. #endif
  56. }
  57.  
  58. //----------------------------------------------------------------
  59. const long kCurrentHandleVersion = 1;
  60. const long kMinHandleVersion = 1;
  61.  
  62. void MyStreamReadHandle(TStream *aStream, Handle h)
  63. {
  64.     long version = aStream->ReadLong();
  65.     MyStreamCheckVersion(version, kMinHandleVersion, kCurrentHandleVersion, "Handle");
  66.     long handleSize = aStream->ReadLong();
  67.     SetPermHandleSize(h, handleSize);
  68.     if (handleSize)
  69.     {
  70.         HLock(h);
  71.         aStream->ReadBytes(*h, handleSize);
  72.         HUnlock(h);
  73.         if (handleSize & 3) 
  74.         {
  75.             long pad = 0;
  76.             aStream->ReadBytes(&pad, 4 - (handleSize & 3));
  77.         }
  78.     }
  79. }
  80.  
  81. void MyStreamWriteHandle(TStream *aStream, Handle h)
  82. {
  83.     aStream->WriteLong(kCurrentHandleVersion);
  84.     long handleSize = GetHandleSize(h);
  85.     aStream->WriteLong(handleSize);
  86.     if (handleSize)
  87.     {
  88.         HLock(h);
  89.         aStream->WriteBytes(*h, handleSize);
  90.         HUnlock(h);
  91.         if (handleSize & 3) 
  92.         {
  93.             long pad = 0;
  94.             aStream->WriteBytes(&pad, 4 - (handleSize & 3));
  95.         }
  96.     }
  97. }
  98.  
  99. long MyStreamSizeOfHandle(Handle h)
  100. {
  101.     return sizeof(long) + sizeof(long) + GetHandleSize(h) + 3 & ~3;
  102. }
  103.  
  104. void DoIronAgeFormatReadHandle(TStream *aStream, Handle h)
  105. {
  106.     long handleSize = aStream->ReadLong();
  107.     SetPermHandleSize(h, handleSize);
  108.     if (handleSize)
  109.     {
  110.         HLock(h);
  111.         aStream->ReadBytes(*h, handleSize);
  112.         HUnlock(h);
  113.         if (handleSize & 3) 
  114.         {
  115.             long pad = 0;
  116.             aStream->ReadBytes(&pad, 4 - (handleSize & 3));
  117.         }
  118.     }
  119. }
  120.  
  121.  
  122. //----------------------------------------------------------------
  123. const long kCurrentArrayVersion = 1;
  124. const long kMinArrayVersion = 1;
  125.  
  126. void ReadDynamicArray(TStream *aStream, TDynamicArray *array)
  127. {
  128.     long version = aStream->ReadLong();
  129.     MyStreamCheckVersion(version, kMinArrayVersion, kCurrentArrayVersion, "TDynamicArray");
  130.     long size = aStream->ReadLong();
  131.     array->SetArraySize(size);
  132.     array->fSize = size;
  133.     if (size)
  134.     {
  135.         Boolean prevLock = array->Lock(true);
  136.         aStream->ReadBytes(array->ComputeAddress(1), array->fSize * array->fElementSize);
  137.         array->Lock(prevLock);
  138.     }
  139. }
  140.  
  141. void WriteDynamicArray(TStream *aStream, TDynamicArray *array)
  142. {
  143.     aStream->WriteLong(kCurrentArrayVersion);
  144.     aStream->WriteLong(array->fSize);
  145.     if (array->fSize) 
  146.     {
  147.         Boolean prevLock = array->Lock(true);
  148.         aStream->WriteBytes(array->ComputeAddress(1), array->fSize * array->fElementSize);
  149.         array->Lock(prevLock);
  150.     }
  151. }
  152.  
  153. long MyStreamSizeOfDynamicArray(TDynamicArray *array)
  154. {
  155.     return 
  156.         sizeof(long) + // version number
  157.         sizeof(long) + // no elements
  158.         array->fSize * array->fElementSize; // actual data
  159. }
  160. //----------------------------------------------------------------
  161.