// get_inventory(self) int NodeMetaRef::l_get_inventory(lua_State *L) { NodeMetaRef *ref = checkobject(L, 1); getmeta(ref, true); // try to ensure the metadata exists InvRef::createNodeMeta(L, ref->m_p); return 1; }
// get_string(self, name) int NodeMetaRef::l_get_string(lua_State *L) { NodeMetaRef *ref = checkobject(L, 1); std::string name = luaL_checkstring(L, 2); NodeMetadata *meta = getmeta(ref, false); if(meta == NULL){ lua_pushlstring(L, "", 0); return 1; } std::string str = meta->getString(name); lua_pushlstring(L, str.c_str(), str.size()); return 1; }
// get_float(self, name) int NodeMetaRef::l_get_float(lua_State *L) { NodeMetaRef *ref = checkobject(L, 1); std::string name = lua_tostring(L, 2); NodeMetadata *meta = getmeta(ref, false); if(meta == NULL){ lua_pushnumber(L, 0); return 1; } std::string str = meta->getString(name); lua_pushnumber(L, stof(str)); return 1; }
// set_float(self, name, var) int NodeMetaRef::l_set_float(lua_State *L) { NodeMetaRef *ref = checkobject(L, 1); std::string name = lua_tostring(L, 2); float a = lua_tonumber(L, 3); std::string str = ftos(a); NodeMetadata *meta = getmeta(ref, true); if(meta == NULL || str == meta->getString(name)) return 0; meta->setString(name, str); reportMetadataChange(ref); return 0; }
// from_table(self, table) int NodeMetaRef::l_from_table(lua_State *L) { MAP_LOCK_REQUIRED; NodeMetaRef *ref = checkobject(L, 1); int base = 2; // clear old metadata first ref->m_env->getMap().removeNodeMetadata(ref->m_p); if(lua_isnil(L, base)){ // No metadata lua_pushboolean(L, true); return 1; } // Create new metadata NodeMetadata *meta = getmeta(ref, true); if(meta == NULL){ lua_pushboolean(L, false); return 1; } // Set fields lua_getfield(L, base, "fields"); int fieldstable = lua_gettop(L); lua_pushnil(L); while(lua_next(L, fieldstable) != 0){ // key at index -2 and value at index -1 std::string name = lua_tostring(L, -2); size_t cl; const char *cs = lua_tolstring(L, -1, &cl); std::string value(cs, cl); meta->setString(name, value); lua_pop(L, 1); // removes value, keeps key for next iteration } // Set inventory Inventory *inv = meta->getInventory(); lua_getfield(L, base, "inventory"); int inventorytable = lua_gettop(L); lua_pushnil(L); while(lua_next(L, inventorytable) != 0){ // key at index -2 and value at index -1 std::string name = lua_tostring(L, -2); read_inventory_list(L, -1, inv, name.c_str(), getServer(L)); lua_pop(L, 1); // removes value, keeps key for next iteration } reportMetadataChange(ref); lua_pushboolean(L, true); return 1; }
afs_int32 util_GetInt64(char *as, afs_int64 * aval) { afs_int64 total; int tc; int base; int negative; total = 0; /* initialize things */ negative = 0; /* skip over leading spaces */ while ((tc = *as)) { if (tc != ' ' && tc != '\t') break; } /* compute sign */ if (*as == '-') { negative = 1; as++; /* skip over character */ } /* compute the base */ if (*as == '0') { as++; if (*as == 'x' || *as == 'X') { base = 16; as++; } else base = 8; } else base = 10; /* compute the # itself */ while ((tc = *as)) { if (!ismeta(tc, base)) return -1; total *= base; total += getmeta(tc); as++; } if (negative) *aval = -total; else *aval = total; return 0; }
// set_string(self, name, var) int NodeMetaRef::l_set_string(lua_State *L) { NodeMetaRef *ref = checkobject(L, 1); std::string name = luaL_checkstring(L, 2); size_t len = 0; const char *s = lua_tolstring(L, 3, &len); std::string str(s, len); NodeMetadata *meta = getmeta(ref, !str.empty()); if(meta == NULL || str == meta->getString(name)) return 0; meta->setString(name, str); reportMetadataChange(ref); return 0; }
// set_int(self, name, var) int NodeMetaRef::l_set_int(lua_State *L) { MAP_LOCK_REQUIRED; NodeMetaRef *ref = checkobject(L, 1); std::string name = lua_tostring(L, 2); int a = lua_tointeger(L, 3); std::string str = itos(a); NodeMetadata *meta = getmeta(ref, true); if(meta == NULL || str == meta->getString(name)) return 0; meta->setString(name, str); reportMetadataChange(ref); return 0; }
// to_table(self) int NodeMetaRef::l_to_table(lua_State *L) { MAP_LOCK_REQUIRED; NodeMetaRef *ref = checkobject(L, 1); NodeMetadata *meta = getmeta(ref, true); if (meta == NULL) { lua_pushnil(L); return 1; } lua_newtable(L); // fields lua_newtable(L); { StringMap fields = meta->getStrings(); for (StringMap::const_iterator it = fields.begin(); it != fields.end(); ++it) { const std::string &name = it->first; const std::string &value = it->second; lua_pushlstring(L, name.c_str(), name.size()); lua_pushlstring(L, value.c_str(), value.size()); lua_settable(L, -3); } } lua_setfield(L, -2, "fields"); // inventory lua_newtable(L); Inventory *inv = meta->getInventory(); if (inv) { std::vector<const InventoryList *> lists = inv->getLists(); for(std::vector<const InventoryList *>::const_iterator i = lists.begin(); i != lists.end(); i++) { push_inventory_list(L, inv, (*i)->getName().c_str()); lua_setfield(L, -2, (*i)->getName().c_str()); } } lua_setfield(L, -2, "inventory"); return 1; }
afs_uint32 util_GetUInt64(char *as, afs_uint64 * aval) { afs_uint64 total; int tc; int base; total = 0; /* initialize things */ /* skip over leading spaces */ while ((tc = *as)) { if (tc != ' ' && tc != '\t') break; } /* compute the base */ if (*as == '0') { as++; if (*as == 'x' || *as == 'X') { base = 16; as++; } else base = 8; } else base = 10; /* compute the # itself */ while ((tc = *as)) { if (!ismeta(tc, base)) return -1; total *= base; total += getmeta(tc); as++; } *aval = total; return 0; }