private static void io_write() { lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); if (o1 == null) /* new line */ { fprintf(@out, "\n"); lua_pushnumber(1); } else if (o2 == null) /* free format */ { int status = 0; if (lua_isnumber(o1) != 0) { status = fprintf(@out, "%g", lua_getnumber(o1)); } else if (lua_isstring(o1) != 0) { status = fprintf(@out, "%s", lua_getstring(o1)); } lua_pushnumber(status); } else /* formated */ { if (lua_isstring(o2) == 0) { lua_error("incorrect format to function `write'"); lua_pushnumber(0); return; } lua_pushnumber(fprintf(@out, "%s", buildformat(lua_getstring(o2), o1))); } }
/* ** Internal function: convert an object to a number */ public static void lua_obj2number() { lua_Object o = lua_getparam(1); if (0 != lua_isnumber(o)) { lua_pushobject(o); } else if (0 != lua_isstring(o)) { char c = (char)0; float f = 0; if (sscanf(lua_getstring(o), "%f %c", f, c) == 1) { lua_pushnumber(f); } else { lua_pushnil(); } } else { lua_pushnil(); } }
public static void lua_next() { Hash t; lua_Object o = lua_getparam(1); lua_Object r = lua_getparam(2); if (o == LUA_NOOBJECT || r == LUA_NOOBJECT) { lua_error("too few arguments to function `next'"); } if (lua_getparam(3) != LUA_NOOBJECT) { lua_error("too many arguments to function `next'"); } if (0 == lua_istable(o)) { lua_error("first argument of function `next' is not a table"); } t = avalue(luaI_Address(o)); if (0 != lua_isnil(r)) { hashnext(t, 0); } else { Word h = present(t, luaI_Address(r)); hashnext(t, (Word)(h + 1)); } }
/* ** Return the substring of a string, from start to end ** LUA interface: ** substring = strsub (string, start, end) */ private static void str_sub() { int start, end; CharPtr s; lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); lua_Object o3 = lua_getparam(3); if (lua_isstring(o1) == 0 || lua_isnumber(o2) == 0 || lua_isnumber(o3) == 0) { lua_error("incorrect arguments to function `strsub'"); return; } s = strdup(lua_getstring(o1)); start = (int)lua_getnumber(o2); end = (int)lua_getnumber(o3); if (end < start || start < 1 || end > strlen(s)) { lua_pushstring(""); } else { s[end] = '\0'; //FIXME:s = s.Substring(0, end); lua_pushstring(new CharPtr(s, start - 1)); //s.Substring(start - 1)); } free(s); }
/* ** Return the substring of a string, from start to end ** LUA interface: ** substring = strsub (string, start, end) */ private static void str_sub() { int start, end; CharPtr s; lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); lua_Object o3 = lua_getparam(3); if (0 == lua_isstring(o1) || 0 == lua_isnumber(o2)) { lua_error("incorrect arguments to function `strsub'"); } if (o3 != LUA_NOOBJECT && 0 == lua_isnumber(o3)) { lua_error("incorrect third argument to function `strsub'"); } s = newstring(o1); start = (int)lua_getnumber(o2); end = o3 == LUA_NOOBJECT ? (int)strlen(s) : (int)lua_getnumber(o3); if (end < start || start < 1 || end > strlen(s)) { lua_pushliteral(""); } else { s[end] = '\0'; lua_pushstring(s.add(start - 1)); } free(s); }
/* ** Return the substring of a string, from start to end ** LUA interface: ** substring = strsub (string, start, end) */ private static void str_sub() { int start, end; CharPtr s; lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); lua_Object o3 = lua_getparam(3); if (lua_isstring(o1) == 0 || lua_isnumber(o2) == 0) { lua_error("incorrect arguments to function `strsub'"); return; } if (o3 != null && lua_isnumber(o3) == 0) { lua_error("incorrect third argument to function `strsub'"); return; } s = lua_copystring(o1); start = (int)lua_getnumber(o2); end = o3 == null ? (int)strlen(s) : (int)lua_getnumber(o3); if (end < start || start < 1 || end > strlen(s)) { lua_pushstring(""); } else { s[end] = '\0'; lua_pushstring(s.add(start - 1)); } free(s); }
private static void execstr() { lua_Object obj = lua_getparam(1); if (lua_isstring(obj) != 0) { lua_dostring(lua_getstring(obj)); } }
private static void callfunc() { lua_Object obj = lua_getparam(1); if (lua_isstring(obj) != 0) { lua_call(lua_getstring(obj), 0); } }
/* ** Return the string length ** LUA interface: ** n = strlen (string) */ private static void str_len() { lua_Object o = lua_getparam(1); if (lua_isstring(o) == 0) { lua_error("incorrect arguments to function `strlen'"); return; } lua_pushnumber(strlen(lua_getstring(o))); }
/* ** Return the position of the first caracter of a substring into a string ** LUA interface: ** n = strfind (string, substring, init, end) */ private static void str_find() { CharPtr s1, s2, f; int init; lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); lua_Object o3 = lua_getparam(3); lua_Object o4 = lua_getparam(4); if (0 == lua_isstring(o1) || 0 == lua_isstring(o2)) { lua_error("incorrect arguments to function `strfind'"); } if (o3 == LUA_NOOBJECT) { init = 0; } else if (0 != lua_isnumber(o3)) { init = (int)(lua_getnumber(o3) - 1); } else { lua_error("incorrect arguments to function `strfind'"); return; /* to avoid warnings */ } s1 = lua_getstring(o1); s2 = lua_getstring(o2); f = strstr(s1 + init, s2); if (f != null) { int pos = f - s1 + 1; if (o4 == LUA_NOOBJECT) { lua_pushnumber(pos); } else if (0 == lua_isnumber(o4)) { lua_error("incorrect arguments to function `strfind'"); } else if ((int)lua_getnumber(o4) >= pos + strlen(s2) - 1) { lua_pushnumber(pos); } else { lua_pushnil(); } } else { lua_pushnil(); } }
//char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $"; //#include <string.h> //#include <stdlib.h> //#include <ctype.h> //#include "lua.h" //#include "lualib.h" private static CharPtr newstring(lua_Object o) { CharPtr s = lua_getstring(o); CharPtr ns = malloc(strlen(s) + 1); if (ns == null) { lua_error("not enough memory for new string"); } strcpy(ns, s); return(ns); }
/* ** Internal function: do a string */ public static void lua_internaldostring() { lua_Object obj = lua_getparam(1); if (0 != lua_isstring(obj) && 0 == lua_dostring(lua_getstring(obj))) { lua_pushnumber(1); } else { lua_pushnil(); } }
private static void math_mod() { int d1, d2; lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); if (0 == lua_isnumber(o1) || 0 == lua_isnumber(o2)) { lua_error("incorrect arguments to function `mod'"); return; } d1 = (int)lua_getnumber(o1); d2 = (int)lua_getnumber(o2); lua_pushnumber(d1 % d2); }
/* ** Open a file to write appended. ** LUA interface: ** status = appendto (filename) ** where: ** status = 2 -> success (already exist) ** status = 1 -> success (new file) ** status = 0 -> error */ private static void io_appendto() { lua_Object o = lua_getparam(1); if (o == null) /* restore standart output */ { if (@out != stdout) { fclose(@out); @out = stdout; } lua_pushnumber(1); } else { if (0 == lua_isstring(o)) { lua_error("incorrect argument to function 'appendto`"); lua_pushnumber(0); } else { int r; FILE fp; stat_struct st = new stat_struct(); if (stat(lua_getstring(o), st) == -1) { r = 1; } else { r = 2; } fp = fopen(lua_getstring(o), "a"); if (fp == null) { lua_pushnumber(0); } else { if (@out != stdout) { fclose(@out); } @out = fp; lua_pushnumber(r); } } } }
private static void math_pow() { double d1, d2; lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); if (lua_isnumber(o1) == 0 || lua_isnumber(o2) == 0) { lua_error("incorrect arguments to function `pow'"); return; } d1 = lua_getnumber(o1); d2 = lua_getnumber(o2); lua_pushnumber((real)pow(d1, d2)); }
private static void math_tan() { double d; lua_Object o = lua_getparam(1); if (o == null) { lua_error("too few arguments to function `tan'"); return; } if (0 == lua_isnumber(o)) { lua_error("incorrect arguments to function `tan'"); return; } d = lua_getnumber(o); lua_pushnumber((real)tan(TORAD(d))); }
private static void math_cos() { double d; lua_Object o = lua_getparam(1); if (o == LUA_NOOBJECT) { lua_error("too few arguments to function `cos'"); } if (0 == lua_isnumber(o)) { lua_error("incorrect arguments to function `cos'"); } d = lua_getnumber(o); lua_pushnumber((real)cos(TORAD(d))); }
internal static void math_sqrt() { double d; lua_Object o = lua_getparam(1); if (o == null) { lua_error("too few arguments to function `sqrt'"); return; } if (0 == lua_isnumber(o)) { lua_error("incorrect arguments to function `sqrt'"); return; } d = lua_getnumber(o); lua_pushnumber((real)sqrt(d)); }
private static void math_rad() { float d; lua_Object o = lua_getparam(1); if (o == LUA_NOOBJECT) { lua_error("too few arguments to function `rad'"); } if (0 == lua_isnumber(o)) { lua_error("incorrect arguments to function `rad'"); } d = lua_getnumber(o); lua_pushnumber((real)(d / 180.0 * PI)); }
/* ** Return the position of the first caracter of a substring into a string ** LUA interface: ** n = strfind (string, substring) */ private static void str_find() { int n; CharPtr s1, s2; lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); if (lua_isstring(o1) == 0 || lua_isstring(o2) == 0) { lua_error("incorrect arguments to function `strfind'"); return; } s1 = lua_getstring(o1); s2 = lua_getstring(o2); n = strstr(s1, s2) - s1 + 1; lua_pushnumber(n); }
/* ** Execute a executable program using "sustem". ** On error put 0 on stack, otherwise put 1. */ public static void io_execute() { lua_Object o = lua_getparam(1); if (o == null || lua_isstring(o) == 0) { lua_error("incorrect argument to function 'execute`"); lua_pushnumber(0); } else { system(lua_getstring(o)); lua_pushnumber(1); } return; }
/* ** Convert a string to lower case. ** LUA interface: ** lowercase = strlower (string) */ private static void str_lower() { CharPtr s, c; lua_Object o = lua_getparam(1); if (lua_isstring(o) == 0) { lua_error("incorrect arguments to function `strlower'"); return; } s = lua_getstring(o); c = new CharPtr(s); while (c[0] != '\0') { c[0] = tolower(c[0]); c.inc(); } lua_pushstring(s); free(s); }
private static void math_abs() { double d; lua_Object o = lua_getparam(1); if (o == null) { lua_error("too few arguments to function `abs'"); return; } if (0 == lua_isnumber(o)) { lua_error("incorrect arguments to function `abs'"); return; } d = lua_getnumber(o); if (d < 0) { d = -d; } lua_pushnumber((real)d); }
//char *rcs_strlib="$Id: strlib.c,v 1.2 1994/03/28 15:14:02 celes Exp $"; //#include <stdlib.h> //#include <string.h> //#include <ctype.h> //#include "mm.h" //#include "lua.h" /* ** Return the position of the first caracter of a substring into a string ** LUA interface: ** n = strfind (string, substring) */ private static void str_find() { CharPtr s1, s2, f; lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); if (lua_isstring(o1) == 0 || lua_isstring(o2) == 0) { lua_error("incorrect arguments to function `strfind'"); return; } s1 = lua_getstring(o1); s2 = lua_getstring(o2); f = strstr(s1, s2); if (f != null) { lua_pushnumber(f - s1 + 1); } else { lua_pushnil(); } }
/* ** Open a file to read. ** LUA interface: ** status = readfrom (filename) ** where: ** status = 1 -> success ** status = 0 -> error */ private static void io_readfrom() { lua_Object o = lua_getparam(1); if (o == null) /* restore standart input */ { if (@in != stdin) { fclose(@in); @in = stdin; } lua_pushnumber(1); } else { if (lua_isstring(o) == 0) { lua_error("incorrect argument to function 'readfrom`"); lua_pushnumber(0); } else { FILE fp = fopen(lua_getstring(o), "r"); if (fp == null) { lua_pushnumber(0); } else { if (@in != stdin) { fclose(@in); } @in = fp; lua_pushnumber(1); } } } }
/* ** Internal function: return an object type. */ public static void luaI_type() { lua_Object o = lua_getparam(1); if (o == LUA_NOOBJECT) { lua_error("no parameter to function 'type'"); } switch (lua_type(o)) { case (int)lua_Type.LUA_T_NIL: lua_pushliteral("nil"); break; case (int)lua_Type.LUA_T_NUMBER: lua_pushliteral("number"); break; case (int)lua_Type.LUA_T_STRING: lua_pushliteral("string"); break; case (int)lua_Type.LUA_T_ARRAY: lua_pushliteral("table"); break; case (int)lua_Type.LUA_T_FUNCTION: lua_pushliteral("function"); break; case (int)lua_Type.LUA_T_CFUNCTION: lua_pushliteral("cfunction"); break; default: lua_pushliteral("userdata"); break; } }
/* ** Open a file to write. ** LUA interface: ** status = writeto (filename) ** where: ** status = 1 -> success ** status = 0 -> error */ private static void io_writeto() { lua_Object o = lua_getparam(1); if (o == null) /* restore standart output */ { if (@out != stdout) { fclose(@out); @out = stdout; } lua_pushnumber(1); } else { if (lua_isstring(o) == 0) { lua_error("incorrect argument to function 'writeto`"); lua_pushnumber(0); } else { FILE fp = fopen(lua_getstring(o), "w"); if (fp == null) { lua_pushnumber(0); } else { if (@out != stdout) { fclose(@out); } @out = fp; lua_pushnumber(1); } } } }
private static void math_pow() { lua_Object o1 = lua_getparam(1); lua_Object o2 = lua_getparam(2); lua_Object op = lua_getparam(3); if (0 == lua_isnumber(o1) || 0 == lua_isnumber(o2) || lua_getstring(op)[0] != 'p') { lua_Object old = lua_getlocked(old_pow); lua_pushobject(o1); lua_pushobject(o2); lua_pushobject(op); if (lua_callfunction(old) != 0) { lua_error(null); } } else { double d1 = lua_getnumber(o1); double d2 = lua_getnumber(o2); lua_pushnumber((real)pow(d1, d2)); } }
/* ** Store top of the stack at an array index. Return 1 on error, 0 on success. */ public static int lua_storeindexed(lua_Object @object, float index) { if (tag(@object) != Type.T_ARRAY) { return(1); } else { Object_ @ref = new Object_(), h; tag(@ref, Type.T_NUMBER); nvalue(@ref, index); //FIXME:real->float h = lua_hashdefine(avalue(@object), @ref); if (h == null) { return(1); } if (tag(top.get(-1)) == Type.T_MARK) { return(1); } top.dec(); h.set(top.get()); } return(0); }
/* ** Store top of the stack at an array field. Return 1 on error, 0 on success. */ public static int lua_storefield(lua_Object @object, CharPtr field) { if (tag(@object) != Type.T_ARRAY) { return(1); } else { Object_ @ref = new Object_(), h; tag(@ref, Type.T_STRING); svalue(@ref, lua_createstring(lua_strdup(field))); h = lua_hashdefine(avalue(@object), @ref); if (h == null) { return(1); } if (tag(top.get(-1)) == Type.T_MARK) { return(1); } top.dec(); h.set(top.get()); } return(0); }