2 * Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 // $Id: LDAPAttribute.cpp,v 1.3 2000/08/31 17:43:48 rhafer Exp $
9 // * Spend some thoughts about binary attributes
10 // * handling of subtypes (;de; and so on)
11 // * For binary attributes use one of the other constructors (provided later )
12 // * creatind LDAPAttributes from the CAPI-structures.
13 // * Defining return values and error codes
14 // * some documentation
17 #include <ac/string.h>
19 #include "LDAPAttribute.h"
23 LDAPAttribute::LDAPAttribute(const LDAPAttribute& attr){
24 this->setName(attr.m_name);
25 ValueList::const_iterator i;
26 for (i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
27 this->m_values.push_back(ber_bvdup(*i));
31 //This Constructor expects the parameter value to be either UTF-8 encoded
32 // (for LDAPv3) or T.61 encoded (for LDAPv2).
33 LDAPAttribute::LDAPAttribute(const char *name=0, const char *value=0){
35 this->addValue(value);
38 LDAPAttribute::LDAPAttribute(const char *name, char **values){
40 this->setValues(values);
44 LDAPAttribute::LDAPAttribute(const char *name, BerValue **values){
46 this->setValues(values);
49 LDAPAttribute::~LDAPAttribute(){
51 ValueList::const_iterator i;
52 for(i=m_values.begin(); i!=m_values.end(); i++){
58 int LDAPAttribute::addValue(const char *value){
60 BerValue *berval=new BerValue;
61 berval->bv_len=strlen(value);
62 berval->bv_val=strdup(value);
63 m_values.push_back(berval);
69 int LDAPAttribute::addValue(const BerValue *value){
71 m_values.push_back(ber_bvdup(value));
77 int LDAPAttribute::setValues(char **values){
78 ValueList::const_iterator i;
79 for(i=m_values.begin(); i!=m_values.end(); i++){
83 for( char **i=values; *i!=0; i++){
89 int LDAPAttribute::setValues(BerValue **values){
90 ValueList::const_iterator i;
91 for(i=m_values.begin(); i!=m_values.end(); i++){
95 for( BerValue **i=values; *i!=0; i++){
101 BerValue** LDAPAttribute::getValues() const{
102 size_t size=m_values.size();
103 BerValue **temp = new BerValue*[size+1];
104 ValueList::const_iterator i;
107 for(i=m_values.begin(), p=0; i!=m_values.end(); i++,p++){
108 temp[p]=ber_bvdup( (*i) );
114 int LDAPAttribute::getNumValues() const{
115 return m_values.size();
118 char* LDAPAttribute::getName(){
119 return strdup(m_name);
122 int LDAPAttribute::setName(const char *name){
129 // The bin-FLAG of the mod_op is always set to LDAP_MOD_BVALUES (0x80)
130 LDAPMod* LDAPAttribute::toLDAPMod() const {
131 LDAPMod* ret=new LDAPMod();
132 ret->mod_op=LDAP_MOD_BVALUES; //alway asume binary-Values
133 ret->mod_type=strdup(m_name);
134 ret->mod_bvalues=this->getValues();
138 bool LDAPAttribute::isNotPrintable() const {
139 ValueList::const_iterator i;
140 for(i=m_values.begin(); i!=m_values.end(); i++){
141 ber_len_t len=(*i)->bv_len;
142 for(ber_len_t j=0; j<len; j++){
143 if (! isprint( (*i)->bv_val[j] ) ){
151 ostream& operator << (ostream& s, const LDAPAttribute& attr){
152 s << attr.m_name << "=";
153 ValueList::const_iterator i;
154 if (attr.isNotPrintable()){
155 s << "NOT_PRINTABLE" ;
157 for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
158 s << (*i)->bv_val << " ";