diff options
Diffstat (limited to '')
| -rw-r--r-- | strlib.c | 159 |
1 files changed, 92 insertions, 67 deletions
| @@ -3,9 +3,10 @@ | |||
| 3 | ** String library to LUA | 3 | ** String library to LUA |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_strlib="$Id: strlib.c,v 1.11 1995/02/02 20:05:37 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $"; |
| 7 | 7 | ||
| 8 | #include <string.h> | 8 | #include <string.h> |
| 9 | #include <stdio.h> | ||
| 9 | #include <stdlib.h> | 10 | #include <stdlib.h> |
| 10 | #include <ctype.h> | 11 | #include <ctype.h> |
| 11 | 12 | ||
| @@ -13,9 +14,31 @@ char *rcs_strlib="$Id: strlib.c,v 1.11 1995/02/02 20:05:37 roberto Exp roberto $ | |||
| 13 | #include "lualib.h" | 14 | #include "lualib.h" |
| 14 | 15 | ||
| 15 | 16 | ||
| 16 | static char *newstring (lua_Object o) | 17 | static void str_error(char *funcname) |
| 18 | { | ||
| 19 | char buff[250]; | ||
| 20 | sprintf(buff, "incorrect arguments to function `%s'", funcname); | ||
| 21 | lua_error(buff); | ||
| 22 | } | ||
| 23 | |||
| 24 | static char *check_and_get_string (int numArg, char *funcname) | ||
| 25 | { | ||
| 26 | lua_Object o = lua_getparam(numArg); | ||
| 27 | if (!(lua_isstring(o) || lua_isnumber(o))) | ||
| 28 | str_error(funcname); | ||
| 29 | return lua_getstring(o); | ||
| 30 | } | ||
| 31 | |||
| 32 | static int check_and_get_int (int numArg, char *funcname) | ||
| 33 | { | ||
| 34 | lua_Object o = lua_getparam(numArg); | ||
| 35 | if (!lua_isnumber(o)) | ||
| 36 | str_error(funcname); | ||
| 37 | return (int)lua_getnumber(o); | ||
| 38 | } | ||
| 39 | |||
| 40 | static char *newstring (char *s) | ||
| 17 | { | 41 | { |
| 18 | char *s = lua_getstring(o); | ||
| 19 | char *ns = (char *)malloc(strlen(s)+1); | 42 | char *ns = (char *)malloc(strlen(s)+1); |
| 20 | if (ns == 0) | 43 | if (ns == 0) |
| 21 | lua_error("not enough memory for new string"); | 44 | lua_error("not enough memory for new string"); |
| @@ -31,34 +54,17 @@ static char *newstring (lua_Object o) | |||
| 31 | */ | 54 | */ |
| 32 | static void str_find (void) | 55 | static void str_find (void) |
| 33 | { | 56 | { |
| 34 | char *s1, *s2, *f; | 57 | char *s1 = check_and_get_string(1, "strfind"); |
| 35 | int init; | 58 | char *s2 = check_and_get_string(2, "strfind"); |
| 36 | lua_Object o1 = lua_getparam (1); | 59 | int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 : |
| 37 | lua_Object o2 = lua_getparam (2); | 60 | check_and_get_int(3, "strfind")-1; |
| 38 | lua_Object o3 = lua_getparam (3); | 61 | char *f = strstr(s1+init,s2); |
| 39 | lua_Object o4 = lua_getparam (4); | ||
| 40 | if (!lua_isstring(o1) || !lua_isstring(o2)) | ||
| 41 | lua_error ("incorrect arguments to function `strfind'"); | ||
| 42 | if (o3 == LUA_NOOBJECT) | ||
| 43 | init = 0; | ||
| 44 | else if (lua_isnumber(o3)) | ||
| 45 | init = lua_getnumber(o3)-1; | ||
| 46 | else | ||
| 47 | { | ||
| 48 | lua_error ("incorrect arguments to function `strfind'"); | ||
| 49 | return; /* to avoid warnings */ | ||
| 50 | } | ||
| 51 | s1 = lua_getstring(o1); | ||
| 52 | s2 = lua_getstring(o2); | ||
| 53 | f = strstr(s1+init,s2); | ||
| 54 | if (f != NULL) | 62 | if (f != NULL) |
| 55 | { | 63 | { |
| 56 | int pos = f-s1+1; | 64 | int pos = f-s1+1; |
| 57 | if (o4 == LUA_NOOBJECT) | 65 | if (lua_getparam (4) == LUA_NOOBJECT) |
| 58 | lua_pushnumber (pos); | 66 | lua_pushnumber (pos); |
| 59 | else if (!lua_isnumber(o4)) | 67 | else if (check_and_get_int(4, "strfind") >= pos+strlen(s2)-1) |
| 60 | lua_error ("incorrect arguments to function `strfind'"); | ||
| 61 | else if ((int)lua_getnumber(o4) >= pos+strlen(s2)-1) | ||
| 62 | lua_pushnumber (pos); | 68 | lua_pushnumber (pos); |
| 63 | else | 69 | else |
| 64 | lua_pushnil(); | 70 | lua_pushnil(); |
| @@ -74,10 +80,8 @@ static void str_find (void) | |||
| 74 | */ | 80 | */ |
| 75 | static void str_len (void) | 81 | static void str_len (void) |
| 76 | { | 82 | { |
| 77 | lua_Object o = lua_getparam (1); | 83 | char *s = check_and_get_string(1, "strlen"); |
| 78 | if (!lua_isstring(o)) | 84 | lua_pushnumber(strlen(s)); |
| 79 | lua_error ("incorrect arguments to function `strlen'"); | ||
| 80 | lua_pushnumber(strlen(lua_getstring(o))); | ||
| 81 | } | 85 | } |
| 82 | 86 | ||
| 83 | 87 | ||
| @@ -88,48 +92,47 @@ static void str_len (void) | |||
| 88 | */ | 92 | */ |
| 89 | static void str_sub (void) | 93 | static void str_sub (void) |
| 90 | { | 94 | { |
| 91 | int start, end; | 95 | char *s = check_and_get_string(1, "strsub"); |
| 92 | char *s; | 96 | int start = check_and_get_int(2, "strsub"); |
| 93 | lua_Object o1 = lua_getparam (1); | 97 | int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) : |
| 94 | lua_Object o2 = lua_getparam (2); | 98 | check_and_get_int(3, "strsub"); |
| 95 | lua_Object o3 = lua_getparam (3); | ||
| 96 | if (!lua_isstring(o1) || !lua_isnumber(o2)) | ||
| 97 | lua_error ("incorrect arguments to function `strsub'"); | ||
| 98 | if (o3 != LUA_NOOBJECT && !lua_isnumber(o3)) | ||
| 99 | lua_error ("incorrect third argument to function `strsub'"); | ||
| 100 | s = newstring(o1); | ||
| 101 | start = lua_getnumber (o2); | ||
| 102 | end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3); | ||
| 103 | if (end < start || start < 1 || end > strlen(s)) | 99 | if (end < start || start < 1 || end > strlen(s)) |
| 104 | lua_pushliteral(""); | 100 | lua_pushliteral(""); |
| 105 | else | 101 | else |
| 106 | { | 102 | { |
| 103 | char temp = s[end]; | ||
| 107 | s[end] = 0; | 104 | s[end] = 0; |
| 108 | lua_pushstring (&s[start-1]); | 105 | lua_pushstring (&s[start-1]); |
| 106 | s[end] = temp; | ||
| 109 | } | 107 | } |
| 110 | free(s); | ||
| 111 | } | 108 | } |
| 112 | 109 | ||
| 113 | /* | 110 | /* |
| 114 | ** Convert a string to lower case. | 111 | ** Convert a string according to given function. |
| 115 | ** LUA interface: | ||
| 116 | ** lowercase = strlower (string) | ||
| 117 | */ | 112 | */ |
| 118 | static void str_lower (void) | 113 | typedef int (*strfunc)(int s); |
| 114 | static void str_apply (strfunc f, char *funcname) | ||
| 119 | { | 115 | { |
| 120 | char *s, *c; | 116 | char *s, *c; |
| 121 | lua_Object o = lua_getparam (1); | 117 | c = s = newstring(check_and_get_string(1, funcname)); |
| 122 | if (!lua_isstring(o)) | ||
| 123 | lua_error ("incorrect arguments to function `strlower'"); | ||
| 124 | c = s = newstring(o); | ||
| 125 | while (*c != 0) | 118 | while (*c != 0) |
| 126 | { | 119 | { |
| 127 | *c = tolower(*c); | 120 | *c = f(*c); |
| 128 | c++; | 121 | c++; |
| 129 | } | 122 | } |
| 130 | lua_pushstring(s); | 123 | lua_pushstring(s); |
| 131 | free(s); | 124 | free(s); |
| 132 | } | 125 | } |
| 126 | |||
| 127 | /* | ||
| 128 | ** Convert a string to lower case. | ||
| 129 | ** LUA interface: | ||
| 130 | ** lowercase = strlower (string) | ||
| 131 | */ | ||
| 132 | static void str_lower (void) | ||
| 133 | { | ||
| 134 | str_apply(tolower, "strlower"); | ||
| 135 | } | ||
| 133 | 136 | ||
| 134 | 137 | ||
| 135 | /* | 138 | /* |
| @@ -139,20 +142,40 @@ static void str_lower (void) | |||
| 139 | */ | 142 | */ |
| 140 | static void str_upper (void) | 143 | static void str_upper (void) |
| 141 | { | 144 | { |
| 142 | char *s, *c; | 145 | str_apply(toupper, "strupper"); |
| 143 | lua_Object o = lua_getparam (1); | 146 | } |
| 144 | if (!lua_isstring(o)) | ||
| 145 | lua_error ("incorrect arguments to function `strlower'"); | ||
| 146 | c = s = newstring(o); | ||
| 147 | while (*c != 0) | ||
| 148 | { | ||
| 149 | *c = toupper(*c); | ||
| 150 | c++; | ||
| 151 | } | ||
| 152 | lua_pushstring(s); | ||
| 153 | free(s); | ||
| 154 | } | ||
| 155 | 147 | ||
| 148 | /* | ||
| 149 | ** get ascii value of a character in a string | ||
| 150 | */ | ||
| 151 | static void str_ascii (void) | ||
| 152 | { | ||
| 153 | char *s = check_and_get_string(1, "ascii"); | ||
| 154 | lua_Object o2 = lua_getparam(2); | ||
| 155 | int pos; | ||
| 156 | pos = (o2 == LUA_NOOBJECT) ? 0 : check_and_get_int(2, "ascii")-1; | ||
| 157 | if (pos<0 || pos>=strlen(s)) | ||
| 158 | str_error("ascii"); | ||
| 159 | lua_pushnumber(s[pos]); | ||
| 160 | } | ||
| 161 | |||
| 162 | /* | ||
| 163 | ** converts one or more integers to chars in a string | ||
| 164 | */ | ||
| 165 | #define maxparams 50 | ||
| 166 | static void str_int2str (void) | ||
| 167 | { | ||
| 168 | char s[maxparams+1]; | ||
| 169 | int i = 0; | ||
| 170 | while (lua_getparam(++i) != LUA_NOOBJECT) | ||
| 171 | { | ||
| 172 | if (i > maxparams) | ||
| 173 | lua_error("too many parameters to function `int2str'"); | ||
| 174 | s[i-1] = check_and_get_int(i, "int2str"); | ||
| 175 | } | ||
| 176 | s[i-1] = 0; | ||
| 177 | lua_pushstring(s); | ||
| 178 | } | ||
| 156 | 179 | ||
| 157 | /* | 180 | /* |
| 158 | ** Open string library | 181 | ** Open string library |
| @@ -164,4 +187,6 @@ void strlib_open (void) | |||
| 164 | lua_register ("strsub", str_sub); | 187 | lua_register ("strsub", str_sub); |
| 165 | lua_register ("strlower", str_lower); | 188 | lua_register ("strlower", str_lower); |
| 166 | lua_register ("strupper", str_upper); | 189 | lua_register ("strupper", str_upper); |
| 190 | lua_register ("ascii", str_ascii); | ||
| 191 | lua_register ("int2str", str_int2str); | ||
| 167 | } | 192 | } |
