-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathttkrunobject.cpp
71 lines (62 loc) · 1.72 KB
/
ttkrunobject.cpp
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
#include "ttkrunobject.h"
#include "ttkversion.h"
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <shellapi.h>
#else
# include <unistd.h>
#endif
namespace TTK
{
static bool endWith(const TTKString &value, const TTKString &tail)
{
if(value.empty() || tail.empty() || value.size() < tail.size())
{
return false;
}
return value.compare(value.length() - tail.size(), tail.size(), tail) == 0;
}
}
void TTKRunObject::run(int argc, char **argv) const
{
TTKString args;
for(int i = 0; i < argc; ++i)
{
TTKString arg(argv[i]);
if(!TTK::endWith(arg, TTK_APP_FILE_NAME))
{
const size_t pos = arg.find('\"');
if(pos != TTKString::npos)
{
arg.insert(arg.begin() + pos, '\\');
}
args.append(TTK_STR_QUOTES(arg));
args.append(TTK_SPACE);
}
}
char path[TTK_LOW_BUFFER] = {};
const char * const suffix = TTK_STR_CAT(TTK_SEPARATOR, TTK_VERSION_STR, TTK_SEPARATOR, TTK_SERVICE_RUN_NAME);
#ifdef _WIN32
GetModuleFileNameA(nullptr, path, TTK_LOW_BUFFER);
TTKString filePath = path;
const size_t pos = filePath.find_last_of('\\');
if(pos != TTKString::npos)
{
filePath.erase(pos);
}
ShellExecuteA(nullptr, "open", (filePath + suffix).c_str(), args.c_str(), nullptr, SW_HIDE);
#else
TTKString filePath = get_current_dir_name();
if(readlink("/proc/self/exe", path, sizeof(path) - 1) != 0)
{
filePath = path;
const size_t pos = filePath.find_last_of('/');
if(pos != TTKString::npos)
{
filePath.erase(pos);
}
}
system((filePath + suffix + TTK_SPACE + args).c_str());
#endif
}