/* core/info.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include "auxiliary/functions.h" #include "core/info.h" #include "sys/sys.h" #include "core/info.h" namespace core { Info::Registry Info::info_registry; Info::Info(const std::string & label) { info_label.assign(label); aux::to_lowercase(info_label); aux::strip_quotes(info_label); info_timestamp = 0; } void Info::set_name(const std::string & name) { info_name.assign(name); } void Info::set_name(const char *name) { info_name.assign(name); } void Info::set_modelname(const std::string & modelname) { info_modelname.assign(modelname); } void Info::set_modelname(const char *modelname) { info_modelname.assign(modelname); } void Info::set_timestamp(const unsigned long timestamp) { info_timestamp = timestamp; } void Info::clear_timestamp() { info_timestamp = 0; } void Info::add_text(const char *text) { std::string str(text); aux::strip_quotes(str); info_text.push_back(str); } void Info::add_text(const std::string & text) { add_text(text.c_str()); } void Info::clear_text() { info_text.clear(); } void Info::serialize_server_update(std::ostream & os) const { os << '"' << name() << "\" \"" << modelname() << "\" " << info_text.size() << " "; for (Text::const_iterator it = info_text.begin(); it != info_text.end(); it++) { if (it != info_text.begin()) os << ' '; os << '"' << (*it) << '"'; } } void Info::receive_server_update(std::istream &is) { std::string n; char c; // read name while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; info_name.assign(n); // read model name n.clear(); while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; info_modelname.assign(n); // read info text size_t s; info_text.clear(); is >> s; for (size_t i = 0; (i < s) && is.good(); i++) { n.clear(); while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; info_text.push_back(n); } } 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; for (Text::const_iterator it = info_text.begin(); it != info_text.end(); it++) { con_print << " " << (*it) << std::endl; } } /* ---- static info registry --------------------------------------- */ void Info::add(Info *info) { if (find(info->label())) return; info_registry[info->label()] = info; } Info *Info::find(const char *label) { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = (*it).second; if (info->label().compare(label) == 0) { return info; } } return 0; } Info *Info::find(const std::string & label) { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = (*it).second; if (info->label().compare(label) == 0) { return info; } } return 0; } void Info::clear() { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = (*it).second;; delete info; } info_registry.clear(); } void Info::list() { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = (*it).second;; con_print << info->label() << std::endl; } con_print << info_registry.size() << " registered info " << aux::plural("record", info_registry.size()) << std::endl; } }