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