MCPcopy Create free account
hub / github.com/DFHack/dfhack / g_read

Function g_read

depends/lua/src/liolib.c:531–578  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

529
530
531static int g_read (lua_State *L, FILE *f, int first) {
532 int nargs = lua_gettop(L) - 1;
533 int success;
534 int n;
535 clearerr(f);
536 if (nargs == 0) { /* no arguments? */
537 success = read_line(L, f, 1);
538 n = first+1; /* to return 1 result */
539 }
540 else { /* ensure stack space for all results and for auxlib's buffer */
541 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
542 success = 1;
543 for (n = first; nargs-- && success; n++) {
544 if (lua_type(L, n) == LUA_TNUMBER) {
545 size_t l = (size_t)luaL_checkinteger(L, n);
546 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
547 }
548 else {
549 const char *p = luaL_checkstring(L, n);
550 if (*p == '*') p++; /* skip optional '*' (for compatibility) */
551 switch (*p) {
552 case 'n': /* number */
553 success = read_number(L, f);
554 break;
555 case 'l': /* line */
556 success = read_line(L, f, 1);
557 break;
558 case 'L': /* line with end-of-line */
559 success = read_line(L, f, 0);
560 break;
561 case 'a': /* file */
562 read_all(L, f); /* read entire file */
563 success = 1; /* always success */
564 break;
565 default:
566 return luaL_argerror(L, n, "invalid format");
567 }
568 }
569 }
570 }
571 if (ferror(f))
572 return luaL_fileresult(L, 0, NULL);
573 if (!success) {
574 lua_pop(L, 1); /* remove last result */
575 lua_pushnil(L); /* push nil instead */
576 }
577 return n - first;
578}
579
580
581static int io_read (lua_State *L) {

Callers 3

io_readFunction · 0.85
f_readFunction · 0.85
io_readlineFunction · 0.85

Calls 12

lua_gettopFunction · 0.85
read_lineFunction · 0.85
luaL_checkstackFunction · 0.85
lua_typeFunction · 0.85
luaL_checkintegerFunction · 0.85
test_eofFunction · 0.85
read_charsFunction · 0.85
read_numberFunction · 0.85
read_allFunction · 0.85
luaL_argerrorFunction · 0.85
luaL_fileresultFunction · 0.85
lua_pushnilFunction · 0.85

Tested by

no test coverage detected