path.cpp

Go to the documentation of this file.
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     // This function returns a bool indicating whether or not the
00032     // specified path is a directory.
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     // This function returns a bool indicating whether or not the
00054     // specified path is a regular file.
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         // if(S_ISREG(statBuf.st_mode)) {
00063         if((statBuf.st_mode & S_IFREG) == S_IFREG) {
00064           return true;
00065         }
00066       }
00067       return false;
00068     }
00069 
00070 
00071     // Joins two path elements with the appropriate delimiter.
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     // Returns the names of the entries in the specified directory, in
00080     // no particular order.
00081     std::vector<std::string>
00082     listDirectory(const std::string& directoryName, bool fullPath)
00083     {
00084       // Dispatch to dlrPortability function.
00085       return dlr::portability::listDirectory(directoryName, fullPath);
00086     }
00087 
00088 
00089     // Returns the names of files in the directory tree below the
00090     // specified directory.
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       // We'll accumulate the total listing in fileNameList.
00101       std::list<std::string> fileNameList;
00102 
00103       // For starters, just find the entries in the specified directory.
00104       std::vector<std::string> topLevelFileNameList =
00105         listDirectory(directoryName);
00106 
00107       // Process each entry in turn.
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           // Looks like this entry is a directory.  Add it, if
00115           // appropriate.
00116           if(includeDirectoryNames) {
00117             fileNameList.push_back(*fileNameIter);
00118           }
00119 
00120           if(*fileNameIter != "." && *fileNameIter != "..") {
00121             // Recurse into non-trivial directory entries, and add the
00122             // result to our accumulated list.
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 {  // if(isDirectory(...))
00130           fileNameList.push_back(*fileNameIter);
00131         }
00132       }
00133     
00134       // Now copy the file names into a vector.  Adding the full path,
00135       // if appriate.
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     // Returns a std::pair<std::string, std::string> containing the fileName
00149     // without its extension, and the extension.
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     // This function accepts a path returns a pair of strings in which
00170     // the first element is the directory name and the second is the
00171     // filename.
00172     std::pair<std::string, std::string>
00173     splitPath(const std::string& path)
00174     {
00175       return dlr::portability::splitPath(path);
00176     }
00177 
00178   } // namespace utilities
00179   
00180 } // namespace dlr

Generated on Mon Jul 9 20:34:03 2007 for dlrLibs Utility Libraries by  doxygen 1.5.2