]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPAttribute.cpp
Initial check of the LDAP C++ SDK written by Ralf Haferkamp <rhafer@suse.de>
[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 // $Id: LDAPAttribute.cpp,v 1.3 2000/08/31 17:43:48 rhafer Exp $
7
8 //TODO!!!
9 //  * Spend some thoughts about binary attributes
10 //  * handling of subtypes (;de; and so on)
11 //  * For binary attributes use one of the other constructors (provided later )
12 //  * creatind LDAPAttributes from the CAPI-structures.
13 //  * Defining return values and error codes
14 //  * some documentation
15
16 #include <ldap.h> 
17 #include <ac/string.h>
18 #include <ctype.h>
19 #include "LDAPAttribute.h"
20
21
22 //Copy-constructor
23 LDAPAttribute::LDAPAttribute(const LDAPAttribute& attr){
24         this->setName(attr.m_name);
25         ValueList::const_iterator i;
26         for (i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
27                 this->m_values.push_back(ber_bvdup(*i));
28         }
29 }
30
31 //This Constructor expects the parameter value to be either UTF-8 encoded
32 // (for LDAPv3) or T.61 encoded (for LDAPv2).
33 LDAPAttribute::LDAPAttribute(const char *name=0, const char *value=0){
34         this->setName(name);
35         this->addValue(value);
36 }
37
38 LDAPAttribute::LDAPAttribute(const char *name, char **values){
39         this->setName(name);
40         this->setValues(values);
41 }
42
43
44 LDAPAttribute::LDAPAttribute(const char *name, BerValue **values){
45         this->setName(name);
46         this->setValues(values);
47 }
48
49 LDAPAttribute::~LDAPAttribute(){
50         delete[] m_name;
51         ValueList::const_iterator i;
52         for(i=m_values.begin(); i!=m_values.end(); i++){
53                 ber_bvfree(*i);
54         }
55         m_values.clear();
56 }
57
58 int LDAPAttribute::addValue(const char *value){
59         if(value!=0){
60                 BerValue *berval=new BerValue;
61                 berval->bv_len=strlen(value);
62                 berval->bv_val=strdup(value);
63                 m_values.push_back(berval);
64                 return 0;
65         }
66         return -1;
67 }
68
69 int LDAPAttribute::addValue(const BerValue *value){
70         if(value!=0){
71                 m_values.push_back(ber_bvdup(value));
72                 return 0;
73         }
74         return -1;
75 }
76
77 int LDAPAttribute::setValues(char **values){
78         ValueList::const_iterator i;
79         for(i=m_values.begin(); i!=m_values.end(); i++){
80                 delete[](*i);
81         }
82         m_values.clear();
83         for( char **i=values; *i!=0; i++){
84                 this->addValue(*i);
85         }
86         return 0;
87 }
88
89 int LDAPAttribute::setValues(BerValue **values){
90         ValueList::const_iterator i;
91         for(i=m_values.begin(); i!=m_values.end(); i++){
92                 delete[](*i);
93         }
94         m_values.clear();
95         for( BerValue **i=values; *i!=0; i++){
96                 this->addValue(*i);
97         }
98         return 0;
99 }
100         
101 BerValue** LDAPAttribute::getValues() const{
102         size_t size=m_values.size();
103         BerValue **temp = new BerValue*[size+1];
104         ValueList::const_iterator i;
105         int p;
106
107         for(i=m_values.begin(), p=0; i!=m_values.end(); i++,p++){
108                 temp[p]=ber_bvdup( (*i) );
109         }
110         temp[size]=0;
111         return temp;
112 }
113
114 int LDAPAttribute::getNumValues() const{
115         return m_values.size();
116 }
117
118 char* LDAPAttribute::getName(){
119         return strdup(m_name);
120 }
121
122 int LDAPAttribute::setName(const char *name){
123         if (name!=0){
124                 m_name=strdup(name);
125         }
126         return 0;
127 }
128
129 // The bin-FLAG of the mod_op  is always set to LDAP_MOD_BVALUES (0x80) 
130 LDAPMod* LDAPAttribute::toLDAPMod() const {
131         LDAPMod* ret=new LDAPMod();
132         ret->mod_op=LDAP_MOD_BVALUES;   //alway asume binary-Values
133         ret->mod_type=strdup(m_name);
134         ret->mod_bvalues=this->getValues();
135         return ret;
136 }
137
138 bool LDAPAttribute::isNotPrintable() const {
139         ValueList::const_iterator i;
140         for(i=m_values.begin(); i!=m_values.end(); i++){
141                 ber_len_t len=(*i)->bv_len;
142                 for(ber_len_t j=0; j<len; j++){
143                         if (! isprint( (*i)->bv_val[j] ) ){
144                                 return true;
145                         }
146                 }
147         }
148         return false;
149 }
150
151 ostream& operator << (ostream& s, const LDAPAttribute& attr){
152         s << attr.m_name << "=";
153         ValueList::const_iterator i;
154         if (attr.isNotPrintable()){
155                 s << "NOT_PRINTABLE" ;
156         }else{
157                 for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
158                         s << (*i)->bv_val << " ";
159                 }
160         }
161         return s;
162 }