diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-09 17:00:23 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-09 17:00:23 -0200 |
| commit | 801722825d94b3bddd94d83887b081c328772147 (patch) | |
| tree | a76eedfe7e3e9105c0adcfbeb5d861167bc4dcce | |
| parent | 3abc25fa5424ebc857d445792dd0689926b7ea34 (diff) | |
| download | lua-801722825d94b3bddd94d83887b081c328772147.tar.gz lua-801722825d94b3bddd94d83887b081c328772147.tar.bz2 lua-801722825d94b3bddd94d83887b081c328772147.zip | |
"lua_check_number" accepts strings convertible to numbers.
| -rw-r--r-- | lualib.h | 4 | ||||
| -rw-r--r-- | strlib.c | 22 |
2 files changed, 17 insertions, 9 deletions
| @@ -2,7 +2,7 @@ | |||
| 2 | ** Libraries to be used in LUA programs | 2 | ** Libraries to be used in LUA programs |
| 3 | ** Grupo de Tecnologia em Computacao Grafica | 3 | ** Grupo de Tecnologia em Computacao Grafica |
| 4 | ** TeCGraf - PUC-Rio | 4 | ** TeCGraf - PUC-Rio |
| 5 | ** $Id: lualib.h,v 1.4 1995/11/10 17:54:31 roberto Exp roberto $ | 5 | ** $Id: lualib.h,v 1.5 1996/01/22 17:38:57 roberto Exp roberto $ |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | #ifndef lualib_h | 8 | #ifndef lualib_h |
| @@ -16,7 +16,7 @@ void mathlib_open (void); | |||
| 16 | /* auxiliar functions (private) */ | 16 | /* auxiliar functions (private) */ |
| 17 | void lua_arg_error(char *funcname); | 17 | void lua_arg_error(char *funcname); |
| 18 | char *lua_check_string (int numArg, char *funcname); | 18 | char *lua_check_string (int numArg, char *funcname); |
| 19 | float lua_check_number (int numArg, char *funcname); | 19 | double lua_check_number (int numArg, char *funcname); |
| 20 | char *luaI_addchar (int c); | 20 | char *luaI_addchar (int c); |
| 21 | 21 | ||
| 22 | #endif | 22 | #endif |
| @@ -3,7 +3,7 @@ | |||
| 3 | ** String library to LUA | 3 | ** String library to LUA |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_strlib="$Id: strlib.c,v 1.15 1996/01/22 17:38:57 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.16 1996/01/26 12:11:28 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <string.h> | 8 | #include <string.h> |
| 9 | #include <stdio.h> | 9 | #include <stdio.h> |
| @@ -29,12 +29,20 @@ char *lua_check_string (int numArg, char *funcname) | |||
| 29 | return lua_getstring(o); | 29 | return lua_getstring(o); |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | float lua_check_number (int numArg, char *funcname) | 32 | double lua_check_number (int numArg, char *funcname) |
| 33 | { | 33 | { |
| 34 | lua_Object o = lua_getparam(numArg); | 34 | lua_Object o = lua_getparam(numArg); |
| 35 | if (!lua_isnumber(o)) | 35 | if (lua_isnumber(o)) |
| 36 | lua_arg_error(funcname); | 36 | return lua_getnumber(o); |
| 37 | return lua_getnumber(o); | 37 | else if (lua_isstring(o)) |
| 38 | { | ||
| 39 | float t; | ||
| 40 | char c; | ||
| 41 | if (sscanf(lua_getstring(o), "%f %c",&t, &c) == 1) | ||
| 42 | return t; | ||
| 43 | } | ||
| 44 | lua_arg_error(funcname); | ||
| 45 | return 0; /* to avoid warnings */ | ||
| 38 | } | 46 | } |
| 39 | 47 | ||
| 40 | char *luaI_addchar (int c) | 48 | char *luaI_addchar (int c) |
| @@ -171,7 +179,7 @@ static void str_ascii (void) | |||
| 171 | #define MAX_CONVERTION 2000 | 179 | #define MAX_CONVERTION 2000 |
| 172 | #define MAX_FORMAT 50 | 180 | #define MAX_FORMAT 50 |
| 173 | 181 | ||
| 174 | static void io_format (void) | 182 | static void str_format (void) |
| 175 | { | 183 | { |
| 176 | int arg = 1; | 184 | int arg = 1; |
| 177 | char *strfrmt = lua_check_string(arg++, "format"); | 185 | char *strfrmt = lua_check_string(arg++, "format"); |
| @@ -244,5 +252,5 @@ void strlib_open (void) | |||
| 244 | lua_register ("strlower", str_lower); | 252 | lua_register ("strlower", str_lower); |
| 245 | lua_register ("strupper", str_upper); | 253 | lua_register ("strupper", str_upper); |
| 246 | lua_register ("ascii", str_ascii); | 254 | lua_register ("ascii", str_ascii); |
| 247 | lua_register ("format", io_format); | 255 | lua_register ("format", str_format); |
| 248 | } | 256 | } |
