Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 4e0cb53918699c2aae4938a6e07c1857406ae491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
   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::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;

	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 info " << aux::plural("record", registry.size()) << std::endl;
}

}