]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPAttributeList.cpp
Suck in HEAD changes since 2.1alpha
[openldap] / contrib / ldapc++ / src / LDAPAttributeList.cpp
1 /*
2  * Copyright 2000-2002, OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6
7 #include "debug.h"
8
9 #include "LDAPAttributeList.h"
10
11 #include "LDAPException.h"
12 #include "LDAPAttribute.h"
13 #include "LDAPAsynConnection.h"
14 #include "LDAPMessage.h"
15
16 using namespace std;
17
18 // little helper function for doing case insensitve string comparison
19 bool nocase_compare(char c1, char c2);
20
21 LDAPAttributeList::LDAPAttributeList(){
22     DEBUG(LDAP_DEBUG_CONSTRUCT,
23             "LDAPAttributeList::LDAPAttributList( )" << endl);
24 }
25
26 LDAPAttributeList::LDAPAttributeList(const LDAPAttributeList& al){
27     DEBUG(LDAP_DEBUG_CONSTRUCT,
28             "LDAPAttributeList::LDAPAttributList(&)" << endl);
29     m_attrs=al.m_attrs;
30 }
31
32 LDAPAttributeList::LDAPAttributeList(const LDAPAsynConnection *ld, 
33         LDAPMessage *msg){
34     DEBUG(LDAP_DEBUG_CONSTRUCT,
35             "LDAPAttributeList::LDAPAttributList()" << endl);
36     BerElement *ptr=0;
37     char *name=ldap_first_attribute(ld->getSessionHandle(), msg, &ptr);
38 /*
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..
43
44    There will be some changes in the new version of the C-API so that this
45    code should work in the future.
46    if(name == 0){
47         ber_free(ptr,0);
48         ldap_memfree(name);
49         throw LDAPException(ld);
50     }else{
51 */        BerValue **values;
52         for (;name !=0;
53                 name=ldap_next_attribute(ld->getSessionHandle(),msg,ptr) ){
54             values=ldap_get_values_len(ld->getSessionHandle(),
55                     msg, name);
56             this->addAttribute(LDAPAttribute(name, values));
57             ldap_memfree(name);
58             ldap_value_free_len(values);
59         }
60         ber_free(ptr,0);
61 //    }
62 }
63
64 LDAPAttributeList::~LDAPAttributeList(){
65     DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttributeList::~LDAPAttributList()" << endl);
66 }
67
68 size_t LDAPAttributeList::size() const{
69     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::size()" << endl);
70     return m_attrs.size();
71 }
72
73 bool LDAPAttributeList::empty() const{
74     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::empty()" << endl);
75     return m_attrs.empty();
76 }
77
78 LDAPAttributeList::const_iterator LDAPAttributeList::begin() const{
79     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::begin()" << endl);
80     return m_attrs.begin();
81 }
82
83 LDAPAttributeList::const_iterator LDAPAttributeList::end() const{
84     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::end()" << endl);
85     return m_attrs.end();
86 }
87
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(),
98                     nocase_compare)){
99                 return &(*i);
100                 DEBUG(LDAP_DEBUG_TRACE,"    found:" << name << endl);
101             }
102         }
103     }
104     return 0;
105 }
106
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;
114     bool done=false;
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(),
121                     nocase_compare)){
122                 const StringList& values = attr.getValues();
123                 StringList::const_iterator j;
124                 for(j = values.begin(); j != values.end(); j++){
125                     i->addValue(*j);
126                 }
127                 DEBUG(LDAP_DEBUG_TRACE,"Attribute" << i->getName() 
128                         << "already present" << endl);
129                 done=true;
130                 break; // The AttributeType was already present,
131                        // we are done here
132             }
133         }
134     }
135     if(! done){
136         m_attrs.push_back(attr);
137     }
138 }
139
140
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;
145     int j=0;
146     for (i=m_attrs.begin(); i!= m_attrs.end(); i++, j++){
147         ret[j]=i->toLDAPMod();
148     }
149     ret[m_attrs.size()]=0;
150     return ret;
151 }
152
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++){
156         s << *i << "; ";
157     }
158     return s;
159 }
160
161 bool nocase_compare( char c1, char c2){
162     return toupper(c1) == toupper(c2);
163 }
164