48 #define access _access
49 inline char* realpath(
const char* path,
char* resolved_path) {
50 return _fullpath(resolved_path, path, PATH_MAX);
57 #define pclose _pclose
65 inline bool existFile(
const std::string& name) {
66 struct stat buffer = {};
67 if (stat(name.c_str(), &buffer) == 0) {
68 if ((buffer.st_mode & S_IFMT) != 0) {
78 inline bool existDir(
const std::string& name) {
79 struct stat buffer = {};
80 if (stat(name.c_str(), &buffer) == 0) {
81 if ((buffer.st_mode & S_IFDIR) != 0) {
92 return existFile(name) && (access(name.c_str(), X_OK) == 0);
98 inline std::string
which(
const std::string& name) {
100 if (name.find(
'/') != std::string::npos) {
104 const char* syspath = ::getenv(
"PATH");
105 if (syspath ==
nullptr) {
109 std::stringstream sstr;
114 while (std::getline(sstr,
sub,
':')) {
115 std::string path =
sub +
"/" + name;
126 inline std::string
dirName(
const std::string& name) {
130 size_t lastNotSlash = name.find_last_not_of(
'/');
132 if (lastNotSlash == std::string::npos) {
135 size_t leadingSlash = name.find_last_of(
'/', lastNotSlash);
137 if (leadingSlash == std::string::npos) {
141 if (leadingSlash == 0) {
144 return name.substr(0, leadingSlash);
150 inline std::string
absPath(
const std::string& path) {
152 char* res = realpath(path.c_str(), buf);
153 return (res ==
nullptr) ?
"" : std::string(buf);
159 inline std::string
pathJoin(
const std::string& first,
const std::string& second) {
160 unsigned firstPos =
static_cast<unsigned>(first.size()) - 1;
161 while (first.at(firstPos) ==
'/') {
164 unsigned secondPos = 0;
165 while (second.at(secondPos) ==
'/') {
168 return first.substr(0, firstPos + 1) +
'/' + second.substr(secondPos);
176 inline std::string
findTool(
const std::string& tool,
const std::string&
base,
const std::string& path) {
178 std::stringstream sstr(path);
181 while (std::getline(sstr,
sub,
':')) {
182 std::string subpath = dir +
"/" +
sub +
'/' + tool;
193 inline std::string
baseName(
const std::string& filename) {
194 if (filename.empty()) {
198 size_t lastNotSlash = filename.find_last_not_of(
'/');
199 if (lastNotSlash == std::string::npos) {
203 size_t lastSlashBeforeBasename = filename.find_last_of(
'/', lastNotSlash - 1);
204 if (lastSlashBeforeBasename == std::string::npos) {
205 lastSlashBeforeBasename =
static_cast<size_t>(-1);
207 return filename.substr(lastSlashBeforeBasename + 1, lastNotSlash - lastSlashBeforeBasename);
213 inline std::string
simpleName(
const std::string& path) {
215 const size_t lastDot = name.find_last_of(
'.');
217 if (lastDot == std::string::npos) {
220 const size_t lastSlash = name.find_last_of(
'/');
222 if (lastSlash != std::string::npos && lastSlash > lastDot) {
226 return name.substr(0, lastDot);
233 std::string name = path;
234 const size_t lastDot = name.find_last_of(
'.');
236 if (lastDot == std::string::npos) {
237 return std::string();
239 const size_t lastSlash = name.find_last_of(
'/');
241 if (lastSlash != std::string::npos && lastSlash > lastDot) {
242 return std::string();
245 return name.substr(lastDot + 1);
254 std::FILE* f =
nullptr;
255 while (f ==
nullptr) {
256 templ = std::tmpnam(
nullptr);
257 f = fopen(templ.c_str(),
"wx");
262 char templ[40] =
"./souffleXXXXXX";
263 close(mkstemp(templ));
264 return std::string(templ);
268 inline std::stringstream
execStdOut(
char const* cmd) {
269 FILE* in = popen(cmd,
"r");
270 std::stringstream
data;
271 while (in !=
nullptr) {
276 data << static_cast<char>(c);
282 inline std::stringstream
execStdOut(std::string
const& cmd) {
286 class TempFileStream :
public std::fstream {