]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPAttribute.cpp
- removed char* in favour of string
[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
23 LDAPAttribute::LDAPAttribute(){
24     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute( )" << endl);
25     m_name=string();
26 }
27
28 LDAPAttribute::LDAPAttribute(const LDAPAttribute& attr){
29     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute(&)" << endl);
30     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
31             "   attr:" << attr << endl);
32         m_name=attr.m_name;
33     m_values=StringList(attr.m_values);
34 }
35
36 LDAPAttribute::LDAPAttribute(const string& name, const string& value){
37     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
38     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
39             "   name:" << name << endl << "   value:" << value << endl);
40     this->setName(name);
41     if(value != ""){
42         this->addValue(value);
43     }
44 }
45
46
47 LDAPAttribute::LDAPAttribute(const string& name, const StringList& values){
48     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
49     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
50             "   name:" << name << endl);
51     m_name=name;
52     m_values=values;
53 }
54
55 LDAPAttribute::LDAPAttribute(const char *name, char **values){
56     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
57     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
58             "   name:" << name << endl);
59         this->setName(name);
60         this->setValues(values);
61 }
62
63 LDAPAttribute::LDAPAttribute(const char *name, BerValue **values){
64     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
65     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
66             "   name:" << name << endl);
67         this->setName(name);
68         this->setValues(values);
69 }
70
71 LDAPAttribute::~LDAPAttribute(){
72     DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttribute::~LDAPAttribute()" << endl);
73 }
74
75 void LDAPAttribute::addValue(const string& value){
76     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
77     m_values.add(value);
78 }
79
80 int LDAPAttribute::addValue(const BerValue *value){
81     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
82         if(value!=0){
83                 this->addValue(string(value->bv_val, value->bv_len));
84                 return 0;
85         }
86         return -1;
87 }
88
89 int LDAPAttribute::setValues(char **values){
90     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
91         if(values){
92         m_values.clear();
93         for( char **i=values; *i!=0; i++){
94             this->addValue(*i);
95         }
96     }
97     return 0;
98 }
99
100 int LDAPAttribute::setValues(BerValue **values){
101     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
102     if(values){
103             m_values.clear();
104         for( BerValue **i=values; *i!=0; i++){
105             if( this->addValue(*i) ){
106                 return -1;
107             }
108         }
109     }
110         return 0;
111 }
112
113 void LDAPAttribute::setValues(const StringList& values){
114     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
115     m_values=values;
116 }
117
118 const StringList& LDAPAttribute::getValues() const{
119     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getValues()" << endl);
120     return m_values;
121 }
122
123 BerValue** LDAPAttribute::getBerValues() const{
124     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getBerValues()" << endl);
125         size_t size=m_values.size();
126     if (size == 0){
127         return 0;
128     }else{
129         BerValue **temp = new BerValue*[size+1];
130         StringList::const_iterator i;
131         int p=0;
132
133         for(i=m_values.begin(), p=0; i!=m_values.end(); i++,p++){
134             temp[p]=new BerValue;
135             temp[p]->bv_len= i->size();
136             temp[p]->bv_val= new char[i->size()+1];
137             i->copy(temp[p]->bv_val,string::npos);
138         }
139         temp[size]=0;
140         return temp;
141     }
142 }
143
144 int LDAPAttribute::getNumValues() const{
145     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getNumValues()" << endl);
146         return m_values.size();
147 }
148
149 const string& LDAPAttribute::getName() const {
150     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::getName()" << endl);
151         return m_name;
152 }
153
154 void LDAPAttribute::setName(const string& name){
155     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::setName()" << endl);
156     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   name:" << name << endl);
157     m_name.erase();
158     m_name=name;
159 }
160
161 // The bin-FLAG of the mod_op  is always set to LDAP_MOD_BVALUES (0x80) 
162 LDAPMod* LDAPAttribute::toLDAPMod() const {
163     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::toLDAPMod()" << endl);
164         LDAPMod* ret=new LDAPMod();
165         ret->mod_op=LDAP_MOD_BVALUES;   //always assume binary-Values
166         ret->mod_type= new char[m_name.size()+1];
167     m_name.copy(ret->mod_type,string::npos);
168     cerr << "                         " << m_name<< endl;
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 }