From 5ddb64795cc959916eeedbec8dc3f65c06f49698 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Thu, 12 Nov 2009 20:53:35 +0000 Subject: initial commodities and entity inventory, bump network proto version to 18 --- src/core/info.cc | 111 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 94 insertions(+), 17 deletions(-) (limited to 'src/core/info.cc') diff --git a/src/core/info.cc b/src/core/info.cc index 745bc3b..4e5399a 100644 --- a/src/core/info.cc +++ b/src/core/info.cc @@ -12,14 +12,65 @@ namespace core { -Info::Registry Info::info_registry; +/* ---- class Info ------------------------------------------------- */ + +// default constructor +Info::Info() +{ + static_info_id_counter++; + info_id = static_info_id_counter; + + info_model = 0; + info_timestamp = 0; + + add(this); +} Info::Info(const std::string & label) { + static_info_id_counter++; + info_id = static_info_id_counter; + + info_timestamp = 0; + info_model = 0; + info_label.assign(label); aux::to_lowercase(info_label); aux::strip_quotes(info_label); + + add(this); +} + +Info::Info(const unsigned int id) +{ + info_id = id; + + info_timestamp = 0; + info_model = 0; + + add(this); +} + +Info::~Info() +{ + info_text.clear(); + info_label.clear(); + info_modelname.clear(); + info_text.clear(); info_timestamp = 0; + info_model = 0; +} + +void Info::set_label(const std::string & label) +{ + info_label.assign(label); + aux::to_label(info_label); +} + +void Info::set_label(const char *label) +{ + info_label.assign(label); + aux::to_label(info_label); } void Info::set_name(const std::string & name) @@ -42,6 +93,11 @@ void Info::set_modelname(const char *modelname) info_modelname.assign(modelname); } +void Info::set_model(const model::Model *model) +{ + info_model = model; +} + void Info::set_timestamp(const unsigned long timestamp) { info_timestamp = timestamp; @@ -71,7 +127,7 @@ void Info::clear_text() void Info::serialize_server_update(std::ostream & os) const { - os << '"' << name() << "\" \"" << modelname() << "\" " << info_text.size() << " "; + os << '"' << label() << "\" \"" << name() << "\" \"" << modelname() << "\" " << info_text.size() << " "; for (Text::const_iterator it = info_text.begin(); it != info_text.end(); it++) { if (it != info_text.begin()) @@ -86,6 +142,12 @@ void Info::receive_server_update(std::istream &is) std::string n; char c; + // read label + while ((is.get(c)) && (c != '"')); + while ((is.get(c)) && (c != '"')) + n += c; + info_label.assign(n); + // read name while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) @@ -114,13 +176,6 @@ void Info::receive_server_update(std::istream &is) } } -Info::~Info() -{ - info_text.clear(); - info_modelname.clear(); - info_text.clear(); -} - void Info::print() const { con_print << "label: ^B" << label() << " ^Nname: ^B" << name() << " ^Nmodel: ^B" << modelname() << "^N" << std::endl; @@ -130,20 +185,24 @@ void Info::print() const } } + /* ---- static info registry --------------------------------------- */ +Info::Registry Info::info_registry; +unsigned int Info::static_info_id_counter = 0; + void Info::add(Info *info) { - if (find(info->label())) - return; - - info_registry[info->label()] = info; + info_registry.push_back(info); } Info *Info::find(const char *label) { + if (!label) + return 0; + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { - Info *info = (*it).second; + Info *info = (*it); if (info->label().compare(label) == 0) { return info; } @@ -153,8 +212,11 @@ Info *Info::find(const char *label) Info *Info::find(const std::string & label) { + if (!label.size()) + return 0; + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { - Info *info = (*it).second; + Info *info = (*it); if (info->label().compare(label) == 0) { return info; } @@ -162,19 +224,34 @@ Info *Info::find(const std::string & label) return 0; } +Info *Info::find(const unsigned int id) +{ + if (!id) + return 0; + + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { + Info *info = (*it); + if (info->id() == id) { + return info; + } + } + return 0; +} + void Info::clear() { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { - Info *info = (*it).second;; + Info *info = (*it); delete info; } info_registry.clear(); + static_info_id_counter = 0; } void Info::list() { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { - Info *info = (*it).second;; + Info *info = (*it); con_print << info->label() << std::endl; } con_print << info_registry.size() << " registered info " << aux::plural("record", info_registry.size()) << std::endl; -- cgit v1.2.3