utility: added FilePath abstraction and rewrote data management to use it
The utility class FilePath wraps an instance of boost::filesystem::path. Using FilePath allows for easy comparision of file paths on different platforms using absolute or relative paths. Whereever file paths are compared, this wrapper should be used.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "FileInfo.h"
|
||||
|
||||
FileInfo::FileInfo(std::string path, boost::posix_time::ptime lastWriteTime)
|
||||
FileInfo::FileInfo(const FilePath& path, boost::posix_time::ptime lastWriteTime)
|
||||
: path(path)
|
||||
, lastWriteTime(lastWriteTime)
|
||||
{
|
||||
|
||||
@@ -2,13 +2,16 @@
|
||||
#define FILE_INFO_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "boost/date_time.hpp"
|
||||
|
||||
#include "utility/file/FilePath.h"
|
||||
|
||||
struct FileInfo
|
||||
{
|
||||
FileInfo(std::string path, boost::posix_time::ptime lastWriteTime);
|
||||
FileInfo(const FilePath& path, boost::posix_time::ptime lastWriteTime);
|
||||
|
||||
std::string path;
|
||||
FilePath path;
|
||||
boost::posix_time::ptime lastWriteTime;
|
||||
};
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ void FileManager::fetchFilePaths()
|
||||
m_updatedFiles.clear();
|
||||
m_removedFiles.clear();
|
||||
|
||||
for (std::map<std::string, FileInfo>::iterator it = m_files.begin(); it != m_files.end(); it++)
|
||||
for (std::map<FilePath, FileInfo>::iterator it = m_files.begin(); it != m_files.end(); it++)
|
||||
{
|
||||
m_removedFiles.insert(it->first);
|
||||
}
|
||||
@@ -52,8 +52,8 @@ void FileManager::fetchFilePaths()
|
||||
|
||||
for (FileInfo fileInfo: fileInfos)
|
||||
{
|
||||
const std::string& filePath = fileInfo.path;
|
||||
std::map<std::string, FileInfo>::iterator it = m_files.find(filePath);
|
||||
const FilePath& filePath = fileInfo.path;
|
||||
std::map<FilePath, FileInfo>::iterator it = m_files.find(filePath);
|
||||
if (it != m_files.end())
|
||||
{
|
||||
m_removedFiles.erase(filePath);
|
||||
@@ -65,44 +65,44 @@ void FileManager::fetchFilePaths()
|
||||
}
|
||||
else
|
||||
{
|
||||
m_files.insert(std::pair<std::string, FileInfo>(filePath, fileInfo));
|
||||
m_files.insert(std::pair<FilePath, FileInfo>(filePath, fileInfo));
|
||||
m_addedFiles.insert(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const std::string filePath : m_removedFiles)
|
||||
for (const FilePath& filePath : m_removedFiles)
|
||||
{
|
||||
m_files.erase(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
std::set<std::string> FileManager::getAddedFilePaths() const
|
||||
std::set<FilePath> FileManager::getAddedFilePaths() const
|
||||
{
|
||||
return m_addedFiles;
|
||||
}
|
||||
|
||||
std::set<std::string> FileManager::getUpdatedFilePaths() const
|
||||
std::set<FilePath> FileManager::getUpdatedFilePaths() const
|
||||
{
|
||||
return m_updatedFiles;
|
||||
}
|
||||
|
||||
std::set<std::string> FileManager::getRemovedFilePaths() const
|
||||
std::set<FilePath> FileManager::getRemovedFilePaths() const
|
||||
{
|
||||
return m_removedFiles;
|
||||
}
|
||||
|
||||
bool FileManager::hasFilePath(const std::string& filePath) const
|
||||
bool FileManager::hasFilePath(const FilePath& filePath) const
|
||||
{
|
||||
return (m_files.find(FileSystem::absoluteFilePath(filePath)) != m_files.end());
|
||||
return (m_files.find(filePath) != m_files.end());
|
||||
}
|
||||
|
||||
bool FileManager::hasSourceExtension(const std::string& filePath) const
|
||||
bool FileManager::hasSourceExtension(const FilePath& filePath) const
|
||||
{
|
||||
return FileSystem::hasExtension(filePath, m_sourceExtensions);
|
||||
return filePath.hasExtension(m_sourceExtensions);
|
||||
}
|
||||
|
||||
bool FileManager::hasIncludeExtension(const std::string& filePath) const
|
||||
bool FileManager::hasIncludeExtension(const FilePath& filePath) const
|
||||
{
|
||||
return FileSystem::hasExtension(filePath, m_includeExtensions);
|
||||
return filePath.hasExtension(m_includeExtensions);
|
||||
}
|
||||
|
||||
@@ -20,13 +20,13 @@ public:
|
||||
void reset();
|
||||
void fetchFilePaths();
|
||||
|
||||
std::set<std::string> getAddedFilePaths() const;
|
||||
std::set<std::string> getUpdatedFilePaths() const;
|
||||
std::set<std::string> getRemovedFilePaths() const;
|
||||
std::set<FilePath> getAddedFilePaths() const;
|
||||
std::set<FilePath> getUpdatedFilePaths() const;
|
||||
std::set<FilePath> getRemovedFilePaths() const;
|
||||
|
||||
virtual bool hasFilePath(const std::string& filePath) const;
|
||||
virtual bool hasSourceExtension(const std::string& filePath) const;
|
||||
virtual bool hasIncludeExtension(const std::string& filePath) const;
|
||||
virtual bool hasFilePath(const FilePath& filePath) const;
|
||||
virtual bool hasSourceExtension(const FilePath& filePath) const;
|
||||
virtual bool hasIncludeExtension(const FilePath& filePath) const;
|
||||
|
||||
private:
|
||||
std::vector<std::string> m_sourcePaths;
|
||||
@@ -34,10 +34,10 @@ private:
|
||||
std::vector<std::string> m_sourceExtensions;
|
||||
std::vector<std::string> m_includeExtensions;
|
||||
|
||||
std::map<std::string, FileInfo> m_files;
|
||||
std::set<std::string> m_addedFiles;
|
||||
std::set<std::string> m_updatedFiles;
|
||||
std::set<std::string> m_removedFiles;
|
||||
std::map<FilePath, FileInfo> m_files;
|
||||
std::set<FilePath> m_addedFiles;
|
||||
std::set<FilePath> m_updatedFiles;
|
||||
std::set<FilePath> m_removedFiles;
|
||||
};
|
||||
|
||||
#endif // FILE_MANAGER_H
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
#include "utility/file/FilePath.h"
|
||||
|
||||
FilePath::FilePath(const char* filePath)
|
||||
: m_path(filePath)
|
||||
{
|
||||
}
|
||||
|
||||
FilePath::FilePath(const std::string& filePath)
|
||||
: m_path(filePath)
|
||||
{
|
||||
}
|
||||
|
||||
FilePath::FilePath(const boost::filesystem::path& filePath)
|
||||
: m_path(filePath)
|
||||
{
|
||||
}
|
||||
|
||||
bool FilePath::exists() const
|
||||
{
|
||||
return boost::filesystem::exists(m_path);
|
||||
}
|
||||
|
||||
std::string FilePath::str() const
|
||||
{
|
||||
return m_path.generic_string();
|
||||
}
|
||||
|
||||
std::string FilePath::absoluteStr() const
|
||||
{
|
||||
return boost::filesystem::absolute(m_path).generic_string();
|
||||
}
|
||||
|
||||
std::string FilePath::fileName() const
|
||||
{
|
||||
return m_path.filename().generic_string();
|
||||
}
|
||||
|
||||
std::string FilePath::extension() const
|
||||
{
|
||||
return m_path.extension().generic_string();
|
||||
}
|
||||
|
||||
FilePath FilePath::withoutExtension() const
|
||||
{
|
||||
return FilePath(boost::filesystem::path(m_path).replace_extension());
|
||||
}
|
||||
|
||||
bool FilePath::hasExtension(const std::vector<std::string>& extensions) const
|
||||
{
|
||||
std::string e = extension();
|
||||
for (std::string ext : extensions)
|
||||
{
|
||||
if (e == ext)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FilePath::operator==(const FilePath& other) const
|
||||
{
|
||||
if (exists() && other.exists())
|
||||
{
|
||||
return boost::filesystem::equivalent(m_path, other.m_path);
|
||||
}
|
||||
|
||||
return m_path.compare(other.m_path) == 0;
|
||||
}
|
||||
|
||||
bool FilePath::operator!=(const FilePath& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
bool FilePath::operator<(const FilePath& other) const
|
||||
{
|
||||
return m_path.compare(other.m_path) < 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef FILE_PATH_H
|
||||
#define FILE_PATH_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "boost/filesystem.hpp"
|
||||
|
||||
class FilePath
|
||||
{
|
||||
public:
|
||||
FilePath(const char* filePath);
|
||||
FilePath(const std::string& filePath);
|
||||
FilePath(const boost::filesystem::path& filePath);
|
||||
|
||||
bool exists() const;
|
||||
|
||||
std::string str() const;
|
||||
std::string absoluteStr() const;
|
||||
std::string fileName() const;
|
||||
|
||||
std::string extension() const;
|
||||
FilePath withoutExtension() const;
|
||||
bool hasExtension(const std::vector<std::string>& extensions) const;
|
||||
|
||||
bool operator==(const FilePath& other) const;
|
||||
bool operator!=(const FilePath& other) const;
|
||||
bool operator<(const FilePath& other) const;
|
||||
|
||||
private:
|
||||
boost::filesystem::path m_path;
|
||||
};
|
||||
|
||||
#endif // FILE_PATH_H
|
||||
@@ -3,10 +3,10 @@
|
||||
#include "utility/file/FileManager.h"
|
||||
#include "utility/file/FileSystem.h"
|
||||
|
||||
FileRegister::FileRegister(const FileManager* fileManager, const std::vector<std::string>& filePaths)
|
||||
FileRegister::FileRegister(const FileManager* fileManager, const std::vector<FilePath>& filePaths)
|
||||
: m_fileManager(fileManager)
|
||||
{
|
||||
for (const std::string& path : filePaths)
|
||||
for (const FilePath& path : filePaths)
|
||||
{
|
||||
if (m_fileManager->hasSourceExtension(path))
|
||||
{
|
||||
@@ -24,14 +24,14 @@ const FileManager* FileRegister::getFileManager() const
|
||||
return m_fileManager;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& FileRegister::getSourceFilePaths() const
|
||||
const std::vector<FilePath>& FileRegister::getSourceFilePaths() const
|
||||
{
|
||||
return m_sourceFilePaths;
|
||||
}
|
||||
|
||||
bool FileRegister::includeFileIsParsing(const std::string& filePath) const
|
||||
{
|
||||
std::map<std::string, ParseState>::const_iterator it = m_includeFilePaths.find(FileSystem::absoluteFilePath(filePath));
|
||||
std::map<FilePath, ParseState>::const_iterator it = m_includeFilePaths.find(FilePath(filePath));
|
||||
if (it == m_includeFilePaths.end())
|
||||
{
|
||||
return false;
|
||||
@@ -42,7 +42,7 @@ bool FileRegister::includeFileIsParsing(const std::string& filePath) const
|
||||
|
||||
void FileRegister::markIncludeFileParsing(const std::string& filePath)
|
||||
{
|
||||
std::map<std::string, ParseState>::iterator it = m_includeFilePaths.find(FileSystem::absoluteFilePath(filePath));
|
||||
std::map<FilePath, ParseState>::iterator it = m_includeFilePaths.find(FilePath(filePath));
|
||||
if (it == m_includeFilePaths.end())
|
||||
{
|
||||
return;
|
||||
@@ -56,7 +56,7 @@ void FileRegister::markIncludeFileParsing(const std::string& filePath)
|
||||
|
||||
void FileRegister::markParsingIncludeFilesParsed()
|
||||
{
|
||||
for (std::pair<std::string, ParseState>&& p : m_includeFilePaths)
|
||||
for (std::pair<FilePath, ParseState>&& p : m_includeFilePaths)
|
||||
{
|
||||
if (p.second == STATE_PARSING)
|
||||
{
|
||||
|
||||
@@ -5,16 +5,18 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "utility/file/FilePath.h"
|
||||
|
||||
class FileManager;
|
||||
|
||||
class FileRegister
|
||||
{
|
||||
public:
|
||||
FileRegister(const FileManager* fileManager, const std::vector<std::string>& filePaths);
|
||||
FileRegister(const FileManager* fileManager, const std::vector<FilePath>& filePaths);
|
||||
|
||||
const FileManager* getFileManager() const;
|
||||
|
||||
const std::vector<std::string>& getSourceFilePaths() const;
|
||||
const std::vector<FilePath>& getSourceFilePaths() const;
|
||||
|
||||
bool includeFileIsParsing(const std::string& filePath) const;
|
||||
|
||||
@@ -31,8 +33,8 @@ private:
|
||||
|
||||
const FileManager* m_fileManager;
|
||||
|
||||
std::vector<std::string> m_sourceFilePaths;
|
||||
std::map<std::string, ParseState> m_includeFilePaths;
|
||||
std::vector<FilePath> m_sourceFilePaths;
|
||||
std::map<FilePath, ParseState> m_includeFilePaths;
|
||||
};
|
||||
|
||||
#endif // FILE_REGISTER_H
|
||||
|
||||
@@ -69,7 +69,7 @@ std::vector<FileInfo> FileSystem::getFileInfosFromDirectoryPaths(
|
||||
{
|
||||
std::time_t t = boost::filesystem::last_write_time(*it);
|
||||
boost::posix_time::ptime lastWriteTime = boost::posix_time::from_time_t(t);
|
||||
files.push_back(FileInfo(absoluteFilePath(it->path().generic_string()), lastWriteTime));
|
||||
files.push_back(FileInfo(it->path(), lastWriteTime));
|
||||
}
|
||||
++it;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user