]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPAttribute.cpp
Entry rwlock is no longer needed as concurrency is managed
[openldap] / contrib / ldapc++ / src / LDAPAttribute.cpp
1 /*
2  * Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6
7 //TODO!!!
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
12
13
14 #include <ldap.h> 
15 //#include <ctype.h>
16
17 #include "debug.h"
18 #include "StringList.h"
19
20 #include "LDAPAttribute.h"
21
22 using namespace std;
23
24 LDAPAttribute::LDAPAttribute(){
25     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute( )" << endl);
26     m_name=string();
27 }
28
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);
33         m_name=attr.m_name;
34     m_values=StringList(attr.m_values);
35 }
36
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);
41     this->setName(name);
42     if(value != ""){
43         this->addValue(value);
44     }
45 }
46
47
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);
52     m_name=name;
53     m_values=values;
54 }
55
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);
60         this->setName(name);
61         this->setValues(values);
62 }
63
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);
68         this->setName(name);
69         this->setValues(values);
70 }
71
72 LDAPAttribute::~LDAPAttribute(){
73     DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttribute::~LDAPAttribute()" << endl);
74 }
75
76 void LDAPAttribute::addValue(const string& value){
77     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
78     m_values.add(value);
79 }
80
81 int LDAPAttribute::addValue(const BerValue *value){
82     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
83         if(value!=0){
84                 this->addValue(string(value->bv_val, value->bv_len));
85                 return 0;
86         }
87         return -1;
88 }
89
90 int LDAPAttribute::setValues(char **values){
91     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
92         if(values){
93         m_values.clear();
94         for( char **i=values; *i!=0; i++){
95             this->addValue(*i);
96         }
97     }
98     return 0;
99 }
100
101 int LDAPAttribute::setValues(BerValue **values){
102     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
103     if(values){
104             m_values.clear();
105         for( BerValue **i=values; *i!=0; i++){
106             if( this->addValue(*i) ){
107                 return -1;
108             }
109         }
110     }
111         return 0;
112 }
113
114 void LDAPAttribute::setValues(const StringList& values){
115     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
116     m_values=values;
117 }
118
119 const StringList& LDAPAttribute::getValues() const{
120     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getValues()" << endl);
121     return m_values;
122 }
123
124 BerValue** LDAPAttribute::getBerValues() const{
125     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getBerValues()" << endl);
126     size_t size=m_values.size();
127     if (size == 0){
128         return 0;
129     }else{
130         BerValue **temp = (BerValue**) malloc(sizeof(BerValue*) * (size+1));
131         StringList::const_iterator i;
132         int p=0;
133
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);
139         }
140         temp[size]=0;
141         return temp;
142     }
143 }
144
145 int LDAPAttribute::getNumValues() const{
146     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getNumValues()" << endl);
147         return m_values.size();
148 }
149
150 const string& LDAPAttribute::getName() const {
151     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::getName()" << endl);
152         return m_name;
153 }
154
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);
158     m_name.erase();
159     m_name=name;
160 }
161
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();
171     return ret;
172 }
173
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] ) ){
180                 return true;
181             }
182         }
183     }
184     return false;
185 }
186
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" ;
192     }else{
193         for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
194             s << *i << " ";
195         }
196     }
197         return s;
198 }