path.cpp
Go to the documentation of this file.00001
00015 #include <cstring>
00016 #include <sys/types.h>
00017 #include <sys/stat.h>
00018 #include <algorithm>
00019 #include <list>
00020 #include <dlrCommon/functional.h>
00021 #include <dlrPortability/filesystem.h>
00022 #include <dlrUtilities/path.h>
00023
00024 namespace dlr {
00025
00026 namespace utilities {
00027
00028 using dlr::portability::pathDelimiter;
00029 using dlr::portability::extensionDelimiter;
00030
00031
00032
00033
00034 bool
00035 isDirectory(const std::string& path)
00036 {
00037 return dlr::portability::isDirectory(path);
00038 }
00039
00040
00041 bool
00042 isExistingPath(const std::string& path)
00043 {
00044 struct stat statBuf;
00045 memset(&statBuf, 0, sizeof(statBuf));
00046 int returnValue = stat(path.c_str(), &statBuf);
00047 if(returnValue == 0) {
00048 return true;
00049 }
00050 return false;
00051 }
00052
00053
00054
00055
00056 bool
00057 isRegularFile(const std::string& path)
00058 {
00059 struct stat statBuf;
00060 memset(&statBuf, 0, sizeof(statBuf));
00061 int returnValue = stat(path.c_str(), &statBuf);
00062 if(returnValue == 0) {
00063
00064 if((statBuf.st_mode & S_IFREG) == S_IFREG) {
00065 return true;
00066 }
00067 }
00068 return false;
00069 }
00070
00071
00072
00073 std::string
00074 joinPath(const std::string& part0, const std::string& part1)
00075 {
00076 return dlr::portability::joinPath(part0, part1);
00077 }
00078
00079
00080
00081
00082 std::vector<std::string>
00083 listDirectory(const std::string& directoryName, bool fullPath)
00084 {
00085
00086 return dlr::portability::listDirectory(directoryName, fullPath);
00087 }
00088
00089
00090
00091
00092 std::vector<std::string>
00093 recursiveListDirectory(const std::string& directoryName,
00094 bool fullPath,
00095 bool includeDirectoryNames)
00096 {
00097 typedef std::vector<std::string>::const_iterator FileNameIter;
00098 typedef PointerToBinaryFunctionRA<std::string, std::string, std::string>
00099 functorType;
00100
00101
00102 std::list<std::string> fileNameList;
00103
00104
00105 std::vector<std::string> topLevelFileNameList =
00106 listDirectory(directoryName);
00107
00108
00109 for(FileNameIter fileNameIter = topLevelFileNameList.begin();
00110 fileNameIter != topLevelFileNameList.end();
00111 ++fileNameIter) {
00112
00113 std::string fullName = joinPath(directoryName, *fileNameIter);
00114 if(isDirectory(fullName)) {
00115
00116
00117 if(includeDirectoryNames) {
00118 fileNameList.push_back(*fileNameIter);
00119 }
00120
00121 if(*fileNameIter != "." && *fileNameIter != "..") {
00122
00123
00124 std::vector<std::string> subListing =
00125 recursiveListDirectory(fullName, false, includeDirectoryNames);
00126 std::transform(subListing.begin(), subListing.end(),
00127 std::back_inserter(fileNameList),
00128 std::bind1st(functorType(joinPath), *fileNameIter));
00129 }
00130 } else {
00131 fileNameList.push_back(*fileNameIter);
00132 }
00133 }
00134
00135
00136
00137 std::vector<std::string> finalVector(fileNameList.size());
00138 if(fullPath) {
00139 std::transform(fileNameList.begin(), fileNameList.end(),
00140 finalVector.begin(),
00141 std::bind1st(functorType(joinPath), directoryName));
00142 } else {
00143 std::copy(fileNameList.begin(), fileNameList.end(), finalVector.begin());
00144 }
00145 return finalVector;
00146 }
00147
00148
00149
00150
00151 std::pair<std::string, std::string>
00152 splitExtension(const std::string& fileName)
00153 {
00154 std::string::size_type extensionIndex =
00155 fileName.rfind(extensionDelimiter());
00156 if(extensionIndex == std::string::npos) {
00157 return std::make_pair(fileName, std::string(""));
00158 }
00159 std::string::size_type pathIndex =
00160 fileName.rfind(pathDelimiter());
00161 if((pathIndex != std::string::npos)
00162 && (pathIndex > extensionIndex)) {
00163 return std::make_pair(fileName, std::string(""));
00164 }
00165 return std::make_pair(fileName.substr(0, extensionIndex),
00166 fileName.substr(extensionIndex, std::string::npos));
00167 }
00168
00169
00170
00171
00172
00173 std::pair<std::string, std::string>
00174 splitPath(const std::string& path)
00175 {
00176 return dlr::portability::splitPath(path);
00177 }
00178
00179 }
00180
00181 }