Accounting Software
Small Business Software Estimating Software
Inventory SoftwareInventory Control SoftwareInventory Tracking SoftwareInventory Management SoftwareConstruction Management SoftwareProject Management SoftwareBusiness Management Software

Data Comparators (Source Code)

Link to: header | other data directory

Copyright Turtle Creek Software 1996-2006. All Rights Reserved.

Comments

DB_Comparator

SUPERCLASS = CTCS_ArrayComparator

a comparator specifically for comparing two database objects.

This class compares data for the Goldenseal job cost accounting software,
project management software and construction project estimating software.

************

CObjectInfoComparator
CDateMoneyIDComparator
CMoneyIDComparator
CMoneyIDValueComparator
CDateIDComparator
CTimeIDComparator
CNumberIDComparator
CNumberInfoComparator

specific comparators for comparing values in various structs. These
are usually used when sorting arrays.

SUPERCLASS = CTCS_ArrayComparator

Constructor

/*********************************************************************************
default constructor
*********************************************************************************/
DB_Comparator::DB_Comparator(SInt32 memberTag, SInt32 memberType)
{
mMemberTag = memberTag;
mMemberType = memberType;
}

Source Code

/*********************************************************************************

DB_Comparator Compare

compare the two passed objects. The return values are either compare_Less,
compare_Equal, or compare_Greater
*********************************************************************************/
SInt32 DB_Comparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// we want to be sure we are being passed pointers!
TCS_ASSERTMsg(inSizeOne == sizeof(DB_PersistentObject *) &&
inSizeTwo == sizeof(DB_PersistentObject *), TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));

// cast the two pointers to persistent objects. Note that
// we do not do a safe_cast here, because we don't want the
// speed hit that would naturally occur when sorting. The caller
// is responsible for ensuring that the comparator is only used
// for arrays of persistent objects
DB_PersistentObject *object1 = *((DB_PersistentObject **)inItemOne),
*object2 = *((DB_PersistentObject **)inItemTwo);

TCS_FailNILMsg(object1, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(object2, TCS_GetErrString(errID_BadComparator));

// case through the various member types and do the comparison
SInt32 result = compare_Equal;
switch (mMemberType)
{
case type_cstring:
case type_cstringcv:
case type_longcstring:
{
CTextString str1, str2; // rev TCS 12/12/01

TCS_ASSERTMsg(object1->GetMemberValue(mMemberTag, type_cstring, &str1),
TCS_GetValueErrString(mMemberTag));
TCS_ASSERTMsg(object2->GetMemberValue(mMemberTag, type_cstring, &str2),
TCS_GetValueErrString(mMemberTag));
if (str1 < str2)
result = compare_Less;
else if (str1 > str2)
result = compare_Greater;
}
break;

case type_date:
{
CDate date1,
date2;

TCS_ASSERTMsg(object1->GetMemberValue(mMemberTag, type_date, &date1),
TCS_GetValueErrString(mMemberTag));
TCS_ASSERTMsg(object2->GetMemberValue(mMemberTag, type_date, &date2),
TCS_GetValueErrString(mMemberTag));
if (date1 < date2)
result = compare_Less;
else if (date1 > date2)
result = compare_Greater;
}
break;

case type_long:
case type_objectid:
case type_objectidx:
{
SInt32 member1,
member2;

TCS_ASSERTMsg(object1->GetMemberValue(mMemberTag, mMemberType, &member1),
TCS_GetValueErrString(mMemberTag));
TCS_ASSERTMsg(object2->GetMemberValue(mMemberTag, mMemberType, &member2),
TCS_GetValueErrString(mMemberTag));
if (member1 < member2)
result = compare_Less;
else if (member1 > member2)
result = compare_Greater;
}
break;

default:
THROW_SIGNAL("Oops, unknown data type in DB_Comparator::Compare()!");
break;
}

return result;
}
/*****************************************************************************
CObjectInfoComparator Compare TCS 12/11/00 rev 7/3/02
Compare two object info structs. We sort first by class, and then by ID
******************************************************************************/
SInt32 CObjectInfoComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// We want to be sure we are being passed SObjectInfo pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SObjectInfo) &&
inSizeTwo == sizeof(SObjectInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));
// Get the structs
SObjectInfo *info1 = ((SObjectInfo *)inItemOne),
*info2 = ((SObjectInfo *)inItemTwo);

TCS_FailNILMsg(info1, TCS_GetErrString(errID_BadComparator)); // TCS 12/2/02
TCS_FailNILMsg(info2, TCS_GetErrString(errID_BadComparator));

if (info1->classID == info2->classID && info1->itemID == info2->itemID)
{
return compare_Equal;
}
else if (info1->classID < info2->classID)
{
return compare_Less;
}
else if (info1->classID > info2->classID)
{
return compare_Greater;
}
else if (info1->itemID < info2->itemID)
{
return compare_Less;
}
else
{
return compare_Greater;
}

}
/*****************************************************************************
Compare TCS 12/11/00
Compare two date money structs. We only compare the ID with this comparator.
Use it for finding objects rather than for sorting by value.
******************************************************************************/
SInt32 CDateMoneyIDComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// We want to be sure we are being passed SObjectInfo pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SDateMoneyInfo) &&
inSizeTwo == sizeof(SDateMoneyInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));
// Get the structs
SDateMoneyInfo *info1 = ((SDateMoneyInfo *)inItemOne),
*info2 = ((SDateMoneyInfo *)inItemTwo);

TCS_FailNILMsg(info1, TCS_GetErrString(errID_BadComparator)); // TCS 12/2/02
TCS_FailNILMsg(info2, TCS_GetErrString(errID_BadComparator));

// compare class ID's
if (info1->classID < info2->classID || info1->itemID < info2->itemID)
{
return compare_Less;
}
else if (info1->classID == info2->classID && info1->itemID == info2->itemID)
{
return compare_Equal;
}
else
{
return compare_Greater;
}
}
/*****************************************************************************
Compare TCS 12/11/00
Compare two money id structs. We only compare the ID with this comparator.
Use it for finding objects rather than for sorting by value.
******************************************************************************/
SInt32 CMoneyIDComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// We want to be sure we are being passed SObjectInfo pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SMoneyIDInfo) &&
inSizeTwo == sizeof(SMoneyIDInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));
// Get the structs
SMoneyIDInfo *structOne = ((SMoneyIDInfo *)inItemOne);
SMoneyIDInfo *structTwo = ((SMoneyIDInfo *)inItemTwo);

TCS_FailNILMsg(structOne, TCS_GetErrString(errID_BadComparator)); // TCS 12/2/02
TCS_FailNILMsg(structTwo, TCS_GetErrString(errID_BadComparator));

if (structOne->id < structTwo->id)
{
return compare_Less;
}
else if (structOne->id == structTwo->id)
{
return compare_Equal;
}
else
{
return compare_Greater;
}
}
/*****************************************************************************
Compare TCS 9/27/01
Compare two money id structs. We compare the money portion
******************************************************************************/
SInt32 CMoneyIDValueComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// We want to be sure we are being passed SObjectInfo pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SMoneyIDInfo) &&
inSizeTwo == sizeof(SMoneyIDInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));
// Get the structs
SMoneyIDInfo *structOne = ((SMoneyIDInfo *)inItemOne);
SMoneyIDInfo *structTwo = ((SMoneyIDInfo *)inItemTwo);

TCS_FailNILMsg(structOne, TCS_GetErrString(errID_BadComparator)); // TCS 12/2/02
TCS_FailNILMsg(structTwo, TCS_GetErrString(errID_BadComparator));

if (structOne->amount < structTwo->amount)
{
return compare_Less;
}
else if (structOne->amount == structTwo->amount)
{
return compare_Equal;
}
else
{
return compare_Greater;
}
}
/*****************************************************************************
Compare TCS 9/20/00
Compare two date id structs-- we compare the date values here.
******************************************************************************/
SInt32 CDateIDValueComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// We want to be sure we are being passed SDateIDInfo pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SDateIDInfo) &&
inSizeTwo == sizeof(SDateIDInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));
// Get the structs
SDateIDInfo *structOne = ((SDateIDInfo *)inItemOne);
SDateIDInfo *structTwo = ((SDateIDInfo *)inItemTwo);

TCS_FailNILMsg(structOne, TCS_GetErrString(errID_BadComparator)); // TCS 12/2/02
TCS_FailNILMsg(structTwo, TCS_GetErrString(errID_BadComparator));

CDate date1 = structOne->date,
date2 = structTwo->date;
// Compare the dates
if (date1 < date2)
{
return compare_Less;
}
else if (date1 == date2)
{
return compare_Equal;
}
else
{
return compare_Greater;
}
}
/*****************************************************************************
CTimeIDValueComparator Compare TCS 9/20/00
Compare two time id structs
******************************************************************************/
SInt32 CTimeIDValueComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// We want to be sure we are being passed SDateIDInfo pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SDateIDInfo) &&
inSizeTwo == sizeof(SDateIDInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));
// Get the structs
SDateIDInfo *structOne = ((SDateIDInfo *)inItemOne);
SDateIDInfo *structTwo = ((SDateIDInfo *)inItemTwo);

TCS_FailNILMsg(structOne, TCS_GetErrString(errID_BadComparator)); // TCS 12/2/02
TCS_FailNILMsg(structTwo, TCS_GetErrString(errID_BadComparator));

CDate date1 = structOne->date,
date2 = structTwo->date;
// Compare the times
return date1.CompareTime(date2); // rev TCS 12/2/02
}
/*****************************************************************************
Compare TCS 9/20/00
Compare two number id structs
******************************************************************************/
SInt32 CLongIDValueComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// We want to be sure we are being passed SNameIDInfo pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SLongIDInfo) &&
inSizeTwo == sizeof(SLongIDInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));
// Get the structs
SLongIDInfo *structOne = ((SLongIDInfo *)inItemOne);
SLongIDInfo *structTwo = ((SLongIDInfo *)inItemTwo);

TCS_FailNILMsg(structOne, TCS_GetErrString(errID_BadComparator)); // TCS 12/2/02
TCS_FailNILMsg(structTwo, TCS_GetErrString(errID_BadComparator));
// Compare the numbers
if (structOne->number < structTwo->number)
{
return compare_Less;
}
else if (structOne->number == structTwo->number)
{
return compare_Equal;
}
else
{
return compare_Greater;
}
}
/*****************************************************************************
CNumberInfoValueComparator Compare TCS 10/11/01
Compare two number object info structs
******************************************************************************/
SInt32 CNumberInfoValueComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// We want to be sure we are being passed SNameIDInfo pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SNumberObjectInfo) &&
inSizeTwo == sizeof(SNumberObjectInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));
// Get the structs
SNumberObjectInfo *structOne = ((SNumberObjectInfo *)inItemOne);
SNumberObjectInfo *structTwo = ((SNumberObjectInfo *)inItemTwo);

TCS_FailNILMsg(structOne, TCS_GetErrString(errID_BadComparator)); // TCS 12/2/02
TCS_FailNILMsg(structTwo, TCS_GetErrString(errID_BadComparator));
// Compare the numbers
if (structOne->number < structTwo->number)
{
return compare_Less;
}
else if (structOne->number == structTwo->number)
{
return compare_Equal;
}
else
{
return compare_Greater;
}
}
/*****************************************************************************
CTwoNumberInfoValueComparator Compare TCS 2/13/02
Compare two two-number object info structs
******************************************************************************/
SInt32 CTwoNumberInfoValueComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne, UInt32 inSizeTwo) const
{
// We want to be sure we are being passed SNameIDInfo pointers
TCS_ASSERTMsg(inSizeOne == sizeof(STwoNumberObjectInfo) &&
inSizeTwo == sizeof(STwoNumberObjectInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));
// Get the structs
STwoNumberObjectInfo *structOne = ((STwoNumberObjectInfo *)inItemOne);
STwoNumberObjectInfo *structTwo = ((STwoNumberObjectInfo *)inItemTwo);

TCS_FailNILMsg(structOne, TCS_GetErrString(errID_BadComparator)); // TCS 12/2/02
TCS_FailNILMsg(structTwo, TCS_GetErrString(errID_BadComparator));
// Compare the numbers
if (structOne->number < structTwo->number)
{
return compare_Less;
}
else if (structOne->number == structTwo->number)
{
return compare_Equal;
}
else
{
return compare_Greater;
}
}
#if CAN_USE_MARK
#pragma mark -
#endif
/*********************************************************************************

CMemberTagComparator Compare TCS 12/17/02

compare the tag of two member info structs.
*********************************************************************************/
SInt32 CMemberTagComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne,
UInt32 inSizeTwo) const
{
// We want to be sure we are being passed the correct pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SMemberInfo) &&
inSizeTwo == sizeof(SMemberInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));

// sanity check
SMemberInfo *firstEntry = (SMemberInfo *)inItemOne;
SMemberInfo *secondEntry = (SMemberInfo *)inItemTwo;

TCS_FailNILMsg(firstEntry, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(secondEntry, TCS_GetErrString(errID_BadComparator));

// cast the given items to entries. This will crash horribly
// if the items aren't entries
SInt32 id1 = firstEntry->tag,
id2 = secondEntry->tag;

// now do the comparison, which is trivial
if (id1 > id2)
return compare_Greater;
else if (id1 < id2)
return compare_Less;
else
return compare_Equal;
}
/*********************************************************************************

CMemberNameComparator Compare TCS 12/17/02

compare the name of two member info structs.

This replaces use of CEntryNameComparator. For some reason the custom member
array items were not acting correctly as CEntry objects. Some subtle setup
thing that's wrong in DB_ClassDescriptor::AddCustomField when it loads in
members from DB_ObjectClassInfo, apparently.
*********************************************************************************/
SInt32 CMemberNameComparator::Compare(const void *inItemOne, const void *inItemTwo,
UInt32 inSizeOne,
UInt32 inSizeTwo) const
{
// We want to be sure we are being passed the correct pointers
TCS_ASSERTMsg(inSizeOne == sizeof(SMemberInfo) &&
inSizeTwo == sizeof(SMemberInfo), TCS_GetErrString(errID_ProblemDataSize));
TCS_FailNILMsg(inItemOne, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(inItemTwo, TCS_GetErrString(errID_BadComparator));

// sanity check
SMemberInfo *firstEntry = (SMemberInfo *)inItemOne;
SMemberInfo *secondEntry = (SMemberInfo *)inItemTwo;

TCS_FailNILMsg(firstEntry, TCS_GetErrString(errID_BadComparator));
TCS_FailNILMsg(secondEntry, TCS_GetErrString(errID_BadComparator));

CTextString str1 = CTextString(firstEntry->memberName),
str2 = CTextString(secondEntry->memberName);

// now do the comparison, which is trivial
if (str1 > str2) // typo fixed 4/29/98 TCS per V
return compare_Greater;
else if (str1 < str2)
return compare_Less;
else
return compare_Equal;
}