Code: Select all
char base[32];
Code: Select all
char base[32];
Code: Select all
void COM_FileBase (const char *in, char *out)
{
const char *start, *end;
int length;
if (!(end = strrchr(in, '.')))
{
end = in + strlen(in);
}
if (!(start = strrchr(in, '/')))
{
start = in;
}
else
{
start += 1;
}
length = end - start;
if (length < 0)
{
strlcpy (out, "?empty filename?", 0);
}
else if (length == 0)
{
out[0] = 0;
}
else
{
if (length > 31)
{
length = 31;
}
memcpy (out, start, length);
out[length] = 0;
}
}
I'd say just add an size_t outsize param to the function and then manipulate accordinglyreckless wrote:like this ?
[snip]
I use strlcpy, too, from OpenBSD. Here's what I'm using, which was adapted from quakeforge:reckless wrote:dont remember where i got the above function but its been in my realm engine for years now.
the size_t idea might come in handy in my own engine since this ones a bit bloated (works though)
but it needs a strlcpy implementation which does not exist normally on windows, so i used the one from darkplaces.
Code: Select all
/*
============
COM_FileBase
take 'somedir/otherdir/filename.ext',
write only 'filename' to the output
============
*/
void COM_FileBase (const char *in, char *out, size_t outsize)
{
const char *dot, *slash, *s;
s = in;
slash = in;
dot = NULL;
while (*s)
{
if (*s == '/')
slash = s + 1;
if (*s == '.')
dot = s;
s++;
}
if (dot == NULL)
dot = s;
if (dot - slash < 2)
q_strlcpy (out, "?model?", outsize);
else
{
size_t len = dot - slash;
if (len >= outsize)
len = outsize - 1;
memcpy (out, slash, len);
out[len] = '\0';
}
}
That's possibly because the unmodified quake version of the function has a nasty oversight.reckless wrote:[...] the old one would bitch like hell with my pk3 code hmm.