Skip to content

Commit

Permalink
fixed opening files containing a space
Browse files Browse the repository at this point in the history
  • Loading branch information
antonvw committed Nov 19, 2024
1 parent 79af0e7 commit 2ad4804
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Fixed

- openening files that contain a space in the filename
- 2addr range for global commands
- global v command and set indicator
- macro playback and undo includes sub commands
Expand Down
25 changes: 17 additions & 8 deletions include/wex/common/tocontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
#include <wx/filedlg.h>
#include <wx/generic/dirctrlg.h>

#define WEX_CONVERT(IN) \
if (IN.GetParent() == nullptr) \
return; \
wxArrayString paths; \
in.GetPaths(paths); \
#define WEX_CONVERT(IN) \
if (IN.GetParent() == nullptr) \
return; \
wxArrayString paths; \
in.GetPaths(paths); \
from_array_string(paths);

namespace wex
Expand Down Expand Up @@ -57,15 +57,16 @@ template <class T> class to_container
for (auto it = tok.begin(); it != tok.end(); ++it)
{
std::string token(*it);

// if escape space, add next token
if (token.back() == '\\')
while (token.back() == '\\')
{
++it;
if (it != tok.end())
if (++it != tok.end())
{
token = token.substr(0, token.size() - 1) + " " + *it;
}
}

m_container.emplace_back(token);
}
}
Expand All @@ -74,7 +75,9 @@ template <class T> class to_container
explicit to_container(const wxComboBox* cb, size_t max_items = UINT_MAX)
{
if (max_items == 0)
{
return;
}

// wxArrayString has no emplace_back.
m_container.push_back(cb->GetValue().ToStdString());
Expand All @@ -85,13 +88,17 @@ template <class T> class to_container
// The string is already present as the first one, add
// all other items.
for (size_t i = 1; i < cb->GetCount() && i < max_items; i++)
{
m_container.push_back(cb->GetString(i).ToStdString());
}
break;
case wxNOT_FOUND:
// Add the string, as it is not in the combobox, to the text,
// simply by appending all combobox items.
for (size_t i = 0; i < cb->GetCount() && i < max_items; i++)
{
m_container.push_back(cb->GetString(i).ToStdString());
}
break;
default:
// Reorder. The new first element already present, just add all
Expand All @@ -100,7 +107,9 @@ template <class T> class to_container
{
const std::string cb_element(cb->GetString(i));
if (cb_element != cb->GetValue())
{
m_container.emplace_back(cb_element);
}
}
}
};
Expand Down
9 changes: 8 additions & 1 deletion src/common/dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Copyright: (c) 2021-2024 Anton van Wezenbeek
////////////////////////////////////////////////////////////////////////////////

#include <boost/algorithm/string.hpp>
#include <wex/common/dir.h>
#include <wex/common/stream.h>
#include <wex/common/util.h>
Expand Down Expand Up @@ -33,6 +34,11 @@ class string_dir : public dir
}

private:
const std::string escape_spaces(const std::string& text) const
{
return boost::algorithm::replace_all_copy(text, " ", "\\ ");
}

bool on_dir(const path& p) const final
{
m_container.emplace_back(
Expand All @@ -43,7 +49,8 @@ class string_dir : public dir
bool on_file(const path& p) const final
{
m_container.emplace_back(
data().type().test(data::dir::RECURSIVE) ? p.string() : p.filename());
data().type().test(data::dir::RECURSIVE) ? escape_spaces(p.string()) :
escape_spaces(p.filename()));
return true;
};

Expand Down
24 changes: 17 additions & 7 deletions test/common/test-dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Name: test-dir.cpp
// Purpose: Implementation for wex unit testing
// Author: Anton van Wezenbeek
// Copyright: (c) 2021 Anton van Wezenbeek
// Copyright: (c) 2021-2024 Anton van Wezenbeek
////////////////////////////////////////////////////////////////////////////////

#include <thread>
Expand Down Expand Up @@ -101,12 +101,22 @@ TEST_CASE("wex::dir")

SUBCASE("get_all_files")
{
REQUIRE(
wex::get_all_files(
std::string("./"),
wex::data::dir().file_spec("*.txt").type(
wex::data::dir::type_t().set(wex::data::dir::FILES)))
.size() == 4);
const auto& v(wex::get_all_files(
std::string("./"),
wex::data::dir().file_spec("*.txt").type(
wex::data::dir::type_t().set(wex::data::dir::FILES))));

REQUIRE(v.size() == 5);

const auto& v2(wex::get_all_files(
std::string("./"),
wex::data::dir()
.file_spec("*containing*")
.type(wex::data::dir::type_t().set(wex::data::dir::FILES))));

REQUIRE(v2.size() == 1);
CAPTURE(v2.front());
REQUIRE(v2.front().contains("\\ "));
}

SUBCASE("invalid")
Expand Down
Empty file.

0 comments on commit 2ad4804

Please sign in to comment.