50#define CHECK_BIT(variable,position) ((variable) & (1ULL<<(position)))
54template<
typename ... Args>
58string double_to_string(
double number,
int precision,
bool is_fixed_point =
true);
77string find_first_number(
string const &str,
size_t &beg_pos,
size_t &end_pos,
bool isOnlyPositive =
true);
string find_first_double_number(
string const &str,
size_t &beg_pos,
size_t &end_pos,
bool isOnlyPositive =
true);
78string find_last_number(
string const &str,
size_t &beg_pos,
size_t &end_pos,
bool isOnlyPositive =
true);
string find_last_double_number(
string const &str,
size_t &beg_pos,
size_t &end_pos,
bool isOnlyPositive =
true);
88string trim(
const string& str,
const string& whitespace =
" \t\r");
90string reduce(
const string& str,
const string& fill =
" ",
const string& whitespace =
" \t\r");
92bool endswith(
string const &full_str,
string const &ending);
120#ifndef ONLY_DECLARATIONS
132 if ((
f = popen(aCommand.c_str(),
"r")) ==
nullptr)
134 cout<<
"system_command_linux error: popen failed"<<endl;
138 const int BUFSIZE = 4096;
140 if (fgets(buf, BUFSIZE,
f) !=
nullptr)
155 size_t find_result = path.find(
'$');
156 if (find_result != string::npos)
158 string pre = path.substr(0, find_result);
159 bool is_brace =
false;
160 if (path[find_result] ==
'{')
165 string post = path.substr(find_result+1);
167 if (is_brace) find_result = post.find(
'}');
168 else find_result = post.find(
'/');
169 if ((find_result != string::npos) || (!is_brace))
171 string variable = post.substr(0, find_result);
174 if (is_brace) find_result++;
175 post = post.substr(find_result);
177 const char*
v = getenv(variable.c_str());
178 if (
v != NULL) value = string(
v);
184 find_result = path.find(
'~');
185 if (find_result != string::npos)
188 const char*
v = getenv(
"HOME");
189 if (
v != NULL) value = string(
v);
193 path.replace(find_result, 1, value);
194 find_result = path.find(
'~');
195 }
while (find_result != string::npos);
205 size_t find_result = path.find(
"sudo");
206 if (find_result != string::npos)
207 if ((path[find_result+4] ==
' ') || (path[find_result+4] ==
'\t'))
210 string aCommand, result;
211 aCommand =
string_format(
"echo \"$(cd \"$(dirname \"%s\")\" 2>/dev/null; pwd)/$(basename \"%s\")\" 2>/dev/null", path.c_str(), path.c_str());
220 return sysconf(_SC_NPROCESSORS_ONLN);
228 const int MAX_BUFFER = 255;
229 char buffer[MAX_BUFFER];
230 string command_line, data, strDiff;
235 if (queue_name ==
"")
236 queue_name =
"all.q";
237 command_line = string(
"export SGE_SINGLE_LINE=1; qconf -sq ") + queue_name + string(
" | grep slots");
241 if (queue_name ==
"")
242 queue_name =
"batch";
243 command_line = string(
"qstat -f -Q ") + queue_name + string(
" | grep max_running");
247 if (queue_name ==
"")
248 queue_name =
"interactive";
249 command_line = string(
"scontrol show partition ") + queue_name + string(
" | grep -o 'TotalCPUs=[0-9]*'");
253 FILE* stream = popen(command_line.c_str(),
"r");
254 while (fgets(buffer, MAX_BUFFER, stream) !=
nullptr)
259 size_t found = data.find(
"="), found2 = string::npos;
262 while (found != string::npos)
264 found2 = data.find(
"]", found);
265 strDiff = data.substr(found+1, found2 - found - 1);
266 proc_count += atoi(strDiff.c_str());
269 found = data.find(
"=", found2);
274 if (found != string::npos)
276 strDiff = data.substr(found+1, data.length() - found - 1);
277 proc_count = atoi(strDiff.c_str());
282 if (found != string::npos)
284 strDiff = data.substr(found+1, data.length() - found - 1);
285 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);
349template<
typename ... Args>
352 int size_s = snprintf(
nullptr, 0, format.c_str(), args ...) + 1;
353 if (size_s <= 0)
throw runtime_error(
"Error during formatting.");
354 auto size =
static_cast<size_t>(size_s);
355 unique_ptr<char[]> buf(
new char[size]);
356 snprintf(buf.get(), size, format.c_str(), args ...);
357 return string(buf.get(), buf.get() + size - 1);
365 if (is_fixed_point) 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())
return -1;
405 size_t indText = byte_size_in_string.find_first_not_of(
"0123456789,.");
406 if (indText == 0)
return -2;
408 string units =
"BKMGTP";
410 size_t convert_dim = units.find(::toupper(convert_to)), value_dim = -1;
411 if (convert_dim == string::npos) convert_dim = 0;
414 if (indText == string::npos)
416 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) value_dim = 0;
426 if (convert_dim != value_dim)
427 number *= pow(1024, value_dim - convert_dim);
435 constexpr const char FILE_SIZE_UNITS[8][3]{
436 "B",
"KB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB"
456 int order = floor(
log(byte_size) /
log(1024));
457 float num = ((float) byte_size) / pow(1024, order);
458 return string_format(
"%.*f %s", precision, num, FILE_SIZE_UNITS[order]);
465 string::const_iterator it = s.begin();
466 while (it != s.end() && std::isdigit(*it)) ++it;
467 return !s.empty() && it == s.end();
473 size_t n = str.find_first_of(
"0123456789");
474 if (n != string::npos)
476 size_t m = str.find_first_not_of(
"0123456789", n);
477 if ((!isOnlyPositive) && (n > 0) && ((str[n-1] ==
'+') || (str[n-1] ==
'-')))
479 return str.substr(n,
m != string::npos ?
m-n :
m);
487 size_t n = str.find_last_of(
"0123456789");
488 if (n != string::npos)
490 string temp = str.substr(0, n+1);
491 size_t m = temp.find_last_not_of(
"0123456789");
492 if (
m != string::npos)
494 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
496 return temp.substr(
m+1, n-
m);
506 size_t n = str.find_first_of(
"0123456789");
507 if (n != string::npos)
509 size_t m = str.find_first_not_of(
"0123456789,.", n);
510 if ((!isOnlyPositive) && (n > 0) && ((str[n-1] ==
'+') || (str[n-1] ==
'-')))
512 return str.substr(n,
m != string::npos ?
m-n :
m);
520 size_t n = str.find_last_of(
"0123456789");
521 if (n != string::npos)
523 string temp = str.substr(0, n+1);
524 size_t m = temp.find_last_not_of(
"0123456789,.");
525 if (
m != string::npos)
527 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
529 return temp.substr(
m+1, n-
m);
537string find_first_number(
string const &str,
size_t &beg_pos,
size_t &end_pos,
bool isOnlyPositive)
539 beg_pos = str.find_first_of(
"0123456789", beg_pos);
540 if (beg_pos != string::npos)
542 size_t m = str.find_first_not_of(
"0123456789", beg_pos);
543 end_pos = (
m != string::npos ?
m-1 : str.length()-1);
544 if ((!isOnlyPositive) && (beg_pos > 0) && ((str[beg_pos-1] ==
'+') || (str[beg_pos-1] ==
'-')))
546 return str.substr(beg_pos,
m != string::npos ?
m-beg_pos :
m);
549 end_pos = string::npos;
553string find_last_number(
string const &str,
size_t &beg_pos,
size_t &end_pos,
bool isOnlyPositive)
555 end_pos = str.find_last_of(
"0123456789", end_pos);
556 if (end_pos != string::npos)
558 string temp = str.substr(0, end_pos + 1);
559 size_t m = temp.find_last_not_of(
"0123456789");
560 beg_pos = (
m != string::npos ?
m+1 : 0);
561 if (
m != string::npos)
563 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
565 return temp.substr(beg_pos, end_pos-beg_pos+1);
570 beg_pos = string::npos;
576 beg_pos = str.find_first_of(
"0123456789", beg_pos);
577 if (beg_pos != string::npos)
579 size_t m = str.find_first_not_of(
"0123456789,.", beg_pos);
580 end_pos = (
m != string::npos ?
m-1 : str.length()-1);
581 if ((!isOnlyPositive) && (beg_pos > 0) && ((str[beg_pos-1] ==
'+') || (str[beg_pos-1] ==
'-')))
583 return str.substr(beg_pos,
m != string::npos ?
m-beg_pos :
m);
586 end_pos = string::npos;
592 end_pos = str.find_last_of(
"0123456789", end_pos);
593 if (end_pos != string::npos)
595 string temp = str.substr(0, end_pos+1);
596 size_t m = temp.find_last_not_of(
"0123456789,.");
597 beg_pos = (
m != string::npos ?
m+1 : 0);
598 if (
m != string::npos)
600 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
602 return temp.substr(beg_pos, end_pos-beg_pos+1);
607 beg_pos = string::npos;
614 if (input_char_array ==
nullptr)
617 const int length = strlen(input_char_array);
618 char* lower =
new char[length + 1];
622 for(
int i = 0;
i < length;
i++ )
623 lower[
i] = tolower(input_char_array[
i]);
635 start = text.find(old_substring, start + 1);
638 text.replace(start, old_substring.length(), new_substring.c_str());
639 start += new_substring.length() - old_substring.length();
648 string new_substring =
"";
652 start = text.find(old_substring, start + 1);
655 if (new_substring ==
"")
658 sprintf(buf_int,
"%d", new_subinteger);
659 new_substring = buf_int;
661 text.replace(start, old_substring.length(), new_substring.c_str());
662 start += new_substring.length() - old_substring.length();
671 int replace_count = 0;
672 char* cur_pos = strchr(str, find);
677 cur_pos = strchr(cur_pos, find);
680 return replace_count;
684string trim(
const string& str,
const string& whitespace)
686 size_t strBegin = str.find_first_not_of(whitespace);
687 if (strBegin == string::npos)
690 size_t strEnd = str.find_last_not_of(whitespace);
691 size_t strRange = strEnd - strBegin + 1;
693 return str.substr(strBegin, strRange);
697string reduce(
const string& str,
const string& fill,
const string& whitespace)
700 string result =
trim(str, whitespace);
703 size_t beginSpace = result.find_first_of(whitespace);
704 while (beginSpace != string::npos)
706 size_t endSpace = result.find_first_not_of(whitespace, beginSpace);
707 size_t range = endSpace - beginSpace;
709 result.replace(beginSpace, range, fill);
711 size_t newStart = beginSpace + fill.length();
712 beginSpace = result.find_first_of(whitespace, newStart);
719bool endswith(
string const &full_str,
string const &ending)
721 if (full_str.length() >= ending.length())
722 return (0 == full_str.compare(full_str.length() - ending.length(), ending.length(), ending));
739 if (stat(path, &info) != 0)
741 else if (info.st_mode & S_IFDIR)
750 struct stat info = {0};
752 if (stat(path, &info) == -1)
754 int status = mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
759 if (info.st_mode & S_IFDIR)
770 string str_path(path);
771 size_t pos = str_path.find_last_of(
'/');
772 if (pos == string::npos)
774 str_path = str_path.substr(0, pos);
784 status = mkdir(str_path.c_str(), mode);
802 size_t last_slash_idx = path.find_last_of(
"\\/");
803 if (last_slash_idx != string::npos)
804 path.erase(0, last_slash_idx + 1);
807 size_t period_idx = path.rfind(
'.');
808 if (period_idx != string::npos)
809 path.erase(period_idx);
818 size_t last_slash_idx = path.find_last_of(
"\\/");
819 if (last_slash_idx != string::npos)
820 path.erase(0, last_slash_idx + 1);
828 string directory =
"";
829 const size_t last_slash_idx = file_path.find_last_of(
"\\/");
830 if (last_slash_idx != string::npos)
831 directory = file_path.substr(0, last_slash_idx);
839 string directory =
"";
840 const size_t last_slash_idx = file_path.find_last_of(
"\\/");
841 if (last_slash_idx != string::npos)
843 const size_t penult_slash_idx = file_path.find_last_of(
"\\/", last_slash_idx-1);
844 if (penult_slash_idx != string::npos)
845 directory = file_path.substr(penult_slash_idx+1, last_slash_idx-penult_slash_idx-1);
865 timeinfo = localtime(&rawtime);
868 strftime(buffer, 80,
"%d-%m-%Y", timeinfo);
878 strptime(str_datetime.c_str(), format, tmbuf);
886 struct timespec ts = {
887 .tv_sec = (time_t) sec,
888 .tv_nsec = (
long) (sec - (long)(sec)) * 1000000000,
friend F32vec4 log(const F32vec4 &a)
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()
struct timespec convert_double_to_timespec(double sec)
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()
string string_format(const string &format, Args ... args)
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)