diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-14 12:57:43 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-14 12:57:43 -0300 |
| commit | 27f09415e38b00f16013649d3d91c430a6cc33ff (patch) | |
| tree | 37a57e6299ce49d59079f01860eae618a32dd49a | |
| parent | 4ad99706493dbf3e04e26897f07147c9b6321381 (diff) | |
| download | lua-27f09415e38b00f16013649d3d91c430a6cc33ff.tar.gz lua-27f09415e38b00f16013649d3d91c430a6cc33ff.tar.bz2 lua-27f09415e38b00f16013649d3d91c430a6cc33ff.zip | |
new read format "*i" for reading integers
| -rw-r--r-- | liolib.c | 18 |
1 files changed, 17 insertions, 1 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.111 2013/03/21 13:57:27 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.112 2013/04/11 18:34:06 roberto Exp roberto $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -347,6 +347,19 @@ static int io_lines (lua_State *L) { | |||
| 347 | */ | 347 | */ |
| 348 | 348 | ||
| 349 | 349 | ||
| 350 | static int read_integer (lua_State *L, FILE *f) { | ||
| 351 | lua_Integer d; | ||
| 352 | if (fscanf(f, LUA_INTEGER_SCAN, &d) == 1) { | ||
| 353 | lua_pushinteger(L, d); | ||
| 354 | return 1; | ||
| 355 | } | ||
| 356 | else { | ||
| 357 | lua_pushnil(L); /* "result" to be removed */ | ||
| 358 | return 0; /* read fails */ | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 350 | static int read_number (lua_State *L, FILE *f) { | 363 | static int read_number (lua_State *L, FILE *f) { |
| 351 | lua_Number d; | 364 | lua_Number d; |
| 352 | if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { | 365 | if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { |
| @@ -442,6 +455,9 @@ static int g_read (lua_State *L, FILE *f, int first) { | |||
| 442 | const char *p = lua_tostring(L, n); | 455 | const char *p = lua_tostring(L, n); |
| 443 | luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); | 456 | luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); |
| 444 | switch (p[1]) { | 457 | switch (p[1]) { |
| 458 | case 'i': /* integer */ | ||
| 459 | success = read_integer(L, f); | ||
| 460 | break; | ||
| 445 | case 'n': /* number */ | 461 | case 'n': /* number */ |
| 446 | success = read_number(L, f); | 462 | success = read_number(L, f); |
| 447 | break; | 463 | break; |
