aboutsummaryrefslogtreecommitdiff
path: root/include/FileUtil.hpp
blob: aa2b82f042b6d4024fd21d54dac596c91350e4a1 (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
#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
    #define toUtf8(input) 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

    using FileWalkCallbackFunc = std::function<void(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);
    void 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