home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-09-25 | 15.2 KB | 754 lines | [TEXT/CWIE] |
- // COPYRIGHT 1994 A.D. Software, All rights reserved
-
- // backend parent layer of OOFILE database
-
- #include "oof2.hpp"
-
- // static variables
- dbRelChain* OOF_mixRelChainEndPoint::sTransitoryRelChainLinks;
- char OOF_String::sEmptyStr = '\0';
-
-
- // -------------------------------------------------------
- // O O F _ t a b l e B a c k e n d
- // -------------------------------------------------------
- OOF_tableBackend::OOF_tableBackend(const OOF_tableBackend& rhs, const OOF_Dictionary& tablesFields) :
- mFields(tablesFields),
- mTableName(rhs.mTableName),
- mDirty(false)
- {
- // mTableName = rhs.mTableName;
- mOIDfield = rhs.mOIDfield; // NOT YET IMPLEMENTED - fix this, it shouldn't be copied!
- }
-
- // PUT BACK INLINE LATER
- void OOF_tableBackend::setOIDfield(const dbField &oidfield)
- {
- mOIDfield = (dbField *) &oidfield; // should use const_cast as not changing
- }
-
-
- // PUT BACK INLINE LATER
- bool OOF_tableBackend::isDirty() const
- {
- return mDirty;
- }
-
-
-
-
-
- // -------------------------------------------------------
- // O O F _ E x p a n d a b l e L o n g A r r a y
- // -------------------------------------------------------
- OOF_ExpandableLongArray::OOF_ExpandableLongArray(unsigned long defaultValue, unsigned long numSlots, unsigned int expandBySlots) :
- mBits(0),
- mDefaultValue(defaultValue),
- mNextFreeEntry(0),
- mNumSlots(0),
- mExpansionChunk(expandBySlots)
- {
- if (mExpansionChunk<1)
- mExpansionChunk = 1;
- if (numSlots)
- {
- mNumSlots = ((numSlots-1) / expandBySlots + 1) * expandBySlots;
- mBits = new unsigned long[mNumSlots];
- }
- CreateArrayRefCount();
- }
-
-
- OOF_ExpandableLongArray::OOF_ExpandableLongArray(const OOF_ExpandableLongArray& rhs)
- {
- mBits = rhs.mBits;
- mDefaultValue = rhs.mDefaultValue;
- mNextFreeEntry = rhs.mNextFreeEntry;
- mNumSlots = rhs.mNumSlots;
- mInternalIter = 0;
- mExpansionChunk = rhs.mExpansionChunk;
- mArrayRefCount = rhs.mArrayRefCount;
- // inc storage refs
- *mArrayRefCount += 1;
- }
-
-
- OOF_ExpandableLongArray& OOF_ExpandableLongArray::operator=(const OOF_ExpandableLongArray& rhs)
- {
- if ((this == &rhs) || (mBits == rhs.mBits))
- return *this;
-
- DeleteBits();
- mBits = rhs.mBits;
- mDefaultValue = rhs.mDefaultValue;
- mNextFreeEntry = rhs.mNextFreeEntry;
- mNumSlots = rhs.mNumSlots;
- mInternalIter = 0;
- mExpansionChunk = rhs.mExpansionChunk;
- mArrayRefCount = rhs.mArrayRefCount;
- // inc storage refs
- *mArrayRefCount += 1;
- return *this;
- }
-
-
- void OOF_ExpandableLongArray::copyContents(const OOF_ExpandableLongArray& rhs)
- {
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- DeleteBits();
- mNumSlots=rhs.mNumSlots;
- mBits = new unsigned long[mNumSlots];
- assert(mBits);
- memcpy(mBits, rhs.mBits, sizeof(mBits));
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- CreateArrayRefCount();
- }
-
-
- OOF_ExpandableLongArray::~OOF_ExpandableLongArray()
- {
- DeleteBits();
- }
-
-
- void OOF_ExpandableLongArray::DeleteBits()
- {
- *mArrayRefCount-=1;
- if (!*mArrayRefCount) {
- delete[] mBits;
- delete mArrayRefCount;
- }
- }
-
-
- void OOF_ExpandableLongArray::CreateArrayRefCount()
- {
- mArrayRefCount = new int;
- assert(mArrayRefCount);
- *mArrayRefCount = 1;
- }
-
-
- void OOF_ExpandableLongArray::append(unsigned long aBit) {
- operator[](mNextFreeEntry) = aBit;
- mNextFreeEntry++;
- }
-
-
- void OOF_ExpandableLongArray::deleteCell(unsigned long index) {
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- for (unsigned long i=index; i<mNextFreeEntry; i++)
- mBits[i] = mBits[i+1];
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- mNextFreeEntry--;
- mBits[mNextFreeEntry] = mDefaultValue;
- }
-
-
- void OOF_ExpandableLongArray::deleteAllCells() {
- mNextFreeEntry=0;
- // NOT YET IMPLEMENTED - delete mBits if past threshold
- }
-
-
- unsigned long& OOF_ExpandableLongArray::operator[](unsigned long index)
- {
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- if (index >= mNumSlots)
- ExpandToInclude(index);
- return mBits[index];
- }
-
-
- unsigned long OOF_ExpandableLongArray::value(unsigned long index) const
- {
- assert (index < mNumSlots);
- return mBits[index];
- }
-
-
- void OOF_ExpandableLongArray::ExpandToInclude(unsigned long indexToCover)
- {
- // expand the storage to include the specified index
- // (ie: expand to the next chunk multiple >= indexToCover
- unsigned int newSize = (indexToCover / mExpansionChunk + 1) * mExpansionChunk;
- unsigned long *oldBits = mBits;
- mBits = new unsigned long[newSize];
- // NOT YET IMPLEMENTED - need error handling here
-
- assert(mBits);
- if (oldBits) {
- for (unsigned long i=0; i<mNextFreeEntry; i++)
- mBits[i] = oldBits[i];
- delete[] oldBits;
- }
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- for (unsigned long i=mNextFreeEntry; i<newSize; i++)
- mBits[i] = mDefaultValue;
- mNumSlots = newSize;
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
-
-
-
- // -------------------------------------------------------
- // O O F _ D i c t r e p
- // -------------------------------------------------------
- void OOF_DictRep::Append(OOF_bitPointer aBit) {
- operator[](mNextFreeEntry) = aBit;
- mNextFreeEntry++;
- }
-
-
- void OOF_DictRep::Reset()
- {
- mNextFreeEntry=0;
- }
-
-
- OOF_DictRep::OOF_DictRep(unsigned int numSlots, unsigned int expandBySlots) :
- mBits(0),
- mExpansionChunk(expandBySlots),
- mNextFreeEntry(0),
- mNumSlots(0)
- {
- if (mExpansionChunk<1)
- mExpansionChunk = 1;
- if (numSlots)
- {
- mNumSlots = ((numSlots-1) / expandBySlots + 1) * expandBySlots;
- mBits = new OOF_bitPointer[mNumSlots];
- assert(mBits);
- }
- }
-
-
- OOF_DictRep::OOF_DictRep(const OOF_DictRep* rhs) :
- mBits(0),
- mExpansionChunk(rhs->mExpansionChunk),
- mNextFreeEntry(rhs->mNextFreeEntry),
- mNumSlots(rhs->mNumSlots)
- {
- if (mNumSlots)
- {
- mBits = new OOF_bitPointer[mNumSlots];
- assert(mBits);
- for (unsigned long i=0; i<mNumSlots; i++)
- mBits[i] = rhs->mBits[i];
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
- }
-
-
- OOF_DictRep::~OOF_DictRep()
- {
- if (mNumSlots)
- delete[] mBits;
- }
-
-
- OOF_bitPointer& OOF_DictRep::operator[](unsigned int index)
- {
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- #ifdef OOF_Debug
- if (index > mNumSlots) {
- cout << "OOF_DictRep::operator[] skipped a cell - suspect index" << endl;
- assert(0);
- }
- #endif
- if (index >= mNumSlots)
- ExpandToInclude(index);
- return mBits[index];
- }
-
-
- OOF_bitPointer& OOF_DictRep::operator[](const char *name)
- {
- return mBits[0]; // NOT YET IMPLEMENTED
- }
-
-
- OOF_bitPointer OOF_DictRep::value(unsigned int index) const
- {
- assert(index < mNumSlots);
- return mBits[index];
- }
-
-
-
-
- void OOF_DictRep::ExpandToInclude(unsigned int indexToCover)
- {
- unsigned int newSize = (indexToCover / mExpansionChunk + 1) * mExpansionChunk;
- OOF_bitPointer *oldBits = mBits;
- mBits = new OOF_bitPointer[newSize];
- // NOT YET IMPLEMENTED - need error handling here
-
- assert(mBits);
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- if (oldBits) {
- for (unsigned long i=0; i<mNextFreeEntry; i++)
- mBits[i] = oldBits[i];
- delete[] oldBits;
- }
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- for (unsigned long i=mNextFreeEntry; i<newSize; i++)
- mBits[i] = 0;
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- mNumSlots = newSize;
- }
-
-
-
- // -------------------------------------------------------
- // O O F _ D i c t i o n a r y
- // -------------------------------------------------------
- OOF_Dictionary::OOF_Dictionary() :
- mOwnsContents(false)
- {
- mRep = new OOF_DictRep(0, kDefExpansionChunk);
- }
-
-
- OOF_Dictionary::OOF_Dictionary(const OOF_DictRep* rep) :
- mOwnsContents(false)
- {
- mRep = new OOF_DictRep(rep);
- }
-
-
- OOF_Dictionary::OOF_Dictionary(unsigned int numSlots) :
- mOwnsContents(false)
- {
- mRep = new OOF_DictRep(numSlots, kDefExpansionChunk);
- }
-
-
- OOF_Dictionary::OOF_Dictionary(unsigned int numSlots, unsigned int expansionChunk) :
- mOwnsContents(false)
- {
- mRep = new OOF_DictRep(numSlots, expansionChunk);
- }
-
-
- OOF_Dictionary::OOF_Dictionary(const OOF_Dictionary& rhs)
- {
- mRep = rhs.mRep;
- mOwnsContents = rhs.mOwnsContents;
- mRep->mReferences++;
- }
-
-
-
- OOF_Dictionary::~OOF_Dictionary()
- {
- if (--mRep->mReferences<=0) {
- if (mOwnsContents)
- deleteAll();
- delete mRep;
- }
- }
-
-
- OOF_Dictionary& OOF_Dictionary::operator=(const OOF_Dictionary& rhs)
- {
- if ((this == &rhs) || (mRep == rhs.mRep))
- return *this;
-
- if (--mRep->mReferences<=0)
- delete mRep;
-
- mRep = rhs.mRep;
- mRep->mReferences++;
- return *this;
- }
-
-
- OOF_Dictionary OOF_Dictionary::clone() const
- {
- OOF_Dictionary ret(mRep);
- return ret;
- }
-
-
- unsigned int OOF_Dictionary::countVisible(bool wantVisible)
- {
- unsigned int ret=0;
- unsigned int theCount = count();
- for (unsigned int i=0; i<theCount; i++) {
- dbClass* entry = mRep->operator[](i);
- assert(entry); // looping known entries!
- if (EntryMatchesVisibility(entry, wantVisible))
- ret++;
- }
- return ret;
- }
-
-
- bool OOF_Dictionary::moreVisible(bool wantVisible)
- {
- if (!more())
- return false;
- dbClass* entry=0;
- while (more()) {
- dbClass* entry = operator()();
- assert(entry); // looping known entries!
- if (EntryMatchesVisibility(entry, wantVisible))
- return true;
- next();
- }
- return (EntryMatchesVisibility(entry, wantVisible));
- }
-
-
- void OOF_Dictionary::deleteAll()
- {
- unsigned int numEntries = count();
- for (unsigned int i=0; i<numEntries; i++) {
- delete &item(i);
- next();
- }
- }
-
-
- void OOF_Dictionary::describe(ostream& os)
- {
- unsigned int numEntries = count();
- for (unsigned int i=0; i<numEntries; i++) {
- (operator[](i))->describe(os);
- next();
- }
- os << endl;
- }
-
-
- void OOF_Dictionary::describeVisible(ostream& os, bool wantVisible)
- {
- startVisible(wantVisible);
- while (moreVisible(wantVisible)) {
- (operator()())->describe(os);
- nextVisible(wantVisible);
- }
- os << endl;
- }
-
-
- bool OOF_Dictionary::allObjectsMatch(OOF_Dictionary& rhs)
- {
- if (mRep==rhs.mRep)
- return true; // trivial case of comparing aliases
-
- if (count() != rhs.count())
- return false;
-
- unsigned int numEntries = count();
- for (unsigned int i=0; i<numEntries; i++)
- if (operator[](i) != rhs.operator[](i))
- return false;
-
- return true;
- }
-
-
- // PUT BACK INLINE LATER
- OOF_bitPointer& OOF_Dictionary::operator[](const char *name)
- {
- return mRep->operator[](name);
- }
-
- // PUT BACK INLINE LATER
- OOF_bitPointer& OOF_Dictionary::operator[](unsigned int index)
- {
- return mRep->operator[](index);
- }
-
-
- // -------------------------------------------------------
- // O O F _ S t r i n g
- // -------------------------------------------------------
- OOF_String::OOF_String(const OOF_String& rhs)
- {
- mLen = rhs.mLen;
- if (mLen) {
- mBody = new char[mLen + 1]; // room for null
- assert(mBody);
- strcpy(mBody, rhs.mBody);
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
- else
- mBody=0;
- }
-
-
- OOF_String::OOF_String(const OOF_String& str1, const char *chars, const OOF_String& str3)
- {
- unsigned int charLen = 0;
- if (chars)
- charLen = strlen(chars);
- mLen = str1.mLen + charLen + str3.mLen;
- if (mLen==0)
- mBody = 0;
- else {
- mBody = new char[mLen + 1]; // room for null
- assert(mBody);
- if (str1.mBody)
- strcpy(mBody, str1.mBody);
- else
- mBody[0]='\0';
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- if (charLen)
- strcat(mBody, chars);
- if (str3.mBody)
- strcat(mBody, str3.mBody);
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
- }
-
-
- OOF_String::OOF_String(const OOF_String& str1, const char *chars)
- {
- unsigned int charLen = 0;
- if (chars)
- charLen = strlen(chars);
- mLen = str1.mLen + charLen;
- if (mLen==0)
- mBody = 0;
- else {
- mBody = new char[mLen + 1]; // room for null
- assert(mBody);
- if (str1.mBody)
- strcpy(mBody, str1.mBody);
- else
- mBody[0]='\0';
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- if (charLen)
- strcat(mBody, chars);
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
- }
-
-
- OOF_String::OOF_String(const OOF_String& str1, const char ch, const char *chars)
- {
- const unsigned int len1 = str1.mLen;
- unsigned int charLen = 0;
- if (chars)
- charLen = strlen(chars);
- mLen = len1 + charLen + 1;
-
- mBody = new char[mLen + 1]; // room for null
- assert(mBody);
- if (str1.mBody)
- strcpy(mBody, str1.mBody);
- else
- mBody[len1]=ch;
- mBody[len1+1]='\0';
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- if (charLen)
- strcat(mBody, chars);
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
-
-
- OOF_String::OOF_String(const char *chars)
- {
- mLen = 0;
- mBody = 0;
- if (chars) {
- mLen = strlen(chars);
- if (mLen) {
- mBody = new char[mLen + 1]; // room for null
- assert(mBody);
- strcpy(mBody, chars);
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
- }
- }
-
-
- OOF_String::~OOF_String()
- {
- delete [] mBody;
- }
-
-
- OOF_String& OOF_String::operator=(const OOF_String& rhs)
- {
- if (*this==rhs)
- return *this;
-
- delete[] mBody;
- mLen = rhs.mLen;
- if (mLen==0)
- mBody = 0;
- else {
- mBody = new char[mLen + 1]; // room for null
- assert(mBody);
- strcpy(mBody, rhs.mBody);
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
- return *this;
- }
-
-
- OOF_String& OOF_String::operator=(const char* chars)
- {
- delete[] mBody;
- if (chars) {
- mLen = strlen(chars);
- mBody = new char[mLen + 1]; // room for null
- assert(mBody);
- strcpy(mBody, chars);
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
- else {
- mLen = 0;
- mBody = 0;
- }
- return *this;
- }
-
-
- OOF_String::operator char *() const
- {
- // WARNING this code exposes the internal container
- // but is safe so long as this is a read-only cast.
- // char *ret;
- if (mBody)
- return mBody;
- else
- return &sEmptyStr;
- }
-
-
- char *OOF_String::adopt()
- {
- char *ret=mBody;
- mBody = 0;
- mLen = 0;
- return ret;
- }
-
-
- ostream& operator<<(ostream& os, const OOF_String& str)
- {
- os << (char*) str;
- return os;
- }
-
-
- #ifdef _Macintosh
- // -------------------------------------------------------
- // O O F _ M a c S t r i n g
- // -------------------------------------------------------
- OOF_MacString::OOF_MacString(const Str63 str)
- {
- mLen = str[0];
- mBody = 0;
- if (mLen) {
- mBody = new char[mLen + 1]; // room for null
- assert(mBody);
- memcpy(mBody, &(str[1]), mLen);
- mBody[mLen] = '\0';
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
- }
- #endif
-
- // -------------------------------------------------------
- // O O F _ I n d e x O p t i o n s
- // -------------------------------------------------------
- ostream& operator<<(ostream& os, OOF_IndexOptions opt)
- {
- switch (opt) {
- case (kNotIndexed) :
- os << "not indexed";
- break;
-
- case (kIndexed) :
- os << "indexed, allowing dups & ignoring case";
- break;
-
- case (kIndexNoDups) :
- os << "indexed, no dups & ignoring case";
- break;
-
- case (kIndexCaseSensitive) :
- os << "indexed, allowing dups but case-sensitive";
- break;
-
- case (kIndexCompressPaddingNoDups) :
- os << "indexed, no dups & ignoring case, compressing padding";
- break;
-
- case (kIndexCompressLeadingNoDups) :
- os << "indexed, no dups & ignoring case, compressing front";
- break;
-
- case (kIndexCompressNoDups) :
- os << "indexed, no dups & ignoring case, compress front & padding";
- break;
-
- case (kIndexCompress) :
- os << "indexed, ignoring case, compress front & padding";
- break;
-
- case (kIndexCompressPadding) :
- os << "indexed, ignoring case, compressing padding";
- break;
-
- case (kIndexCompressLeading) :
- os << "indexed, ignoring case, compressing front";
- break;
-
- }
- return os;
- }
-
-