Skip to content

Commit

Permalink
Replace instances of std::map<std::string, std::string> with StringMap
Browse files Browse the repository at this point in the history
Also, clean up surrounding code style
Replace by-value parameter passing with const refs when possible
Fix post-increment of iterators
  • Loading branch information
kwolekr committed May 19, 2015
1 parent 603297c commit da34a2b
Show file tree
Hide file tree
Showing 25 changed files with 180 additions and 193 deletions.
49 changes: 20 additions & 29 deletions src/ban.cpp
Expand Up @@ -56,7 +56,7 @@ void BanManager::load()
infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
throw SerializationError("BanManager::load(): Couldn't open file");
}

while(!is.eof() && is.good())
{
std::string line;
Expand All @@ -74,18 +74,14 @@ void BanManager::load()
void BanManager::save()
{
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);

for(std::map<std::string, std::string>::iterator
i = m_ips.begin();
i != m_ips.end(); i++)
{
ss << i->first << "|" << i->second << "\n";
}
for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it)
ss << it->first << "|" << it->second << "\n";

if(!fs::safeWriteToFile(m_banfilepath, ss.str())) {
infostream<<"BanManager: failed saving to "<<m_banfilepath<<std::endl;
if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
throw SerializationError("BanManager::save(): Couldn't write file");
}

Expand All @@ -102,25 +98,23 @@ std::string BanManager::getBanDescription(const std::string &ip_or_name)
{
JMutexAutoLock lock(m_mutex);
std::string s = "";
for(std::map<std::string, std::string>::iterator
i = m_ips.begin();
i != m_ips.end(); i++)
{
if(i->first == ip_or_name || i->second == ip_or_name
|| ip_or_name == "")
s += i->first + "|" + i->second + ", ";
for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it) {
if (it->first == ip_or_name || it->second == ip_or_name
|| ip_or_name == "") {
s += it->first + "|" + it->second + ", ";
}
}
s = s.substr(0, s.size()-2);
s = s.substr(0, s.size() - 2);
return s;
}

std::string BanManager::getBanName(const std::string &ip)
{
JMutexAutoLock lock(m_mutex);
std::map<std::string, std::string>::iterator i = m_ips.find(ip);
if(i == m_ips.end())
StringMap::iterator it = m_ips.find(ip);
if (it == m_ips.end())
return "";
return i->second;
return it->second;
}

void BanManager::add(const std::string &ip, const std::string &name)
Expand All @@ -133,19 +127,16 @@ void BanManager::add(const std::string &ip, const std::string &name)
void BanManager::remove(const std::string &ip_or_name)
{
JMutexAutoLock lock(m_mutex);
for(std::map<std::string, std::string>::iterator
i = m_ips.begin();
i != m_ips.end();)
{
if((i->first == ip_or_name) || (i->second == ip_or_name)) {
m_ips.erase(i++);
for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
m_ips.erase(it++);
} else {
++i;
++it;
}
}
m_modified = true;
}


bool BanManager::isModified()
{
Expand Down
5 changes: 2 additions & 3 deletions src/ban.h
Expand Up @@ -20,8 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef BAN_HEADER
#define BAN_HEADER

#include <map>
#include <string>
#include "util/string.h"
#include "jthread/jthread.h"
#include "jthread/jmutex.h"
#include "exceptions.h"
Expand All @@ -43,7 +42,7 @@ class BanManager
private:
JMutex m_mutex;
std::string m_banfilepath;
std::map<std::string, std::string> m_ips;
StringMap m_ips;
bool m_modified;

};
Expand Down
31 changes: 15 additions & 16 deletions src/client.cpp
Expand Up @@ -1102,7 +1102,7 @@ void Client::sendRemovedSounds(std::vector<s32> &soundList)
}

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();

Expand All @@ -1112,10 +1112,10 @@ void Client::sendNodemetaFields(v3s16 p, const std::string &formname,

pkt << p << formname << (u16) (fields_size & 0xFFFF);

for(std::map<std::string, std::string>::const_iterator
i = fields.begin(); i != fields.end(); i++) {
const std::string &name = i->first;
const std::string &value = i->second;
StringMap::const_iterator it;
for (it = fields.begin(); it != fields.end(); ++it) {
const std::string &name = it->first;
const std::string &value = it->second;
pkt << name;
pkt.putLongString(value);
}
Expand All @@ -1124,18 +1124,18 @@ void Client::sendNodemetaFields(v3s16 p, 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();
FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of inventory fields");

NetworkPacket pkt(TOSERVER_INVENTORY_FIELDS, 0);
pkt << formname << (u16) (fields_size & 0xFFFF);

for(std::map<std::string, std::string>::const_iterator
i = fields.begin(); i != fields.end(); i++) {
const std::string &name = i->first;
const std::string &value = i->second;
StringMap::const_iterator it;
for (it = fields.begin(); it != fields.end(); ++it) {
const std::string &name = it->first;
const std::string &value = it->second;
pkt << name;
pkt.putLongString(value);
}
Expand Down Expand Up @@ -1918,14 +1918,13 @@ ParticleManager* Client::getParticleManager()

scene::IAnimatedMesh* Client::getMesh(const std::string &filename)
{
std::map<std::string, std::string>::const_iterator i =
m_mesh_data.find(filename);
if(i == m_mesh_data.end()){
errorstream<<"Client::getMesh(): Mesh not found: \""<<filename<<"\""
<<std::endl;
StringMap::const_iterator it = m_mesh_data.find(filename);
if (it == m_mesh_data.end()) {
errorstream << "Client::getMesh(): Mesh not found: \"" << filename
<< "\"" << std::endl;
return NULL;
}
const std::string &data = i->second;
const std::string &data = it->second;
scene::ISceneManager *smgr = m_device->getSceneManager();

// Create the mesh, remove it from cache and return it
Expand Down
8 changes: 4 additions & 4 deletions src/client.h
Expand Up @@ -405,13 +405,13 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
void interact(u8 action, const PointedThing& pointed);

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,
const std::map<std::string, std::string> &fields);
const StringMap &fields);
void sendInventoryAction(InventoryAction *a);
void sendChatMessage(const std::wstring &message);
void sendChangePassword(const std::string &oldpassword,
const std::string &newpassword);
const std::string &newpassword);
void sendDamage(u8 damage);
void sendBreath(u16 breath);
void sendRespawn();
Expand Down Expand Up @@ -648,7 +648,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
std::map<std::string, Inventory*> m_detached_inventories;

// 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
LocalClientState m_state;
Expand Down
11 changes: 5 additions & 6 deletions src/content_mapnode.cpp
Expand Up @@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapnode.h"
#include "nodedef.h"
#include "nameidmapping.h"
#include <map>
#include "util/string.h"

/*
Legacy node content type IDs
Expand Down Expand Up @@ -218,14 +218,13 @@ class NewNameGetter
}
std::string get(const std::string &old)
{
std::map<std::string, std::string>::const_iterator i;
i = old_to_new.find(old);
if(i == old_to_new.end())
StringMap::const_iterator it = old_to_new.find(old);
if (it == old_to_new.end())
return "";
return i->second;
return it->second;
}
private:
std::map<std::string, std::string> old_to_new;
StringMap old_to_new;
};

NewNameGetter newnamegetter;
Expand Down
26 changes: 14 additions & 12 deletions src/game.cpp
Expand Up @@ -87,11 +87,11 @@ struct TextDestNodeMetadata : public TextDest {
std::string ntext = wide_to_narrow(text);
infostream << "Submitting 'text' field of node at (" << m_p.X << ","
<< m_p.Y << "," << m_p.Z << "): " << ntext << std::endl;
std::map<std::string, std::string> fields;
StringMap fields;
fields["text"] = ntext;
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);
}
Expand All @@ -111,7 +111,7 @@ struct TextDestPlayerInventory : public TextDest {
m_client = client;
m_formname = formname;
}
void gotText(std::map<std::string, std::string> fields)
void gotText(const StringMap &fields)
{
m_client->sendInventoryFields(m_formname, fields);
}
Expand All @@ -138,7 +138,7 @@ struct LocalFormspecHandler : public TextDest {
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 (fields.find("btn_sound") != fields.end()) {
Expand Down Expand Up @@ -180,9 +180,9 @@ struct LocalFormspecHandler : public TextDest {

if ((fields.find("btn_send") != fields.end()) ||
(fields.find("quit") != fields.end())) {
if (fields.find("f_text") != fields.end()) {
m_client->typeChatMessage(narrow_to_wide(fields["f_text"]));
}
StringMap::const_iterator it = fields.find("f_text");
if (it != fields.end())
m_client->typeChatMessage(narrow_to_wide(it->second));

return;
}
Expand Down Expand Up @@ -210,12 +210,14 @@ struct LocalFormspecHandler : public TextDest {
return;
}

errorstream << "LocalFormspecHandler::gotText unhandled >" << m_formname << "< event" << std::endl;
int i = 0;
errorstream << "LocalFormspecHandler::gotText unhandled >"
<< m_formname << "< event" << std::endl;

for (std::map<std::string, std::string>::iterator iter = fields.begin();
iter != fields.end(); iter++) {
errorstream << "\t" << i << ": " << iter->first << "=" << iter->second << std::endl;
int i = 0;
StringMap::const_iterator it;
for (it = fields.begin(); it != fields.end(); ++it) {
errorstream << "\t" << i << ": " << it->first
<< "=" << it->second << std::endl;
i++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/guiEngine.cpp
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/guiEngine.h
Expand Up @@ -73,7 +73,7 @@ class TextDestGuiEngine : public TextDest
* receive fields transmitted by guiFormSpecMenu
* @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
Expand Down
2 changes: 1 addition & 1 deletion src/guiFileSelectMenu.cpp
Expand Up @@ -84,7 +84,7 @@ void GUIFileSelectMenu::drawMenu()

void GUIFileSelectMenu::acceptInput() {
if ((m_text_dst != 0) && (this->m_formname != "")){
std::map<std::string, std::string> fields;
StringMap fields;

if (m_accepted)
fields[m_formname + "_accepted"] = wide_to_narrow(m_fileOpenDialog->getFileName());
Expand Down
2 changes: 1 addition & 1 deletion src/guiFormSpecMenu.cpp
Expand Up @@ -2579,7 +2579,7 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
{
if(m_text_dst)
{
std::map<std::string, std::string> fields;
StringMap fields;

if (quitmode == quit_mode_accept) {
fields["quit"] = "true";
Expand Down
2 changes: 1 addition & 1 deletion src/guiFormSpecMenu.h
Expand Up @@ -56,7 +56,7 @@ struct TextDest
virtual ~TextDest() {};
// This is deprecated I guess? -celeron55
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)
{ m_formname = formname;};

Expand Down
9 changes: 3 additions & 6 deletions src/httpfetch.cpp
Expand Up @@ -266,8 +266,7 @@ HTTPFetchOngoing::HTTPFetchOngoing(HTTPFetchRequest request_, CurlHandlePool *po
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
} else if (request.multipart) {
curl_httppost *last = NULL;
for (std::map<std::string, std::string>::iterator it =
request.post_fields.begin();
for (StringMap::iterator it = request.post_fields.begin();
it != request.post_fields.end(); ++it) {
curl_formadd(&post, &last,
CURLFORM_NAMELENGTH, it->first.size(),
Expand All @@ -282,10 +281,8 @@ HTTPFetchOngoing::HTTPFetchOngoing(HTTPFetchRequest request_, CurlHandlePool *po
} else if (request.post_data.empty()) {
curl_easy_setopt(curl, CURLOPT_POST, 1);
std::string str;
for (std::map<std::string, std::string>::iterator it =
request.post_fields.begin();
it != request.post_fields.end();
++it) {
for (StringMap::iterator it = request.post_fields.begin();
it != request.post_fields.end(); ++it) {
if (str != "")
str += "&";
str += urlencode(it->first);
Expand Down
5 changes: 2 additions & 3 deletions src/httpfetch.h
Expand Up @@ -20,9 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef HTTPFETCH_HEADER
#define HTTPFETCH_HEADER

#include <string>
#include <vector>
#include <map>
#include "util/string.h"
#include "config.h"

// Can be used in place of "caller" in asynchronous transfers to discard result
Expand Down Expand Up @@ -54,7 +53,7 @@ struct HTTPFetchRequest

// POST fields. Fields are escaped properly.
// 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.
std::string post_data;
Expand Down

0 comments on commit da34a2b

Please sign in to comment.