From 64b59757322e29c331c0a75262baec4382673e6f Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 28 Jan 2011 01:38:16 +0200 Subject: Now texture handling is fast. Also now players are saved on disk. --- src/utility.h | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 174 insertions(+), 13 deletions(-) (limited to 'src/utility.h') diff --git a/src/utility.h b/src/utility.h index b517848b1..4f74a0649 100644 --- a/src/utility.h +++ b/src/utility.h @@ -760,17 +760,20 @@ class Settings { public: - // Returns false on EOF - bool parseConfigObject(std::istream &is) + void writeLines(std::ostream &os) { - if(is.eof()) - return false; - - // NOTE: This function will be expanded to allow multi-line settings - std::string line; - std::getline(is, line); - //dstream<<"got line: \""<::Iterator + i = m_settings.getIterator(); + i.atEnd() == false; i++) + { + std::string name = i.getNode()->getKey(); + std::string value = i.getNode()->getValue(); + os<>f; - return f; + return stof(get(name)); } u16 getU16(std::string name) @@ -1128,6 +1145,34 @@ public: return stoi(get(name)); } + v3f getV3F(std::string name) + { + v3f value; + Strfnd f(get(name)); + f.next("("); + value.X = stof(f.next(",")); + value.Y = stof(f.next(",")); + value.Z = stof(f.next(")")); + return value; + } + + void setS32(std::string name, s32 value) + { + set(name, itos(value)); + } + + void setFloat(std::string name, float value) + { + set(name, ftos(value)); + } + + void setV3F(std::string name, v3f value) + { + std::ostringstream os; + os<<"("< m_list; }; +#if 0 +template +class MutexedCache +{ +public: + MutexedCache() + { + m_mutex.Init(); + assert(m_mutex.IsInitialized()); + } + + void set(const Key &name, const Value &value) + { + JMutexAutoLock lock(m_mutex); + + m_values[name] = value; + } + + bool get(const Key &name, Value *result) + { + JMutexAutoLock lock(m_mutex); + + typename core::map::Node *n; + n = m_values.find(name); + + if(n == NULL) + return false; + + *result = n->getValue(); + return true; + } + +private: + core::map m_values; + JMutex m_mutex; +}; +#endif + +/* + Generates ids for comparable values. + Id=0 is reserved for "no value". + + Is fast at: + - Returning value by id (very fast) + - Returning id by value + - Generating a new id for a value + + Is not able to: + - Remove an id/value pair (is possible to implement but slow) +*/ +template +class MutexedIdGenerator +{ +public: + MutexedIdGenerator() + { + m_mutex.Init(); + assert(m_mutex.IsInitialized()); + } + + // Returns true if found + bool getValue(u32 id, T &value) + { + if(id == 0) + return false; + JMutexAutoLock lock(m_mutex); + if(m_id_to_value.size() < id) + return false; + value = m_id_to_value[id-1]; + return true; + } + + // If id exists for value, returns the id. + // Otherwise generates an id for the value. + u32 getId(const T &value) + { + JMutexAutoLock lock(m_mutex); + typename core::map::Node *n; + n = m_value_to_id.find(value); + if(n != NULL) + return n->getValue(); + m_id_to_value.push_back(value); + u32 new_id = m_id_to_value.size(); + m_value_to_id.insert(value, new_id); + return new_id; + } + +private: + JMutex m_mutex; + // Values are stored here at id-1 position (id 1 = [0]) + core::array m_id_to_value; + core::map m_value_to_id; +}; + +/* + Checks if a string contains only supplied characters +*/ +inline bool string_allowed(const std::string &s, const std::string &allowed_chars) +{ + for(u32 i=0; i