Back to index

lightning-sunbird  0.9+nobinonly
Functions
nsLDAPOperation.cpp File Reference
#include "nsLDAPInternal.h"
#include "nsLDAP.h"
#include "nsLDAPOperation.h"
#include "nsLDAPConnection.h"
#include "nsILDAPMessage.h"
#include "nsIComponentManager.h"
#include "nsXPIDLString.h"
#include "nspr.h"
#include "nsISimpleEnumerator.h"
#include "nsLDAPControl.h"

Go to the source code of this file.

Functions

static nsresult convertControlArray (nsIArray *aXpcomArray, LDAPControl ***aArray)
 Given an nsIArray of nsILDAPControls, return the appropriate zero-terminated array of LDAPControls ready to pass in to the C SDK.

Function Documentation

static nsresult convertControlArray ( nsIArray aXpcomArray,
LDAPControl ***  aArray 
) [static]

Given an nsIArray of nsILDAPControls, return the appropriate zero-terminated array of LDAPControls ready to pass in to the C SDK.

Definition at line 251 of file nsLDAPOperation.cpp.

{
    // get the size of the original array
    PRUint32 length;
    nsresult rv  = aXpcomArray->GetLength(&length);
    NS_ENSURE_SUCCESS(rv, rv);

    // don't allocate an array if someone passed us in an empty one
    if (!length) {
        *aArray = 0;
        return NS_OK;
    }

    // allocate a local array of the form understood by the C-SDK;
    // +1 is to account for the final null terminator.  PR_Calloc is
    // is used so that ldap_controls_free will work anywhere during the
    // iteration
    LDAPControl **controls = 
        NS_STATIC_CAST(LDAPControl **,
                       PR_Calloc(length+1, sizeof(LDAPControl)));

    // prepare to enumerate the array
    nsCOMPtr<nsISimpleEnumerator> enumerator;
    rv = aXpcomArray->Enumerate(getter_AddRefs(enumerator));
    NS_ENSURE_SUCCESS(rv, rv);

    PRBool moreElements;
    rv = enumerator->HasMoreElements(&moreElements);
    NS_ENSURE_SUCCESS(rv, rv);

    PRUint32 i = 0;
    while (moreElements) {

        // get the next array element
        nsCOMPtr<nsISupports> isupports;
        rv = enumerator->GetNext(getter_AddRefs(isupports));
        if (NS_FAILED(rv)) {
            ldap_controls_free(controls);
            return rv;
        }
        nsCOMPtr<nsILDAPControl> control = do_QueryInterface(isupports, &rv);
        if (NS_FAILED(rv)) {
            ldap_controls_free(controls);
            return NS_ERROR_INVALID_ARG; // bogus element in the array
        }
        nsLDAPControl *ctl = NS_STATIC_CAST(nsLDAPControl *,
                                            NS_STATIC_CAST(nsILDAPControl *,
                                                           control.get()));

        // convert it to an LDAPControl structure placed in the new array
        rv = ctl->ToLDAPControl(&controls[i]);
        if (NS_FAILED(rv)) {
            ldap_controls_free(controls);
            return rv;
        }

        // on to the next element
        rv = enumerator->HasMoreElements(&moreElements);
        if (NS_FAILED(rv)) {
            ldap_controls_free(controls);
            return NS_ERROR_UNEXPECTED;
        }
        ++i;
    }

    *aArray = controls;
    return NS_OK;
}

Here is the call graph for this function: