Implement GetIndStringC

This commit is contained in:
Iliyas Jorio 2021-01-09 10:48:30 +01:00
parent 0640b1356d
commit 774b9fe1b4
4 changed files with 67 additions and 6 deletions

View File

@ -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

View File

@ -45,11 +45,6 @@ void FlushEvents(short, short)
TODOMINOR();
}
int NumToStringC(long theNum, Str255 theString)
{
return snprintf(theString, 256, "%ld", theNum);
}
//-----------------------------------------------------------------------------
// Mouse cursor

View File

@ -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

View File

@ -0,0 +1,55 @@
#include "Pomme.h"
#include "PommeTime.h"
#include "PommeTypes.h"
#include "PommeDebug.h"
#include <cstring>
#include <Utilities/memstream.h>
#include <Utilities/BigEndianIStream.h>
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<int16_t>();
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<uint8_t>();
f.Skip(pstrlen);
}
pstrlen = f.Read<uint8_t>();
f.Read(theStringC, pstrlen);
theStringC[pstrlen] = '\0';
static_assert(sizeof(Str255) == 256);
ReleaseResource(strListHandle);
}