58#define CHECK_BIT(variable, position) ((variable) & (1ULL << (position)))
62template<
typename... Args>
67string double_to_string(
double number,
int precision,
bool is_fixed_point =
true);
91string find_first_number(
const string& str,
size_t& beg_pos,
size_t& end_pos,
bool isOnlyPositive =
true);
93string find_last_number(
const string& str,
size_t& beg_pos,
size_t& end_pos,
bool isOnlyPositive =
true);
104string trim(
const string& str,
const string& whitespace =
" \t\r");
106string reduce(
const string& str,
const string& fill =
" ",
const string& whitespace =
" \t\r");
108bool endswith(
const string& full_str,
const string& ending);
135#ifndef ONLY_DECLARATIONS
147 if ((
f = popen(aCommand.c_str(),
"r")) ==
nullptr) {
148 cout <<
"system_command_linux error: popen failed" << endl;
152 const int BUFSIZE = 4096;
154 if (fgets(buf, BUFSIZE,
f) !=
nullptr)
168 size_t find_result = path.find(
'$');
169 if (find_result != string::npos) {
170 string pre = path.substr(0, find_result);
171 bool is_brace =
false;
172 if (path[find_result] ==
'{') {
176 string post = path.substr(find_result + 1);
179 find_result = post.find(
'}');
181 find_result = post.find(
'/');
182 if ((find_result != string::npos) || (!is_brace)) {
183 string variable = post.substr(0, find_result);
188 post = post.substr(find_result);
190 const char*
v = getenv(variable.c_str());
198 find_result = path.find(
'~');
199 if (find_result != string::npos) {
201 const char*
v = getenv(
"HOME");
206 path.replace(find_result, 1, value);
207 find_result = path.find(
'~');
208 }
while (find_result != string::npos);
218 size_t find_result = path.find(
"sudo");
219 if (find_result != string::npos)
220 if ((path[find_result + 4] ==
' ') || (path[find_result + 4] ==
'\t'))
223 string aCommand, result;
224 aCommand =
string_format(
"echo \"$(cd \"$(dirname \"%s\")\" 2>/dev/null; pwd)/$(basename \"%s\")\" 2>/dev/null",
225 path.c_str(), path.c_str());
235 return sysconf(_SC_NPROCESSORS_ONLN);
243 const int MAX_BUFFER = 255;
244 char buffer[MAX_BUFFER];
245 string command_line, data, strDiff;
249 if (queue_name ==
"")
250 queue_name =
"all.q";
251 command_line = string(
"export SGE_SINGLE_LINE=1; qconf -sq ") + queue_name + string(
" | grep slots");
254 if (queue_name ==
"")
255 queue_name =
"batch";
256 command_line = string(
"qstat -f -Q ") + queue_name + string(
" | grep max_running");
259 if (queue_name ==
"")
260 queue_name =
"interactive";
261 command_line = string(
"scontrol show partition ") + queue_name + string(
" | grep -o 'TotalCPUs=[0-9]*'");
265 FILE* stream = popen(command_line.c_str(),
"r");
266 while (fgets(buffer, MAX_BUFFER, stream) !=
nullptr)
271 size_t found = data.find(
"="), found2 = string::npos;
273 while (found != string::npos) {
274 found2 = data.find(
"]", found);
275 strDiff = data.substr(found + 1, found2 - found - 1);
276 proc_count += atoi(strDiff.c_str());
279 found = data.find(
"=", found2);
283 if (found != string::npos) {
284 strDiff = data.substr(found + 1, data.length() - found - 1);
285 proc_count = atoi(strDiff.c_str());
289 if (found != string::npos) {
290 strDiff = data.substr(found + 1, data.length() - found - 1);
291 proc_count = atoi(strDiff.c_str());
309 pid_t procpid = getpid();
311 toCom <<
"cat /proc/" << procpid <<
"/comm";
314 size_t last_pos = fRes.find_last_not_of(
" \n\r\t") + 1;
315 if (last_pos != string::npos)
316 fRes.erase(last_pos);
324 pid_t procpid = getpid();
326 stringstream command;
327 command <<
"readlink /proc/" << procpid <<
"/exe | sed \"s/\\(\\/" << appName <<
"\\)$//\"";
332 fRes = fRes.erase(fRes.length() - 1, 1);
351template<
typename... Args>
354 int size_s = snprintf(
nullptr, 0, format.c_str(), args...) + 1;
356 throw runtime_error(
"Error during formatting.");
357 auto size =
static_cast<size_t>(size_s);
358 unique_ptr<char[]> buf(
new char[size]);
359 snprintf(buf.get(), size, format.c_str(), args...);
360 return string(buf.get(), buf.get() + size - 1);
370 stream << std::fixed;
371 stream << std::setprecision(precision) << number;
387 stream << std::hex << number;
396 stream << std::hex << hex_string;
405 if (byte_size_in_string.empty())
411 size_t indText = byte_size_in_string.find_first_not_of(
"0123456789,.");
415 string units =
"BKMGTP";
417 size_t convert_dim = units.find(::toupper(convert_to)), value_dim = -1;
418 if (convert_dim == string::npos)
422 if (indText == string::npos) {
423 number = atof(byte_size_in_string.c_str());
426 number = atof(byte_size_in_string.substr(0, indText).c_str());
427 value_dim = units.find(::toupper(byte_size_in_string[indText]));
428 if (value_dim == string::npos)
432 if (convert_dim != value_dim)
433 number *= pow(1024, value_dim - convert_dim);
441 constexpr const char FILE_SIZE_UNITS[8][3]{
"B",
"KB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB"};
459 int order = floor(
log(byte_size) /
log(1024));
460 float num = ((float)byte_size) / pow(1024, order);
461 return string_format(
"%.*f %s", precision, num, FILE_SIZE_UNITS[order]);
468 string::const_iterator it = s.begin();
469 while (it != s.end() && std::isdigit(*it))
471 return !s.empty() && it == s.end();
477 vector<string> result_vector;
479 if (delimiters ==
"") {
480 result_vector.push_back(str);
481 return result_vector;
484 char delimiter = delimiters[0];
486 for (
size_t i = 1;
i < delimiters.length();
i++)
487 replace(str.begin(), str.end(), delimiters[
i], delimiter);
489 stringstream ss(str);
493 while (getline(ss, segment, delimiter))
494 result_vector.push_back(segment);
496 return result_vector;
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);
533 size_t n = str.find_first_of(
"0123456789");
534 if (n != string::npos) {
535 size_t m = str.find_first_not_of(
"0123456789,.", n);
536 if ((!isOnlyPositive) && (n > 0) && ((str[n - 1] ==
'+') || (str[n - 1] ==
'-')))
538 return str.substr(n,
m != string::npos ?
m - n :
m);
546 size_t n = str.find_last_of(
"0123456789");
547 if (n != string::npos) {
548 string temp = str.substr(0, n + 1);
549 size_t m = temp.find_last_not_of(
"0123456789,.");
550 if (
m != string::npos) {
551 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
553 return temp.substr(
m + 1, n -
m);
561string find_first_number(
string const& str,
size_t& beg_pos,
size_t& end_pos,
bool isOnlyPositive)
563 beg_pos = str.find_first_of(
"0123456789", beg_pos);
564 if (beg_pos != string::npos) {
565 size_t m = str.find_first_not_of(
"0123456789", beg_pos);
566 end_pos = (
m != string::npos ?
m - 1 : str.length() - 1);
567 if ((!isOnlyPositive) && (beg_pos > 0) && ((str[beg_pos - 1] ==
'+') || (str[beg_pos - 1] ==
'-')))
569 return str.substr(beg_pos,
m != string::npos ?
m - beg_pos :
m);
572 end_pos = string::npos;
576string find_last_number(
string const& str,
size_t& beg_pos,
size_t& end_pos,
bool isOnlyPositive)
578 end_pos = str.find_last_of(
"0123456789", end_pos);
579 if (end_pos != string::npos) {
580 string temp = str.substr(0, end_pos + 1);
581 size_t m = temp.find_last_not_of(
"0123456789");
582 beg_pos = (
m != string::npos ?
m + 1 : 0);
583 if (
m != string::npos) {
584 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
586 return temp.substr(beg_pos, end_pos - beg_pos + 1);
591 beg_pos = string::npos;
597 beg_pos = str.find_first_of(
"0123456789", beg_pos);
598 if (beg_pos != string::npos) {
599 size_t m = str.find_first_not_of(
"0123456789,.", beg_pos);
600 end_pos = (
m != string::npos ?
m - 1 : str.length() - 1);
601 if ((!isOnlyPositive) && (beg_pos > 0) && ((str[beg_pos - 1] ==
'+') || (str[beg_pos - 1] ==
'-')))
603 return str.substr(beg_pos,
m != string::npos ?
m - beg_pos :
m);
606 end_pos = string::npos;
612 end_pos = str.find_last_of(
"0123456789", end_pos);
613 if (end_pos != string::npos) {
614 string temp = str.substr(0, end_pos + 1);
615 size_t m = temp.find_last_not_of(
"0123456789,.");
616 beg_pos = (
m != string::npos ?
m + 1 : 0);
617 if (
m != string::npos) {
618 if ((!isOnlyPositive) && ((str[
m] ==
'+') || (str[
m] ==
'-')))
620 return temp.substr(beg_pos, end_pos - beg_pos + 1);
625 beg_pos = string::npos;
632 if (input_char_array ==
nullptr)
635 const int length = strlen(input_char_array);
636 char* lower =
new char[length + 1];
640 for (
int i = 0;
i < length;
i++)
641 lower[
i] = tolower(input_char_array[
i]);
651 start = text.find(old_substring, start + 1);
653 text.replace(start, old_substring.length(), new_substring.c_str());
654 start += new_substring.length() - old_substring.length();
656 }
while (start > -1);
662 string new_substring =
"";
665 start = text.find(old_substring, start + 1);
667 if (new_substring ==
"") {
669 sprintf(buf_int,
"%d", new_subinteger);
670 new_substring = buf_int;
672 text.replace(start, old_substring.length(), new_substring.c_str());
673 start += new_substring.length() - old_substring.length();
675 }
while (start > -1);
681 int replace_count = 0;
682 char* cur_pos = strchr(str, find);
686 cur_pos = strchr(cur_pos, find);
689 return replace_count;
693string trim(
const string& str,
const string& whitespace)
695 size_t strBegin = str.find_first_not_of(whitespace);
696 if (strBegin == string::npos)
699 size_t strEnd = str.find_last_not_of(whitespace);
700 size_t strRange = strEnd - strBegin + 1;
702 return str.substr(strBegin, strRange);
706string reduce(
const string& str,
const string& fill,
const string& whitespace)
709 string result =
trim(str, whitespace);
712 size_t beginSpace = result.find_first_of(whitespace);
713 while (beginSpace != string::npos) {
714 size_t endSpace = result.find_first_not_of(whitespace, beginSpace);
715 size_t range = endSpace - beginSpace;
717 result.replace(beginSpace, range, fill);
719 size_t newStart = beginSpace + fill.length();
720 beginSpace = result.find_first_of(whitespace, newStart);
727bool endswith(
string const& full_str,
string const& ending)
729 if (full_str.length() >= ending.length())
730 return (0 == full_str.compare(full_str.length() - ending.length(), ending.length(), ending));
746 if (stat(path, &info) != 0)
748 else if (info.st_mode & S_IFDIR)
757 struct stat info = {0};
759 if (stat(path, &info) == -1) {
760 int status = mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
763 if (info.st_mode & S_IFDIR)
774 string str_path(path);
775 size_t pos = str_path.find_last_of(
'/');
776 if (pos == string::npos)
778 str_path = str_path.substr(0, pos);
788 status = mkdir(str_path.c_str(), mode);
805 size_t last_slash_idx = path.find_last_of(
"\\/");
806 if (last_slash_idx != string::npos)
807 path.erase(0, last_slash_idx + 1);
810 size_t period_idx = path.rfind(
'.');
811 if (period_idx != string::npos)
812 path.erase(period_idx);
821 size_t last_slash_idx = path.find_last_of(
"\\/");
822 if (last_slash_idx != string::npos)
823 path.erase(0, last_slash_idx + 1);
831 string directory =
"";
832 const size_t last_slash_idx = file_path.find_last_of(
"\\/");
833 if (last_slash_idx != string::npos)
834 directory = file_path.substr(0, last_slash_idx);
842 string directory =
"";
843 const size_t last_slash_idx = file_path.find_last_of(
"\\/");
844 if (last_slash_idx != string::npos) {
845 const size_t penult_slash_idx = file_path.find_last_of(
"\\/", last_slash_idx - 1);
846 if (penult_slash_idx != string::npos)
847 directory = file_path.substr(penult_slash_idx + 1, last_slash_idx - penult_slash_idx - 1);
866 timeinfo = localtime(&rawtime);
869 strftime(buffer, 80,
"%d-%m-%Y", timeinfo);
879 strptime(str_datetime.c_str(), format, tmbuf);
887 struct timespec ts = {
888 .tv_sec = (time_t)sec,
889 .tv_nsec = (
long)(sec - (long)(sec)) * 1000000000,
friend F32vec4 log(const F32vec4 &a)
string find_last_double_number(const string &str, bool isOnlyPositive=true)
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_first_number(const string &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)
tm convert_string_to_datetime(string str_datetime, const char *format="%d.%m.%Y %H:%M:%S")
string find_first_double_number(const string &str, bool isOnlyPositive=true)
char * convert_pchar_to_lowercase_new(char *input_char_array)
vector< string > string_to_vector(string str, const string &delimiters=",;")
int system_command_linux(string aCommand, string &result)
string expand_path(string path)
bool endswith(const string &full_str, const string &ending)
string get_file_name(string path)
string get_app_name_linux()
struct timespec convert_double_to_timespec(double sec)
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 find_last_number(const string &str, bool isOnlyPositive=true)
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)
string byte_size_to_human(uint64_t byte_size, unsigned precision=2)
string string_format(const string &format, Args... args)