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 / Utilities / NmSpcUtl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  4.2 KB  |  209 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        NmSpcUtl.cpp
  3.  
  4.     Contains:    Utilities for working with NameSpaces.
  5.  
  6.     Owned by:    David McCusker
  7.  
  8.     Copyright:    © 1993-1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     5/24/96    jpa        Fixed header comment.
  13.          <2>     5/24/96    jpa        1339104: Optimized ValueNameSpaceGetODName
  14.                                     [for EditorSetup]
  15.  
  16.     To Do:
  17. */
  18.  
  19. #ifndef _NMSPCUTL_
  20. #include <NmSpcUtl.h>
  21. #endif
  22.  
  23. #ifndef SOM_ODNameSpaceManager_xh
  24. #include <NmSpcMg.xh>
  25. #endif
  26.  
  27. #ifndef SOM_ODValueNameSpace_xh
  28. #include <ValueNS.xh>
  29. #endif
  30.  
  31. #ifndef SOM_ODValueIterator_xh
  32. #include <ValueItr.xh>
  33. #endif
  34.  
  35. #ifndef SOM_ODSession_xh
  36. #include <ODSessn.xh>
  37. #endif
  38.  
  39. #ifndef _BARRAY_
  40. #include <BArray.h>
  41. #endif
  42.  
  43. #ifndef _MEMMGR_
  44. #include <MemMgr.h>
  45. #endif
  46.  
  47. #ifndef _ODMEMORY_
  48. #include "ODMemory.h"
  49. #endif
  50.  
  51. #ifndef _ITEXT_
  52. #include "IText.h"
  53. #endif
  54.  
  55.  
  56. void ValueNameSpaceRegister(ODValueNameSpace* ns, 
  57.                             Environment* ev,
  58.                             ODISOStr key,
  59.                             ODPtr buffer, 
  60.                             ODULong size)
  61. {
  62.     ODByteArray    ba;
  63.     ba._buffer = (octet*) buffer;
  64.     ba._length = size;
  65.     ba._maximum = size;
  66.     
  67.     ns->Register(ev, key, &ba);
  68. }
  69.  
  70. ODBoolean ValueNameSpaceGetEntry(ODValueNameSpace* ns, 
  71.                                  Environment* ev,
  72.                                    ODISOStr key,
  73.                                    ODPtr* value, 
  74.                                    ODULong* valueLength)
  75. {
  76.     ODByteArray ba;
  77.  
  78.     if (ns->GetEntry(ev, key, &ba))
  79.     {
  80.         *value = ba._buffer;
  81.         *valueLength = ba._length;
  82.         return kODTrue;
  83.     }
  84.     else
  85.     {
  86.         *value = kODNULL;
  87.         return kODFalse;
  88.     }
  89. }
  90.  
  91. ODBoolean ValueNameSpaceGetODName(ODValueNameSpace* ns, 
  92.                                  Environment* ev,
  93.                                    ODISOStr key,
  94.                                    ODName** value)
  95. {
  96.     ODByteArray ba;
  97.     
  98.     if (ns->GetEntry(ev, key, &ba))
  99.     {
  100.         // Better way:
  101.         (*value) = new ODIText;
  102.         (*value)->format = kODTraditionalMacText;
  103.         (*value)->text = ba;
  104.         return kODTrue;
  105.     }
  106.     else
  107.     {
  108.         *value = kODNULL;
  109.         return kODFalse;
  110.     }
  111. }
  112.  
  113. void ValueIteratorFirst(ODValueIterator* iter, 
  114.                         Environment* ev,
  115.                         ODISOStr* key,
  116.                         ODPtr* value, 
  117.                         ODULong* valueLength)
  118. {
  119.     ODByteArray ba;
  120.     ba._length = 0;
  121.     
  122.     iter->First(ev, key, &ba);
  123.     
  124.     if (ba._length > 0)
  125.     {
  126.         *value = ba._buffer;
  127.         *valueLength = ba._length;
  128.     }
  129.     else
  130.         *value = kODNULL;
  131. }
  132.  
  133. void ValueIteratorNext(ODValueIterator* iter, 
  134.                         Environment* ev,
  135.                         ODISOStr* key,
  136.                         ODPtr* value, 
  137.                         ODULong* valueLength)
  138. {
  139.     ODByteArray ba;
  140.     ba._length = 0;
  141.     
  142.     iter->Next(ev, key, &ba);
  143.     
  144.     if (ba._length > 0)
  145.     {
  146.         *value = ba._buffer;
  147.         *valueLength = ba._length;
  148.     }
  149.     else
  150.         *value = kODNULL;
  151. }
  152.  
  153.  
  154. ODISOStr ValueNameSpaceGetString( ODSession* sessn, ODISOStr nameSpaceName, ODISOStr key )
  155. {
  156.     ODByteArray ba;
  157.     ODISOStr result = kODNULL;
  158.     Environment* ev = somGetGlobalEnvironment();
  159.     ODNameSpaceManager* mgr = sessn->GetNameSpaceManager(ev);
  160.     ODValueNameSpace* ns = (ODValueNameSpace*) mgr->HasNameSpace(ev, nameSpaceName);
  161.     if ( ns && ns->GetEntry(ev, key, &ba) && ba._length > 0 )
  162.         result = (ODISOStr) ba._buffer;
  163.  
  164.     // The old version of this function used to allocate space for a terminating
  165.     // null byte; but as near as I can tell, the only ways that strings become
  166.     // values also allocate the null byte space (as they should) when the entry
  167.     // is orginally created.
  168.     
  169.     return result;
  170. }
  171.  
  172. void ValueNameSpaceWriteToFile( ODValueNameSpace* ns, PlatformFile* file )
  173. {
  174.     Environment* ev = somGetGlobalEnvironment();
  175.     ODByteArray ba; 
  176.     ba._buffer = (octet*) file; 
  177.     ba._length = sizeof(PlatformFile); 
  178.     ns->WriteToFile( ev, &ba );
  179. }
  180.  
  181. void ValueNameSpaceReadFromFile( ODValueNameSpace* ns, PlatformFile* file )
  182. {
  183.     Environment* ev = somGetGlobalEnvironment();
  184.     ODByteArray ba; 
  185.     ba._buffer = (octet*) file; 
  186.     ba._length = sizeof(PlatformFile); 
  187.     ns->ReadFromFile( ev, &ba );
  188. }
  189.  
  190.  
  191. //------------------------------------------------------------------------------
  192. // CreateFlatIText
  193. //------------------------------------------------------------------------------
  194.  
  195. ODTradITextData* CreateFlatIText(ODScriptCode theScriptCode, ODLangCode theLangCode, 
  196.                                                 char* theText, ODUShort textLen)
  197. {
  198.     ODTradITextData* result = (ODTradITextData*)
  199.              ODNewPtrClear(textLen+sizeof(ODTradITextDataHeader), kDefaultHeapID);
  200.     result->theScriptCode = theScriptCode;
  201.     result->theLangCode = theLangCode;
  202.     
  203.     ODBlockMove((ODPtr)theText, (ODPtr)result->theText, textLen);
  204.     
  205.     return result;
  206. }
  207.  
  208.  
  209.