/* 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); } namespace cvar { std::map cvarregistry; Cvar get(const char *name, const char *value, int flags) { Cvar c = cvarregistry[std::string(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); cvarregistry[std::string(name)] = c; (*c) = value; } return c; } Cvar get(const char *name, int value, int flags) { Cvar c = cvarregistry[std::string(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); cvarregistry[std::string(name)] = c; (*c) = value; } return c; } Cvar set(const char *name, const char *value, int flags) { Cvar c = cvarregistry[std::string(name)]; if (!c) { c = new Cvar_t(flags); cvarregistry[std::string(name)] = c; } con_debug << "cvar::set " << name << " " << value << std::endl; (*c) = value; return c; } Cvar set(const char *name, int value, int flags) { Cvar c = cvarregistry[std::string(name)]; if (!c) { c = new Cvar_t(flags); cvarregistry[std::string(name)] = c; } con_debug << "cvar::set " << name << " " << value << std::endl; (*c) = value; return c; } void unset(const char *cvarname) { Cvar c = cvarregistry[std::string(cvarname)]; if (c) { con_debug << "cvar::unset " << cvarname << std::endl; cvarregistry.erase(std::string(cvarname)); delete c; } } void unset(const std::string &cvarname) { Cvar c = cvarregistry[cvarname]; if (c) { con_debug << "cvar::unset " << cvarname << std::endl; cvarregistry.erase(cvarname); delete c; } } Cvar find(const std::string &cvarname) { return cvarregistry[cvarname]; } Cvar find(const char *cvarname) { return cvarregistry[std::string(cvarname)]; } void list() { con_print << "-- listcvar -----------------" << std::endl; std::map::iterator cvarregistryiterator; for (cvarregistryiterator = cvarregistry.begin(); cvarregistryiterator != cvarregistry.end(); cvarregistryiterator++) { con_print << " "<< (*cvarregistryiterator).first << " " << (*cvarregistryiterator).second->text() << std::endl; } } } // namespace cvar } // namespace core