55#define CHECK_BIT(variable, position) ((variable) & (1ULL << (position)))
59template<
typename... Args>
64string double_to_string(
double number,
int precision,
bool is_fixed_point =
true);
86string find_first_number(
string const& str,
size_t& beg_pos,
size_t& end_pos,
bool isOnlyPositive =
true);
88string find_last_number(
string const& str,
size_t& beg_pos,
size_t& end_pos,
bool isOnlyPositive =
true);
99string trim(
const string& str,
const string& whitespace =
" \t\r");
101string reduce(
const string& str,
const string& fill =
" ",
const string& whitespace =
" \t\r");
103bool endswith(
string const& full_str,
string const& ending);
130#ifndef ONLY_DECLARATIONS
142 if ((f = popen(aCommand.c_str(),
"r")) ==
nullptr) {
143 cout <<
"system_command_linux error: popen failed" << endl;
147 const int BUFSIZE = 4096;
149 if (fgets(buf, BUFSIZE, f) !=
nullptr)
163 size_t find_result = path.find(
'$');
164 if (find_result != string::npos) {
165 string pre = path.substr(0, find_result);
166 bool is_brace =
false;
167 if (path[find_result] ==
'{') {
171 string post = path.substr(find_result + 1);
174 find_result = post.find(
'}');
176 find_result = post.find(
'/');
177 if ((find_result != string::npos) || (!is_brace)) {
178 string variable = post.substr(0, find_result);
183 post = post.substr(find_result);
185 const char*
v = getenv(variable.c_str());
193 find_result = path.find(
'~');
194 if (find_result != string::npos) {
196 const char*
v = getenv(
"HOME");
201 path.replace(find_result, 1, value);
202 find_result = path.find(
'~');
203 }
while (find_result != string::npos);
213 size_t find_result = path.find(
"sudo");
214 if (find_result != string::npos)
215 if ((path[find_result + 4] ==
' ') || (path[find_result + 4] ==
'\t'))
218 string aCommand, result;
219 aCommand =
string_format(
"echo \"$(cd \"$(dirname \"%s\")\" 2>/dev/null; pwd)/$(basename \"%s\")\" 2>/dev/null",
220 path.c_str(), path.c_str());
230 return sysconf(_SC_NPROCESSORS_ONLN);
238 const int MAX_BUFFER = 255;
239 char buffer[MAX_BUFFER];
240 string command_line, data, strDiff;
244 if (queue_name ==
"")
245 queue_name =
"all.q";
246 command_line = string(
"export SGE_SINGLE_LINE=1; qconf -sq ") + queue_name + string(
" | grep slots");
249 if (queue_name ==
"")
250 queue_name =
"batch";
251 command_line = string(
"qstat -f -Q ") + queue_name + string(
" | grep max_running");
254 if (queue_name ==
"")
255 queue_name =
"interactive";
256 command_line = string(
"scontrol show partition ") + queue_name + string(
" | grep -o 'TotalCPUs=[0-9]*'");
260 FILE* stream = popen(command_line.c_str(),
"r");
261 while (fgets(buffer, MAX_BUFFER, stream) !=
nullptr)
266 size_t found = data.find(
"="), found2 = string::npos;
268 while (found != string::npos) {
269 found2 = data.find(
"]", found);
270 strDiff = data.substr(found + 1, found2 - found - 1);
271 proc_count += atoi(strDiff.c_str());
274 found = data.find(
"=", found2);
278 if (found != string::npos) {
279 strDiff = data.substr(found + 1, data.length() - found - 1);
280 proc_count = atoi(strDiff.c_str());
284 if (found != string::npos) {
285 strDiff = data.substr(found + 1, data.length() - found - 1);
286 proc_count = atoi(strDiff.c_str());
304 pid_t procpid = getpid();
306 toCom <<
"cat /proc/" << procpid <<
"/comm";
309 size_t last_pos = fRes.find_last_not_of(
" \n\r\t") + 1;
310 if (last_pos != string::npos)
311 fRes.erase(last_pos);
319 pid_t procpid = getpid();
321 stringstream command;
322 command <<
"readlink /proc/" << procpid <<
"/exe | sed \"s/\\(\\/" << appName <<
"\\)$//\"";
327 fRes = fRes.erase(fRes.length() - 1, 1);
346template<
typename... Args>
349 int size_s = snprintf(
nullptr, 0, format.c_str(), args...) + 1;
351 throw runtime_error(
"Error during formatting.");
352 auto size =
static_cast<size_t>(size_s);
353 unique_ptr<char[]> buf(
new char[size]);
354 snprintf(buf.get(), size, format.c_str(), args...);
355 return string(buf.get(), buf.get() + size - 1);
365 stream << std::fixed;
366 stream << std::setprecision(precision) << number;
382 stream << std::hex << number;
391 stream << std::hex << hex_string;
400 if (byte_size_in_string.empty())
406 size_t indText = byte_size_in_string.find_first_not_of(
"0123456789,.");
410 string units =
"BKMGTP";
412 size_t convert_dim = units.find(::toupper(convert_to)), value_dim = -1;
413 if (convert_dim == string::npos)
417 if (indText == string::npos) {
418 number = atof(byte_size_in_string.c_str());
421 number = atof(byte_size_in_string.substr(0, indText).c_str());
422 value_dim = units.find(::toupper(byte_size_in_string[indText]));
423 if (value_dim == string::npos)
427 if (convert_dim != value_dim)
428 number *=
pow(1024, value_dim - convert_dim);
436 constexpr const char FILE_SIZE_UNITS[8][3]{
"B",
"KB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB"};
455 float num = ((float)byte_size) /
pow(1024, order);
456 return string_format(
"%.*f %s", precision, num, FILE_SIZE_UNITS[order]);
463 string::const_iterator it = s.begin();
464 while (it != s.end() && std::isdigit(*it))
466 return !s.empty() && it == s.end();
473 size_t n = str.find_first_of(
"0123456789");
474 if (
n != string::npos) {
475 size_t m = str.find_first_not_of(
"0123456789",
n);
476 if ((!isOnlyPositive) && (
n > 0) && ((str[
n - 1] ==
'+') || (str[
n - 1] ==
'-')))
478 return str.substr(
n,
m != string::npos ?
m -
n :
m);
486 size_t n = str.find_last_of(
"0123456789");
487 if (
n != string::npos) {
488 string temp = str.substr(0,
n + 1);
489 size_t m = temp.find_last_not_of(
"0123456789");
490 if (
m != string::npos) {
491 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
493 return temp.substr(
m + 1,
n -
m);
503 size_t n = str.find_first_of(
"0123456789");
504 if (
n != string::npos) {
505 size_t m = str.find_first_not_of(
"0123456789,.",
n);
506 if ((!isOnlyPositive) && (
n > 0) && ((str[
n - 1] ==
'+') || (str[
n - 1] ==
'-')))
508 return str.substr(
n,
m != string::npos ?
m -
n :
m);
516 size_t n = str.find_last_of(
"0123456789");
517 if (
n != string::npos) {
518 string temp = str.substr(0,
n + 1);
519 size_t m = temp.find_last_not_of(
"0123456789,.");
520 if (
m != string::npos) {
521 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
523 return temp.substr(
m + 1,
n -
m);
531string find_first_number(
string const& str,
size_t& beg_pos,
size_t& end_pos,
bool isOnlyPositive)
533 beg_pos = str.find_first_of(
"0123456789", beg_pos);
534 if (beg_pos != string::npos) {
535 size_t m = str.find_first_not_of(
"0123456789", beg_pos);
536 end_pos = (
m != string::npos ?
m - 1 : str.length() - 1);
537 if ((!isOnlyPositive) && (beg_pos > 0) && ((str[beg_pos - 1] ==
'+') || (str[beg_pos - 1] ==
'-')))
539 return str.substr(beg_pos,
m != string::npos ?
m - beg_pos :
m);
542 end_pos = string::npos;
546string find_last_number(
string const& str,
size_t& beg_pos,
size_t& end_pos,
bool isOnlyPositive)
548 end_pos = str.find_last_of(
"0123456789", end_pos);
549 if (end_pos != string::npos) {
550 string temp = str.substr(0, end_pos + 1);
551 size_t m = temp.find_last_not_of(
"0123456789");
552 beg_pos = (
m != string::npos ?
m + 1 : 0);
553 if (
m != string::npos) {
554 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
556 return temp.substr(beg_pos, end_pos - beg_pos + 1);
561 beg_pos = string::npos;
567 beg_pos = str.find_first_of(
"0123456789", beg_pos);
568 if (beg_pos != string::npos) {
569 size_t m = str.find_first_not_of(
"0123456789,.", beg_pos);
570 end_pos = (
m != string::npos ?
m - 1 : str.length() - 1);
571 if ((!isOnlyPositive) && (beg_pos > 0) && ((str[beg_pos - 1] ==
'+') || (str[beg_pos - 1] ==
'-')))
573 return str.substr(beg_pos,
m != string::npos ?
m - beg_pos :
m);
576 end_pos = string::npos;
582 end_pos = str.find_last_of(
"0123456789", end_pos);
583 if (end_pos != string::npos) {
584 string temp = str.substr(0, end_pos + 1);
585 size_t m = temp.find_last_not_of(
"0123456789,.");
586 beg_pos = (
m != string::npos ?
m + 1 : 0);
587 if (
m != string::npos) {
588 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
590 return temp.substr(beg_pos, end_pos - beg_pos + 1);
595 beg_pos = string::npos;
602 if (input_char_array ==
nullptr)
605 const int length = strlen(input_char_array);
606 char* lower =
new char[length + 1];
610 for (
int i = 0;
i < length;
i++)
611 lower[
i] = tolower(input_char_array[
i]);
621 start = text.find(old_substring, start + 1);
623 text.replace(start, old_substring.length(), new_substring.c_str());
624 start += new_substring.length() - old_substring.length();
626 }
while (start > -1);
632 string new_substring =
"";
635 start = text.find(old_substring, start + 1);
637 if (new_substring ==
"") {
639 sprintf(buf_int,
"%d", new_subinteger);
640 new_substring = buf_int;
642 text.replace(start, old_substring.length(), new_substring.c_str());
643 start += new_substring.length() - old_substring.length();
645 }
while (start > -1);
651 int replace_count = 0;
652 char* cur_pos = strchr(str, find);
656 cur_pos = strchr(cur_pos, find);
659 return replace_count;
663string trim(
const string& str,
const string& whitespace)
665 size_t strBegin = str.find_first_not_of(whitespace);
666 if (strBegin == string::npos)
669 size_t strEnd = str.find_last_not_of(whitespace);
670 size_t strRange = strEnd - strBegin + 1;
672 return str.substr(strBegin, strRange);
676string reduce(
const string& str,
const string& fill,
const string& whitespace)
679 string result =
trim(str, whitespace);
682 size_t beginSpace = result.find_first_of(whitespace);
683 while (beginSpace != string::npos) {
684 size_t endSpace = result.find_first_not_of(whitespace, beginSpace);
685 size_t range = endSpace - beginSpace;
687 result.replace(beginSpace, range, fill);
689 size_t newStart = beginSpace + fill.length();
690 beginSpace = result.find_first_of(whitespace, newStart);
697bool endswith(
string const& full_str,
string const& ending)
699 if (full_str.length() >= ending.length())
700 return (0 == full_str.compare(full_str.length() - ending.length(), ending.length(), ending));
716 if (stat(path, &info) != 0)
718 else if (info.st_mode & S_IFDIR)
727 struct stat info = {0};
729 if (stat(path, &info) == -1) {
730 int status = mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
733 if (info.st_mode & S_IFDIR)
744 string str_path(path);
745 size_t pos = str_path.find_last_of(
'/');
746 if (pos == string::npos)
748 str_path = str_path.substr(0, pos);
758 status = mkdir(str_path.c_str(), mode);
775 size_t last_slash_idx = path.find_last_of(
"\\/");
776 if (last_slash_idx != string::npos)
777 path.erase(0, last_slash_idx + 1);
780 size_t period_idx = path.rfind(
'.');
781 if (period_idx != string::npos)
782 path.erase(period_idx);
791 size_t last_slash_idx = path.find_last_of(
"\\/");
792 if (last_slash_idx != string::npos)
793 path.erase(0, last_slash_idx + 1);
801 string directory =
"";
802 const size_t last_slash_idx = file_path.find_last_of(
"\\/");
803 if (last_slash_idx != string::npos)
804 directory = file_path.substr(0, last_slash_idx);
812 string directory =
"";
813 const size_t last_slash_idx = file_path.find_last_of(
"\\/");
814 if (last_slash_idx != string::npos) {
815 const size_t penult_slash_idx = file_path.find_last_of(
"\\/", last_slash_idx - 1);
816 if (penult_slash_idx != string::npos)
817 directory = file_path.substr(penult_slash_idx + 1, last_slash_idx - penult_slash_idx - 1);
836 timeinfo = localtime(&rawtime);
839 strftime(buffer, 80,
"%d-%m-%Y", timeinfo);
849 strptime(str_datetime.c_str(), format, tmbuf);
857 struct timespec ts = {
858 .tv_sec = (time_t)sec,
859 .tv_nsec = (
long)(sec - (long)(sec)) * 1000000000,
RVec< PromoteType< T > > floor(const RVec< T > &v)
RVec< PromoteType< T > > log(const RVec< T > &v)
RVec< PromoteTypes< T0, T1 > > pow(const RVec< T0 > &v, const T1 &y)
struct timespec convert_double_to_timespec(double sec)
*brief user defined string literal for JSON values *sa std::size_t n
string double_to_string(double number, int precision, bool is_fixed_point=true)
string int_to_string(int number)
string reduce(const string &str, const string &fill=" ", const string &whitespace=" \t\r")
string find_last_double_number(string const &str, bool isOnlyPositive=true)
int check_directory_exist(const char *path)
string get_current_date()
string int_to_hex_string(int number)
int create_directory(const char *path)
string find_first_double_number(string const &str, bool isOnlyPositive=true)
tm convert_string_to_datetime(string str_datetime, const char *format="%d.%m.%Y %H:%M:%S")
char * convert_pchar_to_lowercase_new(char *input_char_array)
int system_command_linux(string aCommand, string &result)
string find_first_number(string const &str, bool isOnlyPositive=true)
string expand_path(string path)
string get_file_name(string path)
string get_app_name_linux()
string find_last_number(string const &str, bool isOnlyPositive=true)
string get_directory_path(string file_path)
int replace_char(char *&str, char find, char replace)
string get_file_name_with_ext(string path)
bool is_string_number(const string &s)
string absolute_path(string path)
int get_linux_processor_count()
int create_directory_tree(const char *path)
int hex_string_to_int(string hex_string)
string get_app_dir_linux()
string trim(const string &str, const string &whitespace=" \t\r")
double byte_size_to_double(string byte_size_in_string, char convert_to='B')
void replace_string_in_text(string &text, string old_substring, string new_substring)
int get_batch_processor_count(BATCH_SYSTEM_NAME batch_system, string queue_name="")
string get_directory_name(string file_path)
bool endswith(string const &full_str, string const &ending)
string byte_size_to_human(uint64_t byte_size, unsigned precision=2)
string string_format(const string &format, Args... args)