示例#1
0
static int g_read (lua_State *L, FILE *f, int first) {
  int nargs = lua_gettop(L) - 1;
  int success;
  int n;
  if (nargs == 0) {  /* no arguments? */
    success = read_line(L, f);
    n = first+1;  /* to return 1 result */
  }
  else {  /* ensure stack space for all results and for auxlib's buffer */
    luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
    success = 1;
    for (n = first; nargs-- && success; n++) {
      if (lua_type(L, n) == LUA_TNUMBER) {
        size_t l = (size_t)lua_tonumber(L, n);
        success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
      }
      else if (lua_type(L, n) == LUA_TSTRING) {
        const char *p = lua_tostring(L, n);
        luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
        switch (p[1]) {
          case 'n':  /* number */
            success = read_number(L, f);
            break;
          case 'l':  /* line */
            success = read_line(L, f);
            break;
          case 'a':  /* file */
            read_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            return luaL_error(L, "obsolete option `*w' to 'read'");
          default:
            return luaL_argerror(L, n, "invalid format");
        }
      } else if (lua_type(L, n) == LUA_TWSTRING) {
        const lua_WChar *p = lua_towstring(L, n);
        if (!p || p[0] != '*')
          return luaL_error(L, "invalid `read' option");
        switch (p[1]) {
          case 'n':  /* number */
            success = uread_number(L, f);
            break;
          case 'l':  /* line */
            success = uread_line(L, f);
            break;
          case 'a':  /* file */
            uread_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            return luaL_error(L, "obsolete option `*w' to 'read'");
            break;
          default:
            return luaL_argerror(L, n, "invalid format");
        }
      }
    }
  }
  if (!success) {
    lua_pop(L, 1);  /* remove last result */
    lua_pushnil(L);  /* push nil instead */
  }
  return n - first;
}
示例#2
0
文件: luiolib.c 项目: gitrider/wxsj2
static int g_read (lua_State *L, FILE *f, int first) {
  int nargs = lua_gettop(L) - 1;
  int success;
  int n;
  if (nargs == 0) {  /* no arguments? */
    success = read_line(L, f);
    n = first+1;  /* to return 1 result */
  }
  else {  /* ensure stack space for all results and for auxlib's buffer */
    luaL_check_stack(L, nargs+LUA_MINSTACK, "too many arguments");
    success = 1;
    for (n = first; nargs-- && success; n++) {
      if (lua_type(L, n) == LUA_TNUMBER) {
        size_t l = (size_t)lua_tonumber(L, n);
        success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
      }
      else if (lua_type(L, n) == LUA_TSTRING) {
        const char *p = lua_tostring(L, n);
        if (!p || p[0] != '*')
          return luaL_verror(L, "invalid `read' option");
        switch (p[1]) {
          case 'n':  /* number */
            success = read_number(L, f);
            break;
          case 'l':  /* line */
            success = read_line(L, f);
            break;
          case 'a':  /* file */
            read_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            return luaL_verror(L, "obsolete option `*w'");
            break;
          default:
            return luaL_argerror(L, n, "invalid format");
        }
      }
      else if (lua_type(L, n) == LUA_TUSTRING) {
        const wchar_t *p = lua_toustring(L, n);
        if (!p || p[0] != '*')
          lua_error(L, "invalid `read' option");
        switch (p[1]) {
          case 'n':  /* number */
            success = uread_number(L, f);
            break;
          case 'l':  /* line */
            success = uread_until(L, f, L"\n", 1);  /* read until \n */
            break;
          case 'a':  /* file */
            uread_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */
            success = 1; /* always success */
            break;
          case 'w':  /* word */
            lua_error(L, "option `*w' is deprecated");
            break;
          case 'u': {  /* read until */
            size_t pl = lua_strlen(L, n) - 2;
            luaL_arg_check(L, 0 < pl && pl <= LUA_MAXUNTIL, n,
                              "invalid read-until length");
            success = uread_until(L, f, p+2, (int)pl);
            break;
          }
          default:
            luaL_argerror(L, n, "invalid format");
            success = 0;  /* to avoid warnings */
        }
      }
    }
  }
  if (!success) {
    lua_pop(L, 1);  /* remove last result */
    lua_pushnil(L);  /* push nil instead */
  }
  return n - first;
}