aboutsummaryrefslogtreecommitdiff
path: root/include/Storage.hpp
blob: 86c34d2942a0d72a350cc910b09bbcbe1d2a267c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once

#include "Path.hpp"
#include <functional>
#include <rapidjson/fwd.h>

namespace Json {
    class Value;
}

namespace QuickMedia {
    enum class FileType {
        FILE_NOT_FOUND,
        REGULAR,
        DIRECTORY
    };

    enum FileSortDirection {
        ASC,
        DESC
    };

    // Return false to stop the iterator.
    using FileIteratorCallback = std::function<bool(const Path &filepath, FileType file_type, time_t last_modified_seconds)>;

    Path get_home_dir();
    Path get_storage_dir();
    Path get_cache_dir();
    int get_cookies_filepath(Path &path, const std::string &plugin_name);
    int create_directory_recursive(const Path &path);
    FileType get_file_type(const Path &path);
    int file_get_content(const Path &path, std::string &result);
    int file_get_size(const Path &path, int64_t *size);
    bool file_get_last_modified_time_seconds(const char *path, time_t *result);
    int file_overwrite(const Path &path, const std::string &data);
    int file_overwrite_atomic(const Path &path, const std::string &data);
    bool file_append(const Path &path, const std::string &data);
    // The callback is called with 0 as the argument (last_modified_seconds)
    void for_files_in_dir(const Path &path, FileIteratorCallback callback);
    void for_files_in_dir_sort_last_modified(const Path &path, FileIteratorCallback callback, FileSortDirection sort_dir = FileSortDirection::ASC);
    // The callback is called with 0 as the argument (last_modified_seconds)
    void for_files_in_dir_sort_name(const Path &path, FileIteratorCallback callback, FileSortDirection sort_dir = FileSortDirection::ASC);

    bool read_file_as_json(const Path &filepath, Json::Value &result);
    bool save_json_to_file_atomic(const Path &path, const Json::Value &json);
    bool save_json_to_file_atomic(const Path &path, const rapidjson::Value &json);

    int rename_atomic(const char *oldpath, const char *newpath);

    bool is_program_executable_by_name(const char *name);

    std::string file_size_to_human_readable_string(int64_t bytes);
}