From: Ralf Haferkamp Date: Wed, 6 Mar 2002 23:04:38 +0000 (+0000) Subject: - added getAttributeByName()-method X-Git-Tag: OPENLDAP_REL_ENG_2_MP~370 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=025e612920068f186c8fa388422b0a3198b6c0a7;p=openldap - added getAttributeByName()-method - Attributes are now organized by the attribute type (i.e. one can add more LDAPAttribute-Objects with the same type without violating the X.500 data model) --- diff --git a/contrib/ldapc++/src/LDAPAttributeList.cpp b/contrib/ldapc++/src/LDAPAttributeList.cpp index 7c26ef3ec5..5d3b467748 100644 --- a/contrib/ldapc++/src/LDAPAttributeList.cpp +++ b/contrib/ldapc++/src/LDAPAttributeList.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2000, OpenLDAP Foundation, All Rights Reserved. + * Copyright 2000-2002, OpenLDAP Foundation, All Rights Reserved. * COPYING RESTRICTIONS APPLY, see COPYRIGHT file */ @@ -15,6 +15,9 @@ 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,11 +85,56 @@ 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); + AttrList::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; + AttrList::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); + } } @@ -110,3 +158,7 @@ ostream& operator << (ostream& s, const LDAPAttributeList& al){ return s; } +bool nocase_compare( char c1, char c2){ + return toupper(c1) == toupper(c2); +} + diff --git a/contrib/ldapc++/src/LDAPAttributeList.h b/contrib/ldapc++/src/LDAPAttributeList.h index cf8952173c..990a6d8c85 100644 --- a/contrib/ldapc++/src/LDAPAttributeList.h +++ b/contrib/ldapc++/src/LDAPAttributeList.h @@ -1,5 +1,5 @@ /* - * Copyright 2000, OpenLDAP Foundation, All Rights Reserved. + * Copyright 2000-2002, OpenLDAP Foundation, All Rights Reserved. * COPYING RESTRICTIONS APPLY, see COPYRIGHT file */ @@ -9,6 +9,8 @@ #include #include +#include + class LDAPAttribute; class LDAPAsynConnection; class LDAPMsg; @@ -24,6 +26,7 @@ class LDAPAttributeList{ public : typedef AttrList::const_iterator const_iterator; + typedef AttrList::iterator iterator; /** @@ -73,6 +76,15 @@ class LDAPAttributeList{ */ const_iterator end() const; + /** + * Get an Attribute by its AttributeType + * @param name The name of the Attribute to look for + * @return a pointer to the LDAPAttribute with the AttributeType + * "name" or 0, if there is no Attribute of that Type + */ + const LDAPAttribute* getAttributeByName(const std::string& name) const; + + /** * Adds one element to the end of the list. * @param attr The attribute to add to the list. @@ -92,5 +104,6 @@ class LDAPAttributeList{ friend std::ostream& operator << (std::ostream& s, const LDAPAttributeList& al); }; + #endif // LDAP_ATTRIBUTE_LIST_H