/* 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" #include namespace core { /* ---- class InfoType --------------------------------------------- */ // server-side constructor, assigns id InfoType::InfoType(const char *label) { infotype_id_counter++; infotype_id = infotype_id_counter; if (label) set_label(label); infotype_registry.push_back(this); } // client-side constructor, receives id InfoType::InfoType(const unsigned int id) { infotype_id = id; infotype_registry.push_back(this); } InfoType::~InfoType() { infotype_id = 0; } /* ---- static InfoType registry ----------------------------------- */ InfoType::Registry InfoType::infotype_registry; unsigned int InfoType::infotype_id_counter = 0; void InfoType::clear() { for (Registry::iterator it = infotype_registry.begin(); it != infotype_registry.end(); it++) { InfoType *infotype = (*it); delete infotype; } infotype_registry.clear(); infotype_id_counter = 0; } InfoType *InfoType::find(const std::string & label) { if (!label.size()) return 0; for (Registry::iterator it = infotype_registry.begin(); it != infotype_registry.end(); it++) { InfoType *infotype = (*it); if (infotype->label().compare(label) == 0) { return infotype; } } return 0; } InfoType *InfoType::find(const unsigned int id) { if (!id) return 0; for (Registry::iterator it = infotype_registry.begin(); it != infotype_registry.end(); it++) { InfoType *infotype = (*it); if (infotype->id() == id) { return infotype; } } return 0; } /* ---- class Info ------------------------------------------------- */ // server-side constructor, assigns id Info::Info() { info_id_counter++; info_id = info_id_counter; info_type = 0; info_model = 0; info_timestamp = 0; info_price = 0; info_registry.push_back(this); } // client-side constructor, receives id Info::Info(const unsigned int id) { info_id = id; info_type = 0; info_timestamp = 0; info_model = 0; info_price = 0; info_registry.push_back(this); } Info::~Info() { info_text.clear(); info_modelname.clear(); info_text.clear(); info_timestamp = 0; info_model = 0; } void Info::set_type(const InfoType *type) { info_type = type; } 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_model(const model::Model *model) { info_model = model; } void Info::set_price(const long price) { info_price = price; } 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 << " \"" << label() << "\" \"" << 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 label while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; set_label(n); // read name while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; set_name(n); // read model name n.clear(); while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; set_modelname(n); // read info text clear_text(); size_t s; 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; add_text(n); } } void Info::print() const { if (info_text.size()) { for (Text::const_iterator it = info_text.begin(); it != info_text.end(); it++) { con_print << " " << (*it) << std::endl; } } else { con_print << " label: ^B" << label() << " ^Nname: ^B" << name() << " ^Nmodel: ^B" << modelname() << "^N" << std::endl; } } /* ---- static info registry --------------------------------------- */ Info::Registry Info::info_registry; unsigned int Info::info_id_counter = 0; 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); if (info->label().compare(label) == 0) { return info; } } return 0; } 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); if (info->label().compare(label) == 0) { return info; } } 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; } Info *Info::search(const std::string & searchstr) { if (!searchstr.size()) return 0; std::string strsearchkey(aux::lowercase(searchstr)); if (strsearchkey.size() < 3) { return 0; } for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = *(it); std::string labelstr(info->label()); if (labelstr.size() && (labelstr.find(strsearchkey) != std::string::npos)) { return info; } std::string namestr(aux::lowercase(info->name())); if (namestr.size() && (namestr.find(strsearchkey) != std::string::npos)) { return info; } } return 0; } Info *Info::find(const InfoType *type, const char *label) { if (!label) return 0; for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = (*it); if ((info->type() == type) && (info->label().compare(label) == 0)) { return info; } } return 0; } Info *Info::find(const InfoType *type, const std::string & label) { if (!label.size()) return 0; for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = (*it); if ((info->type() == type) && (info->label().compare(label) == 0)) { return info; } } return 0; } Info *Info::find(const InfoType *type, 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->type() == type) && (info->id() == id)) { return info; } } return 0; } Info *Info::search(const InfoType *type, const std::string & searchstr) { if (!searchstr.size()) return 0; std::string strsearchkey(aux::lowercase(searchstr)); if (strsearchkey.size() < 3) { return 0; } for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = *(it); if (info->type() == type) { std::string labelstr(info->label()); if (labelstr.size() && (labelstr.find(strsearchkey) != std::string::npos)) { return info; } std::string namestr(aux::lowercase(info->name())); if (namestr.size() && (namestr.find(strsearchkey) != std::string::npos)) { return info; } } } return 0; } void Info::clear() { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = (*it); delete info; } info_registry.clear(); info_id_counter = 0; InfoType::clear(); } void Info::list() { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = (*it); con_print << " " << "^B" << aux::pad_right(info->type()->label(), 8) << "^N " << "^B" << aux::pad_right(info->label(), 12) << "^N " << info->name() << "^N" << std::endl; } con_print << info_registry.size() << " info " << aux::plural("record", info_registry.size()) << std::endl; } void Info::list_class(const InfoType *type) { size_t count = 0; for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { core::Info *info = (*it); if (info->type() == type) { count++; con_print << " " << "^B" << aux::pad_right(info->label(), 12) << "^N " << info->name() << "^N" << std::endl; } } con_print << count << " info " << aux::plural("record", count) << std::endl; } }