Back to index

openldap  2.4.31
LDAPAttributeList.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 
00008 #include "debug.h"
00009 
00010 #include "LDAPAttributeList.h"
00011 
00012 #include "LDAPException.h"
00013 #include "LDAPAttribute.h"
00014 #include "LDAPAsynConnection.h"
00015 #include "LDAPMessage.h"
00016 
00017 #include <cstdlib>
00018 
00019 using namespace std;
00020 
00021 // little helper function for doing case insensitve string comparison
00022 bool nocase_compare(char c1, char c2);
00023 
00024 LDAPAttributeList::LDAPAttributeList(){
00025     DEBUG(LDAP_DEBUG_CONSTRUCT,
00026             "LDAPAttributeList::LDAPAttributList( )" << endl);
00027 }
00028 
00029 LDAPAttributeList::LDAPAttributeList(const LDAPAttributeList& al){
00030     DEBUG(LDAP_DEBUG_CONSTRUCT,
00031             "LDAPAttributeList::LDAPAttributList(&)" << endl);
00032     m_attrs=al.m_attrs;
00033 }
00034 
00035 LDAPAttributeList::LDAPAttributeList(const LDAPAsynConnection *ld, 
00036         LDAPMessage *msg){
00037     DEBUG(LDAP_DEBUG_CONSTRUCT,
00038             "LDAPAttributeList::LDAPAttributList()" << endl);
00039     BerElement *ptr=0;
00040     char *name=ldap_first_attribute(ld->getSessionHandle(), msg, &ptr);
00041 /*
00042    This code was making problems if no attribute were returned
00043    How am I supposed to find decoding errors? ldap_first/next_attribute
00044    return 0 in case of error or if there are no more attributes. In either
00045    case they set the LDAP* error code to 0x54 (Decoding error) ??? Strange..
00046 
00047    There will be some changes in the new version of the C-API so that this
00048    code should work in the future.
00049    if(name == 0){
00050         ber_free(ptr,0);
00051         ldap_memfree(name);
00052         throw LDAPException(ld);
00053     }else{
00054 */        BerValue **values;
00055         for (;name !=0;
00056                 name=ldap_next_attribute(ld->getSessionHandle(),msg,ptr) ){
00057             values=ldap_get_values_len(ld->getSessionHandle(),
00058                     msg, name);
00059             this->addAttribute(LDAPAttribute(name, values));
00060             ldap_memfree(name);
00061             ldap_value_free_len(values);
00062         }
00063         ber_free(ptr,0);
00064 //    }
00065 }
00066 
00067 LDAPAttributeList::~LDAPAttributeList(){
00068     DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttributeList::~LDAPAttributList()" << endl);
00069 }
00070 
00071 size_t LDAPAttributeList::size() const{
00072     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::size()" << endl);
00073     return m_attrs.size();
00074 }
00075 
00076 bool LDAPAttributeList::empty() const{
00077     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::empty()" << endl);
00078     return m_attrs.empty();
00079 }
00080 
00081 LDAPAttributeList::const_iterator LDAPAttributeList::begin() const{
00082     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::begin()" << endl);
00083     return m_attrs.begin();
00084 }
00085 
00086 LDAPAttributeList::const_iterator LDAPAttributeList::end() const{
00087     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::end()" << endl);
00088     return m_attrs.end();
00089 }
00090 
00091 const LDAPAttribute* LDAPAttributeList::getAttributeByName(
00092        const string& name) const {
00093     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getAttributeByName()" << endl);
00094     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
00095             "   name:" << name << endl);
00096     LDAPAttributeList::const_iterator i;
00097     for( i = m_attrs.begin(); i != m_attrs.end(); i++){
00098        const std::string& tmpType = i->getName();
00099        if(name.size() == tmpType.size()){
00100            if(equal(name.begin(), name.end(), tmpType.begin(),
00101                   nocase_compare)){
00102               return &(*i);
00103               DEBUG(LDAP_DEBUG_TRACE,"    found:" << name << endl);
00104            }
00105        }
00106     }
00107     return 0;
00108 }
00109 
00110 void LDAPAttributeList::addAttribute(const LDAPAttribute& attr){
00111     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addAttribute()" << endl);
00112     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
00113             "   attr:" << attr << endl);
00114     const std::string attrType = attr.getName();
00115     const std::string::size_type attrLen = attrType.size();
00116     std::string::size_type tmpAttrLen = 0;
00117     bool done=false;
00118     LDAPAttributeList::iterator i;
00119     for( i=m_attrs.begin(); i != m_attrs.end(); i++ ){
00120        const std::string tmpAttrType = i->getName();
00121        tmpAttrLen = tmpAttrType.size();
00122        if(tmpAttrLen == attrLen){
00123            if(equal(tmpAttrType.begin(), tmpAttrType.end(), attrType.begin(),
00124                   nocase_compare)){
00125               const StringList& values = attr.getValues();
00126               StringList::const_iterator j;
00127               for(j = values.begin(); j != values.end(); j++){
00128                   i->addValue(*j);
00129               }
00130               DEBUG(LDAP_DEBUG_TRACE,"Attribute" << i->getName() 
00131                      << "already present" << endl);
00132               done=true;
00133               break; // The AttributeType was already present,
00134                      // we are done here
00135            }
00136        }
00137     }
00138     if(! done){
00139        m_attrs.push_back(attr);
00140     }
00141 }
00142 
00143 void LDAPAttributeList::delAttribute(const std::string& type)
00144 {
00145     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::replaceAttribute()" << endl);
00146     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, "   type: " << type << endl);
00147     LDAPAttributeList::iterator i;
00148     for( i = m_attrs.begin(); i != m_attrs.end(); i++){
00149        if(type.size() == i->getName().size()){
00150            if(equal(type.begin(), type.end(), i->getName().begin(),
00151                   nocase_compare)){
00152                 m_attrs.erase(i);
00153                 break;
00154             }
00155         }
00156     }
00157 }
00158 
00159 void LDAPAttributeList::replaceAttribute(const LDAPAttribute& attr)
00160 {
00161     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::replaceAttribute()" << endl);
00162     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
00163             "   attr:" << attr << endl);
00164     
00165     LDAPAttributeList::iterator i;
00166     this->delAttribute( attr.getName() );
00167     m_attrs.push_back(attr);
00168 }
00169 
00170 LDAPMod** LDAPAttributeList::toLDAPModArray() const{
00171     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::toLDAPModArray()" << endl);
00172     LDAPMod **ret = (LDAPMod**) malloc((m_attrs.size()+1) * sizeof(LDAPMod*));
00173     LDAPAttributeList::const_iterator i;
00174     int j=0;
00175     for (i=m_attrs.begin(); i!= m_attrs.end(); i++, j++){
00176         ret[j]=i->toLDAPMod();
00177     }
00178     ret[m_attrs.size()]=0;
00179     return ret;
00180 }
00181 
00182 ostream& operator << (ostream& s, const LDAPAttributeList& al){
00183     LDAPAttributeList::const_iterator i;
00184     for(i=al.m_attrs.begin(); i!=al.m_attrs.end(); i++){
00185         s << *i << "; ";
00186     }
00187     return s;
00188 }
00189 
00190 bool nocase_compare( char c1, char c2){
00191     return toupper(c1) == toupper(c2);
00192 }
00193