home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Applications / Lookup 1.0d2 / Sources / CEntry.cp < prev    next >
Encoding:
Text File  |  1995-10-08  |  4.3 KB  |  223 lines  |  [TEXT/CWIE]

  1. /*
  2.     CEntry.cp
  3.     
  4.     Copyright © 1995 Alastair Rankine.
  5.     All Rights Reserved.
  6. */
  7.  
  8. #include "CEntry.h"
  9. #include "CLookupDocument.h"
  10. #include <UAEGizmos.h>
  11. #include <ostream>
  12.  
  13. const CEntry sEmptyEntry;
  14. const char CEntry::kFieldSeparator = '\t';
  15. const char CEntry::kEntrySeparator = '\n';
  16. const DescType CEntry::kFieldProperties[] = {
  17.     pName,
  18.     'PHN1',
  19.     'PHN2',
  20.     'EMAI'
  21. };
  22.  
  23. CEntry::CEntry(LModelObject * inSuperModel)
  24.     : LModelObject(inSuperModel, kModelID)
  25. {
  26. }
  27.  
  28. const CString & CEntry::GetField(UInt16 inFieldNumber) const
  29. {
  30.     Assert_(inFieldNumber < kFieldCount);
  31.     
  32.     return mField[inFieldNumber];
  33. }
  34.  
  35. CEntry::CEntry(const CEntry & another)
  36.     : LModelObject(another.mSuperModel, kModelID)
  37. {
  38.     for(UInt16 i = 0; i < kFieldCount; i++)
  39.         mField[i] = another.mField[i];
  40. }
  41.  
  42. CEntry & CEntry::operator = (const CEntry & another)
  43. {
  44.     if(&another != this)
  45.         for(UInt16 i = 0; i < kFieldCount; i++)
  46.             mField[i] = another.mField[i];
  47.  
  48.     return *this;
  49. }
  50.  
  51. CEntry::~CEntry()
  52. {
  53. }
  54.  
  55. void CEntry::SetField(UInt16 inField, const CString & inValue)
  56. {
  57.     Assert_(inField < kFieldCount);
  58.     
  59.     mField[inField] = inValue;
  60. }
  61.  
  62. void CEntry::GetAEProperty(DescType inProperty, const AEDesc & inRequestedType,
  63.     AEDesc & outPropertyDesc) const
  64. {
  65.     UInt16 i = 0;
  66.     
  67.     for(; i < kFieldCount; i++)
  68.     {
  69.         if(kFieldProperties[i] == inProperty)
  70.         {
  71.             CString & field = (CString &)mField[i];
  72.  
  73.             LAEStream out;
  74.             out.WriteDesc(typeChar, field.c_str(), field.length());
  75.             out.Close(&outPropertyDesc);
  76.  
  77.             return;
  78.         }
  79.     }
  80.  
  81.     if(i >= kFieldCount)
  82.         LModelObject::GetAEProperty(inProperty, inRequestedType, outPropertyDesc);
  83. }
  84.  
  85. void CEntry::SetAEProperty(DescType inProperty, const AEDesc & inValue, AEDesc & outAEReply)
  86. {
  87.     if(inProperty == pContents)
  88.     {
  89.         // inValue is a record of properties... extract and set them by recursively
  90.         // calling this procedure.
  91.         LAESubDesc properties(inValue, typeAERecord);
  92.  
  93.         unsigned long propCount = properties.CountItems();
  94.         for(unsigned long i = 1; i <= propCount; i++)
  95.         {
  96.             AEKeyword keyword;
  97.             LAESubDesc theProperty = properties.NthItem(i, &keyword);
  98.  
  99.             StAEDescriptor propDesc;
  100.             theProperty.ToDesc(propDesc);
  101.             
  102.             StAEDescriptor bogusReply;
  103.             SetAEProperty(keyword, propDesc, bogusReply.mDesc);
  104.         }
  105.     }
  106.     else
  107.     {
  108.         // Search through the properties array looking for a match:
  109.         UInt16 i = 0;
  110.         for(; i < kFieldCount; i++)
  111.         {
  112.             if(kFieldProperties[i] == inProperty)
  113.             {
  114.                 mField[i].ExtractFromAEDesc(inValue);
  115.     
  116.                 // This is a little ugly.
  117.                 if(mSuperModel)
  118.                 {
  119.                     CLookupDocument * doc = (CLookupDocument *)mSuperModel;
  120.                     doc->NotifyEntryChanged(this);
  121.                 }
  122.     
  123.                 break;
  124.             }
  125.         }
  126.         
  127.         // Not a known property, pass it up the chain!
  128.         if(i >= kFieldCount)
  129.             LModelObject::SetAEProperty(inProperty, inValue, outAEReply);
  130.     }
  131. }
  132.  
  133. void CEntry::GetImportantAEProperties(AERecord & outRecord)
  134. {
  135.     LAEStream    stream;
  136.     
  137.     stream.OpenRecord();
  138.     for (UInt16 i = 0; i < kFieldCount; i++) {
  139.         StAEDescriptor prop(mField[i].ToPString());
  140.         
  141.         stream.WriteKeyDesc(kFieldProperties[i], prop);
  142.     }
  143.     stream.CloseRecord();
  144.     stream.Close(&outRecord);
  145. }
  146.  
  147. void CEntry::HandleDelete(AppleEvent &, AEDesc &)
  148. {
  149.     // This is a little ugly.
  150.     if(mSuperModel)
  151.     {
  152.         CLookupDocument * doc = (CLookupDocument *)mSuperModel;
  153.         doc->DeleteEntry(this);
  154.     }
  155. }
  156.  
  157. void CEntry::GetDifferentAEProperties(const CEntry & inCompare, AERecord & outRecord) const
  158. {
  159.     LAEStream    stream;
  160.     
  161.     stream.OpenRecord();
  162.     for (UInt16 i = 0; i < kFieldCount; i++)
  163.         if(mField[i] != inCompare.mField[i])
  164.         {
  165.             StAEDescriptor prop(mField[i].ToPString());
  166.             
  167.             stream.WriteKeyDesc(kFieldProperties[i], prop);
  168.         }
  169.     stream.CloseRecord();
  170.     stream.Close(&outRecord);
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177. const StringPtr CEntry::GetModelNamePtr() const
  178. {
  179.     Assert_(kFieldProperties[0] == pName);
  180.     
  181.     return mField[0].ToPString();
  182. }
  183.  
  184. ostream & operator << (ostream & stream, CEntry const & rec)
  185. {
  186.     for(UInt16 i = 0; i < CEntry::kFieldCount; i++)
  187.     {
  188.         if(i)
  189.             stream << CEntry::kFieldSeparator;
  190.         
  191.         stream << rec.mField[i];
  192.     }
  193.     stream << CEntry::kEntrySeparator;
  194.     
  195.     return stream;
  196. }
  197.  
  198. istream & operator >> (istream & stream, CEntry & rec)
  199. {
  200.     UInt16 currField = 0;
  201.     char ch = 0;
  202.     
  203.     while(stream.good() && (ch != CEntry::kEntrySeparator))
  204.     {
  205.         stream.get(ch);
  206.         
  207.         switch(ch)
  208.         {
  209.             case CEntry::kFieldSeparator:
  210.                 if(++currField >= CEntry::kFieldCount)
  211.                     ch = CEntry::kEntrySeparator;
  212.                 break;
  213.             case CEntry::kEntrySeparator:
  214.                 break;
  215.             default:
  216.                 rec.mField[currField].append(ch);
  217.                 break;
  218.         }
  219.     }
  220.     
  221.     return stream;
  222. }
  223.