18#include <Misc/Utils.hpp>
53 return (rand() % (max - min + 1) + min);
61 return ((random_int % 2) == 0);
68 return (x < (percent * 100));
75void Utils::Time::delay_ms(
int delay)
77 usleep((useconds_t)delay * 100);
92 if (stat(path.c_str(), &s) < 0)
99 std::string tmp(path);
101 if (Utils::String::back(tmp) ==
'/')
102 tmp[tmp.size() - 1] =
'\0';
104 for (std::string::iterator p = tmp.begin();
111 mkdir(tmp.c_str(), S_IRWXU);
115 mkdir(tmp.c_str(), S_IRWXU);
148 std::string command(
"rm -rf " + path);
150 system(command.c_str());
159 std::string command(
"rm -f " + path);
161 system(command.c_str());
166 FILE* fp = fopen(path.c_str(),
"w");
176 file.open(path.c_str());
183 if (stat(path.c_str(), &s) < 0)
186 return ((S_ISDIR(s.st_mode))?
194 if (stat(path.c_str(), &s) < 0)
197 return ((S_ISREG(s.st_mode))?
203 std::vector<std::string> v;
211 if (! (dir = opendir(path.c_str())))
215 if (Utils::String::back(path) !=
'/')
221 while ((ent = readdir(dir)))
223 std::string s(path + ent->d_name);
226 if ((s == (path +
'.')) || (s == (path +
"..")))
237 if (! getenv(
"HOME"))
240 std::string s(getenv(
"HOME"));
241 if (Utils::String::back(s) !=
'/')
253 Utils::String::pop_back(&s);
256 size_t pos = s.rfind(
'/');
258 if (pos == std::string::npos)
261 return s.substr(pos + 1);
265#if defined(_WIN32) && !defined(__CYGWIN__)
266 char separator =
'\\';
268 char separator =
'/';
271 size_t position = path.rfind(separator);
274 if (position == std::string::npos)
278 return path.substr(position + 1);
286 size_t position = path.find(
basename);
288 if (position == std::string::npos)
292 return path.substr(0, position - 1);
296 size_t position = path.rfind(
'.');
298 if ((position == std::string::npos) ||
303 return path.substr(position + 1);
313 if (position == std::string::npos)
317 return path.substr(0, position - 1);
324char Utils::String::back(std::string& str)
327 return *(str.rbegin());
330char Utils::String::front(std::string& str)
332 return *(str.begin());
335void Utils::String::pop_back(std::string* str)
338 str->resize(str->size() - 1);
341std::string Utils::String::pop_back(std::string& str)
343 return (str.substr(0, str.size() - 1));
346const char trim_blanks[] =
" \t\r\n";
348std::string Utils::String::ltrim(
const std::string& str)
350 size_t startpos = str.find_first_not_of(trim_blanks);
353 if (startpos == std::string::npos)
356 return str.substr(startpos);
358std::string Utils::String::rtrim(
const std::string& str)
360 size_t endpos = str.find_last_not_of(trim_blanks);
363 if (endpos == std::string::npos)
366 return str.substr(0, endpos + 1);
368std::string Utils::String::trim(
const std::string& str)
370 return (Utils::String::ltrim(
371 Utils::String::rtrim(
375std::vector<std::string> Utils::String::split(
const std::string& str,
char delim)
377 std::stringstream ss(str);
379 std::vector<std::string> elems;
381 while (std::getline(ss, item, delim))
382 elems.push_back(Utils::String::trim(item));
387bool Utils::String::caseInsensitiveSmallerChar(
const char x,
const char y)
389 return (std::tolower(x) < std::tolower(y));
392bool Utils::String::caseInsensitiveSmallerString(
const std::string &a,
const std::string &b)
394 return std::lexicographical_compare(a.begin(), a.end(),
396 Utils::String::caseInsensitiveSmallerChar);
427static const std::string base64_chars =
428 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
429 "abcdefghijklmnopqrstuvwxyz"
433static inline bool isBase64(
unsigned char c)
435 return (isalnum(c) || (c ==
'+') || (c ==
'/'));
443 unsigned char const* bytes_to_encode =
reinterpret_cast<const unsigned char*
>(str.c_str());
444 unsigned int string_size = str.size();
449 unsigned char char_array_3[3];
450 unsigned char char_array_4[4];
452 while (string_size--)
454 char_array_3[i++] = *(bytes_to_encode++);
458 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
459 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
460 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
461 char_array_4[3] = char_array_3[2] & 0x3f;
463 for(i = 0; (i <4) ; i++)
464 ret += base64_chars[char_array_4[i]];
471 for(j = i; j < 3; j++)
472 char_array_3[j] =
'\0';
474 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
475 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
476 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
477 char_array_4[3] = char_array_3[2] & 0x3f;
479 for (j = 0; (j < i + 1); j++)
480 ret += base64_chars[char_array_4[j]];
490 int string_size = encoded_string.size();
494 unsigned char char_array_4[4], char_array_3[3];
497 while (string_size-- && ( encoded_string[in_] !=
'=') && isBase64(encoded_string[in_]))
499 char_array_4[i++] = encoded_string[in_]; in_++;
503 for (i = 0; i <4; i++)
504 char_array_4[i] = base64_chars.find(char_array_4[i]);
506 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
507 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
508 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
510 for (i = 0; (i < 3); i++)
511 ret += char_array_3[i];
518 for (j = i; j <4; j++)
521 for (j = 0; j <4; j++)
522 char_array_4[j] = base64_chars.find(char_array_4[j]);
524 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
525 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
526 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
528 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
std::string encode(std::string str)
Transforms str into a Base64 equivalent.
std::string decode(std::string const &s)
Transforms a Base64-encoded str into it's regular string equivalent.
bool isFile(std::string path)
Tells if path is a regular file (not a directory, socket, FIFO device or whatever).
std::string getHome()
Gets the full path of the home directory for the user running this program.
std::string dropBasename(std::string path)
Returns the full pathname up to the last component.
std::vector< std::string > ls(std::string path)
Lists all files withing path.
void rm_rf(std::string path)
Removes recursively all files within directory at path, just like UNIX command rm -rf.
bool create(std::string path)
Creates empty file path.
std::string basename(std::string path)
Returns the component of a pathname (file name and extension).
void rm_f(std::string path)
Forcibly removes file within path.
std::string extension(std::string path)
Returns the extension of a file.
void write(std::string path, std::string contents)
Writes contents to path.
off_t size(std::string path)
Returns the file size of path in bytes.
bool exists(std::string path)
Tells if path exists.
std::string getUser()
Gets the user name of the person running this program.
bool isDirectory(std::string path)
Tells if path is a directory.
std::string dropExtension(std::string path)
Returns the filename without it's extension.
void mkdir_p(std::string path)
Creates path directory hierarchy recursively, just like UNIX command mkdir -p.
bool boolean()
Random boolean.
bool booleanWithChance(float percent)
Random boolean with chance of percent.
int between(int min, int max)
Random number between min and max.
void seed()
Must be called before any of those.