-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatformUtil.cpp
More file actions
183 lines (152 loc) · 5.21 KB
/
Copy pathPlatformUtil.cpp
File metadata and controls
183 lines (152 loc) · 5.21 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "runcpp2/PlatformUtil.hpp"
#include "ssLogger/ssLog.hpp"
#include "ghc/filesystem.hpp"
#include <stdio.h>
#include <cctype>
namespace
{
char GetAltFileSystemSeparator()
{
#ifdef _WIN32
return '/';
#elif __unix__
return '\0';
#else
return '\0';
#endif
}
char GetFileSystemSeparator()
{
#ifdef _WIN32
return '\\';
#elif __unix__
return '/';
#else
return '/';
#endif
}
}
std::string runcpp2::ProcessPath(const std::string& path)
{
std::string processedScriptPath;
char altSeparator = GetAltFileSystemSeparator();
//Convert alternative slashes to native slashes
if(altSeparator != '\0')
{
char separator = GetFileSystemSeparator();
processedScriptPath = path;
for(int i = 0; i < processedScriptPath.size(); ++i)
{
if(processedScriptPath[i] == altSeparator)
processedScriptPath[i] = separator;
}
return processedScriptPath;
}
else
return path;
}
std::vector<std::string> runcpp2::GetPlatformNames()
{
#ifdef _WIN32
return {"Windows", "DefaultPlatform"};
#elif __linux__
return {"Linux", "Unix", "DefaultPlatform"};
#elif __APPLE__
return {"MacOS", "Unix", "DefaultPlatform"};
#elif __unix__
return {"Unix", "DefaultPlatform"};
#else
return {"Unknown", "DefaultPlatform"};
#endif
}
bool runcpp2::RunCommandAndGetOutput( const std::string& command,
std::string& outOutput,
std::string runDirectory)
{
int returnCode;
return RunCommandAndGetOutput(command, outOutput, returnCode, runDirectory);
}
bool runcpp2::RunCommandAndGetOutput( const std::string& command,
std::string& outOutput,
int& outReturnCode,
std::string runDirectory)
{
ssLOG_FUNC_DEBUG();
ssLOG_DEBUG("Running: " << command);
System2CommandInfo commandInfo = {};
if(!runDirectory.empty())
commandInfo.RunDirectory = runDirectory.c_str();
commandInfo.RedirectOutput = true;
SYSTEM2_RESULT sys2Result = System2Run(command.c_str(), &commandInfo);
if(sys2Result != SYSTEM2_RESULT_SUCCESS)
{
ssLOG_ERROR("System2Run failed with result: " << sys2Result);
return false;
}
outOutput.clear();
do
{
uint32_t byteRead = 0;
outOutput.resize(outOutput.size() + 4096);
sys2Result =
System2ReadFromOutput( &commandInfo,
const_cast<char*>(outOutput.data()) + outOutput.size() - 4096,
4096,
&byteRead);
outOutput.resize(outOutput.size() + byteRead);
}
while(sys2Result == SYSTEM2_RESULT_READ_NOT_FINISHED);
sys2Result = System2GetCommandReturnValueSync(&commandInfo, &outReturnCode);
if(sys2Result != SYSTEM2_RESULT_SUCCESS)
{
ssLOG_ERROR("System2GetCommandReturnValueSync failed with result: " << sys2Result);
return false;
}
ssLOG_DEBUG("outOutput: \n" << outOutput.c_str());
if(outReturnCode != 0)
{
ssLOG_DEBUG("Failed when running command with return code: " << outReturnCode);
return false;
}
return true;
}
#if defined(_WIN32)
//Credit: https://stackoverflow.com/a/17387176
std::string runcpp2::GetWindowsError()
{
//Get the error message ID, if any.
DWORD errorMessageID = ::GetLastError();
if(errorMessageID == 0) {
return std::string(); //No error message has been recorded
}
LPSTR messageBuffer = nullptr;
//Ask Win32 to give us the string version of that message ID.
//The parameters we pass in, tell Win32 to create the buffer that holds the message for us (because we don't yet know how long the message string will be).
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
//Copy the error message into a std::string.
std::string message(messageBuffer, size);
//Free the Win32's string's buffer.
LocalFree(messageBuffer);
return message;
}
#endif
std::string runcpp2::GetFileExtensionWithoutVersion(const ghc::filesystem::path& path)
{
std::string filename = path.filename().string();
bool inNumericPart = true;
int lastDotPos = filename.length();
for(int i = filename.length() - 1; i >= 0; --i)
{
if(filename[i] == '.')
{
if(!inNumericPart)
return filename.substr(i, lastDotPos - i);
inNumericPart = true;
lastDotPos = i;
}
else if(!std::isdigit(filename[i]))
inNumericPart = false;
}
return "";
}