Rewrite aligned string drawing, using a modified version of strtok

This commit is contained in:
spherallic 2024-03-09 18:08:02 +01:00
parent 444cfc180f
commit 31e58f1156
3 changed files with 30 additions and 32 deletions

View file

@ -108,6 +108,7 @@ char *nongnu_strcasestr(const char *in, const char *what);
int startswith (const char *base, const char *tag); int startswith (const char *base, const char *tag);
int endswith (const char *base, const char *tag); int endswith (const char *base, const char *tag);
char *xstrtok(char *line, const char *delims);
#if defined (_WIN32) || defined (__HAIKU__) #if defined (_WIN32) || defined (__HAIKU__)
#define HAVE_DOSSTR_FUNCS #define HAVE_DOSSTR_FUNCS

View file

@ -68,3 +68,27 @@ int endswith(const char *base, const char *tag)
return !memcmp(&base[base_length - tag_length], tag, tag_length); return !memcmp(&base[base_length - tag_length], tag, tag_length);
} }
// strtok version that only skips over one delimiter at a time
char *xstrtok(char *line, const char *delims)
{
static char *saveline = NULL;
char *p;
if(line != NULL)
saveline = line;
// see if we have reached the end of the line
if(saveline == NULL || *saveline == '\0')
return NULL;
p = saveline; // save start of this token
saveline += strcspn(saveline, delims); // get the number of non-delims characters, go past delimiter
if(*saveline != '\0') // trash the delim if necessary
*saveline++ = '\0';
return p;
}

View file

@ -2112,41 +2112,12 @@ void V_DrawFontStringAtFixed(fixed_t x, fixed_t y, INT32 option, fixed_t pscale,
void V_DrawAlignedFontStringAtFixed(fixed_t x, fixed_t y, INT32 option, fixed_t pscale, fixed_t vscale, const char *string, fontdef_t font, enum string_align align) void V_DrawAlignedFontStringAtFixed(fixed_t x, fixed_t y, INT32 option, fixed_t pscale, fixed_t vscale, const char *string, fontdef_t font, enum string_align align)
{ {
char line[MAXLINELEN]; char *text = strdup(string);
size_t i, start = 0; char* line = xstrtok(text, "\n");
fixed_t lx = x, ly = y; fixed_t lx = x, ly = y;
while (*(string+start)) while (line)
{ {
for (i = 0; i < strlen(string+start); i++)
{
if (*(string+start+i) == '\n')
{
memset(line, 0, MAXLINELEN);
if (i >= MAXLINELEN)
{
CONS_Printf("V_DrawAlignedFontStringAtFixed: a line exceeds max. length %d (string: %s)\n", MAXLINELEN, string);
return;
}
strncpy(line,string + start, i);
line[i] = '\0';
start += i + 1;
i = (size_t)-1; //added : 07-02-98 : damned!
break;
}
}
if (i == strlen(string + start))
{
if (i >= MAXLINELEN)
{
CONS_Printf("V_DrawAlignedFontStringAtFixed: a line exceeds max. length %d (string: %s)\n", MAXLINELEN, string);
return;
}
strcpy(line, string + start);
start += i;
}
switch(align) switch(align)
{ {
case alignleft: case alignleft:
@ -2163,6 +2134,8 @@ void V_DrawAlignedFontStringAtFixed(fixed_t x, fixed_t y, INT32 option, fixed_t
V_DrawFontStringAtFixed(lx, ly, option, pscale, vscale, line, font); V_DrawFontStringAtFixed(lx, ly, option, pscale, vscale, line, font);
ly += FixedMul(((option & V_RETURN8) ? 8 : font.linespacing)<<FRACBITS, vscale); ly += FixedMul(((option & V_RETURN8) ? 8 : font.linespacing)<<FRACBITS, vscale);
line = xstrtok(NULL, "\n");
} }
} }