-
Notifications
You must be signed in to change notification settings - Fork 912
Expand file tree
/
Copy pathplatform.cpp
More file actions
81 lines (72 loc) · 1.81 KB
/
Copy pathplatform.cpp
File metadata and controls
81 lines (72 loc) · 1.81 KB
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
#include "platform.h"
#include <stdlib.h>
#include <boost/filesystem.hpp>
#include <iostream>
#if defined(WIN32)
#include <codecvt>
#elif define(__linux__)
#include <unistd.h>
#include <sys/reboot.h>
#endif
std::string getHomePath()
{
std::string homePath;
// this should give you something like "/home/YOUR_USERNAME" on Linux and "C:\Users\YOUR_USERNAME\" on Windows
const char * envHome = getenv("HOME");
if(envHome != nullptr)
{
homePath = envHome;
}
#ifdef WIN32
// but does not seem to work for Windows XP or Vista, so try something else
if (homePath.empty()) {
const char * envDir = getenv("HOMEDRIVE");
const char * envPath = getenv("HOMEPATH");
if (envDir != nullptr && envPath != nullptr) {
homePath = envDir;
homePath += envPath;
for(unsigned int i = 0; i < homePath.length(); i++)
if(homePath[i] == '\\')
homePath[i] = '/';
}
}
#endif
// convert path to generic directory seperators
boost::filesystem::path genericPath(homePath);
return genericPath.generic_string();
}
int runShutdownCommand()
{
#if defined(WIN32)
return system("shutdown -s -t 0");
#elif defined(__linux__)
sync();
return reboot(RB_POWER_OFF);
#else
return system("sudo shutdown -h now");
#endif
}
int runRestartCommand()
{
#if defined(WIN32)
return system("shutdown -r -t 0");
#elif defined(__linux__)
sync();
return reboot(RB_AUTOBOOT);
#else
return system("sudo shutdown -r now");
#endif
}
int runSystemCommand(const std::string& cmd_utf8)
{
#ifdef WIN32
// on Windows we use _wsystem to support non-ASCII paths
// which requires converting from utf8 to a wstring
typedef std::codecvt_utf8<wchar_t> convert_type;
std::wstring_convert<convert_type, wchar_t> converter;
std::wstring wchar_str = converter.from_bytes(cmd_utf8);
return _wsystem(wchar_str.c_str());
#else
return system(cmd_utf8.c_str());
#endif
}