home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Core / ToknTabl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  6.0 KB  |  212 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ToknTabl.cpp
  3.  
  4.     Contains:    Implementation of the TokenTable class
  5.  
  6.     Owned by:    Nick Pilch
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <12>     7/26/95    VL        1270320: Dispose of key and value allocated
  13.                                     by ValueIteratorNext in GetType.
  14.         <11>     5/11/95    DM        #1246524, #1246529: ValueIteratorFirst(),
  15.                                     ValueIteratorNext(),
  16.                                     ValueNameSpaceGetEntry(),
  17.                                     must pass through the value allocated.
  18.         <10>     3/24/95    CG        #1225887: Memory leak in
  19.                                     TokenTable::GetType fixed.
  20.          <9>      2/2/95    CG        #1195019:  ValueNameSpace entries are now
  21.                                     copied in and out instead of pointers being
  22.                                     passed around.
  23.          <8>     1/23/95    CG        #1195019: ValueNameSpace methods now take
  24.                                     ODByteArray parameter instead of ODPtr.
  25.          <7>     8/19/94    NP        1181622: Ownership fix.
  26.          <6>     8/17/94    CG        1181440: No more magic nums, delete
  27.                                     iterators.
  28.          <5>     7/29/94    CG        NameSpace API changes.
  29.          <4>     6/27/94    CG        Removed fEv - not thread safe.
  30.          <3>     6/18/94    MB        Add memory component
  31.          <2>     6/14/94    CG        Include MemUtil.h instead of ODMem.h
  32.          <1>      6/7/94    CG        first checked in
  33.          <9>     1/29/94    NP        Fixed parameters to CreateNameSpace.
  34.          <8>     1/20/94    SS        Fixed ::GetType
  35.          <7>     1/18/94    CG        Changes for ODNameSpace::GetValue();
  36.          <6>     1/14/94    CG        Added includes for NmSpcMgr.h and
  37.                                     NmSpcItr.h
  38.          <5>     12/3/93    TÇ        Stop including ODError.h, it is included
  39.                                     as ErrorDef.h inside Except.h
  40.          <4>    11/15/93    NP        Changed ODSymbols to ODNameSpaceManager.
  41.          <3>     11/2/93    NP        Cast kODNULL to proper type.
  42.          <2>    10/28/93    NP        GetType returns a boolean instead of
  43.                                     throwing an exception.
  44.          <1>    10/25/93    NP        first checked in
  45.          <6>    10/22/93    NP        Adjust to new ODStringHashTable protocol.
  46.          <5>     9/21/93    CG        Updated to work with StrHashTbl changes.
  47.          <4>     8/25/93    NP        Changed Fail to Throw.
  48.          <3>     7/21/93    NP        Replace #ifdefs for volatile with
  49.                                     ODVolatile.
  50.          <2>     6/23/93    NP        Fixed bug that did not return the same
  51.                                     ODTypeToken for the same ODType.
  52.          <1>     6/22/93    NP        first checked in
  53.  
  54.     To Do:
  55. */
  56.  
  57. #ifndef _TOKNTABL_
  58. #include "ToknTabl.h"
  59. #endif
  60.  
  61. #ifndef SOM_ODValueNameSpace_xh
  62. #include "ValueNS.xh"
  63. #endif
  64.  
  65. #ifndef _EXCEPT_
  66. #include "Except.h"
  67. #endif
  68.  
  69. #ifndef _ODMEMORY_
  70. #include "ODMemory.h"
  71. #endif
  72.  
  73. #ifndef SOM_ODNameSpaceManager_xh
  74. #include "NmSpcMg.xh"
  75. #endif
  76.  
  77. #ifndef SOM_ODValueIterator_xh
  78. #include <ValueItr.xh>
  79. #endif
  80.  
  81. #ifndef _ODUTILS_
  82. #include <ODUtils.h>
  83. #endif
  84.  
  85. #ifndef _NMSPCUTL_
  86. #include <NmSpcUtl.h>
  87. #endif
  88.  
  89.  
  90. #pragma segment TokenTable
  91.  
  92. //==============================================================================
  93. // Constants
  94. //==============================================================================
  95.  
  96. const ODULong kNumTableSlots = 50;
  97.  
  98. //==============================================================================
  99. // TokenTable
  100. //==============================================================================
  101.  
  102. //------------------------------------------------------------------------------
  103. // TokenTable::TokenTable
  104. //------------------------------------------------------------------------------
  105.  
  106. TokenTable::TokenTable(ODNameSpaceManager* mgr)
  107. {
  108.     fNameSpaceManager = mgr;
  109.     fNameSpace = kODNULL;
  110.     fNextUniqueID = 1;
  111. }
  112.  
  113. //------------------------------------------------------------------------------
  114. // TokenTable::Initialize
  115. //------------------------------------------------------------------------------
  116.  
  117. void TokenTable::Initialize()
  118. {
  119.     Environment* ev = somGetGlobalEnvironment ();
  120.     fNameSpace = (ODValueNameSpace*)fNameSpaceManager->CreateNameSpace(ev, 
  121.                                                 "TokenTable",
  122.                                                 (ODNameSpace*)kODNULL,
  123.                                                 kNumTableSlots, 
  124.                                                 kODNSDataTypeODValue);
  125. }
  126.  
  127. //------------------------------------------------------------------------------
  128. // TokenTable::~TokenTable
  129. //------------------------------------------------------------------------------
  130.  
  131. TokenTable::~TokenTable()
  132. {
  133.     Environment* ev = somGetGlobalEnvironment ();
  134.     if (fNameSpace && fNameSpaceManager)
  135.         fNameSpaceManager->DeleteNameSpace(ev, fNameSpace);
  136. }
  137.  
  138. //------------------------------------------------------------------------------
  139. // TokenTable::Tokenize
  140. //------------------------------------------------------------------------------
  141.  
  142. ODTypeToken TokenTable::Tokenize(ODType type)
  143. {
  144.     ODTypeToken     token;
  145.     ODTypeToken*    tokenPtr;
  146.     ODULong            valueLen;
  147.     Environment*     ev = somGetGlobalEnvironment ();
  148.  
  149.     if (ValueNameSpaceGetEntry(fNameSpace, ev, type, (ODPtr*) &tokenPtr, &valueLen))
  150.     {
  151.         token = *tokenPtr;
  152.         ODDeleteObject( tokenPtr );
  153.     }
  154.     else
  155.     {
  156.         ValueNameSpaceRegister(fNameSpace, ev, type, (ODPtr) &fNextUniqueID, 
  157.                                                         sizeof(fNextUniqueID));
  158.         token = fNextUniqueID++;
  159.     }
  160.     return token;
  161. }
  162.  
  163. //------------------------------------------------------------------------------
  164. // TokenTable::RemoveEntry
  165. //------------------------------------------------------------------------------
  166.  
  167. void TokenTable::RemoveEntry(ODType type)
  168. {
  169.     Environment* ev = somGetGlobalEnvironment ();
  170.     fNameSpace->Unregister(ev, type);
  171. }
  172.  
  173. //------------------------------------------------------------------------------
  174. // TokenTable::GetType
  175. //
  176. //    I need some functions to isolate ODISOStr creation, deletion and other
  177. //    manipulation.
  178. //------------------------------------------------------------------------------
  179.  
  180. ODBoolean TokenTable::GetType(ODTypeToken token, ODType* type)
  181. {
  182.     Environment* ev = somGetGlobalEnvironment ();
  183.     ODValueIterator* iter = fNameSpace->CreateIterator(ev);
  184.     ODTypeToken*    value = kODNULL;
  185.     ODULong            len;
  186.     ODULong            valueLength;
  187.     ODBoolean        retVal = kODFalse;
  188.     ODType            key = kODNULL;
  189.     
  190.     for (ValueIteratorFirst(iter, ev, &key,(ODPtr*) &value,  &valueLength);
  191.         iter->IsNotComplete(ev) && !retVal;
  192.         ValueIteratorNext(iter, ev, &key, (ODPtr*) &value, &valueLength)
  193.         )
  194.     {
  195.         if (token == *value)
  196.         {
  197.             len = strlen(key);
  198.             *type = (ODType) ODNewPtrClear(len + 1, kDefaultHeapID);
  199.             ODBlockMove(key, *type, len);
  200.             retVal = kODTrue;
  201.         }
  202.         ODDeleteObject( key );
  203.         ODDeleteObject( value );
  204.     }
  205.     ODDeleteObject( key );
  206.     ODDeleteObject( value );
  207.     
  208.     ODDeleteObject( iter);
  209.     return retVal;
  210. }
  211.  
  212.