lfilesystem  0.0.1
C++ filesystem library
files_dir_listen.cpp

This code listens to changes to a directory and maintains a list of all JSON files in the directory.

See also
files::FileWatcher
/*
* ======================================================================================
* __ ____ __ __ ____ ___
* ( ) (_ _)( \/ )( ___)/ __)
* )(__ _)(_ ) ( )__) \__ \
* (____)(____)(_/\/\_)(____)(___/
*
* This file is part of the Limes open source library and is licensed under the terms of the GNU Public License.
*
* Commercial licenses are available; contact the maintainers at ben.the.vining@gmail.com to inquire for details.
*
* ======================================================================================
*/
#include <vector>
{
explicit JSONWatcher (const limes::files::Directory& dir)
: limes::files::FileWatcher (dir), directory (dir)
{ }
std::vector<limes::files::File> jsonFiles;
const limes::files::Directory directory;
void fileCreated (const limes::files::FilesystemEntry& file) final
{
if (! directory.contains (file))
return;
if (const auto fileObj = file.getFileObject())
{
if (fileObj->hasFileExtension (".json"))
jsonFiles.emplace_back (*fileObj);
}
}
void fileDeleted (const limes::files::FilesystemEntry& file) final
{
jsonFiles.erase (std::find_if (jsonFiles.begin(), jsonFiles.end(),
{
return f.getAbsolutePath() == p;
}));
}
void fileMoved (const limes::files::FilesystemEntry& file) final
{
if (! directory.contains (file))
{
fileDeleted (file);
return;
}
if (const auto fileObj = file.getFileObject())
{
if (! fileObj->hasFileExtension (".json"))
return;
const auto alreadyInList = (std::find (jsonFiles.begin(),
jsonFiles.end(),
*fileObj)
!= jsonFiles.end());
if (! alreadyInList)
jsonFiles.emplace_back (*fileObj);
}
}
};
const limes::files::Directory dir { "/my/directory" };
JSONWatcher watcher { dir };
This class represents a directory on the filesystem.
bool contains(const FilesystemEntry &entry, std::size_t depthLimit=50) const
Returns true if this directory contains the passed FilesystemEntry.
This class listens for changes to or operations on a certain file, and receives callbacks to be notif...
This class represents a file on the filesystem.
The base class for any kind of object on the filesystem.
Path getAbsolutePath(bool makePreferred=false) const noexcept
Returns the full, absolute path of this filesystem entry.
std::optional< File > getFileObject() const noexcept
If this object refers to a file, constructs a File object referring to the same file.
The main header for the limes_files library.
void fileDeleted(const limes::files::FilesystemEntry &file) final
Called when a file is deleted.
void fileCreated(const limes::files::FilesystemEntry &file) final
Called when a file or subdirectory is created in a watched directory.
void fileMoved(const limes::files::FilesystemEntry &file) final
Called when a file is moved or renamed.