Files
Aerofoil/GpApp/StringUtils.cpp

306 lines
6.8 KiB
C++
Raw Normal View History

2019-11-11 00:11:59 -05:00
//============================================================================
//----------------------------------------------------------------------------
// StringUtils.c
//----------------------------------------------------------------------------
//============================================================================
#include "PLPasStr.h"
#include "Externs.h"
2020-05-21 05:01:16 -04:00
#include "RenderedFont.h"
2019-11-11 00:11:59 -05:00
#include <string.h>
//============================================================== Functions
//-------------------------------------------------------------- PasStringCopy
2020-10-14 18:18:57 -04:00
// Given a source string and storage for a second, this function?
2019-11-11 00:11:59 -05:00
// copies from one to the other. It assumes Pascal style strings.
void PasStringCopy (StringPtr p1, StringPtr p2)
{
2020-10-14 18:18:57 -04:00
short stringLength;
2019-11-11 00:11:59 -05:00
stringLength = *p2++ = *p1++;
while (--stringLength >= 0)
*p2++ = *p1++;
}
//-------------------------------------------------------------- WhichStringFirst
2020-10-14 18:18:57 -04:00
// This is a sorting function that handles two Pascal strings. It?
// will return a 1 to indicate the 1st string is "greater", a 1 to?
// indicate the 2nd was greater and a 0 to indicate that the strings?
2019-11-11 00:11:59 -05:00
// are equal.
short WhichStringFirst (StringPtr p1, StringPtr p2)
{
short smallestLength, seek, greater;
char char1, char2;
Boolean foundIt;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
smallestLength = p1[0];
if (p2[0] < smallestLength)
smallestLength = p2[0];
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
greater = 0; // neither are greater, they are equal
seek = 1; // start at character #1
foundIt = false;
do
{
char1 = p1[seek]; // make upper case (if applicable)
if ((char1 > 0x60) && (char1 < 0x7B))
char1 -= 0x20;
char2 = p2[seek]; // make upper case (if applicable)
if ((char2 > 0x60) && (char2 < 0x7B))
char2 -= 0x20;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
if (char1 > char2) // first string is greater
{
greater = 1;
foundIt = true;
}
else if (char1 < char2) // second string is greater
{
greater = 2;
foundIt = true;
}
seek++;
if (seek > smallestLength) // we've reached the end of the line
{
if (!foundIt)
{
2021-03-08 21:58:46 -05:00
if (p1[0] > p2[0]) // shortest string wins
2019-11-11 00:11:59 -05:00
greater = 1;
2021-03-08 21:58:46 -05:00
else if (p1[0] < p2[0])
2019-11-11 00:11:59 -05:00
greater = 2;
}
foundIt = true;
}
}
while (!foundIt);
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
return (greater);
}
//-------------------------------------------------------------- PasStringCopyNum
2020-10-14 18:18:57 -04:00
// This function copies a specified number of characters from one?
2019-11-11 00:11:59 -05:00
// Pascal string to another.
void PasStringCopyNum (StringPtr p1, StringPtr p2, short charsToCopy)
{
short i;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
if (charsToCopy > *p1) // if trying to copy more chars than there are
charsToCopy = *p1; // reduce the number of chars to copy to this size
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
*p2 = static_cast<unsigned char>(charsToCopy);
2020-10-14 18:18:57 -04:00
p2++;
p1++;
2019-11-11 00:11:59 -05:00
for (i = 0; i < charsToCopy; i++)
*p2++ = *p1++;
}
//-------------------------------------------------------------- PasStringConcat
2020-10-14 18:18:57 -04:00
// This function concatenates the second Pascal string to the end of?
2019-11-11 00:11:59 -05:00
// the first Pascal string.
void PasStringConcat (StringPtr p1, const PLPasStr &p2)
{
short wasLength, addedLength;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
wasLength = *p1;
if (wasLength > 255)
wasLength = 255;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
addedLength = p2.Length();
if ((wasLength + addedLength) > 255)
addedLength = 255 - wasLength;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
*p1 = wasLength + addedLength;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
p1 += (1 + wasLength);
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
memcpy(p1, p2.Chars(), addedLength);
}
//-------------------------------------------------------------- GetLineOfText
2020-10-14 18:18:57 -04:00
// This function walks through a source string and looks for an?
// entire line of text. A "line" of text is assumed to be bounded?
// by carriage returns. The index variable indicates which line?
2019-11-11 00:11:59 -05:00
// is sought.
void GetLineOfText (StringPtr srcStr, short index, StringPtr textLine)
{
short i, srcLength, count, start, stop;
Boolean foundIt;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
PasStringCopy(PSTR(""), textLine);
srcLength = srcStr[0];
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
if (index == 0) // walk through to "index"
start = 1;
else
{
start = 0;
count = 0;
i = 0;
foundIt = false;
do
{
i++;
2019-12-25 22:20:10 -05:00
if (srcStr[i] == '\r')
2019-11-11 00:11:59 -05:00
{
count++;
if (count == index)
{
start = i + 1;
foundIt = true;
}
}
}
while ((i < srcLength) && (!foundIt));
}
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
if (start != 0)
{
i = start;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
foundIt = false;
do
{
2019-12-25 22:20:10 -05:00
if (srcStr[i] == '\r')
2019-11-11 00:11:59 -05:00
{
stop = i;
foundIt = true;
}
i++;
}
while ((i < srcLength) && (!foundIt));
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
if (!foundIt)
{
if (start > srcLength)
{
start = srcLength;
stop = srcLength - 1;
}
else
stop = i;
}
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
count = 0;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
for (i = start; i <= stop; i++)
{
count++;
textLine[count] = srcStr[i];
}
textLine[0] = static_cast<unsigned char>(count);
}
}
//-------------------------------------------------------------- WrapText
2020-10-14 18:18:57 -04:00
// Given a string and the maximum number of characters to put on?
// one line, this function goes through and inserts carriage returns?
2019-11-11 00:11:59 -05:00
// in order to ensure that no line of text exceeds maxChars.
void WrapText (StringPtr theText, short maxChars)
{
short lastChar, count, chars, spaceIs;
Boolean foundEdge, foundSpace;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
lastChar = theText[0];
count = 0;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
do
{
chars = 0;
foundEdge = false;
foundSpace = false;
do
{
count++;
chars++;
2019-12-25 22:20:10 -05:00
if (theText[count] == '\r')
2019-11-11 00:11:59 -05:00
foundEdge = true;
2019-12-25 22:20:10 -05:00
else if (theText[count] == ' ')
2019-11-11 00:11:59 -05:00
{
foundSpace = true;
spaceIs = count;
}
}
while ((count < lastChar) && (chars < maxChars) && (!foundEdge));
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
if ((!foundEdge) && (count < lastChar) && (foundSpace))
{
2019-12-25 22:20:10 -05:00
theText[spaceIs] = '\r';
2019-11-11 00:11:59 -05:00
count = spaceIs + 1;
}
}
while (count < lastChar);
}
//-------------------------------------------------------------- GetFirstWordOfString
// Walks a string looking for a space (denoting first word of string).
void GetFirstWordOfString (StringPtr stringIn, StringPtr stringOut)
{
short isLong, spaceAt, i;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
isLong = stringIn[0];
spaceAt = isLong;
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
for (i = 1; i < isLong; i++)
{
if ((stringIn[i] == ' ') && (spaceAt == isLong))
spaceAt = i - 1;
}
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
if (spaceAt <= 0)
PasStringCopy(PSTR(""), stringOut);
else
PasStringCopyNum(stringIn, stringOut, spaceAt);
}
//-------------------------------------------------------------- CollapseStringToWidth
2020-10-14 18:18:57 -04:00
// Given a string and a maximum width (in pixels), this function?
// calculates how wide the text would be drawn with the current?
// font. If the text would exceed our width limit, characters?
// are dropped off the end of the string and "?" appended.
2019-11-11 00:11:59 -05:00
2020-05-21 05:01:16 -04:00
void CollapseStringToWidth (PortabilityLayer::RenderedFont *font, StringPtr theStr, short wide)
2019-11-11 00:11:59 -05:00
{
short dotsWide;
Boolean tooWide;
2020-10-14 18:18:57 -04:00
dotsWide = font->MeasurePStr(PSTR("\xc9"));
2020-05-21 05:01:16 -04:00
tooWide = font->MeasurePStr(theStr) > wide;
while (tooWide && theStr[0] > 0)
2019-11-11 00:11:59 -05:00
{
theStr[0]--;
2020-05-21 05:01:16 -04:00
tooWide = ((font->MeasurePStr(theStr) + dotsWide) > wide);
2019-11-11 00:11:59 -05:00
if (!tooWide)
2020-10-14 18:18:57 -04:00
PasStringConcat(theStr, PSTR("\xc9"));
2019-11-11 00:11:59 -05:00
}
}
//-------------------------------------------------------------- GetLocalizedString
StringPtr GetLocalizedString (short index, StringPtr theString)
{
#define kLocalizedStringsID 150
2020-10-14 18:18:57 -04:00
2019-11-11 00:11:59 -05:00
GetIndString(theString, kLocalizedStringsID, index);
return (theString);
}