home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Newton Sample Code 1.2 / Data Storage (Soups) / LostInSpaceCode / DataUtils.f < prev    next >
Encoding:
Text File  |  1994-03-07  |  2.9 KB  |  139 lines  |  [TEXT/R*ch]

  1. /*
  2. ** Copyright: © Michael S. Engber, 1994. All Rights Reserved.
  3. **
  4. ** DataUtils.f
  5. **
  6. ** Utilities used to manipulate Data.f
  7. **
  8. */
  9.  
  10. //
  11. // utils for array of frames rep
  12. //
  13.  
  14. //Frames created by this fn all share a common map.
  15. func MakeDataFrame(s1,s2,s3,s4)
  16.   {slot1: s1, slot2: s2, slot3: s3, slot4: s4};
  17.  
  18. //Clone entire data set to use a common map
  19. func CloneWithCommonMap(data)
  20. begin
  21.   local newData := Clone(data);
  22.   local i,elt;
  23.   foreach i,elt in newData do
  24.     newData[i] := MakeDataFrame(elt.slot1,elt.slot2,elt.slot3,elt.slot4);
  25.   return newData;
  26. end;
  27.  
  28.  
  29.  
  30. //
  31. // utils for array of arrays rep
  32. //
  33.  
  34. func MakeDataArray(s1,s2,s3,s4)
  35.   [s1, s2, s3, s4];
  36.  
  37. func DataToArrays(data)
  38. begin
  39.   local newData := Clone(data);
  40.   local i,elt;
  41.   foreach i,elt in newData do
  42.     newData[i] := MakeDataArray(elt.slot1,elt.slot2,elt.slot3,elt.slot4);
  43.   return newData;
  44. end;
  45.  
  46.  
  47. //
  48. // utils for frame of frames rep
  49. //
  50.  
  51. func MakeSubDataFrame(s1,s2,s3,s4)
  52.   {slot1: s1, slot3: s3, slot4: s4};
  53.  
  54. func DataToFrame(data)
  55. begin
  56.   local newData := {};
  57.   local i,elt;
  58.   foreach i,elt in data do
  59.     newData.(Intern(elt.slot2)) := MakeSubDataFrame(elt.slot1,elt.slot2,elt.slot3,elt.slot4);
  60.   return newData;
  61. end;
  62.  
  63.  
  64. //
  65. // utils for binary object rep
  66. //
  67.  
  68. constant kSlot1Offset := 0;  //a long (4 bytes)
  69. constant kSlot2Offset := 4;  //a cstring of (8 bytes = up to 7 chars + terminating null) 
  70. constant kSlot3Offset := 12; //a word (2 bytes)
  71. constant kSlot4Offset := 14; //a byte
  72. constant kDatumSize   := 15;
  73.  
  74. func PutDatum(binObj,offset,s1,s2,s3,s4)
  75. begin
  76.  StuffLong   (binObj,offset + kSlot1Offset, s1);
  77.  StuffCString(binObj,offset + kSlot2Offset, s2);
  78.  StuffWord   (binObj,offset + kSlot3Offset, s3);
  79.  StuffByte   (binObj,offset + kSlot4Offset, if s4 then 1 else 0);
  80. end;
  81.  
  82. func GetDatum(binObj,offset)
  83. {
  84.  slot1: ExtractLong(binObj,offset + kSlot1Offset),
  85.  slot2: ExtractCString(binObj,offset + kSlot2Offset),
  86.  slot3: ExtractWord(binObj,offset + kSlot3Offset),
  87.  slot4: ExtractByte(binObj,offset + kSlot4Offset) <> 0,
  88. };
  89.  
  90.  
  91. func DataToBinObj(data)
  92. begin
  93.  
  94.   binObj := SetLength("",Length(data)*kDatumSize);
  95.   
  96.   local i,elt;
  97.   foreach i,elt in data do
  98.     PutDatum(binObj,i*kDatumSize,elt.slot1,elt.slot2,elt.slot3,elt.slot4);
  99.  
  100.   return binObj;
  101. end;
  102.  
  103.  
  104.  
  105.  
  106. //
  107. // utils for soups
  108. //
  109.  
  110. DefConst('kMakeSoupFn,func(soupName,arrayOfFrames,indexPath,indexType)
  111.     begin
  112.       local store := GetStores()[0];
  113.  
  114.       if store:HasSoup(soupName) then
  115.             store:GetSoup(soupName):RemoveFromStore();
  116.             
  117.       local initialUsedSize := store:UsedSize();
  118.       
  119.       local soup := store:CreateSoup(soupName,[{structure: 'slot, path: indexPath, type: indexType}]);
  120.       
  121.       local i,elt;
  122.       local str := $/ & Length(arrayOfFrames) & $\n;
  123.       //need to clone - arrayOfFrames is most likely read-only
  124.       foreach i,elt in arrayOfFrames do
  125.           begin
  126.               soup:Add(Clone(elt));
  127.               if i MOD 10 = 0 then
  128.                   begin
  129.                       Write(i);
  130.                       Write(str);
  131.                   end;
  132.           end;
  133.       
  134.       Print("soup \"" & soupName & "\" created (" &  store:UsedSize() - initialUsedSize && "bytes)");
  135.       
  136.       soup;
  137.     end
  138. );
  139.