TLDAPResult.cxx

Go to the documentation of this file.
00001 // @(#)root/ldap:$Id: TLDAPResult.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Oleksandr Grebenyuk   21/09/2001
00003 
00004 /*************************************************************************
00005  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00006  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00007  *************************************************************************/
00008 
00009 #include "TLDAPResult.h"
00010 #include "TLDAPEntry.h"
00011 #include "TLDAPAttribute.h"
00012 
00013 
00014 ClassImp(TLDAPResult)
00015 
00016 //______________________________________________________________________________
00017 TLDAPResult::TLDAPResult(LDAP *ld, LDAPMessage *searchresult)
00018    : fLd(ld), fSearchResult(searchresult), fCurrentEntry(searchresult)
00019 {
00020    // TLDAPResult object is just a wrapper of the LDAPMessage structure.
00021    // LDAP *ld:                  The current session handler
00022    // LDAPMessage *searchresult: The LDAPMessage structure returned from
00023    //                            the ldap_search_s() call
00024 
00025    if (!GetCount())
00026       fCurrentEntry = 0;
00027 }
00028 
00029 //______________________________________________________________________________
00030 TLDAPResult::TLDAPResult(const TLDAPResult& ldr) :
00031    TObject(ldr),
00032    fLd(ldr.fLd),
00033    fSearchResult(ldr.fSearchResult),
00034    fCurrentEntry(ldr.fCurrentEntry)
00035 {
00036    // Copy constructor
00037 }
00038 
00039 //______________________________________________________________________________
00040 TLDAPResult& TLDAPResult::operator=(const TLDAPResult& ldr)
00041 {
00042    // Equal operator
00043    if(this!=&ldr) {
00044       TObject::operator=(ldr);
00045       fLd=ldr.fLd;
00046       fSearchResult=ldr.fSearchResult;
00047       fCurrentEntry=ldr.fCurrentEntry;
00048    } return *this;
00049 }
00050 
00051 //______________________________________________________________________________
00052 TLDAPResult::~TLDAPResult()
00053 {
00054    // Deletes the LDAPMessage structure
00055 
00056    if (fSearchResult)
00057       ldap_msgfree(fSearchResult);
00058 }
00059 
00060 //______________________________________________________________________________
00061 TLDAPEntry *TLDAPResult::GetNext()
00062 {
00063    // Returns next entry from the search result.
00064    // After the last entry it returns a zero pointer
00065    // and after this it returns the first entry again.
00066    // The user is responsable for deleting the returned object after use.
00067 
00068    TLDAPEntry *entry = CreateEntry(fCurrentEntry);
00069    fCurrentEntry = (fCurrentEntry != 0 ? ldap_next_entry(fLd, fCurrentEntry) :
00070                    (GetCount() != 0 ? fSearchResult : 0));
00071    return entry;
00072 }
00073 
00074 //______________________________________________________________________________
00075 TLDAPEntry *TLDAPResult::CreateEntry(LDAPMessage *entry)
00076 {
00077    // Creates TLDAPEntry object from the data containing in the LDAPMessage
00078    // structure and returns pointer to it.
00079    // The user is responsable for deleting the returned object after use.
00080    // LDAPMessage *entry: Pointer to the LDAPMessage structure containing
00081    // the entry data.
00082 
00083    if (entry == 0)
00084       return 0;
00085 
00086    char *dn;
00087    char *attr;
00088    BerValue   **vals;
00089    BerElement *ptr;
00090 
00091    dn = ldap_get_dn(fLd, entry);
00092    TLDAPEntry *ldapentry = new TLDAPEntry(dn);
00093    for (attr = ldap_first_attribute(fLd, entry, &ptr); attr != 0;
00094         attr = ldap_next_attribute(fLd, entry, ptr)) {
00095       TLDAPAttribute attribute(attr);
00096       vals = ldap_get_values_len(fLd, entry, attr);
00097       if (vals) {
00098          for (Int_t i = 0; vals[i] != 0; i++) {
00099             attribute.AddValue(vals[i]->bv_val);
00100          }
00101          ldap_value_free_len(vals);
00102       }
00103       ldapentry->AddAttribute(attribute);
00104    }
00105 
00106    return ldapentry;
00107 }
00108 
00109 //______________________________________________________________________________
00110 Int_t TLDAPResult::GetCount() const
00111 {
00112    // Returns the number of entries in the search result
00113 
00114    LDAP *ld = fLd;
00115    LDAPMessage *result = fSearchResult;
00116 
00117    return ldap_count_entries(ld, result);
00118 }
00119 
00120 //______________________________________________________________________________
00121 void TLDAPResult::Print(Option_t *) const
00122 {
00123    // Prints all entries.
00124    // Calls the Print() member function of the each entry.
00125 
00126    TLDAPEntry *e;
00127    Int_t count = GetCount() + 1;
00128    for (Int_t i = 0; i < count; i++) {
00129       e = const_cast<TLDAPResult*>(this)->GetNext();
00130       if (e) {
00131          e->Print();
00132          delete e;
00133       }
00134    }
00135 }

Generated on Tue Jul 5 14:45:41 2011 for ROOT_528-00b_version by  doxygen 1.5.1