/* * Util.cpp * stepmania * * Created by Steve Checkoway on Mon Sep 15 2003. * Copyright (c) 2003 Steve Checkoway. All rights reserved. * */ #include #include #include #include #include #include #include #include "Util.h" void split(const CString& Source, const CString& Deliminator, vector& AddIt) { unsigned startpos = 0; do { unsigned pos = Source.find(Deliminator, startpos); if (pos == Source.npos) pos=Source.size(); CString AddCString = Source.substr(startpos, pos-startpos); AddIt.push_back(AddCString); startpos=pos+Deliminator.size(); } while (startpos <= Source.size()); } bool IsADirectory(const CString& path) { struct stat s; if (lstat(path, &s)) return false; return ((s.st_mode & S_IFDIR) == S_IFDIR) && ((s.st_mode & S_IFLNK) != S_IFLNK); } bool DoesFileExist(const CString& path) { struct stat s; return !stat(path, &s); } CString LastPathComponent(const CString& path) { unsigned pos = path.rfind('/'); if (pos == path.npos) pos = 0; return path.substr(pos+1); } void FileListingForDirectoryWithIgnore(const CString& path, CStringArray& list, CStringArray& dirs, const set& ignore, bool ignoreDot) { DIR *dir = opendir(path); dirent *entry; if (!dir) return; while ((entry = readdir(dir))) { if (ignoreDot && entry->d_name[0] == '.') continue; if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue; if (ignore.find(LastPathComponent(path)) != ignore.end()) continue; CString temp = path + "/"; temp += entry->d_name; if (IsADirectory(temp)) { dirs.push_back(temp); FileListingForDirectoryWithIgnore(temp, list, dirs, ignore, ignoreDot); } else list.push_back(temp); } closedir(dir); } int mkdir_p(const CString& path) { CStringArray components; int ret = 0; int fd = open(".", O_RDONLY, 0); split(path, "/", components); if (path[0] == '/') chdir("/"); for (unsigned i=0; i -1 && fd_in != 0) { dup2(fd_in, 0); close(fd_in); } if (fd_out > -1 && fd_in != 1) { dup2(fd_out, 1); close(fd_out); } if (fd_err > -1 && fd_in != 2) { dup2(fd_err, 2); close(fd_err); } execv(path, arguments); _exit(-1); } /* parent */ if (!blocking) return 0; int status, returnStatus; waitpid(pid, &status, 0); returnStatus = WEXITSTATUS(status); return returnStatus; }