Replace instances of std::map<std::string, std::string> with StringMap

Also, clean up surrounding code style
Replace by-value parameter passing with const refs when possible
Fix post-increment of iterators
This commit is contained in:
kwolekr 2015-05-19 02:24:14 -04:00
parent 603297cc35
commit da34a2b33e
25 changed files with 180 additions and 193 deletions

View File

@ -56,7 +56,7 @@ void BanManager::load()
infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl; infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
throw SerializationError("BanManager::load(): Couldn't open file"); throw SerializationError("BanManager::load(): Couldn't open file");
} }
while(!is.eof() && is.good()) while(!is.eof() && is.good())
{ {
std::string line; std::string line;
@ -74,18 +74,14 @@ void BanManager::load()
void BanManager::save() void BanManager::save()
{ {
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
infostream<<"BanManager: saving to "<<m_banfilepath<<std::endl; infostream << "BanManager: saving to " << m_banfilepath << std::endl;
std::ostringstream ss(std::ios_base::binary); std::ostringstream ss(std::ios_base::binary);
for(std::map<std::string, std::string>::iterator for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it)
i = m_ips.begin(); ss << it->first << "|" << it->second << "\n";
i != m_ips.end(); i++)
{
ss << i->first << "|" << i->second << "\n";
}
if(!fs::safeWriteToFile(m_banfilepath, ss.str())) { if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
infostream<<"BanManager: failed saving to "<<m_banfilepath<<std::endl; infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
throw SerializationError("BanManager::save(): Couldn't write file"); throw SerializationError("BanManager::save(): Couldn't write file");
} }
@ -102,25 +98,23 @@ std::string BanManager::getBanDescription(const std::string &ip_or_name)
{ {
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
std::string s = ""; std::string s = "";
for(std::map<std::string, std::string>::iterator for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it) {
i = m_ips.begin(); if (it->first == ip_or_name || it->second == ip_or_name
i != m_ips.end(); i++) || ip_or_name == "") {
{ s += it->first + "|" + it->second + ", ";
if(i->first == ip_or_name || i->second == ip_or_name }
|| ip_or_name == "")
s += i->first + "|" + i->second + ", ";
} }
s = s.substr(0, s.size()-2); s = s.substr(0, s.size() - 2);
return s; return s;
} }
std::string BanManager::getBanName(const std::string &ip) std::string BanManager::getBanName(const std::string &ip)
{ {
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
std::map<std::string, std::string>::iterator i = m_ips.find(ip); StringMap::iterator it = m_ips.find(ip);
if(i == m_ips.end()) if (it == m_ips.end())
return ""; return "";
return i->second; return it->second;
} }
void BanManager::add(const std::string &ip, const std::string &name) void BanManager::add(const std::string &ip, const std::string &name)
@ -133,19 +127,16 @@ void BanManager::add(const std::string &ip, const std::string &name)
void BanManager::remove(const std::string &ip_or_name) void BanManager::remove(const std::string &ip_or_name)
{ {
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
for(std::map<std::string, std::string>::iterator for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
i = m_ips.begin(); if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
i != m_ips.end();) m_ips.erase(it++);
{
if((i->first == ip_or_name) || (i->second == ip_or_name)) {
m_ips.erase(i++);
} else { } else {
++i; ++it;
} }
} }
m_modified = true; m_modified = true;
} }
bool BanManager::isModified() bool BanManager::isModified()
{ {

View File

@ -20,8 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef BAN_HEADER #ifndef BAN_HEADER
#define BAN_HEADER #define BAN_HEADER
#include <map> #include "util/string.h"
#include <string>
#include "jthread/jthread.h" #include "jthread/jthread.h"
#include "jthread/jmutex.h" #include "jthread/jmutex.h"
#include "exceptions.h" #include "exceptions.h"
@ -43,7 +42,7 @@ public:
private: private:
JMutex m_mutex; JMutex m_mutex;
std::string m_banfilepath; std::string m_banfilepath;
std::map<std::string, std::string> m_ips; StringMap m_ips;
bool m_modified; bool m_modified;
}; };

View File

@ -1102,7 +1102,7 @@ void Client::sendRemovedSounds(std::vector<s32> &soundList)
} }
void Client::sendNodemetaFields(v3s16 p, const std::string &formname, void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
const std::map<std::string, std::string> &fields) const StringMap &fields)
{ {
size_t fields_size = fields.size(); size_t fields_size = fields.size();
@ -1112,10 +1112,10 @@ void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
pkt << p << formname << (u16) (fields_size & 0xFFFF); pkt << p << formname << (u16) (fields_size & 0xFFFF);
for(std::map<std::string, std::string>::const_iterator StringMap::const_iterator it;
i = fields.begin(); i != fields.end(); i++) { for (it = fields.begin(); it != fields.end(); ++it) {
const std::string &name = i->first; const std::string &name = it->first;
const std::string &value = i->second; const std::string &value = it->second;
pkt << name; pkt << name;
pkt.putLongString(value); pkt.putLongString(value);
} }
@ -1124,7 +1124,7 @@ void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
} }
void Client::sendInventoryFields(const std::string &formname, void Client::sendInventoryFields(const std::string &formname,
const std::map<std::string, std::string> &fields) const StringMap &fields)
{ {
size_t fields_size = fields.size(); size_t fields_size = fields.size();
FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of inventory fields"); FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of inventory fields");
@ -1132,10 +1132,10 @@ void Client::sendInventoryFields(const std::string &formname,
NetworkPacket pkt(TOSERVER_INVENTORY_FIELDS, 0); NetworkPacket pkt(TOSERVER_INVENTORY_FIELDS, 0);
pkt << formname << (u16) (fields_size & 0xFFFF); pkt << formname << (u16) (fields_size & 0xFFFF);
for(std::map<std::string, std::string>::const_iterator StringMap::const_iterator it;
i = fields.begin(); i != fields.end(); i++) { for (it = fields.begin(); it != fields.end(); ++it) {
const std::string &name = i->first; const std::string &name = it->first;
const std::string &value = i->second; const std::string &value = it->second;
pkt << name; pkt << name;
pkt.putLongString(value); pkt.putLongString(value);
} }
@ -1918,14 +1918,13 @@ ParticleManager* Client::getParticleManager()
scene::IAnimatedMesh* Client::getMesh(const std::string &filename) scene::IAnimatedMesh* Client::getMesh(const std::string &filename)
{ {
std::map<std::string, std::string>::const_iterator i = StringMap::const_iterator it = m_mesh_data.find(filename);
m_mesh_data.find(filename); if (it == m_mesh_data.end()) {
if(i == m_mesh_data.end()){ errorstream << "Client::getMesh(): Mesh not found: \"" << filename
errorstream<<"Client::getMesh(): Mesh not found: \""<<filename<<"\"" << "\"" << std::endl;
<<std::endl;
return NULL; return NULL;
} }
const std::string &data = i->second; const std::string &data = it->second;
scene::ISceneManager *smgr = m_device->getSceneManager(); scene::ISceneManager *smgr = m_device->getSceneManager();
// Create the mesh, remove it from cache and return it // Create the mesh, remove it from cache and return it

View File

@ -405,13 +405,13 @@ public:
void interact(u8 action, const PointedThing& pointed); void interact(u8 action, const PointedThing& pointed);
void sendNodemetaFields(v3s16 p, const std::string &formname, void sendNodemetaFields(v3s16 p, const std::string &formname,
const std::map<std::string, std::string> &fields); const StringMap &fields);
void sendInventoryFields(const std::string &formname, void sendInventoryFields(const std::string &formname,
const std::map<std::string, std::string> &fields); const StringMap &fields);
void sendInventoryAction(InventoryAction *a); void sendInventoryAction(InventoryAction *a);
void sendChatMessage(const std::wstring &message); void sendChatMessage(const std::wstring &message);
void sendChangePassword(const std::string &oldpassword, void sendChangePassword(const std::string &oldpassword,
const std::string &newpassword); const std::string &newpassword);
void sendDamage(u8 damage); void sendDamage(u8 damage);
void sendBreath(u16 breath); void sendBreath(u16 breath);
void sendRespawn(); void sendRespawn();
@ -648,7 +648,7 @@ private:
std::map<std::string, Inventory*> m_detached_inventories; std::map<std::string, Inventory*> m_detached_inventories;
// Storage for mesh data for creating multiple instances of the same mesh // Storage for mesh data for creating multiple instances of the same mesh
std::map<std::string, std::string> m_mesh_data; StringMap m_mesh_data;
// own state // own state
LocalClientState m_state; LocalClientState m_state;

View File

@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapnode.h" #include "mapnode.h"
#include "nodedef.h" #include "nodedef.h"
#include "nameidmapping.h" #include "nameidmapping.h"
#include <map> #include "util/string.h"
/* /*
Legacy node content type IDs Legacy node content type IDs
@ -218,14 +218,13 @@ public:
} }
std::string get(const std::string &old) std::string get(const std::string &old)
{ {
std::map<std::string, std::string>::const_iterator i; StringMap::const_iterator it = old_to_new.find(old);
i = old_to_new.find(old); if (it == old_to_new.end())
if(i == old_to_new.end())
return ""; return "";
return i->second; return it->second;
} }
private: private:
std::map<std::string, std::string> old_to_new; StringMap old_to_new;
}; };
NewNameGetter newnamegetter; NewNameGetter newnamegetter;

View File

@ -87,11 +87,11 @@ struct TextDestNodeMetadata : public TextDest {
std::string ntext = wide_to_narrow(text); std::string ntext = wide_to_narrow(text);
infostream << "Submitting 'text' field of node at (" << m_p.X << "," infostream << "Submitting 'text' field of node at (" << m_p.X << ","
<< m_p.Y << "," << m_p.Z << "): " << ntext << std::endl; << m_p.Y << "," << m_p.Z << "): " << ntext << std::endl;
std::map<std::string, std::string> fields; StringMap fields;
fields["text"] = ntext; fields["text"] = ntext;
m_client->sendNodemetaFields(m_p, "", fields); m_client->sendNodemetaFields(m_p, "", fields);
} }
void gotText(std::map<std::string, std::string> fields) void gotText(const StringMap &fields)
{ {
m_client->sendNodemetaFields(m_p, "", fields); m_client->sendNodemetaFields(m_p, "", fields);
} }
@ -111,7 +111,7 @@ struct TextDestPlayerInventory : public TextDest {
m_client = client; m_client = client;
m_formname = formname; m_formname = formname;
} }
void gotText(std::map<std::string, std::string> fields) void gotText(const StringMap &fields)
{ {
m_client->sendInventoryFields(m_formname, fields); m_client->sendInventoryFields(m_formname, fields);
} }
@ -138,7 +138,7 @@ struct LocalFormspecHandler : public TextDest {
errorstream << "LocalFormspecHandler::gotText old style message received" << std::endl; errorstream << "LocalFormspecHandler::gotText old style message received" << std::endl;
} }
void gotText(std::map<std::string, std::string> fields) void gotText(const StringMap &fields)
{ {
if (m_formname == "MT_PAUSE_MENU") { if (m_formname == "MT_PAUSE_MENU") {
if (fields.find("btn_sound") != fields.end()) { if (fields.find("btn_sound") != fields.end()) {
@ -180,9 +180,9 @@ struct LocalFormspecHandler : public TextDest {
if ((fields.find("btn_send") != fields.end()) || if ((fields.find("btn_send") != fields.end()) ||
(fields.find("quit") != fields.end())) { (fields.find("quit") != fields.end())) {
if (fields.find("f_text") != fields.end()) { StringMap::const_iterator it = fields.find("f_text");
m_client->typeChatMessage(narrow_to_wide(fields["f_text"])); if (it != fields.end())
} m_client->typeChatMessage(narrow_to_wide(it->second));
return; return;
} }
@ -210,12 +210,14 @@ struct LocalFormspecHandler : public TextDest {
return; return;
} }
errorstream << "LocalFormspecHandler::gotText unhandled >" << m_formname << "< event" << std::endl; errorstream << "LocalFormspecHandler::gotText unhandled >"
int i = 0; << m_formname << "< event" << std::endl;
for (std::map<std::string, std::string>::iterator iter = fields.begin(); int i = 0;
iter != fields.end(); iter++) { StringMap::const_iterator it;
errorstream << "\t" << i << ": " << iter->first << "=" << iter->second << std::endl; for (it = fields.begin(); it != fields.end(); ++it) {
errorstream << "\t" << i << ": " << it->first
<< "=" << it->second << std::endl;
i++; i++;
} }
} }

View File

@ -53,7 +53,7 @@ TextDestGuiEngine::TextDestGuiEngine(GUIEngine* engine)
} }
/******************************************************************************/ /******************************************************************************/
void TextDestGuiEngine::gotText(std::map<std::string, std::string> fields) void TextDestGuiEngine::gotText(const StringMap &fields)
{ {
m_engine->getScriptIface()->handleMainMenuButtons(fields); m_engine->getScriptIface()->handleMainMenuButtons(fields);
} }

View File

@ -73,7 +73,7 @@ public:
* receive fields transmitted by guiFormSpecMenu * receive fields transmitted by guiFormSpecMenu
* @param fields map containing formspec field elements currently active * @param fields map containing formspec field elements currently active
*/ */
void gotText(std::map<std::string, std::string> fields); void gotText(const StringMap &fields);
/** /**
* receive text/events transmitted by guiFormSpecMenu * receive text/events transmitted by guiFormSpecMenu

View File

@ -84,7 +84,7 @@ void GUIFileSelectMenu::drawMenu()
void GUIFileSelectMenu::acceptInput() { void GUIFileSelectMenu::acceptInput() {
if ((m_text_dst != 0) && (this->m_formname != "")){ if ((m_text_dst != 0) && (this->m_formname != "")){
std::map<std::string, std::string> fields; StringMap fields;
if (m_accepted) if (m_accepted)
fields[m_formname + "_accepted"] = wide_to_narrow(m_fileOpenDialog->getFileName()); fields[m_formname + "_accepted"] = wide_to_narrow(m_fileOpenDialog->getFileName());

View File

@ -2579,7 +2579,7 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
{ {
if(m_text_dst) if(m_text_dst)
{ {
std::map<std::string, std::string> fields; StringMap fields;
if (quitmode == quit_mode_accept) { if (quitmode == quit_mode_accept) {
fields["quit"] = "true"; fields["quit"] = "true";

View File

@ -56,7 +56,7 @@ struct TextDest
virtual ~TextDest() {}; virtual ~TextDest() {};
// This is deprecated I guess? -celeron55 // This is deprecated I guess? -celeron55
virtual void gotText(std::wstring text){} virtual void gotText(std::wstring text){}
virtual void gotText(std::map<std::string, std::string> fields) = 0; virtual void gotText(const StringMap &fields) = 0;
virtual void setFormName(std::string formname) virtual void setFormName(std::string formname)
{ m_formname = formname;}; { m_formname = formname;};

View File

@ -266,8 +266,7 @@ HTTPFetchOngoing::HTTPFetchOngoing(HTTPFetchRequest request_, CurlHandlePool *po
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
} else if (request.multipart) { } else if (request.multipart) {
curl_httppost *last = NULL; curl_httppost *last = NULL;
for (std::map<std::string, std::string>::iterator it = for (StringMap::iterator it = request.post_fields.begin();
request.post_fields.begin();
it != request.post_fields.end(); ++it) { it != request.post_fields.end(); ++it) {
curl_formadd(&post, &last, curl_formadd(&post, &last,
CURLFORM_NAMELENGTH, it->first.size(), CURLFORM_NAMELENGTH, it->first.size(),
@ -282,10 +281,8 @@ HTTPFetchOngoing::HTTPFetchOngoing(HTTPFetchRequest request_, CurlHandlePool *po
} else if (request.post_data.empty()) { } else if (request.post_data.empty()) {
curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POST, 1);
std::string str; std::string str;
for (std::map<std::string, std::string>::iterator it = for (StringMap::iterator it = request.post_fields.begin();
request.post_fields.begin(); it != request.post_fields.end(); ++it) {
it != request.post_fields.end();
++it) {
if (str != "") if (str != "")
str += "&"; str += "&";
str += urlencode(it->first); str += urlencode(it->first);

View File

@ -20,9 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef HTTPFETCH_HEADER #ifndef HTTPFETCH_HEADER
#define HTTPFETCH_HEADER #define HTTPFETCH_HEADER
#include <string>
#include <vector> #include <vector>
#include <map> #include "util/string.h"
#include "config.h" #include "config.h"
// Can be used in place of "caller" in asynchronous transfers to discard result // Can be used in place of "caller" in asynchronous transfers to discard result
@ -54,7 +53,7 @@ struct HTTPFetchRequest
// POST fields. Fields are escaped properly. // POST fields. Fields are escaped properly.
// If this is empty a GET request is done instead. // If this is empty a GET request is done instead.
std::map<std::string, std::string> post_fields; StringMap post_fields;
// Raw POST data, overrides post_fields. // Raw POST data, overrides post_fields.
std::string post_data; std::string post_data;

View File

@ -280,26 +280,23 @@ public:
} }
virtual std::string getAlias(const std::string &name) const virtual std::string getAlias(const std::string &name) const
{ {
std::map<std::string, std::string>::const_iterator i; StringMap::const_iterator it = m_aliases.find(name);
i = m_aliases.find(name); if (it != m_aliases.end())
if(i != m_aliases.end()) return it->second;
return i->second;
return name; return name;
} }
virtual std::set<std::string> getAll() const virtual std::set<std::string> getAll() const
{ {
std::set<std::string> result; std::set<std::string> result;
for(std::map<std::string, ItemDefinition*>::const_iterator for(std::map<std::string, ItemDefinition *>::const_iterator
i = m_item_definitions.begin(); it = m_item_definitions.begin();
i != m_item_definitions.end(); i++) it != m_item_definitions.end(); ++it) {
{ result.insert(it->first);
result.insert(i->first);
} }
for(std::map<std::string, std::string>::const_iterator for (StringMap::const_iterator
i = m_aliases.begin(); it = m_aliases.begin();
i != m_aliases.end(); i++) it != m_aliases.end(); ++it) {
{ result.insert(it->first);
result.insert(i->first);
} }
return result; return result;
} }
@ -571,22 +568,24 @@ public:
writeU8(os, 0); // version writeU8(os, 0); // version
u16 count = m_item_definitions.size(); u16 count = m_item_definitions.size();
writeU16(os, count); writeU16(os, count);
for(std::map<std::string, ItemDefinition*>::const_iterator
i = m_item_definitions.begin(); for (std::map<std::string, ItemDefinition *>::const_iterator
i != m_item_definitions.end(); i++) it = m_item_definitions.begin();
{ it != m_item_definitions.end(); ++it) {
ItemDefinition *def = i->second; ItemDefinition *def = it->second;
// Serialize ItemDefinition and write wrapped in a string // Serialize ItemDefinition and write wrapped in a string
std::ostringstream tmp_os(std::ios::binary); std::ostringstream tmp_os(std::ios::binary);
def->serialize(tmp_os, protocol_version); def->serialize(tmp_os, protocol_version);
os<<serializeString(tmp_os.str()); os << serializeString(tmp_os.str());
} }
writeU16(os, m_aliases.size()); writeU16(os, m_aliases.size());
for(std::map<std::string, std::string>::const_iterator
i = m_aliases.begin(); i != m_aliases.end(); i++) for (StringMap::const_iterator
{ it = m_aliases.begin();
os<<serializeString(i->first); it != m_aliases.end(); ++it) {
os<<serializeString(i->second); os << serializeString(it->first);
os << serializeString(it->second);
} }
} }
void deSerialize(std::istream &is) void deSerialize(std::istream &is)
@ -633,7 +632,7 @@ private:
// Key is name // Key is name
std::map<std::string, ItemDefinition*> m_item_definitions; std::map<std::string, ItemDefinition*> m_item_definitions;
// Aliases // Aliases
std::map<std::string, std::string> m_aliases; StringMap m_aliases;
#ifndef SERVER #ifndef SERVER
// The id of the thread that is allowed to use irrlicht directly // The id of the thread that is allowed to use irrlicht directly
threadid_t m_main_thread; threadid_t m_main_thread;

View File

@ -192,7 +192,7 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
{ {
std::string reason; std::string reason;
if(m_script->on_prejoinplayer(playername, addr_s, reason)) { if (m_script->on_prejoinplayer(playername, addr_s, &reason)) {
actionstream << "Server: Player with the name \"" << playerName << "\" " actionstream << "Server: Player with the name \"" << playerName << "\" "
<< "tried to connect from " << addr_s << " " << "tried to connect from " << addr_s << " "
<< "but it was disallowed for the following reason: " << "but it was disallowed for the following reason: "
@ -480,7 +480,7 @@ void Server::handleCommand_Init_Legacy(NetworkPacket* pkt)
{ {
std::string reason; std::string reason;
if (m_script->on_prejoinplayer(playername, addr_s, reason)) { if (m_script->on_prejoinplayer(playername, addr_s, &reason)) {
actionstream << "Server: Player with the name \"" << playername << "\" " actionstream << "Server: Player with the name \"" << playername << "\" "
<< "tried to connect from " << addr_s << " " << "tried to connect from " << addr_s << " "
<< "but it was disallowed for the following reason: " << "but it was disallowed for the following reason: "
@ -1742,7 +1742,7 @@ void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt)
*pkt >> p >> formname >> num; *pkt >> p >> formname >> num;
std::map<std::string, std::string> fields; StringMap fields;
for (u16 k = 0; k < num; k++) { for (u16 k = 0; k < num; k++) {
std::string fieldname; std::string fieldname;
*pkt >> fieldname; *pkt >> fieldname;
@ -1792,7 +1792,7 @@ void Server::handleCommand_InventoryFields(NetworkPacket* pkt)
*pkt >> formname >> num; *pkt >> formname >> num;
std::map<std::string, std::string> fields; StringMap fields;
for (u16 k = 0; k < num; k++) { for (u16 k = 0; k < num; k++) {
std::string fieldname; std::string fieldname;
*pkt >> fieldname; *pkt >> fieldname;

View File

@ -45,10 +45,11 @@ void NodeMetadata::serialize(std::ostream &os) const
{ {
int num_vars = m_stringvars.size(); int num_vars = m_stringvars.size();
writeU32(os, num_vars); writeU32(os, num_vars);
for(std::map<std::string, std::string>::const_iterator for (StringMap::const_iterator
i = m_stringvars.begin(); i != m_stringvars.end(); i++){ it = m_stringvars.begin();
os<<serializeString(i->first); it != m_stringvars.end(); ++it) {
os<<serializeLongString(i->second); os << serializeString(it->first);
os << serializeLongString(it->second);
} }
m_inventory->serialize(os); m_inventory->serialize(os);
@ -203,11 +204,10 @@ void NodeMetadataList::clear()
std::string NodeMetadata::getString(const std::string &name, std::string NodeMetadata::getString(const std::string &name,
unsigned short recursion) const unsigned short recursion) const
{ {
std::map<std::string, std::string>::const_iterator it; StringMap::const_iterator it = m_stringvars.find(name);
it = m_stringvars.find(name); if (it == m_stringvars.end())
if (it == m_stringvars.end()) {
return ""; return "";
}
return resolveString(it->second, recursion); return resolveString(it->second, recursion);
} }

View File

@ -21,10 +21,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define NODEMETADATA_HEADER #define NODEMETADATA_HEADER
#include "irr_v3d.h" #include "irr_v3d.h"
#include <string>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <map> #include "util/string.h"
/* /*
NodeMetadata stores arbitary amounts of data for special blocks. NodeMetadata stores arbitary amounts of data for special blocks.
@ -54,19 +53,19 @@ public:
void setString(const std::string &name, const std::string &var); void setString(const std::string &name, const std::string &var);
// Support variable names in values // Support variable names in values
std::string resolveString(const std::string &str, unsigned short recursion = 0) const; std::string resolveString(const std::string &str, unsigned short recursion = 0) const;
std::map<std::string, std::string> getStrings() const StringMap getStrings() const
{ {
return m_stringvars; return m_stringvars;
} }
// The inventory // The inventory
Inventory* getInventory() Inventory *getInventory()
{ {
return m_inventory; return m_inventory;
} }
private: private:
std::map<std::string, std::string> m_stringvars; StringMap m_stringvars;
Inventory *m_inventory; Inventory *m_inventory;
}; };

View File

@ -53,7 +53,7 @@ void ScriptApiMainMenu::handleMainMenuEvent(std::string text)
scriptError(); scriptError();
} }
void ScriptApiMainMenu::handleMainMenuButtons(std::map<std::string, std::string> fields) void ScriptApiMainMenu::handleMainMenuButtons(const StringMap &fields)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER
@ -69,8 +69,8 @@ void ScriptApiMainMenu::handleMainMenuButtons(std::map<std::string, std::string>
// Convert fields to a Lua table // Convert fields to a Lua table
lua_newtable(L); lua_newtable(L);
std::map<std::string, std::string>::const_iterator it; StringMap::const_iterator it;
for (it = fields.begin(); it != fields.end(); it++){ for (it = fields.begin(); it != fields.end(); ++it) {
const std::string &name = it->first; const std::string &name = it->first;
const std::string &value = it->second; const std::string &value = it->second;
lua_pushstring(L, name.c_str()); lua_pushstring(L, name.c_str());

View File

@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define S_MAINMENU_H_ #define S_MAINMENU_H_
#include "cpp_api/s_base.h" #include "cpp_api/s_base.h"
#include <map> #include "util/string.h"
class ScriptApiMainMenu class ScriptApiMainMenu
: virtual public ScriptApiBase : virtual public ScriptApiBase
@ -43,7 +43,7 @@ public:
* process field data recieved from formspec * process field data recieved from formspec
* @param fields data in field format * @param fields data in field format
*/ */
void handleMainMenuButtons(std::map<std::string, std::string> fields); void handleMainMenuButtons(const StringMap &fields);
}; };
#endif /* S_MAINMENU_H_ */ #endif /* S_MAINMENU_H_ */

View File

@ -200,7 +200,7 @@ bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
void ScriptApiNode::node_on_receive_fields(v3s16 p, void ScriptApiNode::node_on_receive_fields(v3s16 p,
const std::string &formname, const std::string &formname,
const std::map<std::string, std::string> &fields, const StringMap &fields,
ServerActiveObject *sender) ServerActiveObject *sender)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER
@ -220,8 +220,8 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
push_v3s16(L, p); // pos push_v3s16(L, p); // pos
lua_pushstring(L, formname.c_str()); // formname lua_pushstring(L, formname.c_str()); // formname
lua_newtable(L); // fields lua_newtable(L); // fields
std::map<std::string, std::string>::const_iterator it; StringMap::const_iterator it;
for (it = fields.begin(); it != fields.end(); it++){ for (it = fields.begin(); it != fields.end(); it++) {
const std::string &name = it->first; const std::string &name = it->first;
const std::string &value = it->second; const std::string &value = it->second;
lua_pushstring(L, name.c_str()); lua_pushstring(L, name.c_str());

View File

@ -20,11 +20,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef S_NODE_H_ #ifndef S_NODE_H_
#define S_NODE_H_ #define S_NODE_H_
#include <map>
#include "irr_v3d.h" #include "irr_v3d.h"
#include "cpp_api/s_base.h" #include "cpp_api/s_base.h"
#include "cpp_api/s_nodemeta.h" #include "cpp_api/s_nodemeta.h"
#include "util/string.h"
struct MapNode; struct MapNode;
class ServerActiveObject; class ServerActiveObject;
@ -47,7 +46,7 @@ public:
bool node_on_timer(v3s16 p, MapNode node, f32 dtime); bool node_on_timer(v3s16 p, MapNode node, f32 dtime);
void node_on_receive_fields(v3s16 p, void node_on_receive_fields(v3s16 p,
const std::string &formname, const std::string &formname,
const std::map<std::string, std::string> &fields, const StringMap &fields,
ServerActiveObject *sender); ServerActiveObject *sender);
void node_falling_update(v3s16 p); void node_falling_update(v3s16 p);
void node_falling_update_single(v3s16 p); void node_falling_update_single(v3s16 p);

View File

@ -83,7 +83,10 @@ bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
return positioning_handled_by_some; return positioning_handled_by_some;
} }
bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::string &reason) bool ScriptApiPlayer::on_prejoinplayer(
const std::string &name,
const std::string &ip,
std::string *reason)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER
@ -94,7 +97,7 @@ bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::st
lua_pushstring(L, ip.c_str()); lua_pushstring(L, ip.c_str());
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR); script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
if (lua_isstring(L, -1)) { if (lua_isstring(L, -1)) {
reason.assign(lua_tostring(L, -1)); reason->assign(lua_tostring(L, -1));
return true; return true;
} }
return false; return false;
@ -142,7 +145,7 @@ void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player, void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
const std::string &formname, const std::string &formname,
const std::map<std::string, std::string> &fields) const StringMap &fields)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER
@ -156,17 +159,19 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
lua_pushstring(L, formname.c_str()); lua_pushstring(L, formname.c_str());
// param 3 // param 3
lua_newtable(L); lua_newtable(L);
for(std::map<std::string, std::string>::const_iterator StringMap::const_iterator it;
i = fields.begin(); i != fields.end(); i++){ for (it = fields.begin(); it != fields.end(); ++it) {
const std::string &name = i->first; const std::string &name = it->first;
const std::string &value = i->second; const std::string &value = it->second;
lua_pushstring(L, name.c_str()); lua_pushstring(L, name.c_str());
lua_pushlstring(L, value.c_str(), value.size()); lua_pushlstring(L, value.c_str(), value.size());
lua_settable(L, -3); lua_settable(L, -3);
} }
script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC); script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
} }
ScriptApiPlayer::~ScriptApiPlayer() {
ScriptApiPlayer::~ScriptApiPlayer()
{
} }

View File

@ -20,10 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef S_PLAYER_H_ #ifndef S_PLAYER_H_
#define S_PLAYER_H_ #define S_PLAYER_H_
#include <map>
#include "cpp_api/s_base.h" #include "cpp_api/s_base.h"
#include "irr_v3d.h" #include "irr_v3d.h"
#include "util/string.h"
struct ToolCapabilities; struct ToolCapabilities;
@ -36,17 +35,16 @@ public:
void on_newplayer(ServerActiveObject *player); void on_newplayer(ServerActiveObject *player);
void on_dieplayer(ServerActiveObject *player); void on_dieplayer(ServerActiveObject *player);
bool on_respawnplayer(ServerActiveObject *player); bool on_respawnplayer(ServerActiveObject *player);
bool on_prejoinplayer(std::string name, std::string ip, std::string &reason); bool on_prejoinplayer(const std::string &name, const std::string &ip,
std::string *reason);
void on_joinplayer(ServerActiveObject *player); void on_joinplayer(ServerActiveObject *player);
void on_leaveplayer(ServerActiveObject *player); void on_leaveplayer(ServerActiveObject *player);
void on_cheat(ServerActiveObject *player, const std::string &cheat_type); void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
bool on_punchplayer(ServerActiveObject *player, bool on_punchplayer(ServerActiveObject *player,
ServerActiveObject *hitter, float time_from_last_punch, ServerActiveObject *hitter, float time_from_last_punch,
const ToolCapabilities *toolcap, v3f dir, s16 damage); const ToolCapabilities *toolcap, v3f dir, s16 damage);
void on_playerReceiveFields(ServerActiveObject *player, void on_playerReceiveFields(ServerActiveObject *player,
const std::string &formname, const std::string &formname, const StringMap &fields);
const std::map<std::string, std::string> &fields);
}; };

View File

@ -190,32 +190,34 @@ int NodeMetaRef::l_to_table(lua_State *L)
NodeMetaRef *ref = checkobject(L, 1); NodeMetaRef *ref = checkobject(L, 1);
NodeMetadata *meta = getmeta(ref, true); NodeMetadata *meta = getmeta(ref, true);
if(meta == NULL){ if (meta == NULL) {
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;
} }
lua_newtable(L); lua_newtable(L);
// fields // fields
lua_newtable(L); lua_newtable(L);
{ {
std::map<std::string, std::string> fields = meta->getStrings(); StringMap fields = meta->getStrings();
for(std::map<std::string, std::string>::const_iterator for (StringMap::const_iterator
i = fields.begin(); i != fields.end(); i++){ it = fields.begin(); it != fields.end(); ++it) {
const std::string &name = i->first; const std::string &name = it->first;
const std::string &value = i->second; const std::string &value = it->second;
lua_pushlstring(L, name.c_str(), name.size()); lua_pushlstring(L, name.c_str(), name.size());
lua_pushlstring(L, value.c_str(), value.size()); lua_pushlstring(L, value.c_str(), value.size());
lua_settable(L, -3); lua_settable(L, -3);
} }
} }
lua_setfield(L, -2, "fields"); lua_setfield(L, -2, "fields");
// inventory // inventory
lua_newtable(L); lua_newtable(L);
Inventory *inv = meta->getInventory(); Inventory *inv = meta->getInventory();
if(inv){ if (inv) {
std::vector<const InventoryList*> lists = inv->getLists(); std::vector<const InventoryList *> lists = inv->getLists();
for(std::vector<const InventoryList*>::const_iterator for(std::vector<const InventoryList *>::const_iterator
i = lists.begin(); i != lists.end(); i++){ i = lists.begin(); i != lists.end(); i++) {
push_inventory_list(L, inv, (*i)->getName().c_str()); push_inventory_list(L, inv, (*i)->getName().c_str());
lua_setfield(L, -2, (*i)->getName().c_str()); lua_setfield(L, -2, (*i)->getName().c_str());
} }

View File

@ -103,10 +103,8 @@ std::string getShaderPath(const std::string &name_of_shader,
class SourceShaderCache class SourceShaderCache
{ {
public: public:
void insert(const std::string &name_of_shader, void insert(const std::string &name_of_shader, const std::string &filename,
const std::string &filename, const std::string &program, bool prefer_local)
const std::string &program,
bool prefer_local)
{ {
std::string combined = name_of_shader + DIR_DELIM + filename; std::string combined = name_of_shader + DIR_DELIM + filename;
// Try to use local shader instead if asked to // Try to use local shader instead if asked to
@ -122,42 +120,43 @@ public:
} }
m_programs[combined] = program; m_programs[combined] = program;
} }
std::string get(const std::string &name_of_shader, std::string get(const std::string &name_of_shader,
const std::string &filename) const std::string &filename)
{ {
std::string combined = name_of_shader + DIR_DELIM + filename; std::string combined = name_of_shader + DIR_DELIM + filename;
std::map<std::string, std::string>::iterator n; StringMap::iterator n = m_programs.find(combined);
n = m_programs.find(combined); if (n != m_programs.end())
if(n != m_programs.end())
return n->second; return n->second;
return ""; return "";
} }
// Primarily fetches from cache, secondarily tries to read from filesystem // Primarily fetches from cache, secondarily tries to read from filesystem
std::string getOrLoad(const std::string &name_of_shader, std::string getOrLoad(const std::string &name_of_shader,
const std::string &filename) const std::string &filename)
{ {
std::string combined = name_of_shader + DIR_DELIM + filename; std::string combined = name_of_shader + DIR_DELIM + filename;
std::map<std::string, std::string>::iterator n; StringMap::iterator n = m_programs.find(combined);
n = m_programs.find(combined); if (n != m_programs.end())
if(n != m_programs.end())
return n->second; return n->second;
std::string path = getShaderPath(name_of_shader, filename); std::string path = getShaderPath(name_of_shader, filename);
if(path == ""){ if (path == "") {
infostream<<"SourceShaderCache::getOrLoad(): No path found for \"" infostream << "SourceShaderCache::getOrLoad(): No path found for \""
<<combined<<"\""<<std::endl; << combined << "\"" << std::endl;
return ""; return "";
} }
infostream<<"SourceShaderCache::getOrLoad(): Loading path \""<<path infostream << "SourceShaderCache::getOrLoad(): Loading path \""
<<"\""<<std::endl; << path << "\"" << std::endl;
std::string p = readFile(path); std::string p = readFile(path);
if(p != ""){ if (p != "") {
m_programs[combined] = p; m_programs[combined] = p;
return p; return p;
} }
return ""; return "";
} }
private: private:
std::map<std::string, std::string> m_programs; StringMap m_programs;
std::string readFile(const std::string &path) std::string readFile(const std::string &path)
{ {
std::ifstream is(path.c_str(), std::ios::binary); std::ifstream is(path.c_str(), std::ios::binary);
@ -274,23 +273,23 @@ public:
The id 0 points to a null shader. Its material is EMT_SOLID. The id 0 points to a null shader. Its material is EMT_SOLID.
*/ */
u32 getShaderIdDirect(const std::string &name, u32 getShaderIdDirect(const std::string &name,
const u8 material_type, const u8 drawtype); const u8 material_type, const u8 drawtype);
/* /*
If shader specified by the name pointed by the id doesn't If shader specified by the name pointed by the id doesn't
exist, create it, then return id. exist, create it, then return id.
Can be called from any thread. If called from some other thread Can be called from any thread. If called from some other thread
and not found in cache, the call is queued to the main thread and not found in cache, the call is queued to the main thread
for processing. for processing.
*/ */
u32 getShader(const std::string &name, u32 getShader(const std::string &name,
const u8 material_type, const u8 drawtype); const u8 material_type, const u8 drawtype);
ShaderInfo getShaderInfo(u32 id); ShaderInfo getShaderInfo(u32 id);
// Processes queued shader requests from other threads. // Processes queued shader requests from other threads.
// Shall be called from the main thread. // Shall be called from the main thread.
void processQueue(); void processQueue();
@ -391,7 +390,7 @@ ShaderSource::~ShaderSource()
} }
} }
u32 ShaderSource::getShader(const std::string &name, u32 ShaderSource::getShader(const std::string &name,
const u8 material_type, const u8 drawtype) const u8 material_type, const u8 drawtype)
{ {
/* /*
@ -435,7 +434,7 @@ u32 ShaderSource::getShader(const std::string &name,
/* /*
This method generates all the shaders This method generates all the shaders
*/ */
u32 ShaderSource::getShaderIdDirect(const std::string &name, u32 ShaderSource::getShaderIdDirect(const std::string &name,
const u8 material_type, const u8 drawtype) const u8 material_type, const u8 drawtype)
{ {
//infostream<<"getShaderIdDirect(): name=\""<<name<<"\""<<std::endl; //infostream<<"getShaderIdDirect(): name=\""<<name<<"\""<<std::endl;
@ -494,7 +493,7 @@ ShaderInfo ShaderSource::getShaderInfo(u32 id)
void ShaderSource::processQueue() void ShaderSource::processQueue()
{ {
} }
@ -572,7 +571,7 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype,
shaderinfo.base_material = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; shaderinfo.base_material = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
break; break;
} }
bool enable_shaders = g_settings->getBool("enable_shaders"); bool enable_shaders = g_settings->getBool("enable_shaders");
if(!enable_shaders) if(!enable_shaders)
return shaderinfo; return shaderinfo;
@ -648,7 +647,7 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype,
"NDT_FIRELIKE", "NDT_FIRELIKE",
"NDT_GLASSLIKE_FRAMED_OPTIONAL" "NDT_GLASSLIKE_FRAMED_OPTIONAL"
}; };
for (int i = 0; i < 14; i++){ for (int i = 0; i < 14; i++){
shaders_header += "#define "; shaders_header += "#define ";
shaders_header += drawTypes[i]; shaders_header += drawTypes[i];
@ -741,10 +740,10 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype,
shaders_header += "#define ENABLE_WAVING_LEAVES "; shaders_header += "#define ENABLE_WAVING_LEAVES ";
if (g_settings->getBool("enable_waving_leaves")) if (g_settings->getBool("enable_waving_leaves"))
shaders_header += "1\n"; shaders_header += "1\n";
else else
shaders_header += "0\n"; shaders_header += "0\n";
shaders_header += "#define ENABLE_WAVING_PLANTS "; shaders_header += "#define ENABLE_WAVING_PLANTS ";
if (g_settings->getBool("enable_waving_plants")) if (g_settings->getBool("enable_waving_plants"))
shaders_header += "1\n"; shaders_header += "1\n";
else else