utils.hpp 9.69 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
#ifndef __UTILS_CPP_H__
#define __UTILS_CPP_H__
#include <vector>
#include <string>
#include <iostream>


#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <iostream>
#include <cassert>
#include <sstream>
#include <fstream>
#include <algorithm>

#include <pthread.h>

#include "common.hpp"
#include "nodeos_exceptions.hpp"

using std::vector;
using std::string;

#define MAX_BUF_SIZE	1025

/*SHELL ESCAPE*/
#define GREEN_TEXT  "\x1b[32m"
#define RED_TEXT     "\x1b[31m"
#define MAGENTA_TEXT   "\x1b[35m"
#define CYAN_TEXT   "\x1b[36m"
#define NEUTRAL_TEXT   "\x1b[0m"
/*END SHELL ESCAPE*/

/*STRING MANIPULATION*/
/*Tokenize according to delim*/
vector<string> &split(const string &str_in, char delim, vector<string> &tokens);

/*Tokenize using any of the characters in delims as delimiter*/
vector<string> &split(const string &str_in, const string &delims,
        vector<string> &tokens);

/*Returns true if base_str starts with key*/
bool str_start_with(const string& base_str, const string& key);

/*Returns true if base_str contains key*/
bool str_contains(const string& base_str, const string& key);

/*Removes white spaces (' '  and '\t') from the left. The function is
 * destructive*/
string& str_trim_left(string &in);

/*Remove white spaces (' ' and '\t') from the right. The function is destructive*/
string& str_trim_right(string &in);

/*Removes whitespace from both ends. The function is destructive*/
string& str_trim(string &in);

/*Extracts the chunck of string contained between delim0 and delim1 from
 * base_str. Not destructive*/
bool str_extract_delimiter_content(const string& base_str, const string& delim0,
		const string& delim1, string &token);


/*Extract  everything before delim and put it in token.
 * If delim is not contained in base_str, token = base_str unless
 * empty_token_if_delim_not_found is true in which case token is empty
 *
 *Returns false if token is empty
 * */
bool str_extract_before(const string& base_str, const string& delim,
		string &token, bool empty_token_if_delim_not_found = false);


/*Extract  everything after delim and put it in token
 * If delim is not contained in base_str, token = base_str unless
 * empty_token_if_delim_not_found is true in which case token is empty
 *
 * Returns false if token is empty
 * */
bool str_extract_after(const string& base_str, const string& delim,
		string &token, bool empty_token_if_delim_not_found = false);


/*Chop everything before delim and put it in token. The input string
 * is left with what is not chopped.
 *
 * If delim is not contained in base_str, token = base_str unless
 * empty_token_if_delim_not_found is true in which case token is empty
 *
 * if throw_delim_away is false, the delimiter is left at the begining of
 * of base_str; otherwise, it is removed.
 *
 *Returns false if token is empty
 * */
bool str_chop_before(string& base_str, const string& delim,
		string &token, bool throw_delim_away= true,
		bool empty_token_if_delim_not_found = false);


/*Chop everything after delim and put it in token. The input string
 * is left with what is not chopped.
 *
 * If delim is not contained in base_str, token = base_str unless
 * empty_token_if_delim_not_found is true in which case token is empty
 *
 * if throw_delim_away is false, the delimiter is left at the end of
 * of base_str; otherwise, it is removed.
 *
 * Returns false if token is empty
 * */
bool str_chop_after(string& base_str, const string& delim,
		string &token, bool throw_delim_away= true, 
		bool empty_token_if_delim_not_found = false);


bool is_int(const string& in);
bool is_double(const string& in);


/*END OF STING MANIPULATION*/

/*DIAGNOSTIC */
string& make_debug_message(string& initial_message, string& out,
		const char* filename, int lineno);
/*END OF STING MANIPULATION*/

/*DIAGNOSTIC */
string& make_debug_message(string& initial_message, string& out,
		const char* filename, int lineno);
#define MAKE_DEBUG_MESSAGE(initial_msg /*string*/)\
	make_debug_message((initial_msg), (initial_msg), __FILE__, __LINE__)
/*END DIAGNOSTIC */

/*PRIVILEGES*/


void elevate_privileges();
void lower_privileges();

/*END PRIVILEGES*/

/*ERRORS*/

#ifndef NDEBUG
#define ASSERT_ACTION()	assert(false)
#else
#define ASSERT_ACTION() SEG_FAULT()
#endif
//#define ASSERT_ACTION()

#define SEG_FAULT() do{char* a = NULL; *a=0;}while(0)
#define ASSERT_MSG(cond,msg) \
	do{if(!(cond)){std::cout<<msg<<' '<<std::flush; ASSERT_ACTION();}}while(0)


#define ASSERT_ERRNO(MSG)\
	do{int err=errno; \
		if(err){std::cerr<<"["<<__FILE__<<':'<<__LINE__<<"] " \
			<<strerror(err)<<' '<<std::endl; fflush(stdout); ASSERT_ACTION();}}while(0)

#define THROW_ON_BAD_INPUT(msg)\
	do{throw User_option_exception((msg));}while(0)
#define THROW_ON_BAD_INPUT_IF(cond,msg) \
	do{if((cond)) THROW_ON_BAD_INPUT((msg));}while(0)

#define THROW_ON_ERRONEOUS_ACTION(msg)\
	do{throw Erroneous_action_exception((msg));}while(0)
#define THROW_ON_ERRONEOUS_ACTION_IF(cond,msg) \
	do{if((cond)) THROW_ON_ERRONEOUS_ACTION((msg));}while(0)

#define THROW_ON_FORBIDDEN_ACTION(msg)\
	do{throw Forbidden_action_exception((msg));}while(0)
#define THROW_ON_FORBIDDEN_ACTION_IF(cond,msg) \
	do{if((cond)) THROW_ON_FORBIDDEN_ACTION((msg));}while(0)

#define THROW(msg) do {throw std::runtime_error((msg));}while(0)
#define THROW_IF(cond,msg) do{if((cond)) THROW((msg));}while(0)

#define THROW_WITH_ERRNO(msg)\
	do{int err=errno; \
		std::ostringstream ss; ss<<strerror(err)<<'|'<<(msg);\
		THROW(ss.str());}while(0)

#define THROW_WITH_ERRNO_IF(cond, msg)\
	do{if(cond) THROW_WITH_ERRNO((msg));}while(0)

#define THROW_NOT_IMPLEMENTED_EXCEPTION()\
	do{std::ostringstream ss; \
		ss<<'['<<__FUNCTION__<<']' \
		<<"Not implemented! Invocation of this function or method (at this point) is a bug!"; \
		THROW(ss.str());}while(0)


void argo_exit(int exit_status, string exit_message="Normal termination", 
		int errnum = -1);
/*END ERRORS*/

/*MISC*/
const string* const get_hostname();
/*END MISC*/

/*STRINGIFY*/
string log_dest_to_str(Log_dest dest);
string log_type_to_str(Log_type type);
void stringify_int_list(const vector<int>& list, char separator, string &out);
string& format_int_list_for_display(
		const vector<int>& vect, string& str, const string& header = "");
/*END STRINGIFY*/

/*STL CONTAINERS*/

/*
 * Returns -1 if not found
 * */
template <typename T>
int get_object_index(const vector<T>& container, const T& val)
{
	for(int i=0; i<(int)container.size(); i++)
		if(container[i] == val)
			return i;
	return -1;
}

template <typename T>
void remove_object(vector<T>& container, T val)
{
	typename vector<T>::iterator it;
	for(it = container.begin(); it != container.end(); it++)
		if(*it == val)
		{
			container.erase(it);
			break;
		}
}

template <typename T>
void add_object(vector<T>& container, T val)
{
	if(get_object_index(container, val) == -1)
		container.push_back(val);
}

template <typename T>
void add_objects(vector<T>& container, 
		const vector<T>& vals)
{
	container.insert(container.end(), vals.begin(), vals.end());
	
	typename vector<T>::iterator it = std::unique(container.begin(),
			container.end());
	container.erase(it, container.end());
}

template <typename T>
void remove_objects(vector<T>& container, 
		const vector<T>& vals)
{
	for(int i=0; i<(int)vals.size() && !container.empty(); i++)
		remove_object(container, vals[i]);
}

template <typename T>
bool is_superset(const vector<T>& container, const vector<T>& vals)
{
	if(vals.size() > container.size())
		return false;
	for(int i=0; i<(int)vals.size(); i++)
		if(std::find(container.begin(), container.end(), vals[i]) == 
				container.end())
			return false;
	return true;
}

template <typename T>
bool has_intersect(const vector<T>& container, const vector<T>& vals)
{
	if(vals.size() == 0 || container.size() == 0)
		return false;
	for(int i=0; i<(int)vals.size(); i++)
		if(std::find(container.begin(), container.end(), vals[i]) != 
				container.end())
			return true;
	return false;
}

template <typename T>
void get_intersect(const vector<T>& container0, 
		const vector<T> &container1, vector<T> &intersect)
{
	intersect.clear();
	for(int i=0; i<(int)container0.size(); i++)
		if(std::find(container1.begin(), container1.end(), container0[i]) != 
				container1.end())
			intersect.push_back(container0[i]);
}

template <typename T>
void get_complement(const vector<T>& superset, const vector<T>& subset,
		vector<T> &complement)
{
	complement.clear();
	for(int i=0; i<(int)superset.size(); i++)
		if(std::find(subset.begin(), subset.end(), superset[i]) == 
				subset.end())
			complement.push_back(superset[i]);
}
/*END STL CONTAINERS*/

/*SOME FILE I/O*/
template <typename T>
void commit_in_file(const string& filename, const T& data)
{
	std::ofstream file;
	file.open(filename.c_str());
	THROW_WITH_ERRNO_IF(file.fail(), 
			"Committing container change in " + filename + " failed");
	file<<data;
	file.close();
}

template <typename T>
void commit_in_file(const string& filename, const vector<T>& data)
{
	std::ofstream file;
	file.open(filename.c_str(), std::ofstream::app);
	THROW_WITH_ERRNO_IF(file.fail(), 
			"Committing container change in " + filename + " failed");
	for(int i=0; i<(int)data.size(); i++)
		file<<data[i];
	file.close();
}

void retrieve_from_file(const string& filename, string& out);

template <typename T>
void retrieve_from_file(const string& filename, vector<T>& out)
{
	std::ifstream file;
	file.open(filename.c_str());
	THROW_WITH_ERRNO_IF(file.fail(), 
			"Retriving container content from " + filename + " failed");
	T data;
	out.clear();
	while(file.peek() != std::ifstream::traits_type::eof())
	{
		file>>data;
		if(file.eof())
			break;
		out.push_back(data);
	}
	file.close();
}

uid_t get_task_owner(int pid);
/*END SOME FILE I/O*/

/*FILESYSTEM*/
int mkdir_p(string path, mode_t mode);
/*END FILESYSTEM*/

#endif //__UTILS_CPP_H__