summaryrefslogtreecommitdiffstats
path: root/contrib/ldapc++/src/LDAPAttribute.h
blob: e9781373802d1634750703bb0b26f688cd6dfda5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// $OpenLDAP$
/*
 * Copyright 2000-2022 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */


#ifndef LDAP_ATTRIBUTE_H
#define LDAP_ATTRIBUTE_H

#include<iostream>
#include<string>
#include<ldap.h>
#include<lber.h> 

#include <StringList.h>

/**
 * Represents the name an value(s) of an Attribute 
 */
class LDAPAttribute{
    public :
        /** 
         * Default constructor.
         * initializes an empty object.
         */
        LDAPAttribute();

        /**
         * Copy constructor.
         * Copies all values of an Attribute to a new one
         * @param attr   The Attribute that should be copied
         */
        LDAPAttribute(const LDAPAttribute& attr);

        /**
         * Construct an Attribute with a single string value
         * @param name      The attribute's name (type)
         * @param value     The string value of the attribute, if "" the
         *                  attribute will have no values, for LDAPv3 
         *                  this values must be UTF-8 encoded
         */
        LDAPAttribute(const std::string& name, const std::string& value="");

        /** 
         * Construct an attribute with multiple string values
         * @param name      The attribute's name (type)
         * @param values    A 0-terminated array of char*. Each char* specifies
         *                  one value of the attribute (UTF-8 encoded)
         */
        LDAPAttribute(const char* name, char **values);
        
        /** 
         * Construct an attribute with multiple string values
         * @param name      The attribute's name (type)
         * @param values    A list of strings. Each element specifies
         *                  one value of the attribute (UTF-8 or binary 
         *                  encoded)
         */
        LDAPAttribute(const std::string& name, const StringList& values);

        /**
         * Construct an attribute with multiple binary coded values
         * @param name      The attribute's name (type)
         * @param values    0-terminated array of binary attribute values
         *                  The BerValue struct is declared as:<BR>
         *                  struct berval{
         *                      unsigned long bv_len;
         *                      char *bv_val;
         *                  } BerValue;
         */         
        LDAPAttribute(const char* name, BerValue **values);
        
        /**
         * Destructor
         */
        ~LDAPAttribute();

        /**
         * Add a single string value(bin/char) to the Attribute
         * @param value Value that should be added, it is copied inside the
         *              object
         */
        void addValue(const std::string& value);

        /**
         * Add a single binary value to the Attribute
         * @param value The binary coded value that should be added to the
         *              Attribute.
         * @return  0  no problem <BR>
         *          -1 failure (mem. allocation problem)
         */
        int addValue(const BerValue *value);

        /**
         * Set the values of the attribute. If the object contains some values
         * already, they are deleted
         * @param values    0-terminated array of char*, each char* 
         *                  representing a string value to add to the entry
         * 
         * @return  0  no problem <BR>
         *          -1 failure (mem. allocation problem)
         */
        int setValues(char** values);

        /**
         * Set the values of the attribute. If the object does already contain
         * some values, they will be deleted
         * @param values    0-terminated array of BerValue*, each BerValue
         *                  representing a binary value to add to the entry
         * 
         * @return  0  no problem <BR>
         *          -1 failure (mem. allocation problem)
         */
        int setValues(BerValue** values);

        /**
         * Set the values of the attribute. If the object does already contain
         * some values, they will be deleted
         * @param values    A list of string-Objects. Each string is 
         *                  representing a string or binary value to add to
         *                  the entry
         */
        void setValues(const StringList& values); 

        /**
         * For internal use only.
         * This method is used to translate the values of the Attribute to
         * 0-terminated Array of BerValue-structs as used by the C-API
         * @return  The Values of the Attribute as an 0-terminated Array of 
         *          BerValue* (is dynamically allocated, delete it after usage) 
         *          <BR>
         *          0-pointer in case of error
         */
        BerValue** getBerValues() const;

        /**
         * @return The values of the array as a list of strings
         */
        const StringList& getValues() const;
        
        /**
         * @return The number of values of the attribute
         */
        int getNumValues() const;

        /**
         * @return The name(type) of the attribute
         */
        const std::string& getName() const ;

        /**
         * Sets the Attribute's name (type) 
         * @param the new name of the object  
         */
        void setName(const std::string& name);

        /**
         * For internal use only.
         *
         * This method translate the attribute of the object into a
         * LDAPMod-Structure as used by the C-API
         */
        LDAPMod* toLDAPMod() const ;

        /**
         * @return true If the attribute contains non-printable attributes
         */
        bool isNotPrintable() const ;

    private :
        std::string m_name;
        StringList m_values;

    /**
     * This method can be used to dump the data of a LDAPResult-Object.
     * It is only useful for debugging purposes at the moment
     */
    friend std::ostream& operator << (std::ostream& s, const LDAPAttribute& attr);
};
#endif //#ifndef LDAP_ATTRIBUTE_H