3 * Copyright 2000-2011 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
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
19 #include "StringList.h"
21 #include "LDAPAttribute.h"
25 LDAPAttribute::LDAPAttribute(){
26 DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute( )" << endl);
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);
35 m_values=StringList(attr.m_values);
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);
44 this->addValue(value);
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);
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);
62 this->setValues(values);
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);
70 this->setValues(values);
73 LDAPAttribute::~LDAPAttribute(){
74 DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttribute::~LDAPAttribute()" << endl);
77 void LDAPAttribute::addValue(const string& value){
78 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
82 int LDAPAttribute::addValue(const BerValue *value){
83 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
85 this->addValue(string(value->bv_val, value->bv_len));
91 int LDAPAttribute::setValues(char **values){
92 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
95 for( char **i=values; *i!=0; i++){
102 int LDAPAttribute::setValues(BerValue **values){
103 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
106 for( BerValue **i=values; *i!=0; i++){
107 if( this->addValue(*i) ){
115 void LDAPAttribute::setValues(const StringList& values){
116 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
120 const StringList& LDAPAttribute::getValues() const{
121 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getValues()" << endl);
125 BerValue** LDAPAttribute::getBerValues() const{
126 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getBerValues()" << endl);
127 size_t size=m_values.size();
131 BerValue **temp = (BerValue**) malloc(sizeof(BerValue*) * (size+1));
132 StringList::const_iterator i;
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);
146 int LDAPAttribute::getNumValues() const{
147 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getNumValues()" << endl);
148 return m_values.size();
151 const string& LDAPAttribute::getName() const {
152 DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::getName()" << endl);
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);
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();
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] ) ){
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" ;
194 for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){