xref: /netbsd-src/external/bsd/openldap/dist/contrib/ldapc++/src/LDAPAttribute.cpp (revision 74809f044aa1c34df701bf79c7bfb9794896fdf8)
1 // $OpenLDAP: pkg/ldap/contrib/ldapc++/src/LDAPAttribute.cpp,v 1.6.10.2 2008/04/14 23:09:26 quanah Exp $
2 /*
3  * Copyright 2000-2007, OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 
7 
8 //TODO!!!
9 //  * some kind of iterator to step through the attribute values
10 //  * remove values from Attribute
11 //  * handling of subtypes (;de; and so on)
12 //  * some documentation
13 
14 
15 #include <ldap.h>
16 #include <cstdlib>
17 
18 #include "debug.h"
19 #include "StringList.h"
20 
21 #include "LDAPAttribute.h"
22 
23 using namespace std;
24 
25 LDAPAttribute::LDAPAttribute(){
26     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute( )" << endl);
27     m_name=string();
28 }
29 
30 LDAPAttribute::LDAPAttribute(const LDAPAttribute& attr){
31     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute(&)" << endl);
32     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
33             "   attr:" << attr << endl);
34 	m_name=attr.m_name;
35     m_values=StringList(attr.m_values);
36 }
37 
38 LDAPAttribute::LDAPAttribute(const string& name, const string& value){
39     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
40     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
41             "   name:" << name << endl << "   value:" << value << endl);
42     this->setName(name);
43     if(value != ""){
44     	this->addValue(value);
45     }
46 }
47 
48 
49 LDAPAttribute::LDAPAttribute(const string& name, const StringList& values){
50     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
51     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
52             "   name:" << name << endl);
53     m_name=name;
54     m_values=values;
55 }
56 
57 LDAPAttribute::LDAPAttribute(const char *name, char **values){
58     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
59     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
60             "   name:" << name << endl);
61 	this->setName(name);
62 	this->setValues(values);
63 }
64 
65 LDAPAttribute::LDAPAttribute(const char *name, BerValue **values){
66     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
67     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
68             "   name:" << name << endl);
69 	this->setName(name);
70 	this->setValues(values);
71 }
72 
73 LDAPAttribute::~LDAPAttribute(){
74     DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttribute::~LDAPAttribute()" << endl);
75 }
76 
77 void LDAPAttribute::addValue(const string& value){
78     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
79     m_values.add(value);
80 }
81 
82 int LDAPAttribute::addValue(const BerValue *value){
83     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
84 	if(value!=0){
85 		this->addValue(string(value->bv_val, value->bv_len));
86 		return 0;
87 	}
88 	return -1;
89 }
90 
91 int LDAPAttribute::setValues(char **values){
92     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
93 	if(values){
94         m_values.clear();
95         for( char **i=values; *i!=0; i++){
96             this->addValue(*i);
97         }
98     }
99     return 0;
100 }
101 
102 int LDAPAttribute::setValues(BerValue **values){
103     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
104     if(values){
105 	    m_values.clear();
106         for( BerValue **i=values; *i!=0; i++){
107             if( this->addValue(*i) ){
108                 return -1;
109             }
110         }
111     }
112 	return 0;
113 }
114 
115 void LDAPAttribute::setValues(const StringList& values){
116     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
117     m_values=values;
118 }
119 
120 const StringList& LDAPAttribute::getValues() const{
121     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getValues()" << endl);
122     return m_values;
123 }
124 
125 BerValue** LDAPAttribute::getBerValues() const{
126     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getBerValues()" << endl);
127     size_t size=m_values.size();
128     if (size == 0){
129         return 0;
130     }else{
131         BerValue **temp = (BerValue**) malloc(sizeof(BerValue*) * (size+1));
132         StringList::const_iterator i;
133         int p=0;
134 
135         for(i=m_values.begin(), p=0; i!=m_values.end(); i++,p++){
136             temp[p]=(BerValue*) malloc(sizeof(BerValue));
137             temp[p]->bv_len= i->size();
138             temp[p]->bv_val= (char*) malloc(sizeof(char) * (i->size()+1));
139             i->copy(temp[p]->bv_val,string::npos);
140         }
141         temp[size]=0;
142         return temp;
143     }
144 }
145 
146 int LDAPAttribute::getNumValues() const{
147     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getNumValues()" << endl);
148 	return m_values.size();
149 }
150 
151 const string& LDAPAttribute::getName() const {
152     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::getName()" << endl);
153 	return m_name;
154 }
155 
156 void LDAPAttribute::setName(const string& name){
157     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::setName()" << endl);
158     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   name:" << name << endl);
159     m_name.erase();
160     m_name=name;
161 }
162 
163 // The bin-FLAG of the mod_op  is always set to LDAP_MOD_BVALUES (0x80)
164 LDAPMod* LDAPAttribute::toLDAPMod() const {
165     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::toLDAPMod()" << endl);
166     LDAPMod* ret= (LDAPMod*) malloc(sizeof(LDAPMod));
167     ret->mod_op=LDAP_MOD_BVALUES;	//always assume binary-Values
168     ret->mod_type= (char*) malloc(sizeof(char) * (m_name.size()+1));
169     m_name.copy(ret->mod_type,string::npos);
170     ret->mod_type[m_name.size()]=0;
171     ret->mod_bvalues=this->getBerValues();
172     return ret;
173 }
174 
175 bool LDAPAttribute::isNotPrintable() const {
176     StringList::const_iterator i;
177     for(i=m_values.begin(); i!=m_values.end(); i++){
178 	size_t len = i->size();
179 	for(size_t j=0; j<len; j++){
180 	    if (! isprint( (i->data())[j] ) ){
181 		return true;
182 	    }
183 	}
184     }
185     return false;
186 }
187 
188 ostream& operator << (ostream& s, const LDAPAttribute& attr){
189     s << attr.m_name << "=";
190     StringList::const_iterator i;
191     if (attr.isNotPrintable()){
192 	    s << "NOT_PRINTABLE" ;
193     }else{
194 	for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
195 	    s << *i << " ";
196 	}
197     }
198 	return s;
199 }
200