2013-02-04 18:48:05 +01:00
/* -------------------------------------------------------------------------- */
2015-02-24 12:27:59 +01:00
/* Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs */
2013-02-04 18:48:05 +01:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
2013-02-12 12:15:09 +01:00
# ifndef _NEBULA_UTIL_H_
# define _NEBULA_UTIL_H_
2013-02-04 18:48:05 +01:00
# include <string>
2014-10-10 16:53:57 +02:00
# include <sstream>
2013-11-15 12:58:53 +01:00
# include <vector>
2014-10-10 16:53:57 +02:00
# include <set>
2013-02-04 18:48:05 +01:00
namespace one_util
{
2013-02-08 21:58:34 +01:00
std : : string & toupper ( std : : string & st ) ;
2013-02-04 18:48:05 +01:00
2013-02-08 21:58:34 +01:00
std : : string & tolower ( std : : string & st ) ;
2013-02-04 18:48:05 +01:00
2013-02-08 21:50:40 +01:00
std : : string log_time ( time_t the_time ) ;
2013-02-04 18:48:05 +01:00
2013-02-08 21:58:34 +01:00
std : : string log_time ( ) ;
2013-02-09 16:38:54 +01:00
/**
* sha1 digest
* @ param in the string to be hashed
* @ return sha1 hash of str
*/
std : : string sha1_digest ( const std : : string & in ) ;
/**
* Base 64 encoding
* @ param in the string to encoded
* @ return a pointer to the encoded string ( must be freed ) or 0 in case of
* error
*/
std : : string * base64_encode ( const std : : string & in ) ;
/**
2013-06-27 15:49:23 +02:00
* Base 64 decoding
2013-02-09 16:38:54 +01:00
* @ param in the string to decode
* @ return a pointer to the decoded string ( must be freed ) or 0 in case of
* error
*/
std : : string * base64_decode ( const std : : string & in ) ;
2013-07-05 01:31:30 +02:00
/**
* AES256 encryption
* @ param in the string to encrypt
* @ param password to encrypt data
* @ return a pointer to the encrypted string ( must be freed ) or 0 in case of
* error
*/
std : : string * aes256cbc_encrypt ( const std : : string & in , const std : : string password ) ;
2013-06-27 15:49:23 +02:00
/**
* Creates a random number , using time ( 0 ) as seed , and performs an sha1 hash
* @ return a new random password
*/
std : : string random_password ( ) ;
2013-11-15 12:58:53 +01:00
/**
* Splits a string , using the given delimiter
*
* @ param st string to split
* @ param delim delimiter character
* @ param clean_empty true to clean empty split parts .
* Example for st " a::b:c "
* clean_empty true will return [ " a " , " b " , " c " ]
* clean_empty fase will return [ " a " , " " , " b " , " c " ]
*
* @ return a vector containing the resulting substrings
*/
std : : vector < std : : string > split (
const std : : string & st ,
char delim ,
bool clean_empty = true ) ;
2014-09-09 18:13:52 +02:00
/**
2014-10-10 16:53:57 +02:00
* Splits a string , using the given delimiter
*
* @ param st string to split
* @ param delim delimiter character
* @ param result where the result will be saved
* @ param clean_empty true to clean empty split parts .
* Example for st " a::b:c "
* clean_empty true will return [ " a " , " b " , " c " ]
* clean_empty fase will return [ " a " , " " , " b " , " c " ]
*/
template < class T >
2014-11-05 18:44:54 +01:00
void split_unique (
2014-10-10 16:53:57 +02:00
const std : : string & st ,
char delim ,
std : : set < T > & result ,
bool clean_empty = true )
{
T elem ;
std : : vector < std : : string > : : const_iterator it ;
std : : vector < std : : string > strings = split ( st , delim , clean_empty ) ;
for ( it = strings . begin ( ) ; it ! = strings . end ( ) ; it + + )
{
std : : istringstream iss ( * it ) ;
iss > > elem ;
if ( iss . fail ( ) )
{
continue ;
}
result . insert ( elem ) ;
}
}
/**
* Joins the given element with the delimiter
2014-09-09 18:13:52 +02:00
*
2014-10-10 16:53:57 +02:00
* @ param first iterator
* @ param last iterator
2014-09-09 18:13:52 +02:00
* @ param delim delimiter character
* @ return the joined strings
*/
2014-10-10 16:53:57 +02:00
template < class Iterator >
std : : string join ( Iterator first , Iterator last , char delim )
{
std : : ostringstream oss ;
for ( Iterator it = first ; it ! = last ; it + + )
{
if ( it ! = first )
{
oss < < delim ;
}
oss < < * it ;
}
return oss . str ( ) ;
}
2014-12-10 17:28:52 +01:00
2014-11-06 17:41:20 +01:00
/**
* Creates a string from the given float , using fixed notation . If the
* number has any decimals , they will be truncated to 2.
*
* @ param num
* @ return
*/
std : : string float_to_str ( const float & num ) ;
2014-12-28 10:40:58 +01:00
/**
* Checks if a strings matches a regular expression
*
* @ param pattern PCRE extended pattern
* @ param subject the string to test
* @ return 0 on match , another value otherwise
*/
int regex_match ( const char * pattern , const char * subject ) ;
2013-02-07 00:14:25 +01:00
} ;
2013-02-04 18:48:05 +01:00
2013-02-12 12:15:09 +01:00
# endif /* _NEBULA_UTIL_H_ */