view include/core/filesystem.h @ 406:31ce85df55a8 default tip

filesystem: add mac os x directory watcher this code is incredibly stinky tbh we should probably be using C++ threads everywhere else just because they are SO much easier to code for than the shitty Qt threads API
author Paper <paper@tflc.us>
date Mon, 19 Jan 2026 22:48:56 -0500
parents e561b7542b7b
children
line wrap: on
line source

#ifndef MINORI_CORE_FILESYSTEM_H_
#define MINORI_CORE_FILESYSTEM_H_
#include <filesystem>
#include <functional>
#include <string>
#include <unordered_map>

namespace Filesystem {

void CreateDirectories(const std::filesystem::path &path);
std::filesystem::path GetDotPath();    // %APPDATA%/minori/, ~/Library/Application Support/minori/, ~/.config/minori/...
std::filesystem::path GetConfigPath(); // (dotpath)/config.json
std::filesystem::path GetAnimeDBPath();      // (dotpath)/anime/db.json
std::filesystem::path GetTorrentsPath();     // (dotpath)/torrents/...
std::filesystem::path GetAnimePostersPath(); // (dotpath)/anime/posters/

struct PathHash {
	auto operator()(const std::filesystem::path &p) const noexcept {
		return std::filesystem::hash_value(p);
	}
};

template<typename T>
using PathMap = std::unordered_map<std::filesystem::path, T, PathHash>;

/* ------------------------------------------------------------------------ */
/* Filesystem watcher interface. This is implemented differently on
 * different platforms :) */

struct IWatcher {
	enum Event {
		/* File/directory 'path' was created */
		Created,
		/* File/directory 'path' was deleted */
		Deleted,
	};

	using EventHandler = std::function<void(void *opaque, const std::filesystem::path &path, Event event)>;

	virtual ~IWatcher() = default;
	virtual void Process() = 0;
};

/* Constructor functions. Yes, I'm doing this the C way :) */
IWatcher *GetRecursiveFilesystemWatcher(void *opaque, const std::filesystem::path &path,
                                        IWatcher::EventHandler handler);
IWatcher *GetFilesystemWatcher(void *opaque, const std::filesystem::path &path, IWatcher::EventHandler handler);

} // namespace Filesystem

#endif // MINORI_CORE_FILESYSTEM_H_