From b82d3f274458c691c35c826dac0a8c3b1fd8c2a5 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 23 Feb 2011 00:06:45 +0200 Subject: [PATCH] Fixed insufficient error handling in narrow_to_wide, which caused a crash if the path of the userdata directory contained special characters that were not included in the used charset (which is used because of the font) --- src/utility.h | 81 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/src/utility.h b/src/utility.h index 8f34c871..2b01dedf 100644 --- a/src/utility.h +++ b/src/utility.h @@ -613,11 +613,15 @@ inline v3s16 arealim(v3s16 p, s16 d) return p; } +// NOTE: There seems to be some problem with this on some systems: +// http://pastebin.com/vwJP2GZ6 inline std::wstring narrow_to_wide(const std::string& mbs) { size_t wcl = mbs.size(); - SharedBuffer wcs(wcl+1); + Buffer wcs(wcl+1); size_t l = mbstowcs(*wcs, mbs.c_str(), wcl); + if(l == (size_t)(-1)) + return L""; wcs[l] = 0; return *wcs; } @@ -627,7 +631,7 @@ inline std::string wide_to_narrow(const std::wstring& wcs) size_t mbl = wcs.size()*4; SharedBuffer mbs(mbl+1); size_t l = wcstombs(*mbs, wcs.c_str(), mbl); - if((int)l == -1) + if(l == (size_t)(-1)) mbs[0] = 0; else mbs[l] = 0; @@ -1312,7 +1316,7 @@ private: }; /* - FIFO queue + FIFO queue (well, actually a FILO also) */ template class Queue @@ -1326,13 +1330,23 @@ public: T pop_front() { if(m_list.size() == 0) - throw ItemNotFoundException("MutexedQueue: queue is empty"); + throw ItemNotFoundException("Queue: queue is empty"); typename core::list::Iterator begin = m_list.begin(); T t = *begin; m_list.erase(begin); return t; } + T pop_back() + { + if(m_list.size() == 0) + throw ItemNotFoundException("Queue: queue is empty"); + + typename core::list::Iterator last = m_list.getLast(); + T t = *last; + m_list.erase(last); + return t; + } u32 size() { @@ -1344,7 +1358,7 @@ protected: }; /* - Thread-safe FIFO queue + Thread-safe FIFO queue (well, actually a FILO also) */ template @@ -1390,6 +1404,32 @@ public: wait_time_ms += 10; } } + T pop_back(u32 wait_time_max_ms=0) + { + u32 wait_time_ms = 0; + + for(;;) + { + { + JMutexAutoLock lock(m_mutex); + + if(m_list.size() > 0) + { + typename core::list::Iterator last = m_list.getLast(); + T t = *last; + m_list.erase(last); + return t; + } + + if(wait_time_ms >= wait_time_max_ms) + throw ItemNotFoundException("MutexedQueue: queue is empty"); + } + + // Wait a while before trying again + sleep_ms(10); + wait_time_ms += 10; + } + } JMutex & getMutex() { @@ -1838,5 +1878,36 @@ inline std::string deSerializeLongString(std::istream &is) return s; } +// + +inline u32 time_to_daynight_ratio(u32 time_of_day) +{ + const s32 daylength = 16; + const s32 nightlength = 6; + const s32 daytimelength = 8; + s32 d = daylength; + s32 t = (((time_of_day)%24000)/(24000/d)); + if(t < nightlength/2 || t >= d - nightlength/2) + return 300; + else if(t >= d/2 - daytimelength/2 && t < d/2 + daytimelength/2) + return 1000; + else + return 750; +} + +// Random helper. Usually d=BS +inline core::aabbox3d getNodeBox(v3s16 p, float d) +{ + return core::aabbox3d( + (float)p.X * d - 0.5*d, + (float)p.Y * d - 0.5*d, + (float)p.Z * d - 0.5*d, + (float)p.X * d + 0.5*d, + (float)p.Y * d + 0.5*d, + (float)p.Z * d + 0.5*d + ); +} + + #endif