]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPAttributeList.cpp
94881b70ecc942ec09a7adbc229bbdf228be56ae
[openldap] / contrib / ldapc++ / src / LDAPAttributeList.cpp
1 /*
2  * Copyright 2000, 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 LDAPAttributeList::LDAPAttributeList(){
19     DEBUG(LDAP_DEBUG_CONSTRUCT,
20             "LDAPAttributeList::LDAPAttributList( )" << endl);
21 }
22
23 LDAPAttributeList::LDAPAttributeList(const LDAPAttributeList& al){
24     DEBUG(LDAP_DEBUG_CONSTRUCT,
25             "LDAPAttributeList::LDAPAttributList(&)" << endl);
26     m_attrs=al.m_attrs;
27 }
28
29 LDAPAttributeList::LDAPAttributeList(const LDAPAsynConnection *ld, 
30         LDAPMessage *msg){
31     DEBUG(LDAP_DEBUG_CONSTRUCT,
32             "LDAPAttributeList::LDAPAttributList()" << endl);
33     BerElement *ptr=0;
34     char *name=ldap_first_attribute(ld->getSessionHandle(), msg, &ptr);
35 /*
36    This code was making problems if no attribute were returned
37    How am I supposed to find decoding errors? ldap_first/next_attribute
38    return 0 in case of error or if there are no more attributes. In either
39    case they set the LDAP* error code to 0x54 (Decoding error) ??? Strange..
40
41    There will be some changes in the new version of the C-API so that this
42    code should work in the future.
43    if(name == 0){
44         ber_free(ptr,0);
45         ldap_memfree(name);
46         throw LDAPException(ld);
47     }else{
48 */        BerValue **values;
49         for (;name !=0;
50                 name=ldap_next_attribute(ld->getSessionHandle(),msg,ptr) ){
51             values=ldap_get_values_len(ld->getSessionHandle(),
52                     msg, name);
53             this->addAttribute(LDAPAttribute(name, values));
54             ldap_memfree(name);
55             ldap_value_free_len(values);
56         }
57         ber_free(ptr,0);
58 //    }
59 }
60
61 LDAPAttributeList::~LDAPAttributeList(){
62     DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttributeList::~LDAPAttributList()" << endl);
63 }
64
65 size_t LDAPAttributeList::size() const{
66     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::size()" << endl);
67     return m_attrs.size();
68 }
69
70 bool LDAPAttributeList::empty() const{
71     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::empty()" << endl);
72     return m_attrs.empty();
73 }
74
75 LDAPAttributeList::const_iterator LDAPAttributeList::begin() const{
76     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::begin()" << endl);
77     return m_attrs.begin();
78 }
79
80 LDAPAttributeList::const_iterator LDAPAttributeList::end() const{
81     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::end()" << endl);
82     return m_attrs.end();
83 }
84
85 void LDAPAttributeList::addAttribute(const LDAPAttribute& attr){
86     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addAttribute()" << endl);
87     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
88             "   attr:" << attr << endl);
89     m_attrs.push_back(attr);
90 }
91
92
93 LDAPMod** LDAPAttributeList::toLDAPModArray() const{
94     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::toLDAPModArray()" << endl);
95     LDAPMod **ret = new LDAPMod*[m_attrs.size()+1];
96     AttrList::const_iterator i;
97     int j=0;
98     for (i=m_attrs.begin(); i!= m_attrs.end(); i++, j++){
99         ret[j]=i->toLDAPMod();
100     }
101     ret[m_attrs.size()]=0;
102     return ret;
103 }
104
105 ostream& operator << (ostream& s, const LDAPAttributeList& al){
106     AttrList::const_iterator i;
107     for(i=al.m_attrs.begin(); i!=al.m_attrs.end(); i++){
108         s << *i << "; ";
109     }
110     return s;
111 }
112