summaryrefslogtreecommitdiffhomepage
path: root/loader/filesystem.cpp
blob: 850ec84f1086bf3c00c7355567760c01467c5fb1 (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
#include "impl.hpp"
#include "compat/assert.hpp"
#include <cerrno>
#include <Corrade/Containers/Pair.h>
#include <Corrade/Containers/String.h>
#include <Corrade/Utility/Debug.h>
#include <Corrade/Utility/Implementation/ErrorString.h>
#include <Corrade/Utility/Path.h>
#ifdef _WIN32
#include <Corrade/Containers/Array.h>
#include <Corrade/Utility/Unicode.h>
#include <direct.h>
#else
#include <unistd.h>
#endif

namespace floormat::loader_detail {

#ifdef _WIN32
namespace Unicode = Corrade::Utility::Unicode;
#endif

bool loader_impl::chdir(StringView pathname)
{
    fm_assert(pathname.flags() & StringViewFlag::NullTerminated);
    int ret;
#ifdef _WIN32
    ret = ::_wchdir(Unicode::widen(pathname));
#else
    ret = ::chdir(pathname.data());
#endif
    if (ret)
    {
        Error err;
        err << "chdir: can't change directory to" << pathname << Error::nospace << ": ";
        Corrade::Utility::Implementation::printErrnoErrorString(err, errno);
    }
    return !ret;
}

void loader_impl::set_application_working_directory()
{
    static bool once = false;
    if (once)
        return;
    once = true;
    if (const auto loc = Path::executableLocation())
    {
        String path;
#ifdef _WIN32
        path = "\\\\?\\"_s + *loc;
#else
        path = *loc;
#endif
        StringView p = path;
        p = Path::split(p).first();
        p = Path::split(p).first();
        path = p;
#ifdef _WIN32
        for (char& c : path)
            if (c == '/')
                c = '\\';
#endif
        chdir(path);
    }
    else
        fm_warn("can't find install prefix!");
}

} // namespace floormat::loader_detail