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 ...@@ -14,7 +14,15 @@ namespace factory
{ {
public: public:
virtual ~IFileSystem() = default; 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::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 ...@@ -17,9 +17,9 @@ std::unique_ptr<fs::IDirectory> FileSystemDefaultFactory::createDirectory( const
return std::unique_ptr<fs::IDirectory>( new fs::Directory( path ) ); 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 ...@@ -8,6 +8,6 @@ namespace factory
{ {
public: public:
virtual std::unique_ptr<fs::IDirectory> createDirectory( const std::string& path ) override; 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 @@ ...@@ -7,11 +7,11 @@
namespace fs namespace fs
{ {
File::File( const std::string& path, const std::string& fileName ) File::File( const std::string& filePath )
: m_path( path ) : m_path( utils::file::directory( filePath ) )
, m_name( fileName ) , m_name( utils::file::fileName( filePath ) )
, m_fullPath( path + ( *path.rbegin() != '/' ? "/" : "" ) + fileName ) , m_fullPath( filePath )
, m_extension( utils::file::extension( fileName ) ) , m_extension( utils::file::extension( filePath ) )
{ {
struct stat s; struct stat s;
if ( lstat( m_fullPath.c_str(), &s ) ) if ( lstat( m_fullPath.c_str(), &s ) )
......
...@@ -10,7 +10,7 @@ namespace fs ...@@ -10,7 +10,7 @@ namespace fs
class File : public IFile class File : public IFile
{ {
public: public:
File( const std::string& path, const std::string& fileName ); File( const std::string& filePath );
virtual ~File() = default; virtual ~File() = default;
virtual const std::string& name() const override; virtual const std::string& name() const override;
......
...@@ -17,11 +17,11 @@ namespace mock ...@@ -17,11 +17,11 @@ namespace mock
class File : public fs::IFile class File : public fs::IFile
{ {
public: public:
File( const std::string& path, const std::string& fileName ) File( const std::string& filePath )
: m_name( fileName ) : m_name( utils::file::fileName( filePath ) )
, m_path( path ) , m_path( utils::file::directory( filePath ) )
, m_fullPath( path + fileName ) , m_fullPath( filePath )
, m_extension( utils::file::extension( fileName ) ) , m_extension( utils::file::extension( filePath ) )
, m_lastModification( 0 ) , m_lastModification( 0 )
{ {
} }
...@@ -128,13 +128,14 @@ struct FileSystemFactory : public factory::IFileSystem ...@@ -128,13 +128,14 @@ struct FileSystemFactory : public factory::IFileSystem
void addFile( const std::string& path, const std::string& fileName ) void addFile( const std::string& path, const std::string& fileName )
{ {
dirs[path]->addFile( 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 ); auto parent = dirs[parentPath];
dirs[parent + path] = std::unique_ptr<mock::Directory>( new Directory( parent + path, lastModif ) ); 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 virtual std::unique_ptr<fs::IDirectory> createDirectory(const std::string& path) override
...@@ -155,12 +156,11 @@ struct FileSystemFactory : public factory::IFileSystem ...@@ -155,12 +156,11 @@ struct FileSystemFactory : public factory::IFileSystem
return std::unique_ptr<fs::IDirectory>( new Directory( *res ) ); 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( filePath );
const auto it = files.find( fullpath );
if ( it == end( files ) ) 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() ) ) ); 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