Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Move/merge cppassist fs module #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/cppfs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set(headers
${include_path}/Diff.h
${include_path}/Change.h
${include_path}/units.h
${include_path}/directorytraversal.h

${include_path}/${localfs}/LocalFileSystem.h
${include_path}/${localfs}/LocalFileHandle.h
Expand All @@ -92,6 +93,7 @@ set(sources
${source_path}/Tree.cpp
${source_path}/Diff.cpp
${source_path}/Change.cpp
${source_path}/directorytraversal.cpp

${source_path}/${localfs}/LocalFileSystem.cpp
${source_path}/${localfs}/LocalFileHandle.cpp
Expand Down
40 changes: 40 additions & 0 deletions source/cppfs/include/cppfs/FilePath.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,36 @@ class CPPFS_API FilePath
*/
FilePath & operator=(FilePath && filePath);

/**
* @brief
* Equality operator
*
* @param[in] other
* The other file path for comparison
*
* @return
* `true` if both file paths are equal, else `false`
*
* @remark
* Two file paths are considered equal if the contents of the path are equal
*/
bool operator==(const FilePath & other) const;

/**
* @brief
* Inequality operator
*
* @param[in] other
* The other file path for comparison
*
* @return
* `true` if both file paths differ, else `false`
*
* @remark
* Two file paths are considered equal if the contents of the path are equal
*/
bool operator!=(const FilePath & other) const;

/**
* @brief
* Get path as string
Expand All @@ -127,6 +157,15 @@ class CPPFS_API FilePath
*/
const std::string & path() const;

/**
* @brief
* Get original (unprocessed) input string
*
* @return
* Path that was originally provided
*/
const std::string & originalPath() const;

/**
* @brief
* Set path
Expand Down Expand Up @@ -345,6 +384,7 @@ class CPPFS_API FilePath


protected:
std::string m_originalPath; ///< Original unprocessed input string
std::string m_path; ///< Path (unified format)
bool m_pointsToContent; ///< 'true' if the path has a trailing separator, else 'false'

Expand Down
100 changes: 100 additions & 0 deletions source/cppfs/include/cppfs/directorytraversal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

#pragma once


#include <string>
#include <vector>
#include <functional>

#include <cppfs/cppfs_api.h>


namespace cppfs
{


/**
* @brief
* List all files in a directory
*
* @param[in] directory
* Path to directory (exluding a trailing '`/`'!)
* @param[in] recursive
* Search recursively in sub-directories?
* @param[out] files
* List of files
*
* @remark
* Lists all files in the directory, including all
* files in sub-directories if recursive is true.
*
* @remark
* Only files are listed, directories are not included.
*
* @remark
* The search path is included in the file name, e.g.,
* getFile("dir") may result in ["dir/file1.txt", "dir/file2.png", ...].
*/
CPPFS_API void getFiles(const std::string & directory, bool recursive, std::vector<std::string> & files);

/**
* @brief
* List all files in a directory
*
* @param[in] directory
* Path to directory (exluding a trailing '`/`'!)
* @param[in] recursive
* Search recursively in sub-directories?
*
* @return
* List of files
*/
CPPFS_API std::vector<std::string> getFiles(const std::string & directory, bool recursive);

/**
* @brief
* List all files in a directory
*
* @param[in] directories
* Vector of paths to directories (exluding a trailing '`/`'!)
* @param[in] recursive
* Search recursively in sub-directories?
*
* @return
* List of files
*/
CPPFS_API std::vector<std::string> getFiles(const std::vector<std::string> & directories, bool recursive);

/**
* @brief
* Scan directory for files with a specific filename extension
*
* @param[in] directory
* Path to directory
* @param[in] fileExtension
* File extension ('`*`' for all files)
* @param[in] recursive
* Search recursively in sub-directories?
*
* @return
* List of found files, including the directory name
*/
CPPFS_API std::vector<std::string> scanDirectory(const std::string & directory, const std::string & fileExtension, bool recursive = false);

/**
* @brief
* Scan directory for files with a specific filename extension
*
* @param[in] directory
* Path to directory
* @param[in] fileExtension
* File extension ('`*`' for all files)
* @param[in] recursive
* Search recursively in sub-directories?
* @param[in] callback
* Function that is called for each found file
*/
CPPFS_API void scanDirectory(const std::string & directory, const std::string & fileExtension, bool recursive, const std::function<void(const std::string &)> & callback);


} // namespace cppfs
12 changes: 12 additions & 0 deletions source/cppfs/include/cppfs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ CPPFS_API std::string fromBase64(const std::string & base64);
*/
CPPFS_API std::string hashToString(const unsigned char * hash);

/**
* @brief
* Create directory
*
* @param[in] path
* Path to directory
*
* @return
* 0 on success, error code > 0 otherwise
*/
CPPFS_API int makeDir(const std::string & path);


} // namespace fs

Expand Down
38 changes: 35 additions & 3 deletions source/cppfs/source/FilePath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ namespace cppfs


FilePath::FilePath()
: m_path("")
: m_originalPath("")
, m_path("")
, m_pointsToContent(false)
, m_details(false)
{
}

FilePath::FilePath(const FilePath & filePath)
: m_path(filePath.m_path)
: m_originalPath(filePath.m_originalPath)
, m_path(filePath.m_path)
, m_pointsToContent(filePath.m_pointsToContent)
, m_details(filePath.m_details)
, m_fullPath(filePath.m_fullPath)
Expand All @@ -33,7 +35,8 @@ FilePath::FilePath(const FilePath & filePath)
}

FilePath::FilePath(FilePath && filePath)
: m_path(std::move(filePath.m_path))
: m_originalPath(std::move(filePath.m_path))
, m_path(std::move(filePath.m_path))
, m_pointsToContent(std::move(filePath.m_pointsToContent))
, m_details(std::move(filePath.m_details))
, m_fullPath(std::move(filePath.m_fullPath))
Expand Down Expand Up @@ -73,6 +76,7 @@ FilePath::~FilePath()

FilePath & FilePath::operator=(const FilePath & filePath)
{
m_originalPath = filePath.m_originalPath;
m_path = filePath.m_path;
m_pointsToContent = filePath.m_pointsToContent;
m_details = filePath.m_details;
Expand All @@ -89,6 +93,7 @@ FilePath & FilePath::operator=(const FilePath & filePath)

FilePath & FilePath::operator=(FilePath && filePath)
{
m_originalPath = std::move(filePath.m_originalPath);
m_path = std::move(filePath.m_path);
m_pointsToContent = std::move(filePath.m_pointsToContent);
m_details = std::move(filePath.m_details);
Expand All @@ -103,14 +108,40 @@ FilePath & FilePath::operator=(FilePath && filePath)
return *this;
}

bool FilePath::operator==(const FilePath & other) const
{
if (this == &other)
{
return true;
}

return m_path == other.m_path;
}

bool FilePath::operator!=(const FilePath & other) const
{
if (this == &other)
{
return false;
}

return m_path != other.m_path;
}

const std::string & FilePath::path() const
{
return m_path;
}

const std::string & FilePath::originalPath() const
{
return m_originalPath;
}

void FilePath::setPath(const std::string & path)
{
// Set new path
m_originalPath = path;
m_path = path;

// Reset state
Expand Down Expand Up @@ -138,6 +169,7 @@ void FilePath::setPath(const std::string & path)
void FilePath::setPath(std::string && path)
{
// Set new path
m_originalPath = path;
m_path = std::move(path);

// Reset state
Expand Down
Loading