Statistics
| Branch: | Tag: | Revision:

one / include / NebulaUtil.h @ 202b47e9

History | View | Annotate | Download (8.06 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#ifndef _NEBULA_UTIL_H_
18
#define _NEBULA_UTIL_H_
19

    
20
#include <string>
21
#include <sstream>
22
#include <vector>
23
#include <set>
24
#include <algorithm>
25

    
26
#include <openssl/crypto.h>
27

    
28
namespace one_util
29
{
30
    std::string& toupper(std::string& st);
31

    
32
    std::string& tolower(std::string& st);
33

    
34
    std::string log_time(time_t the_time);
35

    
36
    std::string log_time();
37

    
38
    /**
39
     *  sha1 digest
40
     *  @param in the string to be hashed
41
     *  @return sha1 hash of str
42
     */
43
    std::string sha1_digest(const std::string& in);
44

    
45
   /**
46
    *  Base 64 encoding
47
    *    @param in the string to encoded
48
    *    @return a pointer to the encoded string (must be freed) or 0 in case of
49
    *    error
50
    */
51
    std::string * base64_encode(const std::string& in);
52

    
53
   /**
54
    *  Base 64 decoding
55
    *    @param in the string to decode
56
    *    @return a pointer to the decoded string (must be freed) or 0 in case of
57
    *    error
58
    */
59
    std::string * base64_decode(const std::string& in);
60

    
61
   /**
62
    *  AES256 encryption
63
    *    @param in the string to encrypt
64
    *    @param password to encrypt data
65
    *    @return a pointer to the encrypted string (must be freed) or 0 in case of
66
    *    error
67
    */
68
    std::string * aes256cbc_encrypt(const std::string& in, const std::string password);
69

    
70
    /**
71
     *  Creates a random number, using time(0) as seed, and performs an sha1 hash
72
     *    @return a new random password
73
     */
74
    std::string random_password();
75

    
76
    /**
77
     * Splits a string, using the given delimiter
78
     *
79
     * @param st string to split
80
     * @param delim delimiter character
81
     * @param clean_empty true to clean empty split parts.
82
     *  Example for st "a::b:c"
83
     *      clean_empty true will return ["a", "b", "c"]
84
     *      clean_empty fase will return ["a", "", "b", "c"]
85
     *
86
     * @return a vector containing the resulting substrings
87
     */
88
    std::vector<std::string> split(
89
            const std::string& st,
90
            char delim,
91
            bool clean_empty=true);
92

    
93
    /**
94
     * Splits a string, using the given delimiter
95
     *
96
     * @param st string to split
97
     * @param delim delimiter character
98
     * @param result where the result will be saved
99
     */
100
    template <class T>
101
    void split_unique(const std::string& st, char delim, std::set<T>& result)
102
    {
103
        T elem;
104
        std::vector<std::string>::const_iterator it;
105

    
106
        std::vector<std::string> strings = split(st, delim, true);
107

    
108
        for (it = strings.begin(); it != strings.end(); it++)
109
        {
110
            std::istringstream iss(*it);
111
            iss >> elem;
112

    
113
            if ( iss.fail() )
114
            {
115
                continue;
116
            }
117

    
118
            result.insert(elem);
119
        }
120
    }
121

    
122
    /**
123
     * Explicit specialization for strings
124
     */
125
    template <>
126
    void split_unique(const std::string& st, char delim,
127
            std::set<std::string>& result);
128

    
129
    /**
130
     * Joins the given element with the delimiter
131
     *
132
     * @param first iterator
133
     * @param last iterator
134
     * @param delim delimiter character
135
     * @return the joined strings
136
     */
137
    template <class Iterator>
138
    std::string join(Iterator first, Iterator last, char delim)
139
    {
140
        std::ostringstream oss;
141

    
142
        for(Iterator it = first; it != last; it++)
143
        {
144
            if (it != first)
145
            {
146
                oss << delim;
147
            }
148

    
149
            oss << *it;
150
        }
151

    
152
        return oss.str();
153
    }
154

    
155
    /**
156
     * Joins the given element with the delimiter
157
     *
158
     * @param values set of values
159
     * @param delim delimiter character
160
     * @return the joined strings
161
     */
162
    template <class T>
163
    std::string join(const std::set<T> values, char delim)
164
    {
165
        return join(values.begin(), values.end(), delim);
166
    }
167

    
168
    /**
169
     * Creates a string from the given float, using fixed notation. If the
170
     * number has any decimals, they will be truncated to 2.
171
     *
172
     * @param num
173
     * @return
174
     */
175
    std::string float_to_str(const float &num);
176

    
177
    /**
178
     *  Returns a scaped version of a value in the from "<op><val><cl>"
179
     *    @param v the value to be escaped
180
     *    @param op the opening escape string
181
     *    @param cl the closing escape string
182
     */
183
    template <typename ValueType> inline
184
    std::string escape(const ValueType& v, const char * op, const char * cl)
185
    {
186
        std::ostringstream oss;
187

    
188
        oss << op << v << cl;
189

    
190
        return oss.str();
191
    }
192

    
193
    template <typename ValueType> inline
194
    std::string escape_xml(const ValueType &v)
195
    {
196
        return escape(v, "<![CDATA[", "]]>");
197
    }
198

    
199
    template <typename ValueType> inline
200
    std::string escape_xml_attr(const ValueType &v)
201
    {
202
        return escape(v, "'", "'");
203
    }
204
    /**
205
     * Checks if a strings matches a regular expression
206
     *
207
     * @param pattern PCRE extended pattern
208
     * @param subject the string to test
209
     * @return 0 on match, another value otherwise
210
     */
211
    int regex_match(const char *pattern, const char *subject);
212

    
213
    /**
214
     * Trim an string using the isspace function
215
     * @param the string
216
     * @return trimed string
217
     */
218
    std::string trim(const std::string& str);
219

    
220
    /**
221
     * Returns a copy of st with the all occurrences of "find" substituted
222
     * for "replacement"
223
     * @param st string input
224
     * @param sfind string to search for
225
     * @param replacement string to replace occurrences with
226
     * @return a string copy
227
     */
228
    std::string gsub(const std::string& st, const std::string& sfind,
229
            const std::string& replacement);
230

    
231
    template <class T>
232
    std::set<T> set_intersection(const std::set<T> &first, const std::set<T> &second)
233
    {
234
        std::set<T> output;
235

    
236
        std::set_intersection(
237
                first.begin(), first.end(), second.begin(), second.end(),
238
                std::inserter(output, output.begin()));
239

    
240
        return output;
241
    }
242

    
243
        /**
244
     *  Compress the input string unsing zlib
245
     *    @param in input string
246
     *    @param bool64 true to base64 encode output
247
     *    @return pointer to the compressed sting (must be freed) or 0 in case
248
     *    of error
249
     */
250
        std::string * zlib_compress(const std::string& in, bool base64);
251

    
252
        /**
253
     *  Decompress the input string unsing zlib
254
     *    @param in input string
255
     *    @param base64 true if the input is base64 encoded
256
     *    @return pointer to the decompressed sting (must be freed) or 0 in case
257
     *    of error
258
     */
259
        std::string * zlib_decompress(const std::string& in, bool base64);
260

    
261
        extern "C" void sslmutex_lock_callback(int mode, int type, char *file,
262
                int line);
263

    
264
        extern "C" unsigned long sslmutex_id_callback();
265

    
266
        class SSLMutex
267
        {
268
        public:
269
                static void initialize();
270

    
271
                static void finalize();
272

    
273
        private:
274
                friend void sslmutex_lock_callback(int mode, int type, char *file,
275
                        int line);
276

    
277
                SSLMutex();
278

    
279
                ~SSLMutex();
280

    
281
                static SSLMutex * ssl_mutex;
282

    
283
                static std::vector<pthread_mutex_t *> vmutex;
284
        };
285
};
286

    
287
#endif /* _NEBULA_UTIL_H_ */