home *** CD-ROM | disk | FTP | other *** search
- /*
- File: ValueNS.cpp
-
- Contains: Implementation of ODValueNameSpace class
-
- Owned by: Reggie Adkins
-
- Copyright: © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <4> 6/5/96 TJ 1330687: Now correctly pass buffer ptr by
- value in WriteSizeAndBytes
- <3> 5/24/96 jpa 1330687: Fixed parameter ordering for
- PlatformFile::Write calls.
- <2> 5/23/96 RA 1330687: ValueNS should use
- platformFile->write instead of FSWrite
-
- To Do:
- Get prefs file name from a string resource.
- In Progress:
-
- */
-
- #ifndef _PLFMDEF_
- #include "PlfmDef.h"
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #define ODValueNameSpace_Class_Source
- #include <ValueNS.xih>
-
- #ifndef SOM_ODNameSpace_xh
- #include "NamSpac.xh"
- #endif
-
- #ifndef _STRHSHTB_
- #include "StrHshTb.h"
- #endif
-
- #ifndef _PASCLSTR_
- #include "PasclStr.h"
- #endif
-
- #ifndef __STRING__
- #include <string.h>
- #endif
-
- #ifndef __LIMITS__
- #include <limits.h>
- #endif
-
- #ifndef __LOWMEM__
- #include <LowMem.h>
- #endif
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- #ifndef SOM_ODStorageUnitView_xh
- #include <SUView.xh>
- #endif
-
- #ifndef _ODMEMORY_
- #include "ODMemory.h"
- #endif
-
- #ifndef _ISOSTR_
- #include "ISOStr.h"
- #endif
-
- #ifndef SOM_ODValueIterator_xh
- #include <ValueItr.xh>
- #endif
-
- #ifndef _BNDNSUTL_
- #include "BndNSUtl.h"
- #endif
-
- #ifndef _PLFMFILE_
- #include "PLFMFile.h"
- #endif
-
- #ifndef _STORUTIL_
- #include <StorUtil.h>
- #endif
-
- #ifndef _BARRAY_
- #include <BArray.h>
- #endif
-
- #ifndef _TEMPOBJ_
- #include <TempObj.h>
- #endif
-
- #ifndef _UTILERRS_
- #include <UtilErrs.h>
- #endif
-
- #ifndef SOM_ODNameSpaceManager_xh
- #include "NmSpcMg.xh"
- #endif
-
- #pragma segment ODValueNameSpace
-
- //==============================================================================
- // Implementation notes
- //==============================================================================
-
- /*
- Hash tables used for the implementation are lazily instantiated. They will be
- created at the first request to add an entry.
- */
-
- //==============================================================================
- // Constants
- //==============================================================================
-
- //==============================================================================
- // Function Prototype
- //==============================================================================
-
- //==============================================================================
- // Local Functions
- //==============================================================================
-
- //==============================================================================
- // ODValueNameSpace
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: InitValueNameSpace
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpaceInitValueNameSpace(ODValueNameSpace *somSelf, Environment *ev,
- ODNameSpaceManager* mgr,
- ODNameSpace* parent,
- ODULong numExpectedEntries,
- ODISOStr name)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","InitValueNameSpace");
-
- SOM_TRY
-
- somSelf->SetType( ev, kODNSDataTypeODValue );
- somSelf->InitNameSpace(ev, mgr, parent, numExpectedEntries, name);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: ~ODValueNameSpace
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpacesomUninit(ODValueNameSpace *somSelf)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","somUninit");
-
- Environment* ev = somGetGlobalEnvironment();
-
- SOM_TRY
-
- StringHashTable* hashTable = somSelf->GetHashTable(ev);
- StringHashTableIterator* hashTableItr = new StringHashTableIterator(hashTable);
-
- if (hashTableItr)
- {
- ODULong len;
- ODUByte* string;
- ODPtr buffer = kODNULL;
- ODULong size = 0;
- for(hashTableItr->First(&string, &len ,&buffer, &size);
- hashTableItr->IsNotComplete();
- hashTableItr->Next(&string, &len ,&buffer, &size))
- {
- ODDisposePtr( buffer );
- }
-
- delete ( hashTableItr );
- }
-
- ODValueNameSpace_parent_ODNameSpace_somUninit(somSelf);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: Purge
- //------------------------------------------------------------------------------
-
- SOM_Scope ODSize SOMLINK ODValueNameSpacePurge(ODValueNameSpace *somSelf, Environment *ev,
- ODSize size)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","Purge");
-
- ODUnused(size);
- return 0;
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: Register
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpaceRegister(ODValueNameSpace *somSelf, Environment *ev,
- ODISOStr key, ODByteArray* value)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","Register");
-
- ODPtr buffer = kODNULL;
- ODULong size = 0;
-
- SOM_TRY
-
- ODULong len = ODISOStrLength(key);
- StringHashTable* hashTable = somSelf->GetHashTable(ev);
-
- if (hashTable->Find((ODUByte*)key, len, &buffer, &size))
- {
- // destroy pre-existing old value to prevent a memory leak
- ODDisposePtr( buffer );
- hashTable->Remove((ODUByte*)key, len);
- }
-
- ODULong valueLength = value->_length;
- buffer = ODNewPtrClear(valueLength, kDefaultHeapID);
- ODBlockMove( value->_buffer, buffer, valueLength );
- hashTable->Insert((ODUByte*)key,len, buffer, valueLength);
-
- if (somSelf->GetNameSpaceManager(ev)->
- GetMaxISOStrLength(ev) < valueLength)
- somSelf->GetNameSpaceManager(ev)->
- SetMaxISOStrLength(ev, valueLength);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: Unregister
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpaceUnregister(ODValueNameSpace *somSelf, Environment *ev,
- ODISOStr key)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","ODValueNameSpaceUnregister");
-
- ODPtr buffer = kODNULL;
- ODULong size = 0;
-
- SOM_TRY
-
- size_t len = strlen(key);
- somSelf->GetHashTable(ev)->Find((ODUByte*)key, len, &buffer, &size);
- ODDisposePtr( buffer );
- somSelf->GetHashTable(ev)->Remove((ODUByte*)key, len);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: GetEntry
- //------------------------------------------------------------------------------
-
- SOM_Scope ODBoolean SOMLINK ODValueNameSpaceGetEntry(ODValueNameSpace *somSelf, Environment *ev,
- ODISOStr key, ODByteArray* value)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","GetEntry");
-
- ODBoolean result = kODFalse;
- value->_buffer = kODNULL;
- value->_maximum = value->_length = 0;
-
- SOM_TRY
-
- ODPtr buffer = kODNULL;
- ODULong size = 0;
-
- if (somSelf->GetType(ev) == kODNSDataTypeODValue)
- {
- size_t len = strlen(key);
- result = somSelf->GetHashTable(ev)->Find((ODUByte*)key, len,
- &buffer, &size);
- if ( result == kODFalse )
- {
- ODValueNameSpace *parent =(ODValueNameSpace*)somSelf->GetParent(ev);
- if (parent)
- {
- if (parent->GetType(ev) == kODNSDataTypeODValue)
- result = parent->GetEntry(ev, key, value);
- }
- }
- else
- {
- value->_length = value->_maximum = size;
- if ( size > 0 )
- {
- value->_buffer = (octet*) ODNewPtr(size, kDefaultHeapID);
- ODBlockMove( buffer, value->_buffer, size);
- }
- }
- }
-
- SOM_CATCH_ALL
- SOM_ENDTRY
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: CreateIterator
- //------------------------------------------------------------------------------
-
- SOM_Scope ODValueIterator* SOMLINK ODValueNameSpaceCreateIterator(ODValueNameSpace *somSelf, Environment *ev)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","CreateIterator");
-
- ODValueIterator* theValueIterator = kODNULL;
-
- SOM_TRY
-
- theValueIterator = new ODValueIterator;
- THROW_IF_NULL(theValueIterator); // check all SOM new's for nil
- theValueIterator->InitValueIterator(ev, somSelf);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
-
- return theValueIterator;
- }
-
- //------------------------------------------------------------------------------
- // WriteULong
- //------------------------------------------------------------------------------
-
- static void WriteULong(PlatformFile* file, ODStorageUnitView* view,
- ODULong value)
- {
- ODSLong length ;
- Environment* ev = somGetGlobalEnvironment ();
-
- length = sizeof(value);
- if (view) {
- StorageUnitViewSetValue(view, ev, length, &value);
- }
- else
- {
- ODSLong savedLength = length;
- file->Write(&value,&length);
- if (savedLength != length)
- THROW(kODErrWriteErr);
- }
- }
-
- //------------------------------------------------------------------------------
- // WriteSizeAndBytes
- //------------------------------------------------------------------------------
-
- static void WriteSizeAndBytes(PlatformFile* file, ODStorageUnitView* view,
- ODPtr bytes, ODULong length)
- {
- Environment* ev = somGetGlobalEnvironment ();
-
- // WRITE LENGTH OF STREAM
- WriteULong(file, view, length);
-
- // WRITE STREAM
- if (view)
- StorageUnitViewSetValue(view, ev, length, bytes);
- else
- {
- ODSLong writtenLength = length;
- file->Write(bytes,&writtenLength);
- if (writtenLength != length)
- THROW(kODErrWriteErr);
- }
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: Write
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpaceWrite(ODValueNameSpace *somSelf, Environment *ev,
- PlatformFile* file, ODStorageUnitView* view)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","Write");
-
- SOM_TRY
-
- // WRITE NAME
- { TempODISOStr selfName(somSelf->GetName(ev));
- WriteSizeAndBytes(file, view,
- (ODISOStr) selfName, ODISOStrLength( (ODISOStr) selfName ));
- }
-
- // WRITE NUMENTRIES
- ODULong numEntries = somSelf->GetNumEntries(ev);
- WriteULong(file, view, numEntries);
-
- if (numEntries)
- {
- StringHashTableIterator iter(somSelf->GetHashTable(ev));
- ODUByte* key;
- ODULong keyLength;
- ODPtr value;
- ODULong valueLength;
-
- for (iter.First(&key, &keyLength, &value, &valueLength)
- ;iter.IsNotComplete()
- ;iter.Next(&key, &keyLength, &value, &valueLength))
- {
- WriteSizeAndBytes(file, view, key, keyLength);
- WriteSizeAndBytes(file, view, value, valueLength);
- }
- }
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: WriteToFile
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpaceWriteToFile(ODValueNameSpace *somSelf, Environment *ev,
- ODByteArray* file)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","WriteToFile");
-
- SOM_TRY
-
- PlatformFile* platFile = ((PlatformFile*) file->_buffer);
- somSelf->Write(ev, platFile, (ODStorageUnitView*)kODNULL);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: WriteToStorage
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpaceWriteToStorage(ODValueNameSpace *somSelf, Environment *ev,
- ODStorageUnitView* view)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","WriteToStorage");
-
- SOM_TRY
-
- somSelf->Write(ev,0, view);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: Read
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpaceRead(ODValueNameSpace *somSelf, Environment *ev,
- PlatformFile* file, ODStorageUnitView* view)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","Read");
-
- SOM_TRY
-
- { // READ NAME-VERIFY THAT IT'S THE SAME
- ODULong nameLength = ReadSize(file , view);
- TempODISOStr name = (ODISOStr)ODNewPtr(nameLength + 1, kDefaultHeapID);
-
- ReadBytes(file , view, name, &nameLength);
- ((ODISOStr) name)[nameLength] = 0;
-
- TempODISOStr selfName(somSelf->GetName(ev)); // make sure it's deleted
-
- if ( ! ODISOStrEqual(name, selfName))
- THROW(kODErrInvalidNSName);
- }
-
- // READ NUM ENTRIES
- ODULong numEntries = ReadSize(file , view);
-
- // MORE THAN ONE ENTRY?
- if (numEntries)
- {
- ODISOStr key = kODNULL;
- ODPtr value = kODNULL;
- ODULong keyLength;
- ODULong valueLength;
- ODULong bufferSize = UCHAR_MAX;
- ODPtr buffer = ODNewPtr(bufferSize, kDefaultHeapID); // FOR FASTER KEY READING
- ODBoolean allocatedKey = kODFalse;
- ODVolatile(allocatedKey);
-
- ODNameSpaceManager* tempNSMgr = somSelf->GetNameSpaceManager(ev); //cache reference to speed loop
- StringHashTable* tempHashTable = somSelf->GetHashTable(ev);
-
- TRY
- for (ODULong i = 0; i < numEntries; i++)
- {
- keyLength = ReadSize(file , view);
- if (keyLength > bufferSize)
- {
- key = (ODISOStr)ODNewPtr(keyLength, kDefaultHeapID);
- allocatedKey = kODTrue;
- }
- else
- key = (ODISOStr)buffer;
-
- ReadBytes(file , view, key, &keyLength);
-
- TRY
- valueLength = ReadSize(file , view);
- value = ODNewPtr(valueLength, kDefaultHeapID);
- TRY
- ReadBytes(file , view, value,
- &valueLength);
- tempHashTable->Insert((unsigned char*)key,
- keyLength, value, valueLength);
-
- if (tempNSMgr->GetMaxISOStrLength(ev) < valueLength)
- tempNSMgr->SetMaxISOStrLength(ev, valueLength);
-
- CATCH_ALL
- ODDisposePtr(value);
- RERAISE;
- ENDTRY
- CATCH_ALL
- if (allocatedKey)
- ODDisposePtr(key);
- RERAISE;
- ENDTRY
-
- if (allocatedKey)
- ODDisposePtr(key);
- }
-
- ODDisposePtr(buffer);
- CATCH_ALL
- ODDisposePtr(buffer);
- RERAISE;
- ENDTRY
- }
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: ReadFromFile
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpaceReadFromFile(ODValueNameSpace *somSelf, Environment *ev,
- ODByteArray* file)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","ReadFromFile");
-
- SOM_TRY
-
- PlatformFile* platFile = ((PlatformFile*) file->_buffer);
- somSelf->Read(ev, platFile, (ODStorageUnitView*)kODNULL);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODValueNameSpace: ReadFromStorage
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODValueNameSpaceReadFromStorage(ODValueNameSpace *somSelf, Environment *ev,
- ODStorageUnitView* view)
- {
- /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
- ODValueNameSpaceMethodDebug("ODValueNameSpace","ReadFromStorage");
-
- SOM_TRY
-
- somSelf->Read(ev,0, view);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-