I’ve needed to replace my use of fgets (to read a line of text from a file) with SDL functions. There doesn’t seem to be an equivalent function in SDL so I’ve modified the C code for the fgets function to make the following, and wondered if that would be a useful addition to SDL?
char* SDL_RWreadLine(char *szLine, int iMax, SDL_RWops *pFile)
{
int iReturn;
char ch;
char* p;
// get 'max' bytes or up to a newline
for (p = szLine, iMax--; iMax > 0; iMax--)
{
// read the next character
iReturn = SDL_RWread(pFile, &ch, sizeof(ch), 1);
// error, or reached the end of the file?
if (iReturn == 0
|| ch == 0)
{
return NULL;
}
// append this character
*p++ = ch;
// end of line?
if (ch == '\n')
{
break;
}
}
// null terminate
*p = 0;
// return
if (p == szLine)
{
return NULL;
}
return (p);
}