Back to index

lightning-sunbird  0.9+nobinonly
Classes | Public Member Functions | Private Member Functions | Private Attributes
txResultStringComparator Class Reference

#include <txXPathResultComparator.h>

Inheritance diagram for txResultStringComparator:
Inheritance graph
[legend]
Collaboration diagram for txResultStringComparator:
Collaboration graph
[legend]

List of all members.

Classes

class  StringValue

Public Member Functions

 txResultStringComparator (MBool aAscending, MBool aUpperFirst, const nsAFlatString &aLanguage)
virtual ~txResultStringComparator ()
int compareValues (TxObject *aVal1, TxObject *aVal2)
TxObjectcreateSortableValue (txAExprResult *aExprRes)

Private Member Functions

nsresult init (const nsAFlatString &aLanguage)
nsresult createRawSortKey (const PRInt32 aStrength, const nsString &aString, PRUint8 **aKey, PRUint32 *aLength)

Private Attributes

nsCOMPtr< nsICollationmCollation
int mSorting

Detailed Description

Definition at line 77 of file txXPathResultComparator.h.


Constructor & Destructor Documentation

txResultStringComparator::txResultStringComparator ( MBool  aAscending,
MBool  aUpperFirst,
const nsAFlatString aLanguage 
)

Definition at line 60 of file txXPathResultComparator.cpp.

{
    mSorting = 0;
    if (aAscending)
        mSorting |= kAscending;
    if (aUpperFirst)
        mSorting |= kUpperFirst;
#ifndef TX_EXE
    nsresult rv = init(aLanguage);
    if (NS_FAILED(rv))
        NS_ERROR("Failed to initialize txResultStringComparator");
#endif
}

Here is the call graph for this function:

Definition at line 76 of file txXPathResultComparator.cpp.

{
}

Member Function Documentation

int txResultStringComparator::compareValues ( TxObject aVal1,
TxObject aVal2 
) [virtual]

Implements txXPathResultComparator.

Definition at line 149 of file txXPathResultComparator.cpp.

{
    StringValue* strval1 = (StringValue*)aVal1;
    StringValue* strval2 = (StringValue*)aVal2;
#ifdef TX_EXE
    PRUint32 len1 = strval1->mStr.Length();
    PRUint32 len2 = strval2->mStr.Length();
    PRUint32 minLength = (len1 < len2) ? len1 : len2;

    PRUint32 c = 0;
    while (c < minLength) {
        PRUnichar ch1 = strval1->mStr.CharAt(c);
        PRUnichar ch2 = strval2->mStr.CharAt(c);
        if (ch1 < ch2)
            return ((mSorting & kAscending) ? 1 : -1) * -1;
        if (ch2 < ch1)
            return ((mSorting & kAscending) ? 1 : -1) * 1;
        c++;
    }

    if (len1 == len2)
        return 0;

    return ((mSorting & kAscending) ? 1 : -1) * ((len1 < len2) ? -1 : 1);
#else
    if (!mCollation)
        return -1;

    if (strval1->mLength == 0) {
        if (strval2->mLength == 0)
            return 0;
        return ((mSorting & kAscending) ? -1 : 1);
    }

    if (strval2->mLength == 0)
        return ((mSorting & kAscending) ? 1 : -1);

    nsresult rv;
    PRInt32 result = -1;
    rv = mCollation->CompareRawSortKey(strval1->mKey, strval1->mLength,
                                       strval2->mKey, strval2->mLength,
                                       &result);
    if (NS_FAILED(rv)) {
        // XXX ErrorReport
        return -1;
    }

    if (result != 0)
        return ((mSorting & kAscending) ? 1 : -1) * result;

    if ((strval1->mCaseLength == 0) && (strval1->mLength != 0)) {
        nsString* caseString = (nsString *)strval1->mCaseKey;
        rv = mCollation->AllocateRawSortKey(nsICollation::kCollationCaseSensitive,
                                            *caseString,
                                            (PRUint8**)&strval1->mCaseKey, 
                                            &strval1->mCaseLength);
        if (NS_FAILED(rv)) {
            // XXX ErrorReport
            strval1->mCaseKey = caseString;
            strval1->mCaseLength = 0;
            return -1;
        }
        delete caseString;
    }
    if ((strval2->mCaseLength == 0) && (strval2->mLength != 0)) {
        nsString* caseString = (nsString *)strval2->mCaseKey;
        rv = mCollation->AllocateRawSortKey(nsICollation::kCollationCaseSensitive,
                                            *caseString,
                                            (PRUint8**)&strval2->mCaseKey, 
                                            &strval2->mCaseLength);
        if (NS_FAILED(rv)) {
            // XXX ErrorReport
            strval2->mCaseKey = caseString;
            strval2->mCaseLength = 0;
            return -1;
        }
        delete caseString;
    }
    rv = mCollation->CompareRawSortKey((PRUint8*)strval1->mCaseKey, strval1->mCaseLength,
                                       (PRUint8*)strval2->mCaseKey, strval2->mCaseLength,
                                       &result);
    if (NS_FAILED(rv)) {
        // XXX ErrorReport
        return -1;
    }

    return ((mSorting & kAscending) ? 1 : -1) *
           ((mSorting & kUpperFirst) ? -1 : 1) * result;
#endif
}
nsresult txResultStringComparator::createRawSortKey ( const PRInt32  aStrength,
const nsString aString,
PRUint8 **  aKey,
PRUint32 aLength 
) [private]

Implements txXPathResultComparator.

Definition at line 110 of file txXPathResultComparator.cpp.

{
    StringValue* val = new StringValue;

    if (!val)
        return 0;

#ifdef TX_EXE
    aExprRes->stringValue(val->mStr);
    // We don't support case-order on standalone
    TX_ToLowerCase(val->mStr);
#else
    if (!mCollation)
        return 0;

    val->mCaseKey = new nsString;
    if (!val->mCaseKey) {
        delete val;
        return 0;
    }

    nsString& nsCaseKey = *(nsString *)val->mCaseKey;
    aExprRes->stringValue(nsCaseKey);
    if (nsCaseKey.IsEmpty()) {
        return val;        
    }
    nsresult rv = mCollation->AllocateRawSortKey(nsICollation::kCollationCaseInSensitive,
                                                 nsCaseKey,
                                                 &val->mKey, 
                                                 &val->mLength);
    if (NS_FAILED(rv)) {
        NS_ERROR("Failed to create raw sort key");
        delete val;
        return 0;
    }
#endif
    return val;
}

Here is the call graph for this function:

Definition at line 81 of file txXPathResultComparator.cpp.

{
    nsresult rv;

    nsCOMPtr<nsILocaleService> localeService =
                    do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr<nsILocale> locale;
    if (!aLanguage.IsEmpty()) {
        rv = localeService->NewLocale(aLanguage,
                                      getter_AddRefs(locale));
    }
    else {
        rv = localeService->GetApplicationLocale(getter_AddRefs(locale));
    }
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr<nsICollationFactory> colFactory =
                    do_CreateInstance(kCollationFactoryCID, &rv);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = colFactory->CreateCollation(locale, getter_AddRefs(mCollation));
    NS_ENSURE_SUCCESS(rv, rv);

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 88 of file txXPathResultComparator.h.

Definition at line 95 of file txXPathResultComparator.h.


The documentation for this class was generated from the following files: