49#include <boost/filesystem/operations.hpp>
50#include <boost/filesystem/convenience.hpp>
51#include <boost/filesystem/exception.hpp>
54#include <boost/regex.hpp>
56#include <boost/format.hpp>
59#include "tce_config.h"
69using namespace boost::filesystem;
81 Path origString(fileName);
82 string file = origString.string();
84 string::size_type lastPathPos = file.rfind(
DS.string());
85 if (lastPathPos == string::npos) {
89 string dirPath = file.substr(0, lastPathPos);
105 Path origString(pathName);
106 string path = origString.string();
109 return path.substr(index + 1);
125 if (fileName ==
"") {
146 const int increment = 100;
148 char* buf =
new char[bufSize];
151 char* retValue = getcwd(buf, bufSize);
154 while (retValue == NULL && errno == ERANGE) {
156 bufSize += increment;
157 buf =
new char[bufSize];
158 retValue = getcwd(buf, bufSize);
161 std::string dirName =
"";
162 if (retValue == NULL) {
164 std::string procName =
"FileSystem::currentWorkingDir";
165 std::string errorMsg =
"Current working directory cannot be read.";
175 return path.string();
186 return (chdir(path.c_str()) == 0);
198 const std::string& pattern,
199 std::vector<std::string>& filenames) {
202 glob(pattern.c_str(), 0, NULL, &globbuf);
203 for (
size_t i = 0; i < globbuf.gl_pathc; i++) {
204 filenames.push_back(globbuf.gl_pathv[i]);
218 string withoutTilde = stringWithTilde;
219 if (withoutTilde ==
"~" || withoutTilde.substr(0, 2) ==
"~/") {
220 withoutTilde.erase(0, 1);
236 string pathString = path.string();
238 return pathString.substr(0, 1) ==
239 DS.string() ? true :
false;
254 string pathString = path.string();
266 return (!pathName.empty() &&
280 const Path path(fileName);
281 return boost::filesystem::extension(path);
292 const Path path(fileName);
293 return boost::filesystem::basename(path);
304 string absolutePath(
"");
306 absolutePath = pathName;
310 Path path(absolutePath);
312 return path.string();
324 if (chmod(fileName.c_str(), S_IXUSR | S_IRUSR | S_IWUSR) != 0) {
328 if (chmod(fileName.c_str(), S_IXUSR | S_IWUSR) != 0) {
332 if (chmod(fileName.c_str(), S_IXUSR | S_IRUSR) != 0) {
336 if (chmod(fileName.c_str(), S_IXUSR) != 0) {
354 return std::time_t(-1);
357 std::time_t lastModTime;
359 lastModTime = boost::filesystem::last_write_time(filePath);
361 lastModTime = std::time_t(-1);
378 return static_cast<uintmax_t
>(-1);
383 fileSize = boost::filesystem::file_size(filePath);
385 fileSize =
static_cast<uintmax_t
>(-1);
401 std::string p = path;
406 Path orPath(p.substr(1));
407 string origPath = orPath.string();
408 string currentPath =
DS.string();
409 while (origPath.size() > 0) {
410 string::size_type pos = origPath.find(
DS.string());
411 if (pos == string::npos) {
412 currentPath += origPath;
415 currentPath += origPath.substr(0, pos);
416 origPath.replace(0, pos + 1,
"");
419 Path dirPath(currentPath);
420 if (!boost::filesystem::exists(dirPath)) {
421 boost::filesystem::create_directory(dirPath);
427 currentPath +=
DS.string();
442 const std::string& path,
443 const std::string& tempDirPrefix) {
444 const int RANDOM_CHARS = 10;
446 string tempDir = path +
DS + tempDirPrefix;
448 for (
int i = 0; i < RANDOM_CHARS ||
fileExists(tempDir); ++i) {
471 string fileName = filePath.string();
478 FILE* f = fopen(fileName.c_str(),
"w");
497 boost::filesystem::remove_all(tcePath);
525 namespace fs = boost::filesystem;
526 Path sourcePath(source);
527 Path targetPath(target);
530 if (fs::exists(targetPath)) {
532 fs::remove(targetPath);
534 fs::path::iterator lastIt = --(sourcePath.end());
535 targetPath /= *lastIt;
538 fs::remove(targetPath);
542 fs::copy_file(sourcePath, targetPath);
543 }
catch (boost::filesystem::filesystem_error
const& e) {
547 "Unable to copy '%s' to '%s'") %
548 sourcePath.string() % targetPath.string()).str());
563 const std::vector<std::string>& searchPaths,
const std::string& file) {
570 string errorMsg =
"File " + file +
" not found.";
575 for (vector<string>::const_iterator iter = searchPaths.begin();
576 iter != searchPaths.end(); iter++) {
578 string pathToFile = path +
DS + file;
584 string errorMsg =
"File " + file +
" not found in any search path.";
585 errorMsg +=
"Searched paths:\n";
586 for (
unsigned int i = 0; i < searchPaths.size(); i++) {
587 errorMsg += searchPaths.at(i) +
"\n";
599std::vector<std::string>
601 const std::string& directory,
const bool absolutePaths) {
603 std::vector<std::string> contents;
605 boost::filesystem::directory_iterator end_iter;
607 if (directory !=
"") {
608 Path path(directory);
609 for (boost::filesystem::directory_iterator iter(path);
610 iter != end_iter; iter++) {
614 contents.push_back(iter->path().string());
619 for (boost::filesystem::directory_iterator iter(
620 boost::filesystem::current_path());
621 iter != end_iter; iter++) {
625 contents.push_back(iter->path().string());
632 }
catch (
const boost::filesystem::filesystem_error& e) {
646std::vector<std::string>
648 std::vector<std::string> subTrees;
651 directory_iterator end_itr;
653 for (directory_iterator itr(directory); itr != end_itr; ++itr) {
654 if (is_directory(*itr) && exists(*itr) &&
655 (*itr).path().string().find(
".") == string::npos) {
656 subTrees.push_back((*itr).path().string());
658 std::vector<std::string> subSubTrees =
661 for (
size_t i = 0; i < subSubTrees.size(); ++i) {
662 subTrees.push_back(subSubTrees.at(i));
666 }
catch (
const boost::filesystem::filesystem_error& e) {
683 const Path& startDirectory,
684 const std::string& fileName,
687 if (!exists(startDirectory)) {
690 directory_iterator end_itr;
692 for (directory_iterator itr(startDirectory); itr != end_itr; ++itr) {
693 if (is_directory(*itr)) {
694 Path p((*itr).path().string());
698#if BOOST_VERSION >= 103600
699 else if (itr->path().filename() == fileName)
701 else if (itr->leaf() == fileName)
704 pathFound =
Path((*itr).path().string());
725 const std::string& first,
726 const std::string& second,
727 const std::string& rootDirectory) {
729 std::string testRootDir = rootDirectory;
734 if (first == second) {
742 return ((testRootDir + first) == second) ? true :
false;
744 return ((testRootDir + second) == first) ? true :
false;
764 namespace fs = boost::filesystem;
766 Path basePath(baseDir);
767 Path toRelPath(toRelDir);
769 fs::path::iterator dstIt = basePath.begin();
770 fs::path::iterator POIt = toRelPath.begin();
772 fs::path::iterator dstEndIt = basePath.end();
773 fs::path::iterator POEndIt = toRelPath.end();
775 unsigned int sameCount = 0;
776 for (; dstIt != dstEndIt && POIt != POEndIt && *dstIt == *POIt;
777 ++dstIt, ++POIt, ++sameCount) {}
786 if (dstIt == dstEndIt) {
788 while (POIt != POEndIt) {
789#if BOOST_FILESYSTEM_VERSION < 3
790 toRelDir.append(*POIt++);
792 std::string
const tmp = POIt->string();
793 toRelDir.append(tmp);
796 if (POIt != POEndIt) {
803 while (POIt != POEndIt) {
804#if BOOST_FILESYSTEM_VERSION < 3
805 temp.append(*POIt++);
807 std::string
const tmp = POIt->string();
811 if (POIt != POEndIt) {
816 unsigned int diffCount = 0;
817 for (; dstIt != dstEndIt; ++dstIt, ++diffCount) {
821 for (
unsigned int i = 0; diffCount > i; ++i) {
822 toRelDir.append(
"..");
825 toRelDir.append(temp);
845 const std::vector<std::string>& searchPaths,
846 const std::string& basePath,
847 std::string& toRelPath) {
853 for (
unsigned int i = 0; i < searchPaths.size(); ++i) {
854 string searchPath = searchPaths.at(i);
855 string relativePath = basePath;
862 if (relativePath ==
"") {
863 toRelPath = relativePath;
866 if (fullPath.length() == basePath.length()) {
867 toRelPath = relativePath;
895 const std::string& sourceFile,
896 const std::string& blockStartRE,
897 const std::string& blockEndRE,
898 std::string& readBlock,
899 const bool includeMatchingLines) {
901 const int LINESIZE = 256;
904 const boost::regex reStart(blockStartRE,
905 boost::regex::perl|boost::regex::icase);
906 const boost::regex reEnd(blockEndRE,
907 boost::regex::perl|boost::regex::icase);
909 string::const_iterator begin;
910 string::const_iterator end;
912 std::ifstream ifs(sourceFile.c_str(), std::ifstream::in);
913 boost::match_results<string::const_iterator> matches;
914 bool outsideBlock =
true;
917 ifs.getline(line, LINESIZE-1);
918 stemp = string(line);
919 begin = stemp.begin();
923 if (boost::regex_search(begin, end, outsideBlock ? reStart : reEnd)) {
924 if (includeMatchingLines) {
925 readBlock.append(stemp +
"\n");
930 outsideBlock =
false;
932 }
else if (!outsideBlock) {
933 readBlock.append(stemp +
"\n");
959 const std::string& targetFile,
960 const std::string& ARStartRE,
961 const std::string& writeToFile,
962 const std::string& AREndRE,
963 const bool discardBlockBorder) {
965 const int LINESIZE = 1000;
968 const boost::regex reStart(ARStartRE,
969 boost::regex::perl|boost::regex::icase);
971 if (!AREndRE.empty()) {
972 reEnd = boost::regex(AREndRE, boost::regex::perl|boost::regex::icase);
974 boost::regex re = reStart;
976 string::const_iterator begin;
977 string::const_iterator end;
980 int blockStartPos = -1;
981 std::fstream fs(targetFile.c_str(), std::fstream::in | std::fstream::out);
982 boost::match_results<string::const_iterator> matches;
984 bool beforeBlock =
true;
985 bool afterBlock =
false;
988 fs.getline(line, LINESIZE-1);
991 if (blockStartPos != -1) {
993 fs.seekp(blockStartPos);
994 fs.seekg(blockStartPos);
1007 fs.write(writeToFile.c_str(), writeToFile.length());
1008 fs.write(endFile.c_str(), endFile.length());
1010 if (truncate(targetFile.c_str(), fs.tellp()) != 0) {
1017 stemp = string(line);
1018 begin = stemp.begin();
1022 if (!afterBlock && boost::regex_search(begin, end, re)) {
1025 beforeBlock =
false;
1027 afterBlock = AREndRE.empty() ? true : afterBlock;
1029 if (discardBlockBorder) {
1030 blockStartPos =
static_cast<int>(fs.tellg()) -
1031 static_cast<int>(fs.gcount());
1033 blockStartPos = fs.tellg();
1038 if (!discardBlockBorder) {
1039 endFile.append(stemp +
"\n");
1043 }
else if (afterBlock) {
1045 endFile.append(stemp +
"\n");
1061 std::ifstream fileStream(filepath);
1062 if (!fileStream.is_open()) {
1066 std::string dummyLine;
1067 while (std::getline(fileStream, dummyLine)) {
1087 boost::filesystem::path(path) {
1101 if (
this == &pathName) {
1105 boost::filesystem::path::assign(
1106 pathName.string().begin(), pathName.string().end());
1113Path::operator std::string()
const {
1114 return this->string();
1121 return this->string();
1129 return this->
string().c_str();
1136 return Path(boost::filesystem::operator/(path, fileOrDir));
#define abortWithError(message)
#define assert(condition)
#define IGNORE_CLANG_WARNING(X)
Path operator/(const Path &path, const std::string &fileOrDir)
static void writeToErrorLog(const std::string fileName, const int lineNumber, const std::string functionName, const std::string message, const int neededVerbosity=0)
static std::string homeDirectory()
static bool createFile(const std::string &file)
static bool isRelativePath(const std::string &pathName)
static bool findFileInDirectoryTree(const Path &startDirectory, const std::string &fileName, Path &pathFound)
static std::string absolutePathOf(const std::string &pathName)
static bool isAbsolutePath(const std::string &pathName)
static bool readBlockFromFile(const std::string &sourceFile, const std::string &blockStartRE, const std::string &blockEndRE, std::string &readBlock, const bool includeMatchingLines=true)
static bool appendReplaceFile(const std::string &targetFile, const std::string &ARStartRE, const std::string &writeToFile, const std::string &AREndRE="", const bool discardBlockBorder="true")
static void globPath(const std::string &pattern, std::vector< std::string > &filenames)
static bool fileIsReadable(const std::string fileName)
static bool createDirectory(const std::string &path)
static uintmax_t sizeInBytes(const std::string &filePath)
static bool removeFileOrDirectory(const std::string &path)
static bool changeWorkingDir(const std::string &path)
static const std::string DIRECTORY_SEPARATOR
static std::time_t lastModificationTime(const std::string &filePath)
static std::string createTempDirectory(const std::string &path="/tmp", const std::string &tempDirPrefix="tmp_tce_")
static std::string fileNameBody(const std::string &fileName)
static std::vector< std::string > directoryContents(const std::string &directory, const bool absolutePaths=true)
static std::string fileOfPath(const std::string pathName)
static bool compareFileNames(const std::string &first, const std::string &second, const std::string &rootDirectory)
static const std::string CURRENT_DIRECTORY
static void copy(const std::string &source, const std::string &target)
static std::string directoryOfPath(const std::string fileName)
static std::string fileExtension(const std::string &fileName)
static std::string currentWorkingDir()
static std::string findFileInSearchPaths(const std::vector< std::string > &searchPaths, const std::string &file)
static const std::string STRING_WILD_CARD
static bool fileIsDirectory(const std::string fileName)
static bool setFileExecutable(const std::string fileName)
static bool relativeDir(const std::string &baseDir, std::string &toRelDir)
static std::vector< std::string > directorySubTrees(const std::string &directory)
static bool makeRelativePath(const std::vector< std::string > &searchPaths, const std::string &basePath, std::string &toRelPath)
static bool fileIsWritable(const std::string fileName)
static bool isPath(const std::string &pathName)
static int countLines(const std::string &filepath)
static bool fileIsCreatable(const std::string fileName)
static bool fileExists(const std::string fileName)
static std::string expandTilde(const std::string &stringWithTilde)
Path & operator=(const boost::filesystem::path &pathName)
const char * c_str() const
operator std::string() const