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

Generated on Wed Nov 25 11:07:09 2009 for dlrUtilities Utility Library by  doxygen 1.5.8