Skip to content

Commit b21b17b

Browse files
committed
fix #1283
1 parent 84073fc commit b21b17b

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

Siv3D/src/Siv3D-Platform/Linux/Siv3D/System/SivSystem_Linux.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ namespace s3d
2222
{
2323
namespace detail
2424
{
25+
[[nodiscard]]
26+
static std::string Escape(const std::string& s)
27+
{
28+
std::string result;
29+
result.push_back('\'');
30+
31+
for (char ch : s)
32+
{
33+
if (ch == '\'')
34+
{
35+
result.append("'\\''");
36+
}
37+
else
38+
{
39+
result.push_back(ch);
40+
}
41+
}
42+
43+
result.push_back('\'');
44+
return result;
45+
}
46+
2547
[[nodiscard]]
2648
static bool Run(const char* program, char* argv[])
2749
{
@@ -200,7 +222,7 @@ namespace s3d
200222

201223
bool LaunchFile(const FilePathView fileName)
202224
{
203-
const std::string command = ("xdg-open '" + fileName.narrow() + "'");
225+
const std::string command = ("xdg-open " + detail::Escape(fileName.narrow()));
204226

205227
return (std::system(command.c_str()) == 0);
206228
}
@@ -227,7 +249,7 @@ namespace s3d
227249
}
228250
}
229251

230-
const std::string command = (std::string(editor) + " '" + fileName.narrow() + "'");
252+
const std::string command = (std::string(editor) + " " + detail::Escape(fileName.narrow()));
231253

232254
return (std::system(command.c_str()) == 0);
233255
}

Siv3D/src/Siv3D-Platform/macOS/Siv3D/System/SivSystem_macOS.mm

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@
1818
{
1919
namespace detail
2020
{
21+
[[nodiscard]]
22+
static std::string Escape(const std::string& s)
23+
{
24+
std::string result;
25+
result.push_back('\'');
26+
27+
for (char ch : s)
28+
{
29+
if (ch == '\'')
30+
{
31+
result.append("'\\''");
32+
}
33+
else
34+
{
35+
result.push_back(ch);
36+
}
37+
}
38+
39+
result.push_back('\'');
40+
return result;
41+
}
42+
2143
[[nodiscard]]
2244
static bool MacOS_OpenURLInBrowser(const char* _url)
2345
{
@@ -185,14 +207,14 @@ String DefaultLanguage()
185207

186208
bool LaunchFile(const FilePathView fileName)
187209
{
188-
const std::string command = ("open '" + FileSystem::NativePath(fileName) + "'");
210+
const std::string command = ("open " + detail::Escape(FileSystem::NativePath(fileName)));
189211

190212
return (std::system(command.c_str()) == 0);
191213
}
192214

193215
bool LaunchFileWithTextEditor(const FilePathView fileName)
194216
{
195-
const std::string command = ("open -t '" + FileSystem::NativePath(fileName) + "'");
217+
const std::string command = ("open -t " + detail::Escape(FileSystem::NativePath(fileName)));
196218

197219
return (std::system(command.c_str()) == 0);
198220
}

0 commit comments

Comments
 (0)