X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=contrib%2Fldapc%2B%2B%2Fsrc%2FLDAPAttributeList.cpp;h=6e5f66e7f2aa863711cdf1b0f2b4754feee71934;hb=cfa8dd6884f7d45154fb92651fd6e04aeb93dd0b;hp=94881b70ecc942ec09a7adbc229bbdf228be56ae;hpb=c0dbaa736d8cb1c86a4a730e8c9b6942e0fdbb32;p=openldap diff --git a/contrib/ldapc++/src/LDAPAttributeList.cpp b/contrib/ldapc++/src/LDAPAttributeList.cpp index 94881b70ec..6e5f66e7f2 100644 --- a/contrib/ldapc++/src/LDAPAttributeList.cpp +++ b/contrib/ldapc++/src/LDAPAttributeList.cpp @@ -1,5 +1,6 @@ +// $OpenLDAP$ /* - * Copyright 2000, OpenLDAP Foundation, All Rights Reserved. + * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved. * COPYING RESTRICTIONS APPLY, see COPYRIGHT file */ @@ -13,8 +14,13 @@ #include "LDAPAsynConnection.h" #include "LDAPMessage.h" +#include + using namespace std; +// little helper function for doing case insensitve string comparison +bool nocase_compare(char c1, char c2); + LDAPAttributeList::LDAPAttributeList(){ DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttributeList::LDAPAttributList( )" << endl); @@ -82,18 +88,89 @@ LDAPAttributeList::const_iterator LDAPAttributeList::end() const{ return m_attrs.end(); } +const LDAPAttribute* LDAPAttributeList::getAttributeByName( + const string& name) const { + DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getAttributeByName()" << endl); + DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, + " name:" << name << endl); + LDAPAttributeList::const_iterator i; + for( i = m_attrs.begin(); i != m_attrs.end(); i++){ + const std::string& tmpType = i->getName(); + if(name.size() == tmpType.size()){ + if(equal(name.begin(), name.end(), tmpType.begin(), + nocase_compare)){ + return &(*i); + DEBUG(LDAP_DEBUG_TRACE," found:" << name << endl); + } + } + } + return 0; +} + void LDAPAttributeList::addAttribute(const LDAPAttribute& attr){ DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addAttribute()" << endl); DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " attr:" << attr << endl); - m_attrs.push_back(attr); + const std::string attrType = attr.getName(); + const std::string::size_type attrLen = attrType.size(); + std::string::size_type tmpAttrLen = 0; + bool done=false; + LDAPAttributeList::iterator i; + for( i=m_attrs.begin(); i != m_attrs.end(); i++ ){ + const std::string tmpAttrType = i->getName(); + tmpAttrLen = tmpAttrType.size(); + if(tmpAttrLen == attrLen){ + if(equal(tmpAttrType.begin(), tmpAttrType.end(), attrType.begin(), + nocase_compare)){ + const StringList& values = attr.getValues(); + StringList::const_iterator j; + for(j = values.begin(); j != values.end(); j++){ + i->addValue(*j); + } + DEBUG(LDAP_DEBUG_TRACE,"Attribute" << i->getName() + << "already present" << endl); + done=true; + break; // The AttributeType was already present, + // we are done here + } + } + } + if(! done){ + m_attrs.push_back(attr); + } } +void LDAPAttributeList::delAttribute(const std::string& type) +{ + DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::replaceAttribute()" << endl); + DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " type: " << type << endl); + LDAPAttributeList::iterator i; + for( i = m_attrs.begin(); i != m_attrs.end(); i++){ + if(type.size() == i->getName().size()){ + if(equal(type.begin(), type.end(), i->getName().begin(), + nocase_compare)){ + m_attrs.erase(i); + break; + } + } + } +} + +void LDAPAttributeList::replaceAttribute(const LDAPAttribute& attr) +{ + DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::replaceAttribute()" << endl); + DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, + " attr:" << attr << endl); + + LDAPAttributeList::iterator i; + this->delAttribute( attr.getName() ); + m_attrs.push_back(attr); +} LDAPMod** LDAPAttributeList::toLDAPModArray() const{ DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::toLDAPModArray()" << endl); - LDAPMod **ret = new LDAPMod*[m_attrs.size()+1]; - AttrList::const_iterator i; + LDAPMod **ret = (LDAPMod**) malloc((m_attrs.size()+1) * sizeof(LDAPMod*)); + LDAPAttributeList::const_iterator i; int j=0; for (i=m_attrs.begin(); i!= m_attrs.end(); i++, j++){ ret[j]=i->toLDAPMod(); @@ -103,10 +180,14 @@ LDAPMod** LDAPAttributeList::toLDAPModArray() const{ } ostream& operator << (ostream& s, const LDAPAttributeList& al){ - AttrList::const_iterator i; + LDAPAttributeList::const_iterator i; for(i=al.m_attrs.begin(); i!=al.m_attrs.end(); i++){ s << *i << "; "; } return s; } +bool nocase_compare( char c1, char c2){ + return toupper(c1) == toupper(c2); +} +