SamRewritten/common/functions.cpp
2018-03-02 00:32:44 +01:00

37 lines
854 B
C++

#include "functions.h"
pid_t create_process()
{
pid_t pid;
do {
pid = fork();
} while ((pid == -1) && (errno == EAGAIN));
return pid;
}
bool file_exists(const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
char* concat(const char *s1, const char *s2)
{
char *result = (char*)malloc(strlen(s1)+strlen(s2)+1); //+1 for the null-terminator
//in real code you would check for errors in malloc here
strcpy(result, s1);
strcat(result, s2);
return result;
}
bool strstri(const std::string & strHaystack, const std::string & strNeedle)
{
auto it = std::search(
strHaystack.begin(), strHaystack.end(),
strNeedle.begin(), strNeedle.end(),
[](char ch1, char ch2) { return std::toupper(ch1) == std::toupper(ch2); }
);
return (it != strHaystack.end() );
}