Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/info.cc')
-rw-r--r--src/core/info.cc183
1 files changed, 183 insertions, 0 deletions
diff --git a/src/core/info.cc b/src/core/info.cc
new file mode 100644
index 0000000..fc10fe5
--- /dev/null
+++ b/src/core/info.cc
@@ -0,0 +1,183 @@
+/*
+ core/info.xx
+ 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"
+
+namespace core
+{
+
+Info::Registry 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() << "^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;
+
+ registry[info->label()] = info;
+}
+
+Info *Info::find(const char *label)
+{
+ for (Registry::iterator it = registry.begin(); it != 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 = registry.begin(); it != registry.end(); it++) {
+ Info *info = (*it).second;
+ if (info->label().compare(label) == 0) {
+ return info;
+ }
+ }
+ return 0;
+}
+
+void Info::clear()
+{
+ for (Registry::iterator it = registry.begin(); it != registry.end(); it++) {
+ Info *info = (*it).second;;
+ delete info;
+ }
+ registry.clear();
+}
+
+void Info::list()
+{
+ for (Registry::iterator it = registry.begin(); it != registry.end(); it++) {
+ Info *info = (*it).second;;
+ con_print << info->label() << std::endl;
+ }
+ con_print << registry.size() << " registered infos" << std::endl;
+}
+
+}
+