From 774b9fe1b4daeeac2b1d4f6e95014478eeb03730 Mon Sep 17 00:00:00 2001 From: Iliyas Jorio Date: Sat, 9 Jan 2021 10:48:30 +0100 Subject: [PATCH] Implement GetIndStringC --- CMakeLists.txt | 1 + src/Pomme.cpp | 5 ---- src/Pomme.h | 12 ++++++++- src/Text/TextUtilities.cpp | 55 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 src/Text/TextUtilities.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a56ed66..2bbc5af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ set(POMME_SOURCES ${POMME_SRCDIR}/Sound/MACE.cpp ${POMME_SRCDIR}/Sound/SoundManager.cpp ${POMME_SRCDIR}/Sound/xlaw.cpp + ${POMME_SRCDIR}/Text/TextUtilities.cpp ${POMME_SRCDIR}/Time/TimeManager.cpp ${POMME_SRCDIR}/Utilities/BigEndianIStream.cpp ${POMME_SRCDIR}/Utilities/FixedPool.h diff --git a/src/Pomme.cpp b/src/Pomme.cpp index 9e1438a..41783f3 100644 --- a/src/Pomme.cpp +++ b/src/Pomme.cpp @@ -45,11 +45,6 @@ void FlushEvents(short, short) TODOMINOR(); } -int NumToStringC(long theNum, Str255 theString) -{ - return snprintf(theString, 256, "%ld", theNum); -} - //----------------------------------------------------------------------------- // Mouse cursor diff --git a/src/Pomme.h b/src/Pomme.h index ae940bc..d36466e 100644 --- a/src/Pomme.h +++ b/src/Pomme.h @@ -250,7 +250,17 @@ void SysBeep(short duration); void FlushEvents(short, short); -int NumToStringC(long theNum, Str255 theString); +//----------------------------------------------------------------------------- +// Text + +// Convert number to Pascal string (with length prefix byte) +void NumToString(long theNum, Str255 theString); + +// Convert number to C string (zero-terminated) +int NumToStringC(long theNum, Str255 theStringC); + +// Get substring in 'STR#' resource as C string (zero-terminated) +void GetIndStringC(Str255 theStringC, short strListID, short index); //----------------------------------------------------------------------------- // Input diff --git a/src/Text/TextUtilities.cpp b/src/Text/TextUtilities.cpp new file mode 100644 index 0000000..298705f --- /dev/null +++ b/src/Text/TextUtilities.cpp @@ -0,0 +1,55 @@ +#include "Pomme.h" +#include "PommeTime.h" +#include "PommeTypes.h" +#include "PommeDebug.h" + +#include +#include +#include + +void NumToString(long theNum, Str255 theString) +{ + int rc = snprintf(theString+1, 254, "%ld", theNum); + theString[0] = (rc < 0 || rc > 255) ? 0 : (unsigned char)rc; +} + +int NumToStringC(long theNum, Str255 theString) +{ + return snprintf(theString, 256, "%ld", theNum); +} + +void GetIndStringC(Str255 theStringC, short strListID, short index) +{ + theStringC[0] = '\0'; + + Handle strListHandle = GetResource('STR#', strListID); + + if (!strListHandle) + return; + + memstream substream(*strListHandle, GetHandleSize(strListHandle)); + Pomme::BigEndianIStream f(substream); + + int16_t nStrings = f.Read(); + + if (index > nStrings) // index starts at 1, hence '>' rather than '>=' + { + ReleaseResource(strListHandle); + return; + } + + // Skip to requested string + uint8_t pstrlen = 0; + for (int i = 1; i < index; i++) // index starts at 1 + { + pstrlen = f.Read(); + f.Skip(pstrlen); + } + + pstrlen = f.Read(); + f.Read(theStringC, pstrlen); + theStringC[pstrlen] = '\0'; + static_assert(sizeof(Str255) == 256); + + ReleaseResource(strListHandle); +}