(This is how it should work and how Nuncabola does it, I just didn't
catch it when Elviz mentioned it.)
git-svn-id: https://s.snth.net/svn/neverball/trunk@2934
78b8d119-cf0a-0410-b17c-
f493084dd1d7
void get_string(fs_file fin, char *s, int max)
{
- int c = -1;
+ int c;
- if (max == 0)
- return;
-
- while (max && c && (c = fs_getc(fin)) >= 0)
+ while ((c = fs_getc(fin)) >= 0)
{
- *s++ = c;
- max--;
- }
+ if (max > 0)
+ {
+ *s++ = c;
+ max--;
- /*
- * Terminate the buffer ourselves, if we ran out of space without
- * seeing a NUL character.
- */
+ /* Terminate the string, but keep reading until NUL. */
- if (max == 0 && *(s - 1) != 0)
- *(s - 1) = 0;
+ if (max == 0)
+ *(s - 1) = 0;
+ }
+
+ if (c == 0)
+ break;
+ }
}
/*---------------------------------------------------------------------------*/