static void GetParam(void) { getparam_data gp; lua_Object func; const char* title = luaL_check_string(1); void* user_data = (void*)&gp; const char* format = luaL_check_string(3); int param_count, param_extra, i, size, ret, line_size = 0, lua_param_start = 4; const char* f = format; const char* s; void* param_data[50]; char param_type[50]; gp.has_func = 0; gp.func_ref = 0; memset(param_data, 0, sizeof(void*)*50); memset(param_type, 0, sizeof(char)*50); param_count = iupGetParamCount(format, ¶m_extra); for (i = 0; i < param_count; i++) { char t = iupGetParamType(f, &line_size); if (t == 't') /* if separator */ { f += line_size; i--; /* compensate next increment */ continue; } switch(t) { case 'b': case 'i': case 'l': param_data[i] = malloc(sizeof(int)); *(int*)(param_data[i]) = (int)luaL_check_number(lua_param_start); lua_param_start++; break; case 'a': case 'r': param_data[i] = malloc(sizeof(float)); *(float*)(param_data[i]) = (float)luaL_check_number(lua_param_start); lua_param_start++; break; case 'f': case 'c': case 's': case 'm': s = luaL_check_string(lua_param_start); lua_param_start++; size = strlen(s); if (size < 512) param_data[i] = malloc(512); else param_data[i] = malloc(2*size); memcpy(param_data[i], s, size+1); break; } param_type[i] = t; f += line_size; } func = lua_getparam(2); if (lua_isfunction(func)) { lua_pushobject(func); gp.func_ref = lua_ref(1); gp.has_func = 1; } ret = IupGetParamv(title, param_action, user_data, format, param_count, param_extra, param_data); lua_pushnumber(ret); if (ret) { for (i = 0; i < param_count; i++) { switch(param_type[i]) { case 'b': case 'i': case 'l': lua_pushnumber(*(int*)(param_data[i])); break; case 'a': case 'r': lua_pushnumber(*(float*)(param_data[i])); break; case 'f': case 'c': case 's': case 'm': lua_pushstring((char*)(param_data[i])); break; } } } for (i = 0; i < param_count; i++) { free(param_data[i]); } if (gp.has_func) lua_unref(gp.func_ref); }
static int GetParam(lua_State *L) { getparam_data gp; const char* title = luaL_checkstring(L, 1); void* user_data = (void*)&gp; const char* format = luaL_checkstring(L, 3); size_t size, max_str; int param_count, param_extra, i, ret, line_size = 0, lua_param_start = 4; const char* f = format; const char* s; void* param_data[50]; char param_type[50]; gp.L = L; gp.has_func = 0; gp.func_ref = 0; memset(param_data, 0, sizeof(void*)*50); memset(param_type, 0, sizeof(char)*50); param_count = iupGetParamCount(format, ¶m_extra); for (i = 0; i < param_count; i++) { char t = iupGetParamType(f, &line_size); switch(t) { case 'u': case 't': f += line_size; i--; /* compensate next increment */ continue; /* notice this will go to the next i */ case 'h': param_data[i] = malloc(sizeof(Ihandle*)); *(Ihandle**)(param_data[i]) = iuplua_checkihandle(L, lua_param_start); lua_param_start++; break; case 'b': /* TO DO: add this code some day: if (lua_isboolean(L, lua_param_start)) { param_data[i] = malloc(sizeof(int)); *(int*)(param_data[i]) = lua_toboolean(L, lua_param_start); lua_param_start++; break; } else continue and get an integer */ case 'i': case 'o': case 'l': param_data[i] = malloc(sizeof(int)); *(int*)(param_data[i]) = (int)luaL_checkinteger(L, lua_param_start); lua_param_start++; break; case 'a': case 'r': param_data[i] = malloc(sizeof(float)); *(float*)(param_data[i]) = (float)luaL_checknumber(L, lua_param_start); lua_param_start++; break; case 'A': case 'R': param_data[i] = malloc(sizeof(double)); *(double*)(param_data[i]) = (double)luaL_checknumber(L, lua_param_start); lua_param_start++; break; case 'd': case 'f': case 's': case 'm': case 'n': case 'c': max_str = 512; if (t == 'f') max_str = 4096; else if (t == 'm') max_str = 10240; s = luaL_checklstring(L, lua_param_start, &size); lua_param_start++; if (size < max_str) param_data[i] = malloc(max_str); else param_data[i] = malloc(2*size); memcpy(param_data[i], s, size+1); break; } param_type[i] = t; f += line_size; } if (lua_isfunction(L, 2)) { lua_pushvalue(L, 2); gp.func_ref = luaL_ref(L, LUA_REGISTRYINDEX); gp.has_func = 1; } ret = IupGetParamv(title, param_action, user_data, format, param_count, param_extra, param_data); lua_pushboolean(L, ret); if (ret) { for (i = 0; i < param_count; i++) { switch(param_type[i]) { case 'b': case 'i': case 'o': case 'l': lua_pushinteger(L, *(int*)(param_data[i])); break; case 'A': case 'R': lua_pushnumber(L, *(double*)(param_data[i])); break; case 'a': case 'r': lua_pushnumber(L, *(float*)(param_data[i])); break; case 'd': case 'f': case 'n': case 'c': case 's': case 'm': lua_pushstring(L, (char*)(param_data[i])); break; } } } for (i = 0; i < param_count; i++) { free(param_data[i]); } if (gp.has_func) luaL_unref(L, LUA_REGISTRYINDEX, gp.func_ref); if (ret) return param_count+1; else return 1; }
static int GetParam(lua_State *L) { getparam_data gp; const char* title = luaL_checkstring(L, 1); void* user_data = (void*)&gp; const char* format = luaL_checkstring(L, 3); int param_count, param_extra, i, size, ret, line_size = 0, lua_param_start = 4; const char* f = format; const char* s; void* param_data[50]; char param_type[50]; gp.L = L; gp.has_func = 0; gp.func_ref = 0; memset(param_data, 0, sizeof(void*)*50); memset(param_type, 0, sizeof(char)*50); param_count = iupGetParamCount(format, ¶m_extra); for (i = 0; i < param_count; i++) { char t = iupGetParamType(f, &line_size); if (t == 't') /* if separator */ { f += line_size; i--; /* compensate next increment */ continue; } switch(t) { case 'b': /* TO DO: add this code some day: if (lua_isboolean(L, lua_param_start)) { param_data[i] = malloc(sizeof(int)); *(int*)(param_data[i]) = lua_toboolean(L, lua_param_start); lua_param_start++; break; } */ /* else continuous and get an integer */ case 'i': case 'l': param_data[i] = malloc(sizeof(int)); *(int*)(param_data[i]) = luaL_checkinteger(L, lua_param_start); lua_param_start++; break; case 'a': case 'r': param_data[i] = malloc(sizeof(float)); *(float*)(param_data[i]) = (float)luaL_checknumber(L, lua_param_start); lua_param_start++; break; case 'f': case 'c': case 's': case 'm': s = luaL_checkstring(L, lua_param_start); lua_param_start++; size = strlen(s); if (size < 512) param_data[i] = malloc(512); else param_data[i] = malloc(2*size); memcpy(param_data[i], s, size+1); break; } param_type[i] = t; f += line_size; } if (lua_isfunction(L, 2)) { lua_pushvalue(L, 2); gp.func_ref = lua_ref(L, 1); gp.has_func = 1; } ret = IupGetParamv(title, param_action, user_data, format, param_count, param_extra, param_data); lua_pushboolean(L, ret); if (ret) { for (i = 0; i < param_count; i++) { switch(param_type[i]) { case 'b': case 'i': case 'l': lua_pushinteger(L, *(int*)(param_data[i])); break; case 'a': case 'r': lua_pushnumber(L, *(float*)(param_data[i])); break; case 'f': case 'c': case 's': case 'm': lua_pushstring(L, (char*)(param_data[i])); break; } } } for (i = 0; i < param_count; i++) { free(param_data[i]); } if (gp.has_func) lua_unref(L, gp.func_ref); if (ret) return param_count+1; else return 1; }