1993-07-28 17:18:00 +04:00
|
|
|
/*
|
|
|
|
** strlib.c
|
|
|
|
** String library to LUA
|
|
|
|
*/
|
|
|
|
|
1995-10-09 15:49:21 +03:00
|
|
|
char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $";
|
1993-12-17 21:41:19 +03:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
#include <string.h>
|
1995-10-09 15:49:21 +03:00
|
|
|
#include <stdio.h>
|
1995-02-02 23:05:37 +03:00
|
|
|
#include <stdlib.h>
|
1993-07-28 17:18:00 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "lua.h"
|
1994-08-17 19:10:04 +04:00
|
|
|
#include "lualib.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1995-02-06 22:37:51 +03:00
|
|
|
|
1995-10-09 15:49:21 +03:00
|
|
|
static void str_error(char *funcname)
|
|
|
|
{
|
|
|
|
char buff[250];
|
|
|
|
sprintf(buff, "incorrect arguments to function `%s'", funcname);
|
|
|
|
lua_error(buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *check_and_get_string (int numArg, char *funcname)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
|
|
|
if (!(lua_isstring(o) || lua_isnumber(o)))
|
|
|
|
str_error(funcname);
|
|
|
|
return lua_getstring(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_and_get_int (int numArg, char *funcname)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
|
|
|
if (!lua_isnumber(o))
|
|
|
|
str_error(funcname);
|
|
|
|
return (int)lua_getnumber(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *newstring (char *s)
|
1995-02-06 22:37:51 +03:00
|
|
|
{
|
|
|
|
char *ns = (char *)malloc(strlen(s)+1);
|
|
|
|
if (ns == 0)
|
|
|
|
lua_error("not enough memory for new string");
|
|
|
|
strcpy(ns, s);
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
|
|
|
** Return the position of the first caracter of a substring into a string
|
|
|
|
** LUA interface:
|
1995-01-06 23:31:10 +03:00
|
|
|
** n = strfind (string, substring, init, end)
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
|
|
|
static void str_find (void)
|
|
|
|
{
|
1995-10-09 15:49:21 +03:00
|
|
|
char *s1 = check_and_get_string(1, "strfind");
|
|
|
|
char *s2 = check_and_get_string(2, "strfind");
|
|
|
|
int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 :
|
|
|
|
check_and_get_int(3, "strfind")-1;
|
|
|
|
char *f = strstr(s1+init,s2);
|
1993-12-17 21:41:19 +03:00
|
|
|
if (f != NULL)
|
1995-01-06 23:31:10 +03:00
|
|
|
{
|
|
|
|
int pos = f-s1+1;
|
1995-10-09 15:49:21 +03:00
|
|
|
if (lua_getparam (4) == LUA_NOOBJECT)
|
1995-01-06 23:31:10 +03:00
|
|
|
lua_pushnumber (pos);
|
1995-10-09 15:49:21 +03:00
|
|
|
else if (check_and_get_int(4, "strfind") >= pos+strlen(s2)-1)
|
1995-01-06 23:31:10 +03:00
|
|
|
lua_pushnumber (pos);
|
|
|
|
else
|
|
|
|
lua_pushnil();
|
|
|
|
}
|
1993-12-17 21:41:19 +03:00
|
|
|
else
|
|
|
|
lua_pushnil();
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the string length
|
|
|
|
** LUA interface:
|
|
|
|
** n = strlen (string)
|
|
|
|
*/
|
|
|
|
static void str_len (void)
|
|
|
|
{
|
1995-10-09 15:49:21 +03:00
|
|
|
char *s = check_and_get_string(1, "strlen");
|
|
|
|
lua_pushnumber(strlen(s));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the substring of a string, from start to end
|
|
|
|
** LUA interface:
|
|
|
|
** substring = strsub (string, start, end)
|
|
|
|
*/
|
|
|
|
static void str_sub (void)
|
|
|
|
{
|
1995-10-09 15:49:21 +03:00
|
|
|
char *s = check_and_get_string(1, "strsub");
|
|
|
|
int start = check_and_get_int(2, "strsub");
|
|
|
|
int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) :
|
|
|
|
check_and_get_int(3, "strsub");
|
1993-07-28 17:18:00 +04:00
|
|
|
if (end < start || start < 1 || end > strlen(s))
|
1994-12-13 18:54:21 +03:00
|
|
|
lua_pushliteral("");
|
1993-07-28 17:18:00 +04:00
|
|
|
else
|
|
|
|
{
|
1995-10-09 15:49:21 +03:00
|
|
|
char temp = s[end];
|
1993-07-28 17:18:00 +04:00
|
|
|
s[end] = 0;
|
|
|
|
lua_pushstring (&s[start-1]);
|
1995-10-09 15:49:21 +03:00
|
|
|
s[end] = temp;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1995-10-09 15:49:21 +03:00
|
|
|
** Convert a string according to given function.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1995-10-09 15:49:21 +03:00
|
|
|
typedef int (*strfunc)(int s);
|
|
|
|
static void str_apply (strfunc f, char *funcname)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
|
|
|
char *s, *c;
|
1995-10-09 15:49:21 +03:00
|
|
|
c = s = newstring(check_and_get_string(1, funcname));
|
1993-07-28 17:18:00 +04:00
|
|
|
while (*c != 0)
|
|
|
|
{
|
1995-10-09 15:49:21 +03:00
|
|
|
*c = f(*c);
|
1993-07-28 17:18:00 +04:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
lua_pushstring(s);
|
1995-02-02 23:05:37 +03:00
|
|
|
free(s);
|
1995-10-09 15:49:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert a string to lower case.
|
|
|
|
** LUA interface:
|
|
|
|
** lowercase = strlower (string)
|
|
|
|
*/
|
|
|
|
static void str_lower (void)
|
|
|
|
{
|
|
|
|
str_apply(tolower, "strlower");
|
|
|
|
}
|
1993-07-28 17:18:00 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert a string to upper case.
|
|
|
|
** LUA interface:
|
|
|
|
** uppercase = strupper (string)
|
|
|
|
*/
|
|
|
|
static void str_upper (void)
|
|
|
|
{
|
1995-10-09 15:49:21 +03:00
|
|
|
str_apply(toupper, "strupper");
|
|
|
|
}
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1995-10-09 15:49:21 +03:00
|
|
|
/*
|
|
|
|
** get ascii value of a character in a string
|
|
|
|
*/
|
|
|
|
static void str_ascii (void)
|
|
|
|
{
|
|
|
|
char *s = check_and_get_string(1, "ascii");
|
|
|
|
lua_Object o2 = lua_getparam(2);
|
|
|
|
int pos;
|
|
|
|
pos = (o2 == LUA_NOOBJECT) ? 0 : check_and_get_int(2, "ascii")-1;
|
|
|
|
if (pos<0 || pos>=strlen(s))
|
|
|
|
str_error("ascii");
|
|
|
|
lua_pushnumber(s[pos]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** converts one or more integers to chars in a string
|
|
|
|
*/
|
|
|
|
#define maxparams 50
|
|
|
|
static void str_int2str (void)
|
|
|
|
{
|
|
|
|
char s[maxparams+1];
|
|
|
|
int i = 0;
|
|
|
|
while (lua_getparam(++i) != LUA_NOOBJECT)
|
|
|
|
{
|
|
|
|
if (i > maxparams)
|
|
|
|
lua_error("too many parameters to function `int2str'");
|
|
|
|
s[i-1] = check_and_get_int(i, "int2str");
|
|
|
|
}
|
|
|
|
s[i-1] = 0;
|
|
|
|
lua_pushstring(s);
|
|
|
|
}
|
1993-07-28 17:18:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Open string library
|
|
|
|
*/
|
|
|
|
void strlib_open (void)
|
|
|
|
{
|
|
|
|
lua_register ("strfind", str_find);
|
|
|
|
lua_register ("strlen", str_len);
|
|
|
|
lua_register ("strsub", str_sub);
|
|
|
|
lua_register ("strlower", str_lower);
|
|
|
|
lua_register ("strupper", str_upper);
|
1995-10-09 15:49:21 +03:00
|
|
|
lua_register ("ascii", str_ascii);
|
|
|
|
lua_register ("int2str", str_int2str);
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|