aboutsummaryrefslogtreecommitdiff
path: root/include/FileUtil.hpp
blob: 048722723770e87b1cf43cb68767217a088ecb51 (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
#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);
#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 getLastErrorAsString();
    void replaceChar(FileString &input, wchar_t charToReplace, wchar_t charToReplaceWith);
#endif

    // 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*)>;

    enum class FileType
    {
        FILE_NOT_FOUND,
        REGULAR,
        DIRECTORY
    };

    FileType getFileType(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);
    Result<StringView> 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);
}

#endif //SIBS_FILEUTIL_HPP