[CHANGE] tostr() now accepts any type
This commit is contained in:
103
lua.cpp
103
lua.cpp
@@ -2,6 +2,86 @@
|
||||
#include "lua/lua.hpp"
|
||||
#include "ascii.h"
|
||||
|
||||
void reverse(char* str, int len) {
|
||||
int i = 0, j = len - 1, temp;
|
||||
while (i < j) {
|
||||
temp = str[i];
|
||||
str[i] = str[j];
|
||||
str[j] = temp;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
int intToStr(int x, char str[], int d) {
|
||||
int i = 0;
|
||||
while (x) {
|
||||
str[i++] = (x % 10) + '0';
|
||||
x = x / 10;
|
||||
}
|
||||
|
||||
while (i < d) str[i++] = '0';
|
||||
reverse(str, i);
|
||||
str[i] = '\0';
|
||||
return i;
|
||||
}
|
||||
|
||||
int ftoa(float n, char* res, int afterpoint) {
|
||||
int ipart = (int)n;
|
||||
float fpart = n - (float)ipart;
|
||||
int i = intToStr(ipart, res, 1);
|
||||
fpart = fpart * SDL_pow(10, afterpoint);
|
||||
if (int(fpart) != 0) {
|
||||
res[i++] = '.';
|
||||
i += intToStr((int)fpart, res + i, afterpoint);
|
||||
while(res[i-1] == '0') res[--i] = '\0';
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
char tempstr[1024];
|
||||
uint16_t ts_index = 0;
|
||||
|
||||
void table_to_str(lua_State *L, int indx);
|
||||
|
||||
void value_to_str(lua_State *L, int indx) {
|
||||
if (lua_isnoneornil(L, indx)) {
|
||||
SDL_memcpy(&tempstr[ts_index], "nil", 3); ts_index+=3;
|
||||
} else if (lua_isfunction(L, indx) || lua_iscfunction(L,indx)) {
|
||||
SDL_memcpy(&tempstr[ts_index], "[function]", 10); ts_index+=10;
|
||||
} else if (lua_istable(L, indx)) {
|
||||
table_to_str(L, indx);
|
||||
} else if (lua_type(L, indx) == LUA_TNUMBER) {
|
||||
const float val = luaL_checknumber(L, indx);
|
||||
const int len = ftoa(val, &tempstr[ts_index], 4); ts_index+=len;
|
||||
} else if (lua_isboolean(L,indx)) {
|
||||
if (lua_toboolean(L, indx)) {
|
||||
SDL_memcpy(&tempstr[ts_index], "true", 4); ts_index+=4;
|
||||
} else {
|
||||
SDL_memcpy(&tempstr[ts_index], "false", 5); ts_index+=5;
|
||||
}
|
||||
} else if (lua_isstring(L,indx)) {
|
||||
const char* str = luaL_checkstring(L,indx);
|
||||
tempstr[ts_index++] = '"';
|
||||
SDL_memcpy(&tempstr[ts_index], str, strlen(str)); ts_index+=strlen(str);
|
||||
tempstr[ts_index++] = '"';
|
||||
}
|
||||
}
|
||||
|
||||
void table_to_str(lua_State *L, int indx) {
|
||||
tempstr[ts_index++] = '{';
|
||||
lua_pushnil(L);
|
||||
bool first = true;
|
||||
while (lua_next(L, indx) != 0) {
|
||||
if (first) { first=false; } else { tempstr[ts_index++] = ','; }
|
||||
value_to_str(L, lua_gettop(L)-1);
|
||||
tempstr[ts_index++] = '=';
|
||||
value_to_str(L, lua_gettop(L));
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
tempstr[ts_index++] = '}';
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
static int cpp_cls(lua_State *L) {
|
||||
uint8_t color = luaL_optinteger(L, 1, 32);
|
||||
@@ -168,9 +248,28 @@ extern "C" {
|
||||
return 0;
|
||||
}
|
||||
static int cpp_tostr(lua_State *L) {
|
||||
float val = luaL_checknumber(L, 1);
|
||||
lua_pushstring(L, tostr(val));
|
||||
ts_index=0;
|
||||
value_to_str(L, 1); tempstr[ts_index] = '\0';
|
||||
lua_pushstring(L, tempstr);
|
||||
return 1;
|
||||
/*
|
||||
if (lua_isnoneornil(L,1)) {
|
||||
lua_pushstring(L, "nil");
|
||||
} else if (lua_isfunction(L,1) || lua_iscfunction(L,1)) {
|
||||
lua_pushstring(L, "[function]");
|
||||
} else if (lua_istable(L,1)) {
|
||||
lua_gettable
|
||||
lua_pushstring(L, "[function]");
|
||||
} else if (lua_isstring(L,1)) {
|
||||
lua_pushstring(L, luaL_checkstring(L,1));
|
||||
} else if (lua_isboolean(L,1)) {
|
||||
lua_pushstring(L, lua_toboolean(L, 1) ? "true" : "false");
|
||||
} else {
|
||||
const float val = luaL_checknumber(L, 1);
|
||||
lua_pushstring(L, tostr(val));
|
||||
}
|
||||
return 1;
|
||||
*/
|
||||
}
|
||||
static int cpp_ascii(lua_State *L) {
|
||||
const char* str = luaL_checkstring(L, 1);
|
||||
|
||||
Reference in New Issue
Block a user