2 * Copyright 2000-2002, OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 #include "LDAPAttributeList.h"
11 #include "LDAPException.h"
12 #include "LDAPAttribute.h"
13 #include "LDAPAsynConnection.h"
14 #include "LDAPMessage.h"
18 // little helper function for doing case insensitve string comparison
19 bool nocase_compare(char c1, char c2);
21 LDAPAttributeList::LDAPAttributeList(){
22 DEBUG(LDAP_DEBUG_CONSTRUCT,
23 "LDAPAttributeList::LDAPAttributList( )" << endl);
26 LDAPAttributeList::LDAPAttributeList(const LDAPAttributeList& al){
27 DEBUG(LDAP_DEBUG_CONSTRUCT,
28 "LDAPAttributeList::LDAPAttributList(&)" << endl);
32 LDAPAttributeList::LDAPAttributeList(const LDAPAsynConnection *ld,
34 DEBUG(LDAP_DEBUG_CONSTRUCT,
35 "LDAPAttributeList::LDAPAttributList()" << endl);
37 char *name=ldap_first_attribute(ld->getSessionHandle(), msg, &ptr);
39 This code was making problems if no attribute were returned
40 How am I supposed to find decoding errors? ldap_first/next_attribute
41 return 0 in case of error or if there are no more attributes. In either
42 case they set the LDAP* error code to 0x54 (Decoding error) ??? Strange..
44 There will be some changes in the new version of the C-API so that this
45 code should work in the future.
49 throw LDAPException(ld);
53 name=ldap_next_attribute(ld->getSessionHandle(),msg,ptr) ){
54 values=ldap_get_values_len(ld->getSessionHandle(),
56 this->addAttribute(LDAPAttribute(name, values));
58 ldap_value_free_len(values);
64 LDAPAttributeList::~LDAPAttributeList(){
65 DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttributeList::~LDAPAttributList()" << endl);
68 size_t LDAPAttributeList::size() const{
69 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::size()" << endl);
70 return m_attrs.size();
73 bool LDAPAttributeList::empty() const{
74 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::empty()" << endl);
75 return m_attrs.empty();
78 LDAPAttributeList::const_iterator LDAPAttributeList::begin() const{
79 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::begin()" << endl);
80 return m_attrs.begin();
83 LDAPAttributeList::const_iterator LDAPAttributeList::end() const{
84 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::end()" << endl);
88 const LDAPAttribute* LDAPAttributeList::getAttributeByName(
89 const string& name) const {
90 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getAttributeByName()" << endl);
91 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
92 " name:" << name << endl);
93 AttrList::const_iterator i;
94 for( i = m_attrs.begin(); i != m_attrs.end(); i++){
95 const std::string& tmpType = i->getName();
96 if(name.size() == tmpType.size()){
97 if(equal(name.begin(), name.end(), tmpType.begin(),
100 DEBUG(LDAP_DEBUG_TRACE," found:" << name << endl);
107 void LDAPAttributeList::addAttribute(const LDAPAttribute& attr){
108 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addAttribute()" << endl);
109 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
110 " attr:" << attr << endl);
111 const std::string attrType = attr.getName();
112 const std::string::size_type attrLen = attrType.size();
113 std::string::size_type tmpAttrLen = 0;
115 AttrList::iterator i;
116 for( i=m_attrs.begin(); i != m_attrs.end(); i++ ){
117 const std::string tmpAttrType = i->getName();
118 tmpAttrLen = tmpAttrType.size();
119 if(tmpAttrLen == attrLen){
120 if(equal(tmpAttrType.begin(), tmpAttrType.end(), attrType.begin(),
122 const StringList& values = attr.getValues();
123 StringList::const_iterator j;
124 for(j = values.begin(); j != values.end(); j++){
127 DEBUG(LDAP_DEBUG_TRACE,"Attribute" << i->getName()
128 << "already present" << endl);
130 break; // The AttributeType was already present,
136 m_attrs.push_back(attr);
141 LDAPMod** LDAPAttributeList::toLDAPModArray() const{
142 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::toLDAPModArray()" << endl);
143 LDAPMod **ret = (LDAPMod**) malloc((m_attrs.size()+1) * sizeof(LDAPMod*));
144 AttrList::const_iterator i;
146 for (i=m_attrs.begin(); i!= m_attrs.end(); i++, j++){
147 ret[j]=i->toLDAPMod();
149 ret[m_attrs.size()]=0;
153 ostream& operator << (ostream& s, const LDAPAttributeList& al){
154 AttrList::const_iterator i;
155 for(i=al.m_attrs.begin(); i!=al.m_attrs.end(); i++){
161 bool nocase_compare( char c1, char c2){
162 return toupper(c1) == toupper(c2);