Skip to content

Commit

Permalink
Replace ghc filesystem with std
Browse files Browse the repository at this point in the history
  • Loading branch information
kraxarn committed Jan 25, 2025
1 parent 5103238 commit 0dc31b7
Show file tree
Hide file tree
Showing 24 changed files with 68 additions and 6,168 deletions.
9 changes: 4 additions & 5 deletions lib/include/lib/cache/jsoncache.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#pragma once

#include "lib/cache.hpp"
#include "lib/json.hpp"
#include "lib/paths/paths.hpp"
#include "thirdparty/filesystem.hpp"
#include "thirdparty/json.hpp"

#include <filesystem>

namespace lib
{
Expand Down Expand Up @@ -52,7 +51,7 @@ namespace lib
/**
* Get parent directory for cache type
*/
auto dir(const std::string &type) const -> ghc::filesystem::path;
auto dir(const std::string &type) const -> std::filesystem::path;

/**
* Get file name for id
Expand All @@ -68,6 +67,6 @@ namespace lib
/**
* Get basename of path
*/
static auto get_url_id(const ghc::filesystem::path &path) -> std::string;
static auto get_url_id(const std::filesystem::path &path) -> std::string;
};
}
11 changes: 4 additions & 7 deletions lib/include/lib/json.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#pragma once

#include "lib/log.hpp"
#include "lib/optional.hpp"

#include "thirdparty/json.hpp"
#include "thirdparty/filesystem.hpp"

#include <fstream>
#include <filesystem>

namespace lib
{
Expand Down Expand Up @@ -67,14 +64,14 @@ namespace lib
* @param path Path to json file, including extension
* @return JSON object, or null object on failure
*/
static auto load(const ghc::filesystem::path &path) -> nlohmann::json;
static auto load(const std::filesystem::path &path) -> nlohmann::json;

/**
* Convenience method to parse generic class from JSON,
* returns instance of T on failure
*/
template<typename T>
static auto load(const ghc::filesystem::path &path) -> T
static auto load(const std::filesystem::path &path) -> T
{
auto json = load(path);
if (json.is_null())
Expand All @@ -89,7 +86,7 @@ namespace lib
* @param path Path to json file, including extension
* @param item Item to save
*/
static void save(const ghc::filesystem::path &path, const nlohmann::json &json);
static void save(const std::filesystem::path &path, const nlohmann::json &json);

/**
* Find the last value in an "item"s array, or from "item" directly
Expand Down
8 changes: 3 additions & 5 deletions lib/include/lib/paths/paths.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include "thirdparty/filesystem.hpp"

#include <string>
#include <filesystem>

namespace lib
{
Expand All @@ -17,13 +15,13 @@ namespace lib
* Get path to spotify-qt.json
* @return Path, for example ~/.config/kraxarn/spotify-qt.json
*/
virtual auto config_file() const -> ghc::filesystem::path = 0;
virtual auto config_file() const -> std::filesystem::path = 0;

/**
* Get path where cached files should be
* @return Path, for example ~/.cache/kraxarn/spotify-qt
*/
virtual auto cache() const -> ghc::filesystem::path = 0;
virtual auto cache() const -> std::filesystem::path = 0;

protected:
/**
Expand Down
12 changes: 3 additions & 9 deletions lib/include/lib/settings.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#pragma once

#include "enum/palette.hpp"
#include "enum/playlistorder.hpp"
#include "enum/spotifycontext.hpp"
#include "log.hpp"
#include "paths/paths.hpp"
#include "thirdparty/json.hpp"
#include "thirdparty/filesystem.hpp"
#include "settings/general.hpp"
#include "settings/account.hpp"
#include "settings/spotify.hpp"
#include "settings/general.hpp"
#include "settings/qt.hpp"
#include "lib/json.hpp"
#include "settings/spotify.hpp"
#include "thirdparty/json.hpp"

#include <mutex>

Expand Down
6 changes: 4 additions & 2 deletions lib/include/lib/spotify/request.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include "lib/fmt.hpp"
#include "lib/httpclient.hpp"
#include "lib/log.hpp"
#include "lib/result.hpp"
#include "lib/spotify/error.hpp"
#include "lib/spotify/util.hpp"
#include "lib/spotify/deviceselect.hpp"
#include "lib/spotify/error.hpp"
#include "lib/spotify/page.hpp"
#include "lib/spotify/util.hpp"

namespace lib
{
Expand Down
1 change: 1 addition & 0 deletions lib/qt/src/httpclient.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "lib/qt/httpclient.hpp"
#include "lib/log.hpp"

lib::qt::http_client::http_client(QObject *parent)
: QObject(parent),
Expand Down
23 changes: 12 additions & 11 deletions lib/src/cache/jsoncache.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "lib/cache/jsoncache.hpp"

#include <fstream>

lib::json_cache::json_cache(const lib::paths &paths)
: paths(paths)
{
Expand Down Expand Up @@ -122,12 +123,12 @@ auto lib::json_cache::all_tracks() const -> std::map<std::string, std::vector<li
auto dir = paths.cache() / "tracks";
std::map<std::string, std::vector<lib::spt::track>> results;

if (!ghc::filesystem::exists(dir))
if (!std::filesystem::exists(dir))
{
return results;
}

for (const auto &entry: ghc::filesystem::directory_iterator(dir))
for (const auto &entry: std::filesystem::directory_iterator(dir))
{
auto entity_id = entry.path().filename().replace_extension().string();
results[entity_id] = get_tracks(entity_id);
Expand Down Expand Up @@ -163,15 +164,15 @@ void lib::json_cache::add_crash(const lib::crash_info &info)

auto lib::json_cache::get_all_crashes() const -> std::vector<lib::crash_info>
{
auto dir = ghc::filesystem::path(paths.cache()) / "crash";
auto dir = std::filesystem::path(paths.cache()) / "crash";
std::vector<lib::crash_info> results;

if (!ghc::filesystem::exists(dir))
if (!std::filesystem::exists(dir))
{
return results;
}

for (const auto &entry: ghc::filesystem::directory_iterator(dir))
for (const auto &entry: std::filesystem::directory_iterator(dir))
{
results.push_back(lib::json::load<lib::crash_info>(entry.path()));
}
Expand All @@ -183,13 +184,13 @@ auto lib::json_cache::get_all_crashes() const -> std::vector<lib::crash_info>

//region private

auto lib::json_cache::dir(const std::string &type) const -> ghc::filesystem::path
auto lib::json_cache::dir(const std::string &type) const -> std::filesystem::path
{
auto file_dir = ghc::filesystem::path(paths.cache()) / type;
auto file_dir = std::filesystem::path(paths.cache()) / type;

if (!ghc::filesystem::exists(file_dir))
if (!std::filesystem::exists(file_dir))
{
ghc::filesystem::create_directories(file_dir);
std::filesystem::create_directories(file_dir);
}

return file_dir;
Expand All @@ -209,7 +210,7 @@ auto lib::json_cache::path(const std::string &type, const std::string &entity_id
return (dir(type) / file(entity_id, extension)).string();
}

auto lib::json_cache::get_url_id(const ghc::filesystem::path &path) -> std::string
auto lib::json_cache::get_url_id(const std::filesystem::path &path) -> std::string
{
return path.stem().string();
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/github/api.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "lib/github/api.hpp"
#include "lib/fmt.hpp"
#include "lib/log.hpp"

lib::gh::api::api(const lib::http_client &http_client)
: http_client(http_client)
Expand Down
11 changes: 7 additions & 4 deletions lib/src/json.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "lib/json.hpp"
#include "lib/log.hpp"

#include <fstream>

auto lib::json::combine(const nlohmann::json &item1, const nlohmann::json &item2) -> nlohmann::json
{
Expand All @@ -16,9 +19,9 @@ auto lib::json::combine(const nlohmann::json &item1, const nlohmann::json &item2
return item;
}

auto lib::json::load(const ghc::filesystem::path &path) -> nlohmann::json
auto lib::json::load(const std::filesystem::path &path) -> nlohmann::json
{
ghc::filesystem::ifstream file(path);
std::ifstream file(path);
if (!file.is_open() || file.bad())
{
// File not found errors fail silently
Expand All @@ -40,11 +43,11 @@ auto lib::json::load(const ghc::filesystem::path &path) -> nlohmann::json
return {};
}

void lib::json::save(const ghc::filesystem::path &path, const nlohmann::json &json)
void lib::json::save(const std::filesystem::path &path, const nlohmann::json &json)
{
try
{
ghc::filesystem::ofstream file(path);
std::ofstream file(path);
file << std::setw(4) << json;
}
catch (const std::exception &e)
Expand Down
1 change: 1 addition & 0 deletions lib/src/lyrics/api.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "lib/lyrics/api.hpp"
#include "lib/fmt.hpp"
#include "lib/uri.hpp"

lib::lrc::api::api(const lib::http_client &http_client)
Expand Down
2 changes: 2 additions & 0 deletions lib/src/search/api.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "lib/search/api.hpp"
#include "lib/fmt.hpp"
#include "lib/log.hpp"
#include "lib/uri.hpp"

lib::ddg::api::api(const lib::http_client &http_client)
Expand Down
9 changes: 6 additions & 3 deletions lib/src/settings.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "lib/settings.hpp"
#include "lib/log.hpp"

#include <fstream>

lib::settings::settings(const paths &paths)
: path(paths)
Expand All @@ -13,7 +16,7 @@ auto lib::settings::file_name() const -> std::string

auto lib::settings::file_path() const -> std::string
{
return ghc::filesystem::path(file_name()).parent_path().string();
return std::filesystem::path(file_name()).parent_path().string();
}

auto lib::settings::qt() -> setting::qt &
Expand Down Expand Up @@ -85,9 +88,9 @@ void lib::settings::save()
std::lock_guard<std::mutex> lock(mutex);

auto file_dir = file_path();
if (!ghc::filesystem::exists(file_dir))
if (!std::filesystem::exists(file_dir))
{
ghc::filesystem::create_directories(file_dir);
std::filesystem::create_directories(file_dir);
}

std::ofstream file(file_name());
Expand Down
2 changes: 2 additions & 0 deletions lib/src/spotify/auth.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "lib/spotify/auth.hpp"
#include "lib/fmt.hpp"
#include "lib/log.hpp"

lib::spt::auth::auth(lib::settings &settings, const lib::http_client &http_client)
: settings(settings),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/spotify/track.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "lib/spotify/track.hpp"

#include "lib/fmt.hpp"
#include "lib/json.hpp"
#include "lib/strings.hpp"

Expand Down
Loading

0 comments on commit 0dc31b7

Please sign in to comment.