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