Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix -Wnrvo warning, use char_traits and avoid alloc for enumerating readers #108

Merged
merged 1 commit into from
Feb 4, 2025
Merged
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
30 changes: 6 additions & 24 deletions lib/libpcsc-cpp/src/listReaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@

#include "Context.hpp"

#include <cstring>
#include <memory>
#include <algorithm>
#include <map>
#include <memory>

namespace
{
Expand All @@ -42,30 +41,13 @@ inline DWORD updateReaderNamesBuffer(const SCARDCONTEXT ctx, string_t::value_typ
return bufferLengthOut;
}

std::vector<const string_t::value_type*> getReaderNamePointerList(const string_t& readerNames)
{
auto readerNamePointerList = std::vector<const string_t::value_type*> {};

if (readerNames.empty())
return readerNamePointerList;

// Reader names are \0 separated and end with double \0.
#ifdef _WIN32
for (const string_t::value_type* name = readerNames.c_str(); *name; name += wcslen(name) + 1) {
#else
for (const string_t::value_type* name = readerNames.c_str(); *name; name += strlen(name) + 1) {
#endif
readerNamePointerList.push_back(name);
}

return readerNamePointerList;
}

std::vector<SCARD_READERSTATE> getReaderStates(const SCARDCONTEXT ctx, const string_t& readerNames)
{
auto readerStates = std::vector<SCARD_READERSTATE> {};
for (const auto& readerNamePointer : getReaderNamePointerList(readerNames)) {
readerStates.push_back({readerNamePointer,
// Reader names are \0 separated and end with double \0.
for (const auto* name = readerNames.c_str(); *name;
name += string_t::traits_type::length(name) + 1) {
readerStates.push_back({name,
nullptr,
SCARD_STATE_UNAWARE,
SCARD_STATE_UNAWARE,
Expand All @@ -76,7 +58,7 @@ std::vector<SCARD_READERSTATE> getReaderStates(const SCARDCONTEXT ctx, const str
}

if (readerStates.empty())
return {};
return readerStates;

SCard(GetStatusChange, ctx, 0U, readerStates.data(), DWORD(readerStates.size()));

Expand Down
Loading