/* core/cvar.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include "core/cvar.h" #include "sys/sys.h" #include #include #include #include #include namespace core { Cvar_t::Cvar_t(unsigned int cvarflags) { cvar_flags = cvarflags; } Cvar_t & Cvar_t::operator=(const std::string &other) { cvar_text = other; std::stringstream s(cvar_text); if (!(s >> cvar_value)) cvar_value = cvar_text.size(); return (*this); } Cvar_t & Cvar_t::operator=(const char *other) { return ((*this) = std::string(other)); } Cvar_t & Cvar_t::operator=(int other) { std::stringstream s; s << other; s >> cvar_text; cvar_value = (float) other; return (*this); } Cvar_t & Cvar_t::operator=(float other) { std::stringstream s; s << other; s >> cvar_text; cvar_value = other; return (*this); } unsigned int Cvar_t::flags() const { return(cvar_flags); } float Cvar_t::value() const { return(cvar_value); } const std::string &Cvar_t::text() const { return(cvar_text); } namespace cvar { std::map registry; Cvar get(const char *name, const char *value, int flags) { Cvar c = find(name); if (c) { con_debug << "cvar::get " << name << " already exist with value " << value << std::endl; } else { con_debug << "cvar::get " << name << " " << value << std::endl; c = new Cvar_t(flags); registry[std::string(name)] = c; (*c) = value; } return c; } Cvar get(const char *name, float value, int flags) { Cvar c = find(name); if (c) { con_debug << "cvar::get " << name << " already exist with value " << value << std::endl; } else { con_debug << "cvar::get " << name << " " << value << std::endl; c = new Cvar_t(flags); registry[std::string(name)] = c; (*c) = value; } return c; } Cvar set(const char *name, const char *value, int flags) { Cvar c = find(name); if (!c) { c = new Cvar_t(flags); registry[std::string(name)] = c; } con_debug << "cvar::set " << name << " " << value << std::endl; (*c) = value; return c; } Cvar set(const char *name, float value, int flags) { Cvar c = find(name); if (!c) { c = new Cvar_t(flags); registry[std::string(name)] = c; } con_debug << "cvar::set " << name << " " << value << std::endl; (*c) = value; return c; } void unset(const std::string &name) { Cvar c = find(name); if (c) { con_debug << "cvar::unset " << name << std::endl; registry.erase(name); delete c; } } void unset(const char *name) { unset(std::string(name)); } Cvar find(const std::string &name) { std::map::iterator it = registry.find(name); if (it == registry.end()) return 0; else return (*it).second; } Cvar find(const char *name) { return(find(std::string(name))); } void list() { con_print << "-- listcvar -----------------" << std::endl; std::map::iterator it; for (it = registry.begin(); it != registry.end(); it++) { con_print << " "<< (*it).first << " " << (*it).second->text() << std::endl; } } } // namespace cvar } // namespace core