diff options
Diffstat (limited to 'strlib.c')
-rw-r--r-- | strlib.c | 42 |
1 files changed, 21 insertions, 21 deletions
@@ -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.12 1995/02/06 19:37:51 roberto Exp $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.13 1995/10/09 12:49:21 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <string.h> | 8 | #include <string.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -14,27 +14,27 @@ char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $"; | |||
14 | #include "lualib.h" | 14 | #include "lualib.h" |
15 | 15 | ||
16 | 16 | ||
17 | static void str_error(char *funcname) | 17 | void lua_arg_error(char *funcname) |
18 | { | 18 | { |
19 | char buff[250]; | 19 | char buff[100]; |
20 | sprintf(buff, "incorrect arguments to function `%s'", funcname); | 20 | sprintf(buff, "incorrect arguments to function `%s'", funcname); |
21 | lua_error(buff); | 21 | lua_error(buff); |
22 | } | 22 | } |
23 | 23 | ||
24 | static char *check_and_get_string (int numArg, char *funcname) | 24 | char *lua_check_string (int numArg, char *funcname) |
25 | { | 25 | { |
26 | lua_Object o = lua_getparam(numArg); | 26 | lua_Object o = lua_getparam(numArg); |
27 | if (!(lua_isstring(o) || lua_isnumber(o))) | 27 | if (!(lua_isstring(o) || lua_isnumber(o))) |
28 | str_error(funcname); | 28 | lua_arg_error(funcname); |
29 | return lua_getstring(o); | 29 | return lua_getstring(o); |
30 | } | 30 | } |
31 | 31 | ||
32 | static int check_and_get_int (int numArg, char *funcname) | 32 | float 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 | str_error(funcname); | 36 | lua_arg_error(funcname); |
37 | return (int)lua_getnumber(o); | 37 | return lua_getnumber(o); |
38 | } | 38 | } |
39 | 39 | ||
40 | static char *newstring (char *s) | 40 | static char *newstring (char *s) |
@@ -54,17 +54,17 @@ static char *newstring (char *s) | |||
54 | */ | 54 | */ |
55 | static void str_find (void) | 55 | static void str_find (void) |
56 | { | 56 | { |
57 | char *s1 = check_and_get_string(1, "strfind"); | 57 | char *s1 = lua_check_string(1, "strfind"); |
58 | char *s2 = check_and_get_string(2, "strfind"); | 58 | char *s2 = lua_check_string(2, "strfind"); |
59 | int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 : | 59 | int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 : |
60 | check_and_get_int(3, "strfind")-1; | 60 | (int)lua_check_number(3, "strfind")-1; |
61 | char *f = strstr(s1+init,s2); | 61 | char *f = strstr(s1+init,s2); |
62 | if (f != NULL) | 62 | if (f != NULL) |
63 | { | 63 | { |
64 | int pos = f-s1+1; | 64 | int pos = f-s1+1; |
65 | if (lua_getparam (4) == LUA_NOOBJECT) | 65 | if (lua_getparam (4) == LUA_NOOBJECT) |
66 | lua_pushnumber (pos); | 66 | lua_pushnumber (pos); |
67 | else if (check_and_get_int(4, "strfind") >= pos+strlen(s2)-1) | 67 | else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1) |
68 | lua_pushnumber (pos); | 68 | lua_pushnumber (pos); |
69 | else | 69 | else |
70 | lua_pushnil(); | 70 | lua_pushnil(); |
@@ -80,7 +80,7 @@ static void str_find (void) | |||
80 | */ | 80 | */ |
81 | static void str_len (void) | 81 | static void str_len (void) |
82 | { | 82 | { |
83 | char *s = check_and_get_string(1, "strlen"); | 83 | char *s = lua_check_string(1, "strlen"); |
84 | lua_pushnumber(strlen(s)); | 84 | lua_pushnumber(strlen(s)); |
85 | } | 85 | } |
86 | 86 | ||
@@ -92,10 +92,10 @@ static void str_len (void) | |||
92 | */ | 92 | */ |
93 | static void str_sub (void) | 93 | static void str_sub (void) |
94 | { | 94 | { |
95 | char *s = check_and_get_string(1, "strsub"); | 95 | char *s = lua_check_string(1, "strsub"); |
96 | int start = check_and_get_int(2, "strsub"); | 96 | int start = (int)lua_check_number(2, "strsub"); |
97 | int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) : | 97 | int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) : |
98 | check_and_get_int(3, "strsub"); | 98 | (int)lua_check_number(3, "strsub"); |
99 | if (end < start || start < 1 || end > strlen(s)) | 99 | if (end < start || start < 1 || end > strlen(s)) |
100 | lua_pushliteral(""); | 100 | lua_pushliteral(""); |
101 | else | 101 | else |
@@ -114,7 +114,7 @@ typedef int (*strfunc)(int s); | |||
114 | static void str_apply (strfunc f, char *funcname) | 114 | static void str_apply (strfunc f, char *funcname) |
115 | { | 115 | { |
116 | char *s, *c; | 116 | char *s, *c; |
117 | c = s = newstring(check_and_get_string(1, funcname)); | 117 | c = s = newstring(lua_check_string(1, funcname)); |
118 | while (*c != 0) | 118 | while (*c != 0) |
119 | { | 119 | { |
120 | *c = f(*c); | 120 | *c = f(*c); |
@@ -150,12 +150,12 @@ static void str_upper (void) | |||
150 | */ | 150 | */ |
151 | static void str_ascii (void) | 151 | static void str_ascii (void) |
152 | { | 152 | { |
153 | char *s = check_and_get_string(1, "ascii"); | 153 | char *s = lua_check_string(1, "ascii"); |
154 | lua_Object o2 = lua_getparam(2); | 154 | lua_Object o2 = lua_getparam(2); |
155 | int pos; | 155 | int pos; |
156 | pos = (o2 == LUA_NOOBJECT) ? 0 : check_and_get_int(2, "ascii")-1; | 156 | pos = (o2 == LUA_NOOBJECT) ? 0 : (int)lua_check_number(2, "ascii")-1; |
157 | if (pos<0 || pos>=strlen(s)) | 157 | if (pos<0 || pos>=strlen(s)) |
158 | str_error("ascii"); | 158 | lua_arg_error("ascii"); |
159 | lua_pushnumber(s[pos]); | 159 | lua_pushnumber(s[pos]); |
160 | } | 160 | } |
161 | 161 | ||
@@ -171,7 +171,7 @@ static void str_int2str (void) | |||
171 | { | 171 | { |
172 | if (i > maxparams) | 172 | if (i > maxparams) |
173 | lua_error("too many parameters to function `int2str'"); | 173 | lua_error("too many parameters to function `int2str'"); |
174 | s[i-1] = check_and_get_int(i, "int2str"); | 174 | s[i-1] = (int)lua_check_number(i, "int2str"); |
175 | } | 175 | } |
176 | s[i-1] = 0; | 176 | s[i-1] = 0; |
177 | lua_pushstring(s); | 177 | lua_pushstring(s); |