[config] Move strutils to src/config/

This removes circular dependencies between the main wx target and the
config/ sub-directory.
This commit is contained in:
Fabrice de Gans 2024-05-05 21:38:14 -07:00
parent d377f7abff
commit 90a56c6937
20 changed files with 173 additions and 75 deletions

View File

@ -1,4 +1,7 @@
#include "core/base/version.h"
#include "core/base/version_gen.h"
const std::string kVbamMainVersion = VBAM_CURRENT_VERSION;
const std::string kVbamNameAndSubversion = VBAM_NAME_AND_SUBVERSION;
const std::string kVbamVersion = VBAM_VERSION;

View File

@ -3,7 +3,9 @@
#include <string>
#include "core/base/version_gen.h"
// Main version information, generated by the build system.
// e.g. "2.1.9"
extern const std::string kVbamMainVersion;
// Full version information, generated by the build system.
// e.g. "VisualBoyAdvance-M 2.1.9-316e4a43 msvc-316e4a43"

View File

@ -31,6 +31,8 @@ set(VBAM_WX_COMMON
config/option-proxy.h
config/option.cpp
config/option.h
config/strutils.cpp
config/strutils.h
config/user-input.cpp
config/user-input.h
dialogs/accel-config.cpp
@ -60,8 +62,6 @@ set(VBAM_WX_COMMON
opts.h
panel.cpp
rpi.h
strutils.cpp
strutils.h
sys.cpp
viewers.cpp
viewsupt.cpp

View File

@ -1,8 +1,8 @@
#ifndef AUTOUPDATER_H
#define AUTOUPDATER_H
#ifndef VBAM_WX_AUTOUPDATER_AUTOUPDATER_H_
#define VBAM_WX_AUTOUPDATER_AUTOUPDATER_H_
void initAutoupdater();
void checkUpdatesUi();
void shutdownAutoupdater();
#endif // AUTOUPDATER_H
#endif // VBAM_WX_AUTOUPDATER_AUTOUPDATER_H_

View File

@ -1,5 +1,6 @@
#include "../autoupdater.h"
#include "sparkle-wrapper.h"
#include "wx/autoupdater/autoupdater.h"
#include "wx/autoupdater/macos/sparkle-wrapper.h"
SparkleWrapper autoupdater;

View File

@ -1,5 +1,5 @@
#ifndef SPARKLE_WRAPPER_H
#define SPARKLE_WRAPPER_H
#ifndef VBAM_WX_AUTOUPDATER_MACOS_SPARKLE_WRAPPER_H_
#define VBAM_WX_AUTOUPDATER_MACOS_SPARKLE_WRAPPER_H_
class SparkleWrapper
{
@ -14,4 +14,4 @@ class SparkleWrapper
Private* d;
};
#endif // SPARKLE_WRAPPER_H
#endif // VBAM_WX_AUTOUPDATER_MACOS_SPARKLE_WRAPPER_H_

View File

@ -1,14 +1,13 @@
#include "../autoupdater.h"
#include "wx/autoupdater/autoupdater.h"
#include "core/base/version.h"
#include "../../strutils.h"
#include "winsparkle-wrapper.h"
#include "wx/autoupdater/wxmsw/winsparkle-wrapper.h"
void initAutoupdater()
{
// even if we are a nightly, only check latest stable version
wxString version = strutils::split(kVbamVersion, '-')[0];
const wxString version(kVbamMainVersion);
#ifndef NO_HTTPS
win_sparkle_set_appcast_url("https://data.visualboyadvance-m.org/appcast.xml");
#else

View File

@ -1,12 +1,13 @@
#include "wx/autoupdater/wxmsw/winsparkle-wrapper.h"
#include <cstdio>
#include <string>
#include <stdexcept>
#include <wx/file.h>
#include <wx/filename.h>
#include <wx/msw/private.h>
#include <wx/utils.h>
#include "wx/wxvbam.h"
#include "winsparkle-wrapper.h"
#include "wx/msw/private.h"
#include "wx/autoupdater/wxmsw/winsparkle-rc.h"
WinSparkleDllWrapper *WinSparkleDllWrapper::GetInstance()
{

View File

@ -4,8 +4,6 @@
#include <wx/string.h>
#include <wx/dynlib.h>
#include "winsparkle-rc.h"
class WinSparkleDllWrapper {
public:
static WinSparkleDllWrapper *GetInstance();

View File

@ -3,7 +3,6 @@
#include <wx/string.h>
#include <wx/translation.h>
#include <wx/xrc/xmlres.h>
#include <unordered_set>
#include "wx/config/user-input.h"

View File

@ -3,7 +3,6 @@
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "wx/config/command.h"

View File

@ -6,7 +6,7 @@
#include <wx/translation.h>
#include "wx/config/cmdtab.h"
#include "wx/strutils.h"
#include "wx/config/strutils.h"
namespace config {
namespace {
@ -131,7 +131,7 @@ nonstd::optional<Command> Command::FromString(const wxString& name) {
return nonstd::nullopt;
}
auto parts = strutils::split(name, "/");
auto parts = config::str_split(name, "/");
if (is_joypad) {
if (parts.size() != 3) {
wxLogDebug("Wrong split size: %d", parts.size());

View File

@ -1,13 +1,13 @@
#include "wx/strutils.h"
#include "wx/config/strutils.h"
#include <wx/tokenzr.h>
namespace strutils {
namespace config {
// From: https://stackoverflow.com/a/7408245/262458
//
// Modified to ignore empty tokens or return sep for them.
wxArrayString split(const wxString& text, const wxString& sep, bool empty_token_is_sep) {
wxArrayString str_split(const wxString& text, const wxString& sep, bool empty_token_is_sep) {
wxArrayString tokens;
size_t start = 0, end = 0;
@ -32,9 +32,9 @@ wxArrayString split(const wxString& text, const wxString& sep, bool empty_token_
return tokens;
}
wxArrayString split_with_sep(const wxString& text, const wxString& sep)
wxArrayString str_split_with_sep(const wxString& text, const wxString& sep)
{
return split(text, sep, true);
return str_split(text, sep, true);
}
} // namespace strutils
} // namespace config

19
src/wx/config/strutils.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef VBAM_WX_CONFIG_STRUTILS_H_
#define VBAM_WX_CONFIG_STRUTILS_H_
#include <wx/string.h>
#include <wx/arrstr.h>
namespace config {
// From: https://stackoverflow.com/a/7408245/262458
wxArrayString str_split(const wxString& text, const wxString& sep, bool empty_token_is_sep=false);
// Same as above, but it includes the sep dir.
// If "A,,,B" is the text and "," is sep, then
// 'A', ',' and 'B' will be in the output.
wxArrayString str_split_with_sep(const wxString& text, const wxString& sep);
} // namespace config
#endif // VBAM_WX_CONFIG_STRUTILS_H_

View File

@ -0,0 +1,96 @@
#include "wx/config/strutils.h"
#include <gtest/gtest.h>
TEST(StrSplitTest, Basic) {
wxString foo = "foo|bar|baz";
auto vec = config::str_split(foo, '|');
EXPECT_EQ(vec.size(), 3);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
}
TEST(StrSplitTest, MultiCharSep) {
wxString foo = "foo|-|bar|-|baz";
auto vec = config::str_split(foo, "|-|");
EXPECT_EQ(vec.size(), 3);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
}
TEST(StrSplitTest, SkipEmptyToken) {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = config::str_split(foo, "|-|");
EXPECT_EQ(vec.size(), 3);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
}
TEST(StrSplitTest, EmptyInput) {
wxString foo;
auto vec = config::str_split(foo, "|-|");
EXPECT_EQ(vec.size(), 0);
}
TEST(StrSplitTest, NoTokens) {
wxString foo = "|-||-||-||-||-|";
auto vec = config::str_split(foo, "|-|");
EXPECT_EQ(vec.size(), 0);
}
TEST(StrSplitWithSepTest, Basic) {
wxString foo = "foo|bar|baz|";
auto vec = config::str_split_with_sep(foo, '|');
EXPECT_EQ(vec.size(), 4);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
EXPECT_EQ(vec[3], "|");
}
TEST(StrSplitWithSepTest, MultiCharSep) {
wxString foo = "foo|-|bar|-|baz|-|";
auto vec = config::str_split_with_sep(foo, "|-|");
EXPECT_EQ(vec.size(), 4);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
EXPECT_EQ(vec[3], "|-|");
}
TEST(StrSplitWithSepTest, MultipleSepTokens) {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = config::str_split_with_sep(foo, "|-|");
EXPECT_EQ(vec.size(), 6);
EXPECT_EQ(vec[0], "|-|");
EXPECT_EQ(vec[1], "foo");
EXPECT_EQ(vec[2], "|-|");
EXPECT_EQ(vec[3], "bar");
EXPECT_EQ(vec[4], "baz");
EXPECT_EQ(vec[5], "|-|");
}

View File

@ -7,7 +7,7 @@
#include <wx/regex.h>
#include <wx/translation.h>
#include "wx/strutils.h"
#include "wx/config/strutils.h"
namespace config {
@ -186,7 +186,7 @@ UserInput StringToUserInput(const wxString& string) {
// Not a joystick.
// Non-ASCII keyboard input are treated as a pair of integers "key:mod".
const auto pair = strutils::split(string, ":");
const auto pair = config::str_split(string, ":");
long mod = 0;
long key = 0;
if (pair.size() == 2 && pair[0].ToLong(&key) && pair[1].ToLong(&mod)) {
@ -330,7 +330,7 @@ std::unordered_set<UserInput> UserInput::FromConfigString(const wxString& string
return user_inputs;
}
for (const auto& token : strutils::split_with_sep(string, ",")) {
for (const auto& token : config::str_split_with_sep(string, ",")) {
UserInput user_input = StringToUserInput(token);
if (!user_input) {
user_inputs.clear();

View File

@ -1,19 +0,0 @@
#ifndef VBAM_WX_STRUTILS_H_
#define VBAM_WX_STRUTILS_H_
#include <wx/string.h>
#include <wx/arrstr.h>
namespace strutils {
// From: https://stackoverflow.com/a/7408245/262458
wxArrayString split(const wxString& text, const wxString& sep, bool empty_token_is_sep=false);
// Same as above, but it includes the sep dir.
// If "A,,,B" is the text and "," is sep, then
// 'A', ',' and 'B' will be in the output.
wxArrayString split_with_sep(const wxString& text, const wxString& sep);
} // namespace strutils
#endif // VBAM_WX_STRUTILS_H_

View File

@ -29,4 +29,4 @@ function(add_doctest_test test_src)
doctest_discover_tests("${test_name}")
endfunction()
add_doctest_test(strutils.cpp ../strutils.h ../strutils.cpp)
add_doctest_test(strutils.cpp ../config/strutils.h ../config/strutils.cpp)

View File

@ -1,11 +1,11 @@
#include "wx/strutils.h"
#include "wx/config/strutils.h"
#include "tests.hpp"
TEST_CASE("strutils::split() basic test") {
TEST_CASE("config::str_split() basic test") {
wxString foo = "foo|bar|baz";
auto vec = strutils::split(foo, '|');
auto vec = config::str_split(foo, '|');
CHECK(vec.size() == 3);
@ -14,10 +14,10 @@ TEST_CASE("strutils::split() basic test") {
CHECK(vec[2] == "baz");
}
TEST_CASE("strutils::split() multi-char separator") {
TEST_CASE("config::str_split() multi-char separator") {
wxString foo = "foo|-|bar|-|baz";
auto vec = strutils::split(foo, "|-|");
auto vec = config::str_split(foo, "|-|");
CHECK(vec.size() == 3);
@ -26,10 +26,10 @@ TEST_CASE("strutils::split() multi-char separator") {
CHECK(vec[2] == "baz");
}
TEST_CASE("strutils::split() skips empty tokens") {
TEST_CASE("config::str_split() skips empty tokens") {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = strutils::split(foo, "|-|");
auto vec = config::str_split(foo, "|-|");
CHECK(vec.size() == 3);
@ -38,26 +38,26 @@ TEST_CASE("strutils::split() skips empty tokens") {
CHECK(vec[2] == "baz");
}
TEST_CASE("strutils::split() empty input") {
TEST_CASE("config::str_split() empty input") {
wxString foo;
auto vec = strutils::split(foo, "|-|");
auto vec = config::str_split(foo, "|-|");
CHECK(vec.size() == 0);
}
TEST_CASE("strutils::split() no tokens, just separators") {
TEST_CASE("config::str_split() no tokens, just separators") {
wxString foo = "|-||-||-||-||-|";
auto vec = strutils::split(foo, "|-|");
auto vec = config::str_split(foo, "|-|");
CHECK(vec.size() == 0);
}
TEST_CASE("strutils::split_with_sep() basic test") {
TEST_CASE("config::str_split_with_sep() basic test") {
wxString foo = "foo|bar|baz|";
auto vec = strutils::split_with_sep(foo, '|');
auto vec = config::str_split_with_sep(foo, '|');
CHECK(vec.size() == 4);
@ -67,10 +67,10 @@ TEST_CASE("strutils::split_with_sep() basic test") {
CHECK(vec[3] == "|");
}
TEST_CASE("strutils::split_with_sep() multi-char sep") {
TEST_CASE("config::str_split_with_sep() multi-char sep") {
wxString foo = "foo|-|bar|-|baz|-|";
auto vec = strutils::split_with_sep(foo, "|-|");
auto vec = config::str_split_with_sep(foo, "|-|");
CHECK(vec.size() == 4);
@ -80,10 +80,10 @@ TEST_CASE("strutils::split_with_sep() multi-char sep") {
CHECK(vec[3] == "|-|");
}
TEST_CASE("strutils::split_with_sep() multiple sep tokens") {
TEST_CASE("config::str_split_with_sep() multiple sep tokens") {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = strutils::split_with_sep(foo, "|-|");
auto vec = config::str_split_with_sep(foo, "|-|");
CHECK(vec.size() == 6);

View File

@ -39,7 +39,7 @@
#include "wx/config/option-proxy.h"
#include "wx/config/option.h"
#include "wx/config/user-input.h"
#include "wx/strutils.h"
#include "wx/config/strutils.h"
#include "wx/wayland.h"
#include "wx/widgets/group-check-box.h"
#include "wx/widgets/user-input-ctrl.h"
@ -433,7 +433,7 @@ bool wxvbamApp::OnInit() {
// process command-line options
for (size_t i = 0; i < pending_optset.size(); i++) {
auto parts = strutils::split(pending_optset[i], wxT('='));
auto parts = config::str_split(pending_optset[i], wxT('='));
opt_set(parts[0], parts[1]);
}
@ -773,7 +773,7 @@ bool wxvbamApp::OnCmdLineParsed(wxCmdLineParser& cl)
for (int i = 0; i < nparm; i++) {
auto p = cl.GetParam(i);
auto parts = strutils::split(p, wxT('='));
auto parts = config::str_split(p, wxT('='));
if (parts.size() > 1) {
opt_set(parts[0], parts[1]);