Back to index

openldap  2.4.31
LDAPAttribute.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 
00008 //TODO!!!
00009 //  * some kind of iterator to step through the attribute values
00010 //  * remove values from Attribute
00011 //  * handling of subtypes (;de; and so on)
00012 //  * some documentation
00013 
00014 
00015 #include <ldap.h> 
00016 #include <cstdlib>
00017 
00018 #include "debug.h"
00019 #include "StringList.h"
00020 
00021 #include "LDAPAttribute.h"
00022 
00023 using namespace std;
00024 
00025 LDAPAttribute::LDAPAttribute(){
00026     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute( )" << endl);
00027     m_name=string();
00028 }
00029 
00030 LDAPAttribute::LDAPAttribute(const LDAPAttribute& attr){
00031     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute(&)" << endl);
00032     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
00033             "   attr:" << attr << endl);
00034        m_name=attr.m_name;
00035     m_values=StringList(attr.m_values);
00036 }
00037 
00038 LDAPAttribute::LDAPAttribute(const string& name, const string& value){
00039     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
00040     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
00041             "   name:" << name << endl << "   value:" << value << endl);
00042     this->setName(name);
00043     if(value != ""){
00044        this->addValue(value);
00045     }
00046 }
00047 
00048 
00049 LDAPAttribute::LDAPAttribute(const string& name, const StringList& values){
00050     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
00051     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
00052             "   name:" << name << endl);
00053     m_name=name;
00054     m_values=values;
00055 }
00056 
00057 LDAPAttribute::LDAPAttribute(const char *name, char **values){
00058     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
00059     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
00060             "   name:" << name << endl);
00061        this->setName(name);
00062        this->setValues(values);
00063 }
00064 
00065 LDAPAttribute::LDAPAttribute(const char *name, BerValue **values){
00066     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
00067     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
00068             "   name:" << name << endl);
00069        this->setName(name);
00070        this->setValues(values);
00071 }
00072 
00073 LDAPAttribute::~LDAPAttribute(){
00074     DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttribute::~LDAPAttribute()" << endl);
00075 }
00076 
00077 void LDAPAttribute::addValue(const string& value){
00078     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
00079     m_values.add(value);
00080 }
00081 
00082 int LDAPAttribute::addValue(const BerValue *value){
00083     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
00084        if(value!=0){
00085               this->addValue(string(value->bv_val, value->bv_len));
00086               return 0;
00087        }
00088        return -1;
00089 }
00090 
00091 int LDAPAttribute::setValues(char **values){
00092     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
00093        if(values){
00094         m_values.clear();
00095         for( char **i=values; *i!=0; i++){
00096             this->addValue(*i);
00097         }
00098     }
00099     return 0;
00100 }
00101 
00102 int LDAPAttribute::setValues(BerValue **values){
00103     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
00104     if(values){
00105            m_values.clear();
00106         for( BerValue **i=values; *i!=0; i++){
00107             if( this->addValue(*i) ){
00108                 return -1;
00109             }
00110         }
00111     }
00112        return 0;
00113 }
00114 
00115 void LDAPAttribute::setValues(const StringList& values){
00116     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
00117     m_values=values;
00118 }
00119 
00120 const StringList& LDAPAttribute::getValues() const{
00121     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getValues()" << endl);
00122     return m_values;
00123 }
00124 
00125 BerValue** LDAPAttribute::getBerValues() const{
00126     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getBerValues()" << endl);
00127     size_t size=m_values.size();
00128     if (size == 0){
00129         return 0;
00130     }else{
00131         BerValue **temp = (BerValue**) malloc(sizeof(BerValue*) * (size+1));
00132         StringList::const_iterator i;
00133         int p=0;
00134 
00135         for(i=m_values.begin(), p=0; i!=m_values.end(); i++,p++){
00136             temp[p]=(BerValue*) malloc(sizeof(BerValue));
00137             temp[p]->bv_len= i->size();
00138             temp[p]->bv_val= (char*) malloc(sizeof(char) * (i->size()+1));
00139             i->copy(temp[p]->bv_val,string::npos);
00140         }
00141         temp[size]=0;
00142         return temp;
00143     }
00144 }
00145 
00146 int LDAPAttribute::getNumValues() const{
00147     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getNumValues()" << endl);
00148        return m_values.size();
00149 }
00150 
00151 const string& LDAPAttribute::getName() const {
00152     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::getName()" << endl);
00153        return m_name;
00154 }
00155 
00156 void LDAPAttribute::setName(const string& name){
00157     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::setName()" << endl);
00158     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   name:" << name << endl);
00159     m_name.erase();
00160     m_name=name;
00161 }
00162 
00163 // The bin-FLAG of the mod_op  is always set to LDAP_MOD_BVALUES (0x80) 
00164 LDAPMod* LDAPAttribute::toLDAPMod() const {
00165     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::toLDAPMod()" << endl);
00166     LDAPMod* ret= (LDAPMod*) malloc(sizeof(LDAPMod));
00167     ret->mod_op=LDAP_MOD_BVALUES;  //always assume binary-Values
00168     ret->mod_type= (char*) malloc(sizeof(char) * (m_name.size()+1));
00169     m_name.copy(ret->mod_type,string::npos);
00170     ret->mod_type[m_name.size()]=0;
00171     ret->mod_bvalues=this->getBerValues();
00172     return ret;
00173 }
00174 
00175 bool LDAPAttribute::isNotPrintable() const {
00176     StringList::const_iterator i;
00177     for(i=m_values.begin(); i!=m_values.end(); i++){
00178        size_t len = i->size();
00179        for(size_t j=0; j<len; j++){
00180            if (! isprint( (i->data())[j] ) ){
00181               return true;
00182            }
00183        }
00184     }
00185     return false;
00186 }
00187 
00188 ostream& operator << (ostream& s, const LDAPAttribute& attr){
00189     s << attr.m_name << "=";
00190     StringList::const_iterator i;
00191     if (attr.isNotPrintable()){
00192            s << "NOT_PRINTABLE" ;
00193     }else{
00194        for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
00195            s << *i << " ";
00196        }
00197     }
00198        return s;
00199 }