diff options
author | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1994-09-20 12:11:11 -0300 |
---|---|---|
committer | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1994-09-20 12:11:11 -0300 |
commit | 98d95096762b363e3efa501996c896675059b6d1 (patch) | |
tree | bb22ef410d1d669eddf45455ba23038b2175ad64 | |
parent | 98263e2ef156c04c305708fa8792d80b3b76aceb (diff) | |
download | lua-98d95096762b363e3efa501996c896675059b6d1.tar.gz lua-98d95096762b363e3efa501996c896675059b6d1.tar.bz2 lua-98d95096762b363e3efa501996c896675059b6d1.zip |
sai strtod, entra sscanf. permite conversao de numeros com espacos em volta.
-rw-r--r-- | opcode.c | 33 |
1 files changed, 18 insertions, 15 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_opcode="$Id: opcode.c,v 2.5 1994/08/17 15:02:03 celes Exp celes $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 2.6 1994/09/08 16:51:49 celes Exp $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
@@ -90,20 +90,26 @@ static char *lua_strconc (char *l, char *r) | |||
90 | return strcat(strcpy(buffer,l),r); | 90 | return strcat(strcpy(buffer,l),r); |
91 | } | 91 | } |
92 | 92 | ||
93 | static int ToReal (char* s, float* f) | ||
94 | { | ||
95 | int n; | ||
96 | float t; | ||
97 | sscanf(s,"%f %n",&t,&n); | ||
98 | if (s[n]==0) { *f=t; return 1; } else return 0; | ||
99 | } | ||
100 | |||
93 | /* | 101 | /* |
94 | ** Convert, if possible, to a number tag. | 102 | ** Convert, if possible, to a number object. |
95 | ** Return 0 in success or not 0 on error. | 103 | ** Return 0 if success, not 0 if error. |
96 | */ | 104 | */ |
97 | static int lua_tonumber (Object *obj) | 105 | static int lua_tonumber (Object *obj) |
98 | { | 106 | { |
99 | char *ptr; | ||
100 | if (tag(obj) != T_STRING) | 107 | if (tag(obj) != T_STRING) |
101 | { | 108 | { |
102 | lua_reportbug ("unexpected type at conversion to number"); | 109 | lua_reportbug ("unexpected type at conversion to number"); |
103 | return 1; | 110 | return 1; |
104 | } | 111 | } |
105 | nvalue(obj) = strtod(svalue(obj), &ptr); | 112 | if (!ToReal(svalue(obj), &nvalue(obj))) |
106 | if (*ptr) | ||
107 | { | 113 | { |
108 | lua_reportbug ("string to number convertion failed"); | 114 | lua_reportbug ("string to number convertion failed"); |
109 | return 2; | 115 | return 2; |
@@ -113,7 +119,7 @@ static int lua_tonumber (Object *obj) | |||
113 | } | 119 | } |
114 | 120 | ||
115 | /* | 121 | /* |
116 | ** Test if is possible to convert an object to a number one. | 122 | ** Test if is possible to convert an object to a number object. |
117 | ** If possible, return the converted object, otherwise return nil object. | 123 | ** If possible, return the converted object, otherwise return nil object. |
118 | */ | 124 | */ |
119 | static Object *lua_convtonumber (Object *obj) | 125 | static Object *lua_convtonumber (Object *obj) |
@@ -126,14 +132,11 @@ static Object *lua_convtonumber (Object *obj) | |||
126 | return &cvt; | 132 | return &cvt; |
127 | } | 133 | } |
128 | 134 | ||
129 | tag(&cvt) = T_NIL; | 135 | if (tag(obj) == T_STRING && ToReal(svalue(obj), &nvalue(obj))) |
130 | if (tag(obj) == T_STRING) | 136 | tag(&cvt) = T_NUMBER; |
131 | { | 137 | else |
132 | char *ptr; | 138 | tag(&cvt) = T_NIL; |
133 | nvalue(&cvt) = strtod(svalue(obj), &ptr); | 139 | |
134 | if (*ptr == 0) | ||
135 | tag(&cvt) = T_NUMBER; | ||
136 | } | ||
137 | return &cvt; | 140 | return &cvt; |
138 | } | 141 | } |
139 | 142 | ||