Browse Source

new read format "*i" for reading integers

Roberto Ierusalimschy 12 years ago
parent
commit
27f09415e3
1 changed files with 17 additions and 1 deletions
  1. 17 1
      liolib.c

+ 17 - 1
liolib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: liolib.c,v 2.111 2013/03/21 13:57:27 roberto Exp roberto $
+** $Id: liolib.c,v 2.112 2013/04/11 18:34:06 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -347,6 +347,19 @@ static int io_lines (lua_State *L) {
 */
 */
 
 
 
 
+static int read_integer (lua_State *L, FILE *f) {
+  lua_Integer d;
+  if (fscanf(f, LUA_INTEGER_SCAN, &d) == 1) {
+    lua_pushinteger(L, d);
+    return 1;
+  }
+  else {
+   lua_pushnil(L);  /* "result" to be removed */
+   return 0;  /* read fails */
+  }
+}
+
+
 static int read_number (lua_State *L, FILE *f) {
 static int read_number (lua_State *L, FILE *f) {
   lua_Number d;
   lua_Number d;
   if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
   if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
@@ -442,6 +455,9 @@ static int g_read (lua_State *L, FILE *f, int first) {
         const char *p = lua_tostring(L, n);
         const char *p = lua_tostring(L, n);
         luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
         luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
         switch (p[1]) {
         switch (p[1]) {
+          case 'i':  /* integer */
+            success = read_integer(L, f);
+            break;
           case 'n':  /* number */
           case 'n':  /* number */
             success = read_number(L, f);
             success = read_number(L, f);
             break;
             break;