aboutsummaryrefslogtreecommitdiff
path: root/include/FileUtil.hpp
blob: 2b4f3c50925e20729679302d6d3e76bf82a59aa1 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef SIBS_FILEUTIL_HPP
#define SIBS_FILEUTIL_HPP

#ifndef UNICODE
#define UNICODE
#endif

#ifndef _UNICODE
#define _UNICODE
#endif

#include "env.hpp"
#include "../external/tinydir.h"
#include "../external/utf8/checked.h"
#include "Result.hpp"
#include "StringView.hpp"
#include <functional>

namespace sibs
{
	using FileString = std::basic_string<_tinydir_char_t, std::char_traits<_tinydir_char_t>, std::allocator<_tinydir_char_t>>;

#if OS_FAMILY == OS_FAMILY_POSIX
    std::string toUtf8(const std::string &input);
    std::string toUtf8(const char *input);
    FileString toFileString(const std::string &utf8Str);
    FileString toFileString(const StringView &utf8Str);
    FileString toFileString(const char *str);
#else
    std::string toUtf8(const sibs::FileString &input);
    std::string toUtf8(const TCHAR *input);
    FileString utf8To16(const StringView &utf8Str);
    FileString utf8To16(const std::string &utf8Str);
    FileString toFileString(const std::string &utf8Str);
    FileString toFileString(const StringView &utf8Str);
    FileString toFileString(const char *str);
    FileString getLastErrorAsString();
    void replaceChar(FileString &input, wchar_t charToReplace, wchar_t charToReplaceWith);
#endif

    class Path {
    public:
        Path(const FileString &str) : data(str) {}

        Path& join(const _tinydir_char_t *str) {
            data += TINYDIR_STRING("/");
            data += str;
            return *this;
        }

        Path& join(const Path &other) {
            data += TINYDIR_STRING("/");
            data += other.data;
            return *this;
        }

        Path& append(const FileString &str) {
            data += str;
            return *this;
        }

        FileString data;
    };

    // Return true if you want to continue iterating the remaining files, return false if you want to stop
    using FileWalkCallbackFunc = std::function<bool(tinydir_file*)>;

    struct FileWithTimestamp {
        FileString filepath;
        FileString extension;
        struct timespec last_modified;
    };

    using SortedFileWalkCallbackFunc = std::function<bool(const FileWithTimestamp&)>;

    enum class FileType
    {
        FILE_NOT_FOUND,
        REGULAR,
        DIRECTORY
    };

    FileType getFileType(const _tinydir_char_t *path);
    bool fileExists(const _tinydir_char_t *path);
    void walkDir(const _tinydir_char_t *directory, FileWalkCallbackFunc callbackFunc);
    void walkDirFiles(const _tinydir_char_t *directory, FileWalkCallbackFunc callbackFunc);
    bool walkDirFilesRecursive(const _tinydir_char_t *directory, FileWalkCallbackFunc callbackFunc);
    bool walkDirFilesRecursiveSortTimestamp(const _tinydir_char_t *directory, SortedFileWalkCallbackFunc callbackFunc);
    Result<std::string> getFileContent(const _tinydir_char_t *filepath);
    Result<bool> fileWrite(const _tinydir_char_t *filepath, StringView data);
    Result<bool> fileOverwrite(const _tinydir_char_t *filepath, StringView data);
    Result<FileString> getHomeDir();
    Result<FileString> getCwd();
    Result<bool> createDirectory(const _tinydir_char_t *path);
    // Note: Will not delete created directories if this operation fails for some reason
    Result<bool> createDirectoryRecursive(const _tinydir_char_t *path);
    Result<FileString> getRealPath(const _tinydir_char_t *path);
    bool pathEquals(const std::string &path, const std::string &otherPath);
    Result<u64> getFileLastModifiedTime(const _tinydir_char_t *path);
    bool isPathAbsolute(const std::string &path);
}

#endif //SIBS_FILEUTIL_HPP