lua-users home
lua-l archive

getF in lauxlib.c--why call feof?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I recently realized some C code I wrote does something very similar to
what is done by luaL_loadfile in src/lauxlib.c. I studied the code
and found, with no surprise, some really good ideas I hadn't thought
of. For example, my code just printed an error message and exited
when a read error occurs, but luaL_loadfile adjusts the Lua stack
so that these errors are accessed as others are.
While almost all of the code was clear to me, I am unable to explain
one line of code in getF. Why is it necessary to call feof before the
fread? Won't fread return 0 if feof returns true? It seems that
fread correctly handles the case of EOF.
static const char *getF (lua_State *L, void *ud, size_t *size) {
 LoadF *lf = (LoadF *)ud;
 (void)L;
 if (lf->extraline) {
 lf->extraline = 0;
 *size = 1;
 return "\n";
 }
 if (feof(lf->f)) return NULL; /* Why? */
 *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
 return (*size > 0) ? lf->buff : NULL;
}
John

AltStyle によって変換されたページ (->オリジナル) /