Skip to content

Instantly share code, notes, and snippets.

@mischief
Created April 29, 2012 21:17
Show Gist options
  • Save mischief/2553298 to your computer and use it in GitHub Desktop.
Save mischief/2553298 to your computer and use it in GitHub Desktop.
static void luafmt(void *data, const char *fmt, ...) {
char *buf;
lua_State *L = (lua_State *) data;
va_list vl;
va_start(vl,fmt);
int size = vsprintf(NULL, fmt, vl);
buf = malloc(size * sizeof(char));
if(!buf) luaL_error(L, "luafmt: no memory");
vsprintf(buf, fmt, vl);
lua_pushstring(L, buf);
free(buf);
}
static int lua_cpuid (lua_State *L) {
lua_newtable(L); /* creates a table */
int size1 = lua_gettop(L); // store first size
// cpu_info_format(&base_cpuid, luafmt, L); //format shit
luafmt((void *) L, "%s", "1 foo");
luafmt((void *) L, "%s", "2 bar");
int size2 = lua_gettop(L);
// makes a string??
for(int i = size2; i < size1; --i) {
lua_rawseti(L, 1, i);
}
// makes a table, but wrong order
/*
for(int i = size2; i > size1; --i) {
lua_rawseti(L, 1, i);
}
*/
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment