1993-07-28 21:18:00 +08:00
|
|
|
/*
|
|
|
|
** strlib.c
|
|
|
|
** String library to LUA
|
|
|
|
*/
|
|
|
|
|
1996-02-13 02:34:44 +08:00
|
|
|
char *rcs_strlib="$Id: strlib.c,v 1.18 1996/02/12 18:32:40 roberto Exp $";
|
1993-12-18 02:41:19 +08:00
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
#include <string.h>
|
1995-10-09 20:49:21 +08:00
|
|
|
#include <stdio.h>
|
1995-02-03 04:05:37 +08:00
|
|
|
#include <stdlib.h>
|
1993-07-28 21:18:00 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "lua.h"
|
1994-08-17 23:10:04 +08:00
|
|
|
#include "lualib.h"
|
1993-07-28 21:18:00 +08:00
|
|
|
|
1995-02-07 03:37:51 +08:00
|
|
|
|
1995-11-11 01:54:31 +08:00
|
|
|
void lua_arg_error(char *funcname)
|
1995-10-09 20:49:21 +08:00
|
|
|
{
|
1995-11-11 01:54:31 +08:00
|
|
|
char buff[100];
|
1995-10-09 20:49:21 +08:00
|
|
|
sprintf(buff, "incorrect arguments to function `%s'", funcname);
|
|
|
|
lua_error(buff);
|
|
|
|
}
|
|
|
|
|
1995-11-11 01:54:31 +08:00
|
|
|
char *lua_check_string (int numArg, char *funcname)
|
1995-10-09 20:49:21 +08:00
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
|
|
|
if (!(lua_isstring(o) || lua_isnumber(o)))
|
1995-11-11 01:54:31 +08:00
|
|
|
lua_arg_error(funcname);
|
1995-10-09 20:49:21 +08:00
|
|
|
return lua_getstring(o);
|
|
|
|
}
|
|
|
|
|
1996-02-10 03:00:23 +08:00
|
|
|
double lua_check_number (int numArg, char *funcname)
|
1995-10-09 20:49:21 +08:00
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
1996-02-10 03:00:23 +08:00
|
|
|
if (lua_isnumber(o))
|
|
|
|
return lua_getnumber(o);
|
|
|
|
else if (lua_isstring(o))
|
|
|
|
{
|
|
|
|
float t;
|
|
|
|
char c;
|
|
|
|
if (sscanf(lua_getstring(o), "%f %c",&t, &c) == 1)
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
lua_arg_error(funcname);
|
|
|
|
return 0; /* to avoid warnings */
|
1995-10-09 20:49:21 +08:00
|
|
|
}
|
|
|
|
|
1996-01-23 01:38:57 +08:00
|
|
|
char *luaI_addchar (int c)
|
1995-02-07 03:37:51 +08:00
|
|
|
{
|
1996-01-23 01:38:57 +08:00
|
|
|
static char *buff = NULL;
|
|
|
|
static int max = 0;
|
|
|
|
static int n = 0;
|
|
|
|
if (n >= max)
|
|
|
|
{
|
|
|
|
if (max == 0)
|
|
|
|
{
|
|
|
|
max = 100;
|
|
|
|
buff = (char *)malloc(max);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max *= 2;
|
|
|
|
buff = (char *)realloc(buff, max);
|
|
|
|
}
|
|
|
|
if (buff == NULL)
|
|
|
|
lua_error("memory overflow");
|
|
|
|
}
|
|
|
|
buff[n++] = c;
|
|
|
|
if (c == 0)
|
|
|
|
n = 0; /* prepare for next string */
|
|
|
|
return buff;
|
1995-02-07 03:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
/*
|
|
|
|
** Return the position of the first caracter of a substring into a string
|
|
|
|
** LUA interface:
|
1995-01-07 04:31:10 +08:00
|
|
|
** n = strfind (string, substring, init, end)
|
1993-07-28 21:18:00 +08:00
|
|
|
*/
|
|
|
|
static void str_find (void)
|
|
|
|
{
|
1995-11-11 01:54:31 +08:00
|
|
|
char *s1 = lua_check_string(1, "strfind");
|
|
|
|
char *s2 = lua_check_string(2, "strfind");
|
1995-10-09 20:49:21 +08:00
|
|
|
int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 :
|
1995-11-11 01:54:31 +08:00
|
|
|
(int)lua_check_number(3, "strfind")-1;
|
1995-10-09 20:49:21 +08:00
|
|
|
char *f = strstr(s1+init,s2);
|
1993-12-18 02:41:19 +08:00
|
|
|
if (f != NULL)
|
1995-01-07 04:31:10 +08:00
|
|
|
{
|
|
|
|
int pos = f-s1+1;
|
1995-10-09 20:49:21 +08:00
|
|
|
if (lua_getparam (4) == LUA_NOOBJECT)
|
1995-01-07 04:31:10 +08:00
|
|
|
lua_pushnumber (pos);
|
1995-11-11 01:54:31 +08:00
|
|
|
else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1)
|
1995-01-07 04:31:10 +08:00
|
|
|
lua_pushnumber (pos);
|
|
|
|
else
|
|
|
|
lua_pushnil();
|
|
|
|
}
|
1993-12-18 02:41:19 +08:00
|
|
|
else
|
|
|
|
lua_pushnil();
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the string length
|
|
|
|
** LUA interface:
|
|
|
|
** n = strlen (string)
|
|
|
|
*/
|
|
|
|
static void str_len (void)
|
|
|
|
{
|
1995-11-11 01:54:31 +08:00
|
|
|
char *s = lua_check_string(1, "strlen");
|
1995-10-09 20:49:21 +08:00
|
|
|
lua_pushnumber(strlen(s));
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the substring of a string, from start to end
|
|
|
|
** LUA interface:
|
|
|
|
** substring = strsub (string, start, end)
|
|
|
|
*/
|
|
|
|
static void str_sub (void)
|
|
|
|
{
|
1995-11-11 01:54:31 +08:00
|
|
|
char *s = lua_check_string(1, "strsub");
|
|
|
|
int start = (int)lua_check_number(2, "strsub");
|
1995-10-09 20:49:21 +08:00
|
|
|
int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) :
|
1995-11-11 01:54:31 +08:00
|
|
|
(int)lua_check_number(3, "strsub");
|
1993-07-28 21:18:00 +08:00
|
|
|
if (end < start || start < 1 || end > strlen(s))
|
1994-12-13 23:54:21 +08:00
|
|
|
lua_pushliteral("");
|
1993-07-28 21:18:00 +08:00
|
|
|
else
|
|
|
|
{
|
1996-02-13 02:34:44 +08:00
|
|
|
luaI_addchar(0);
|
|
|
|
while (start <= end)
|
|
|
|
luaI_addchar(s[start++ - 1]);
|
|
|
|
lua_pushstring (luaI_addchar(0));
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-10-09 20:49:21 +08:00
|
|
|
/*
|
|
|
|
** Convert a string to lower case.
|
|
|
|
** LUA interface:
|
|
|
|
** lowercase = strlower (string)
|
|
|
|
*/
|
|
|
|
static void str_lower (void)
|
|
|
|
{
|
1996-01-23 01:38:57 +08:00
|
|
|
char *s = lua_check_string(1, "strlower");
|
|
|
|
luaI_addchar(0);
|
|
|
|
while (*s)
|
|
|
|
luaI_addchar(tolower(*s++));
|
|
|
|
lua_pushstring(luaI_addchar(0));
|
1995-10-09 20:49:21 +08:00
|
|
|
}
|
1993-07-28 21:18:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert a string to upper case.
|
|
|
|
** LUA interface:
|
|
|
|
** uppercase = strupper (string)
|
|
|
|
*/
|
|
|
|
static void str_upper (void)
|
|
|
|
{
|
1996-01-23 01:38:57 +08:00
|
|
|
char *s = lua_check_string(1, "strupper");
|
|
|
|
luaI_addchar(0);
|
|
|
|
while (*s)
|
|
|
|
luaI_addchar(toupper(*s++));
|
|
|
|
lua_pushstring(luaI_addchar(0));
|
1995-10-09 20:49:21 +08:00
|
|
|
}
|
1993-07-28 21:18:00 +08:00
|
|
|
|
1995-10-09 20:49:21 +08:00
|
|
|
/*
|
|
|
|
** get ascii value of a character in a string
|
|
|
|
*/
|
|
|
|
static void str_ascii (void)
|
|
|
|
{
|
1995-11-11 01:54:31 +08:00
|
|
|
char *s = lua_check_string(1, "ascii");
|
1995-10-09 20:49:21 +08:00
|
|
|
lua_Object o2 = lua_getparam(2);
|
|
|
|
int pos;
|
1995-11-11 01:54:31 +08:00
|
|
|
pos = (o2 == LUA_NOOBJECT) ? 0 : (int)lua_check_number(2, "ascii")-1;
|
1995-10-09 20:49:21 +08:00
|
|
|
if (pos<0 || pos>=strlen(s))
|
1995-11-11 01:54:31 +08:00
|
|
|
lua_arg_error("ascii");
|
1995-10-09 20:49:21 +08:00
|
|
|
lua_pushnumber(s[pos]);
|
|
|
|
}
|
|
|
|
|
1996-01-23 01:38:57 +08:00
|
|
|
|
|
|
|
#define MAX_CONVERTION 2000
|
|
|
|
#define MAX_FORMAT 50
|
|
|
|
|
1996-02-10 03:00:23 +08:00
|
|
|
static void str_format (void)
|
1996-01-23 01:38:57 +08:00
|
|
|
{
|
|
|
|
int arg = 1;
|
|
|
|
char *strfrmt = lua_check_string(arg++, "format");
|
|
|
|
luaI_addchar(0); /* initialize */
|
|
|
|
while (*strfrmt)
|
1995-10-09 20:49:21 +08:00
|
|
|
{
|
1996-01-23 01:38:57 +08:00
|
|
|
if (*strfrmt != '%')
|
|
|
|
luaI_addchar(*strfrmt++);
|
|
|
|
else if (*++strfrmt == '%')
|
|
|
|
luaI_addchar(*strfrmt++); /* %% */
|
|
|
|
else
|
|
|
|
{ /* format item */
|
|
|
|
char form[MAX_FORMAT]; /* store the format ('%...') */
|
|
|
|
char buff[MAX_CONVERTION]; /* store the formated value */
|
|
|
|
int size = 0;
|
|
|
|
int i = 0;
|
|
|
|
form[i++] = '%';
|
|
|
|
form[i] = *strfrmt++;
|
|
|
|
while (!isalpha(form[i]))
|
|
|
|
{
|
|
|
|
if (isdigit(form[i]))
|
|
|
|
{
|
|
|
|
size = size*10 + form[i]-'0';
|
|
|
|
if (size >= MAX_CONVERTION)
|
|
|
|
lua_error("format size/precision too long in function `format'");
|
|
|
|
}
|
|
|
|
else if (form[i] == '.')
|
|
|
|
size = 0; /* re-start */
|
|
|
|
if (++i >= MAX_FORMAT)
|
|
|
|
lua_error("bad format in function `format'");
|
|
|
|
form[i] = *strfrmt++;
|
|
|
|
}
|
|
|
|
form[i+1] = 0; /* ends string */
|
|
|
|
switch (form[i])
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
char *s = lua_check_string(arg++, "format");
|
|
|
|
if (strlen(s) >= MAX_CONVERTION)
|
|
|
|
lua_error("string argument too long in function `format'");
|
|
|
|
sprintf(buff, form, s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'c': case 'd': case 'i': case 'o':
|
|
|
|
case 'u': case 'x': case 'X':
|
|
|
|
sprintf(buff, form, (int)lua_check_number(arg++, "format"));
|
|
|
|
break;
|
|
|
|
case 'e': case 'E': case 'f': case 'g':
|
|
|
|
sprintf(buff, form, lua_check_number(arg++, "format"));
|
|
|
|
break;
|
|
|
|
default: /* also treat cases 'pnLlh' */
|
|
|
|
lua_error("invalid format option in function `format'");
|
|
|
|
}
|
|
|
|
for (i=0; buff[i]; i++) /* move formated value to result */
|
|
|
|
luaI_addchar(buff[i]);
|
|
|
|
}
|
1995-10-09 20:49:21 +08:00
|
|
|
}
|
1996-01-23 01:38:57 +08:00
|
|
|
lua_pushstring(luaI_addchar(0)); /* push the result */
|
1995-10-09 20:49:21 +08:00
|
|
|
}
|
1993-07-28 21:18:00 +08:00
|
|
|
|
1996-01-23 01:38:57 +08:00
|
|
|
|
1993-07-28 21:18:00 +08: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 20:49:21 +08:00
|
|
|
lua_register ("ascii", str_ascii);
|
1996-02-10 03:00:23 +08:00
|
|
|
lua_register ("format", str_format);
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|