Commit b9c71f6f authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Simplify File interface

Require a simple absolute file path to call createFile.
This is more coherent with the fact that IDirectory always returns an
absolute path when calling files() and dirs()
parent 1e300054
......@@ -14,7 +14,15 @@ namespace factory
{
public:
virtual ~IFileSystem() = default;
///
/// \brief createDirectory creates a representation of a directory
/// \param path An absolute path to a directory
///
virtual std::unique_ptr<fs::IDirectory> createDirectory( const std::string& path ) = 0;
virtual std::unique_ptr<fs::IFile> createFile( const std::string& path, const std::string& fileName ) = 0;
///
/// \brief createFile creates a representation of a file
/// \param fileName an absolute path to a file
///
virtual std::unique_ptr<fs::IFile> createFile( const std::string& fileName ) = 0;
};
}
......@@ -17,9 +17,9 @@ std::unique_ptr<fs::IDirectory> FileSystemDefaultFactory::createDirectory( const
return std::unique_ptr<fs::IDirectory>( new fs::Directory( path ) );
}
std::unique_ptr<fs::IFile> FileSystemDefaultFactory::createFile(const std::string& path, const std::string& fileName)
std::unique_ptr<fs::IFile> FileSystemDefaultFactory::createFile(const std::string& fileName)
{
return std::unique_ptr<fs::IFile>( new fs::File( path, fileName ) );
return std::unique_ptr<fs::IFile>( new fs::File( fileName ) );
}
}
......@@ -8,6 +8,6 @@ namespace factory
{
public:
virtual std::unique_ptr<fs::IDirectory> createDirectory( const std::string& path ) override;
virtual std::unique_ptr<fs::IFile> createFile( const std::string& path, const std::string& fileName ) override;
virtual std::unique_ptr<fs::IFile> createFile( const std::string& fileName ) override;
};
}
......@@ -7,11 +7,11 @@
namespace fs
{
File::File( const std::string& path, const std::string& fileName )
: m_path( path )
, m_name( fileName )
, m_fullPath( path + ( *path.rbegin() != '/' ? "/" : "" ) + fileName )
, m_extension( utils::file::extension( fileName ) )
File::File( const std::string& filePath )
: m_path( utils::file::directory( filePath ) )
, m_name( utils::file::fileName( filePath ) )
, m_fullPath( filePath )
, m_extension( utils::file::extension( filePath ) )
{
struct stat s;
if ( lstat( m_fullPath.c_str(), &s ) )
......
......@@ -10,7 +10,7 @@ namespace fs
class File : public IFile
{
public:
File( const std::string& path, const std::string& fileName );
File( const std::string& filePath );
virtual ~File() = default;
virtual const std::string& name() const override;
......
......@@ -17,11 +17,11 @@ namespace mock
class File : public fs::IFile
{
public:
File( const std::string& path, const std::string& fileName )
: m_name( fileName )
, m_path( path )
, m_fullPath( path + fileName )
, m_extension( utils::file::extension( fileName ) )
File( const std::string& filePath )
: m_name( utils::file::fileName( filePath ) )
, m_path( utils::file::directory( filePath ) )
, m_fullPath( filePath )
, m_extension( utils::file::extension( filePath ) )
, m_lastModification( 0 )
{
}
......@@ -128,13 +128,14 @@ struct FileSystemFactory : public factory::IFileSystem
void addFile( const std::string& path, const std::string& fileName )
{
dirs[path]->addFile( fileName );
files[path + fileName] = std::unique_ptr<mock::File>( new mock::File( path, fileName ) );
files[path + fileName] = std::unique_ptr<mock::File>( new mock::File( path + fileName ) );
}
void addFolder( const std::string& parent, const std::string& path, unsigned int lastModif )
void addFolder( const std::string& parentPath, const std::string& path, unsigned int lastModif )
{
dirs[parent]->addFolder( path );
dirs[parent + path] = std::unique_ptr<mock::Directory>( new Directory( parent + path, lastModif ) );
auto parent = dirs[parentPath];
parent->addFolder( path );
dirs[parentPath + path] = std::unique_ptr<mock::Directory>( new Directory( parent, parentPath + path, lastModif ) );
}
virtual std::unique_ptr<fs::IDirectory> createDirectory(const std::string& path) override
......@@ -155,12 +156,11 @@ struct FileSystemFactory : public factory::IFileSystem
return std::unique_ptr<fs::IDirectory>( new Directory( *res ) );
}
virtual std::unique_ptr<fs::IFile> createFile(const std::string &path, const std::string &fileName)
virtual std::unique_ptr<fs::IFile> createFile( const std::string &filePath ) override
{
std::string fullpath = path + fileName;
const auto it = files.find( fullpath );
const auto it = files.find( filePath );
if ( it == end( files ) )
files[fullpath].reset( new File( path, fileName ) );
files[filePath].reset( new File( filePath ) );
return std::unique_ptr<fs::IFile>( new File( static_cast<const mock::File&>( * it->second.get() ) ) );
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment