Fix last character being truncated in clipboard::copy_text due to off-by-one error (#262)

## Link to GitHub Issue, if one exists
n/a, user reported

## Description of change
When copying text to clipboard, the last character was being dropped due
to incorrect string buffer size calculation.

Also, clean up callers to just pass std:string by reference instead of
going back and forth between C-style strings.

Lastly, fix `git archive` in `build_all.sh` which broke after github
migration.

## Compiling
Yes

## Testing
Tested all three callers - IIDX cam hook, scard reader in Cards tab, and
patch manager datecode.
This commit is contained in:
bicarus-dev
2025-03-15 22:54:13 -07:00
committed by GitHub
parent ab5c343c7d
commit 4c215aa5bb
6 changed files with 8 additions and 8 deletions
+1 -1
View File
@@ -189,7 +189,7 @@ mkdir -p ${OUTDIR}/src
if ((INCLUDE_SRC > 0)) if ((INCLUDE_SRC > 0))
then then
echo "Generating source file archive..." echo "Generating source file archive..."
git archive --format tar.gz --prefix=spice2x/ HEAD > ${OUTDIR}/src/spice2x-${GIT_BRANCH}.tar.gz 2>/dev/null || \ git archive --format tar.gz --prefix=spice2x/ HEAD ./ > ${OUTDIR}/src/spice2x-${GIT_BRANCH}.tar.gz 2>/dev/null || \
echo "WARNING: Couldn't get git to create the archive. Is this a git repository?" echo "WARNING: Couldn't get git to create the archive. Is this a git repository?"
fi fi
+3 -3
View File
@@ -142,14 +142,14 @@ namespace clipboard {
handle.detach(); handle.detach();
} }
void copy_text(const std::string str) { void copy_text(const std::string& str) {
if (!OpenClipboard(nullptr)) { if (!OpenClipboard(nullptr)) {
log_warning("clipboard", "Failed to open clipboard"); log_warning("clipboard", "Failed to open clipboard");
return; return;
} }
HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, str.length()); HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, str.length() + 1);
memcpy(GlobalLock(mem), str.c_str(), str.length()); memcpy(GlobalLock(mem), str.c_str(), str.length() + 1);
GlobalUnlock(mem); GlobalUnlock(mem);
EmptyClipboard(); EmptyClipboard();
+1 -1
View File
@@ -4,6 +4,6 @@
namespace clipboard { namespace clipboard {
void copy_image(const std::filesystem::path path); void copy_image(const std::filesystem::path path);
void copy_text(const std::string str); void copy_text(const std::string& str);
const std::string paste_text(); const std::string paste_text();
} }
@@ -74,7 +74,7 @@ namespace overlay::windows {
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Copy")) { if (ImGui::Button("Copy")) {
const auto s = selectedCamera->GetFriendlyName() + "\n" + selectedCamera->GetSymLink(); const auto s = selectedCamera->GetFriendlyName() + "\n" + selectedCamera->GetSymLink();
clipboard::copy_text(s.c_str()); clipboard::copy_text(s);
} }
// Render parameters // Render parameters
+1 -1
View File
@@ -2524,7 +2524,7 @@ namespace overlay::windows {
); );
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Copy")) { if (ImGui::Button("Copy")) {
clipboard::copy_text(card_str.c_str()); clipboard::copy_text(card_str);
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Clear")) { if (ImGui::Button("Clear")) {
@@ -295,7 +295,7 @@ namespace overlay::windows {
ImGui::Text("%s", avs::game::get_identifier().c_str()); ImGui::Text("%s", avs::game::get_identifier().c_str());
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Copy")) { if (ImGui::Button("Copy")) {
clipboard::copy_text(identifiers.c_str()); clipboard::copy_text(identifiers);
} }
ImGui::AlignTextToFramePadding(); ImGui::AlignTextToFramePadding();