forked from MaskRay/ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_win.cc
155 lines (129 loc) · 3.95 KB
/
platform_win.cc
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#if defined(_WIN32)
#include "platform.h"
#include "utils.h"
#include <loguru.hpp>
#include <Windows.h>
#include <direct.h>
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
void PlatformInit() {
// We need to write to stdout in binary mode because in Windows, writing
// \n will implicitly write \r\n. Language server API will ignore a
// \r\r\n split request.
_setmode(_fileno(stdout), O_BINARY);
_setmode(_fileno(stdin), O_BINARY);
}
// See
// https://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from
std::string GetExecutablePath() {
char result[MAX_PATH] = {0};
GetModuleFileName(NULL, result, MAX_PATH);
return NormalizePath(result);
}
// See http://stackoverflow.com/a/19535628
std::string GetWorkingDirectory() {
char result[MAX_PATH];
std::string binary_path(result, GetModuleFileName(NULL, result, MAX_PATH));
return binary_path.substr(0, binary_path.find_last_of("\\/") + 1);
}
std::string NormalizePath(const std::string& path) {
DWORD retval = 0;
TCHAR buffer[MAX_PATH] = TEXT("");
TCHAR** lpp_part = {NULL};
retval = GetFullPathName(path.c_str(), MAX_PATH, buffer, lpp_part);
// fail, return original
if (retval == 0)
return path;
std::string result = buffer;
std::replace(result.begin(), result.end(), '\\', '/');
// std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
bool TryMakeDirectory(const std::string& absolute_path) {
if (_mkdir(absolute_path.c_str()) == -1) {
// Success if the directory exists.
return errno == EEXIST;
}
return true;
}
// See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
const DWORD MS_VC_EXCEPTION = 0x406D1388;
#pragma pack(push, 8)
typedef struct tagTHREADNAME_INFO {
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
void SetCurrentThreadName(const std::string& thread_name) {
loguru::set_thread_name(thread_name.c_str());
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = thread_name.c_str();
info.dwThreadID = (DWORD)-1;
info.dwFlags = 0;
__try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR),
(ULONG_PTR*)&info);
#ifdef _MSC_VER
} __except (EXCEPTION_EXECUTE_HANDLER) {
#else
} catch (...) {
#endif
}
}
optional<int64_t> GetLastModificationTime(const std::string& absolute_path) {
struct _stat buf;
if (_stat(absolute_path.c_str(), &buf) != 0) {
switch (errno) {
case ENOENT:
// std::cerr << "GetLastModificationTime: unable to find file " <<
// absolute_path << std::endl;
return nullopt;
case EINVAL:
// std::cerr << "GetLastModificationTime: invalid param to _stat for
// file file " << absolute_path << std::endl;
return nullopt;
default:
// std::cerr << "GetLastModificationTime: unhandled for " <<
// absolute_path << std::endl; exit(1);
return nullopt;
}
}
return buf.st_mtime;
}
void MoveFileTo(const std::string& destination, const std::string& source) {
MoveFile(source.c_str(), destination.c_str());
}
void CopyFileTo(const std::string& destination, const std::string& source) {
CopyFile(source.c_str(), destination.c_str(), false /*failIfExists*/);
}
bool IsSymLink(const std::string& path) {
return false;
}
std::vector<std::string> GetPlatformClangArguments() {
//
// Found by executing
//
// $ clang++ -E -x c++ - -v
//
// clang-format off
return {
"-fms-extensions", "-fms-compatibility", "-fdelayed-template-parsing"
};
// clang-format on
}
void FreeUnusedMemory() {}
bool RunObjectiveCIndexTests() {
return false;
}
// TODO Wait for debugger to attach
void TraceMe() {}
#endif