Skip to content

Commit 2ad4804

Browse files
committed
fixed opening files containing a space
1 parent 79af0e7 commit 2ad4804

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
2828

2929
### Fixed
3030

31+
- openening files that contain a space in the filename
3132
- 2addr range for global commands
3233
- global v command and set indicator
3334
- macro playback and undo includes sub commands

include/wex/common/tocontainer.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
#include <wx/filedlg.h>
1414
#include <wx/generic/dirctrlg.h>
1515

16-
#define WEX_CONVERT(IN) \
17-
if (IN.GetParent() == nullptr) \
18-
return; \
19-
wxArrayString paths; \
20-
in.GetPaths(paths); \
16+
#define WEX_CONVERT(IN) \
17+
if (IN.GetParent() == nullptr) \
18+
return; \
19+
wxArrayString paths; \
20+
in.GetPaths(paths); \
2121
from_array_string(paths);
2222

2323
namespace wex
@@ -57,15 +57,16 @@ template <class T> class to_container
5757
for (auto it = tok.begin(); it != tok.end(); ++it)
5858
{
5959
std::string token(*it);
60+
6061
// if escape space, add next token
61-
if (token.back() == '\\')
62+
while (token.back() == '\\')
6263
{
63-
++it;
64-
if (it != tok.end())
64+
if (++it != tok.end())
6565
{
6666
token = token.substr(0, token.size() - 1) + " " + *it;
6767
}
6868
}
69+
6970
m_container.emplace_back(token);
7071
}
7172
}
@@ -74,7 +75,9 @@ template <class T> class to_container
7475
explicit to_container(const wxComboBox* cb, size_t max_items = UINT_MAX)
7576
{
7677
if (max_items == 0)
78+
{
7779
return;
80+
}
7881

7982
// wxArrayString has no emplace_back.
8083
m_container.push_back(cb->GetValue().ToStdString());
@@ -85,13 +88,17 @@ template <class T> class to_container
8588
// The string is already present as the first one, add
8689
// all other items.
8790
for (size_t i = 1; i < cb->GetCount() && i < max_items; i++)
91+
{
8892
m_container.push_back(cb->GetString(i).ToStdString());
93+
}
8994
break;
9095
case wxNOT_FOUND:
9196
// Add the string, as it is not in the combobox, to the text,
9297
// simply by appending all combobox items.
9398
for (size_t i = 0; i < cb->GetCount() && i < max_items; i++)
99+
{
94100
m_container.push_back(cb->GetString(i).ToStdString());
101+
}
95102
break;
96103
default:
97104
// Reorder. The new first element already present, just add all
@@ -100,7 +107,9 @@ template <class T> class to_container
100107
{
101108
const std::string cb_element(cb->GetString(i));
102109
if (cb_element != cb->GetValue())
110+
{
103111
m_container.emplace_back(cb_element);
112+
}
104113
}
105114
}
106115
};

src/common/dir.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Copyright: (c) 2021-2024 Anton van Wezenbeek
66
////////////////////////////////////////////////////////////////////////////////
77

8+
#include <boost/algorithm/string.hpp>
89
#include <wex/common/dir.h>
910
#include <wex/common/stream.h>
1011
#include <wex/common/util.h>
@@ -33,6 +34,11 @@ class string_dir : public dir
3334
}
3435

3536
private:
37+
const std::string escape_spaces(const std::string& text) const
38+
{
39+
return boost::algorithm::replace_all_copy(text, " ", "\\ ");
40+
}
41+
3642
bool on_dir(const path& p) const final
3743
{
3844
m_container.emplace_back(
@@ -43,7 +49,8 @@ class string_dir : public dir
4349
bool on_file(const path& p) const final
4450
{
4551
m_container.emplace_back(
46-
data().type().test(data::dir::RECURSIVE) ? p.string() : p.filename());
52+
data().type().test(data::dir::RECURSIVE) ? escape_spaces(p.string()) :
53+
escape_spaces(p.filename()));
4754
return true;
4855
};
4956

test/common/test-dir.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Name: test-dir.cpp
33
// Purpose: Implementation for wex unit testing
44
// Author: Anton van Wezenbeek
5-
// Copyright: (c) 2021 Anton van Wezenbeek
5+
// Copyright: (c) 2021-2024 Anton van Wezenbeek
66
////////////////////////////////////////////////////////////////////////////////
77

88
#include <thread>
@@ -101,12 +101,22 @@ TEST_CASE("wex::dir")
101101

102102
SUBCASE("get_all_files")
103103
{
104-
REQUIRE(
105-
wex::get_all_files(
106-
std::string("./"),
107-
wex::data::dir().file_spec("*.txt").type(
108-
wex::data::dir::type_t().set(wex::data::dir::FILES)))
109-
.size() == 4);
104+
const auto& v(wex::get_all_files(
105+
std::string("./"),
106+
wex::data::dir().file_spec("*.txt").type(
107+
wex::data::dir::type_t().set(wex::data::dir::FILES))));
108+
109+
REQUIRE(v.size() == 5);
110+
111+
const auto& v2(wex::get_all_files(
112+
std::string("./"),
113+
wex::data::dir()
114+
.file_spec("*containing*")
115+
.type(wex::data::dir::type_t().set(wex::data::dir::FILES))));
116+
117+
REQUIRE(v2.size() == 1);
118+
CAPTURE(v2.front());
119+
REQUIRE(v2.front().contains("\\ "));
110120
}
111121

112122
SUBCASE("invalid")

test/data/test-containing space.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)