2 * Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 // * some kind of iterator to step through the attribute values
9 // * remove values from Attribute
10 // * handling of subtypes (;de; and so on)
11 // * some documentation
18 #include "StringList.h"
20 #include "LDAPAttribute.h"
24 LDAPAttribute::LDAPAttribute(){
25 DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute( )" << endl);
29 LDAPAttribute::LDAPAttribute(const LDAPAttribute& attr){
30 DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute(&)" << endl);
31 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
32 " attr:" << attr << endl);
34 m_values=StringList(attr.m_values);
37 LDAPAttribute::LDAPAttribute(const string& name, const string& value){
38 DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
39 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
40 " name:" << name << endl << " value:" << value << endl);
43 this->addValue(value);
48 LDAPAttribute::LDAPAttribute(const string& name, const StringList& values){
49 DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
50 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
51 " name:" << name << endl);
56 LDAPAttribute::LDAPAttribute(const char *name, char **values){
57 DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
58 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
59 " name:" << name << endl);
61 this->setValues(values);
64 LDAPAttribute::LDAPAttribute(const char *name, BerValue **values){
65 DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
66 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
67 " name:" << name << endl);
69 this->setValues(values);
72 LDAPAttribute::~LDAPAttribute(){
73 DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttribute::~LDAPAttribute()" << endl);
76 void LDAPAttribute::addValue(const string& value){
77 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
81 int LDAPAttribute::addValue(const BerValue *value){
82 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
84 this->addValue(string(value->bv_val, value->bv_len));
90 int LDAPAttribute::setValues(char **values){
91 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
94 for( char **i=values; *i!=0; i++){
101 int LDAPAttribute::setValues(BerValue **values){
102 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
105 for( BerValue **i=values; *i!=0; i++){
106 if( this->addValue(*i) ){
114 void LDAPAttribute::setValues(const StringList& values){
115 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
119 const StringList& LDAPAttribute::getValues() const{
120 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getValues()" << endl);
124 BerValue** LDAPAttribute::getBerValues() const{
125 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getBerValues()" << endl);
126 size_t size=m_values.size();
130 BerValue **temp = (BerValue**) malloc(sizeof(BerValue*) * (size+1));
131 StringList::const_iterator i;
134 for(i=m_values.begin(), p=0; i!=m_values.end(); i++,p++){
135 temp[p]=(BerValue*) malloc(sizeof(BerValue));
136 temp[p]->bv_len= i->size();
137 temp[p]->bv_val= (char*) malloc(sizeof(char) * (i->size()+1));
138 i->copy(temp[p]->bv_val,string::npos);
145 int LDAPAttribute::getNumValues() const{
146 DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getNumValues()" << endl);
147 return m_values.size();
150 const string& LDAPAttribute::getName() const {
151 DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::getName()" << endl);
155 void LDAPAttribute::setName(const string& name){
156 DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::setName()" << endl);
157 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," name:" << name << endl);
162 // The bin-FLAG of the mod_op is always set to LDAP_MOD_BVALUES (0x80)
163 LDAPMod* LDAPAttribute::toLDAPMod() const {
164 DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::toLDAPMod()" << endl);
165 LDAPMod* ret= (LDAPMod*) malloc(sizeof(LDAPMod));
166 ret->mod_op=LDAP_MOD_BVALUES; //always assume binary-Values
167 ret->mod_type= (char*) malloc(sizeof(char) * (m_name.size()+1));
168 m_name.copy(ret->mod_type,string::npos);
169 ret->mod_type[m_name.size()]=0;
170 ret->mod_bvalues=this->getBerValues();
174 bool LDAPAttribute::isNotPrintable() const {
175 StringList::const_iterator i;
176 for(i=m_values.begin(); i!=m_values.end(); i++){
177 size_t len = i->size();
178 for(size_t j=0; j<len; j++){
179 if (! isprint( (i->data())[j] ) ){
187 ostream& operator << (ostream& s, const LDAPAttribute& attr){
188 s << attr.m_name << "=";
189 StringList::const_iterator i;
190 if (attr.isNotPrintable()){
191 s << "NOT_PRINTABLE" ;
193 for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){