Ejemplo n.º 1
0
static int g_read (lua_State *L, FILE *f, int first) {
  int nargs = lua_gettop(L) - 1;
  int success;
  int n;
  clearerr(f);
  if (nargs == 0) {  /* no arguments? */
    success = read_line(L, f, 1);
    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_tointeger(L, n);
        success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
      }
      else {
        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, 1);
            break;
          case 'L':  /* line with end-of-line */
            success = read_line(L, f, 0);
            break;
          case 'a':  /* file */
            read_all(L, f);  /* read entire file */
            success = 1; /* always success */
            break;
          default:
            return luaL_argerror(L, n, "invalid format");
        }
      }
    }
  }
  if (ferror(f))
    return luaL_fileresult(L, 0, NULL);
  if (!success) {
    lua_pop(L, 1);  /* remove last result */
    lua_pushnil(L);  /* push nil instead */
  }
  return n - first;
}
Ejemplo n.º 2
0
static int
memfile_read (lua_State *L) {
    MemoryFile *f = luaL_checkudata(L, 1, MEMORYFILE_MT_NAME);
    int nargs = lua_gettop(L) - 1;
    int success;
    int n;

    if (nargs == 0) {   /* no arguments? */
        success = read_line(f, L);
        n = 3;          /* 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 = 2; nargs-- && success; n++) {
            if (lua_type(L, n) == LUA_TNUMBER) {
                size_t l = (size_t) lua_tointeger(L, n);
                success = (l == 0) ? test_eof(f, L) : read_chars(f, L, l);
            }
            else {
                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(f, L);
                        break;
                    case 'l':  /* line */
                        success = read_line(f, L);
                        break;
                    case 'a':  /* all the rest of the file */
                        read_chars(f, L, ~((size_t)0)); /* MAX_SIZE_T bytes */
                        success = 1; /* always success */
                        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 - 2;
}
Ejemplo n.º 3
0
static int g_read (lua_State *L, FILE *f, int first) {
  int nargs = lua_gettop(L) - 1;
  int success;
  int n;
  int i;
  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 {
        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");
        }
      }
    }
  }
  if (!success) {
    lua_pop(L, 1);  /* remove last result */
    lua_pushnil(L);  /* push nil instead */
  }
  for (i=n-first; i > 0; i--)
    lua_settaint(L, -i, 1);
  return n - first;
}
Ejemplo n.º 4
0
static int icu_ufile_read(lua_State *L) {
	int nargs = lua_gettop(L) - 1;
	int success;
	int n;
	UFILE* ufile = icu4lua_checkufile(L,1,UFILE_UV_META);
	if (nargs == 0) { // no arguments?
		success = read_line(L, ufile);
		n = 3; // to return 1 result
	}
	else {
		luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
		success = 1;
		for (n = 2; nargs-- && success; n++) {
			if (lua_type(L,n) == LUA_TNUMBER) {
				int32_t l = (int32_t)lua_tointeger(L, n);
				success = (l == 0) ? test_eof(L, ufile) : read_chars(L, ufile, l);
			}
			else {
				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, ufile);
						break;
					case 'l': // line
						success = read_line(L, ufile);
						break;
					case 'a': // file
						read_chars(L, ufile, INT32_MAX);
						success = 1;
						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 - 2;
}
Ejemplo n.º 5
0
char * returnLine(FILE *fp){
  char c;
  char *line;
  int size = 0;
  line = NULL;
  line = realloc(line, sizeof(char));
  c = fgetc(fp);
  if (test_eof(c)) {
    return NULL;
  } else {
    while (!test_eol(c)) {
      *(line + size) = c;
      size++;
      line = realloc(line, sizeof(char) * (size + 1));
      c = fgetc(fp);
    }
  }
  *(line + size) = '\0';
  return line;
}
Ejemplo n.º 6
0
	bool File::eof()
	{
		if(file == 0 || test_eof(this, file))
			return true;
		return false;
	}
Ejemplo n.º 7
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_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;
}