Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Apps/gamescope_hotkey_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <vector>
#include <span>
#include <optional>
#include "convar.h"
#include "Utils/Version.h"

#include <span>
Expand Down
2 changes: 1 addition & 1 deletion src/Backends/DRMBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "backend.h"
#include "color_helpers.h"
#include "Utils/Defer.h"
#include "Utils/Parsers.h"
#include "drm_include.h"
#include "edid.h"
#include "gamescope_shared.h"
Expand Down Expand Up @@ -4038,4 +4039,3 @@ int HackyDRMPresent( const FrameInfo_t *pFrameInfo, bool bAsync )
{
return static_cast<gamescope::CDRMBackend *>( GetBackend() )->Present( pFrameInfo, bAsync );
}

34 changes: 34 additions & 0 deletions src/Utils/Parsers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <charconv>
#include <cstdint>
#include <optional>
#include <string_view>
#include <strings.h>

namespace gamescope
{
template <typename T>
inline std::optional<T> Parse( std::string_view chars )
{
T obj;
auto result = std::from_chars( chars.begin(), chars.end(), obj );
if ( result.ec == std::errc{} )
return obj;
else
return std::nullopt;
}

template <>
inline std::optional<bool> Parse( std::string_view chars )
{
std::optional<uint32_t> oNumber = Parse<uint32_t>( chars );
if ( oNumber )
return !!*oNumber;

if ( strcasecmp ( chars.data(), "true" ) == 0 )
return true;
else
return false;
}
}
4 changes: 2 additions & 2 deletions src/Utils/Process.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Process.h"
#include "../Utils/Algorithm.h"
#include "../convar.h"
#include "../log.hpp"
#include "../Utils/Algorithm.h"
#include "../Utils/Defer.h"
#include "../Utils/Parsers.h"

#include <algorithm>
#include <array>
Expand Down
1 change: 0 additions & 1 deletion src/WaylandServer/GamescopeActionBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <array>
#include <unordered_set>

#include "convar.h"
#include "Utils/Algorithm.h"

#include "wlr_begin.hpp"
Expand Down
29 changes: 1 addition & 28 deletions src/convar.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

#include <span>
#include <string>
#include <string_view>
#include <optional>
#include <charconv>
#include <type_traits>
#include <cstdint>
#include <functional>

#include "Utils/Dict.h"
#include "Utils/Parsers.h"

#include "log.hpp"

Expand Down Expand Up @@ -37,30 +34,6 @@ namespace gamescope
return std::string( svThing );
}

template <typename T>
inline std::optional<T> Parse( std::string_view chars )
{
T obj;
auto result = std::from_chars( chars.begin(), chars.end(), obj );
if ( result.ec == std::errc{} )
return obj;
else
return std::nullopt;
}

template <>
inline std::optional<bool> Parse( std::string_view chars )
{
std::optional<uint32_t> oNumber = Parse<uint32_t>( chars );
if ( oNumber )
return !!*oNumber;

if ( chars == "true" )
return true;
else
return false;
}

inline void Split( std::vector<std::string_view> &tokens, std::string_view string, std::string_view delims = " " )
{
size_t end = 0;
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
#include "wlserver.hpp"
#include "convar.h"
#include "gpuvis_trace_utils.h"
#include "Utils/Defer.h"
#include "Utils/Parsers.h"
#include "Utils/Process.h"
#include "Utils/TempFiles.h"
#include "Utils/Version.h"
#include "Utils/Process.h"
#include "Utils/Defer.h"

#include "backends.h"
#include "refresh_rate.h"
Expand Down
3 changes: 2 additions & 1 deletion src/steamcompmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@
#include "commit.h"
#include "reshade_effect_manager.hpp"
#include "BufferMemo.h"
#include "Utils/Process.h"
#include "Utils/Algorithm.h"
#include "Utils/Parsers.h"
#include "Utils/Process.h"

#include "wlr_begin.hpp"
#include "wlr/types/wlr_pointer_constraints_v1.h"
Expand Down
8 changes: 8 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ test_convar = executable(
cpp_args: gamescope_cpp_args,
)
test('convar', test_convar)

test_utils_parsers = executable(
'test_utils_parsers',
['test_utils_parsers.cpp'],
include_directories: [srcdir],
dependencies: [catch2_dep],
)
test('utils/parsers', test_utils_parsers)
95 changes: 95 additions & 0 deletions tests/test_utils_parsers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>

#include <type_traits>

#include "Utils/Parsers.h"

using namespace gamescope;

TEST_CASE("Utils/Parsers") {
SECTION("int") {
REQUIRE(Parse<int>("42") == 42);
REQUIRE(Parse<int>("42,foo") == 42);
REQUIRE(Parse<int>("42 foo") == 42);
REQUIRE(Parse<int>("foo,42") == std::nullopt);
REQUIRE(Parse<int>("foo 42") == std::nullopt);
REQUIRE(Parse<int>("42,35") == 42);
REQUIRE(Parse<int>("-35") == -35);
REQUIRE(Parse<int>("000") == 0);
REQUIRE(Parse<int>("-00") == 0);
REQUIRE(Parse<int>("") == std::nullopt);
}

SECTION("uint") {
REQUIRE(Parse<uint>("42") == 42);
REQUIRE(Parse<uint>("42,foo") == 42);
REQUIRE(Parse<uint>("42 foo") == 42);
REQUIRE(Parse<uint>("foo,42") == std::nullopt);
REQUIRE(Parse<uint>("foo 42") == std::nullopt);
REQUIRE(Parse<uint>("42,35") == 42);
REQUIRE(Parse<uint>("-35") == std::nullopt);
REQUIRE(Parse<uint>("000") == 0);
REQUIRE(Parse<uint>("-00") == std::nullopt);
REQUIRE(Parse<uint>("") == std::nullopt);
}

SECTION("float") {
double eps = 0.0000001;
REQUIRE_THAT(*Parse<float>("3.14159"), Catch::Matchers::WithinRel(3.14159, eps));
REQUIRE_THAT(*Parse<float>("-42.3"), Catch::Matchers::WithinRel(-42.3, eps));
REQUIRE_THAT(*Parse<float>("42,3"), Catch::Matchers::WithinRel(42.0, eps));
REQUIRE_THAT(*Parse<float>("42,foo"), Catch::Matchers::WithinRel(42.0, eps));
REQUIRE(Parse<float>("foo,42.3") == std::nullopt);
REQUIRE(Parse<float>("foo 42.3") == std::nullopt);
REQUIRE(Parse<float>("pi") == std::nullopt);
REQUIRE(Parse<float>("") == std::nullopt);
}

SECTION("pid") {
REQUIRE(Parse<pid_t>("42") == 42);
REQUIRE(Parse<pid_t>("42,35,64") == 42);
REQUIRE(Parse<pid_t>("42 35 64") == 42);
REQUIRE(Parse<pid_t>("-42,35") == -42);
REQUIRE(Parse<pid_t>("000") == 0);
REQUIRE(Parse<pid_t>("-00") == 0);
REQUIRE(Parse<pid_t>("foo,42") == std::nullopt);
REQUIRE(Parse<pid_t>("foo 42") == std::nullopt);
REQUIRE(Parse<pid_t>("") == std::nullopt);
}

SECTION("enum") {
enum Something {
SOME_FOO = 1,
SOME_BAR = 2,
SOME_BAZ = 3,
};
REQUIRE(Parse<std::underlying_type<Something>::type>("0") == 0);
REQUIRE(Parse<std::underlying_type<Something>::type>("1") == SOME_FOO);
REQUIRE(Parse<std::underlying_type<Something>::type>("2") == SOME_BAR);
REQUIRE(Parse<std::underlying_type<Something>::type>("3") == SOME_BAZ);
REQUIRE(Parse<std::underlying_type<Something>::type>("4") == 4);
REQUIRE(Parse<std::underlying_type<Something>::type>("SOME_FOO") == std::nullopt);
REQUIRE(Parse<std::underlying_type<Something>::type>("") == std::nullopt);
}

SECTION("bool") {
REQUIRE(Parse<bool>("true") == true);
REQUIRE(Parse<bool>("TRUE") == true);
REQUIRE(Parse<bool>("True") == true);

REQUIRE(Parse<bool>("false") == false);
REQUIRE(Parse<bool>("FALSE") == false);
REQUIRE(Parse<bool>("False") == false);

REQUIRE(Parse<bool>("1") == true);
REQUIRE(Parse<bool>("1.0") == true);
REQUIRE(Parse<bool>("42") == true);
REQUIRE(Parse<bool>("-35") == false);
REQUIRE(Parse<bool>("0") == false);
REQUIRE(Parse<bool>("0.0") == false);
REQUIRE(Parse<bool>("0.1") == false);
REQUIRE(Parse<bool>("foo") == false);
REQUIRE(Parse<bool>("") == false);
}
}