From: <Rel...@us...> - 2007-01-29 21:50:48
|
Revision: 171 http://svn.sourceforge.net/modplug/?rev=171&view=rev Author: Relabsoluness Date: 2007-01-29 13:50:16 -0800 (Mon, 29 Jan 2007) Log Message: ----------- <Relabs> Added missing misc_util.cpp and misc_util.h files. Added Paths: ----------- branches/OpenMPT_MPTm_Tuning/mptrack/misc_util.cpp branches/OpenMPT_MPTm_Tuning/mptrack/misc_util.h Added: branches/OpenMPT_MPTm_Tuning/mptrack/misc_util.cpp =================================================================== --- branches/OpenMPT_MPTm_Tuning/mptrack/misc_util.cpp (rev 0) +++ branches/OpenMPT_MPTm_Tuning/mptrack/misc_util.cpp 2007-01-29 21:50:16 UTC (rev 171) @@ -0,0 +1,44 @@ +#include "stdafx.h" +#include "misc_util.h" + + +bool StringToBinaryStream(ostream& outStream, const string& str) +//---------------------------------------------------- +{ + if(!outStream.good()) return true; + size_t size = str.size(); + outStream.write(reinterpret_cast<char*>(&size), sizeof(size)); + outStream << str; + if(outStream.good()) + return false; + else + return true; + +} + +bool StringFromBinaryStream(istream& inStrm, string& str, const size_t maxSize) +//--------------------------------------------------------- +{ + if(!inStrm.good()) return true; + size_t strSize; + inStrm.read(reinterpret_cast<char*>(&strSize), sizeof(strSize)); + + if(strSize > maxSize) + return true; + + str.resize(strSize); + + //Inefficiently reading to temporary buffer first and + //then setting that to the string. + char* buffer = new char[strSize+1]; + inStrm.read(buffer, strSize); + buffer[strSize] = '\0'; + str = buffer; + delete[] buffer; buffer = 0; + + + if(inStrm.good()) + return false; + else + return true; +} Added: branches/OpenMPT_MPTm_Tuning/mptrack/misc_util.h =================================================================== --- branches/OpenMPT_MPTm_Tuning/mptrack/misc_util.h (rev 0) +++ branches/OpenMPT_MPTm_Tuning/mptrack/misc_util.h 2007-01-29 21:50:16 UTC (rev 171) @@ -0,0 +1,70 @@ +#ifndef MISC_UTIL_H +#define MISC_UTIL_H + +#include <vector> +#include <sstream> +#include <string> + +using namespace std; + +const UINT STRINGMAXSIZE = 1000; +const UINT VECTORMAXSIZE = 1000; +//Default sizelimits to string/vector load methods. +//Size limits are there to prevent corrupted streams from +//causing e.g. program to try to load of vector of size 2 000 000 000. + +template<class T> +bool VectorToBinaryStream(ostream& outStrm, const vector<T>& v) +//------------------------------------------------------------ +{ + if(!outStrm.good()) return true; + size_t s = v.size(); + outStrm.write(reinterpret_cast<const char*>(&s), sizeof(s)); + vector<T>::const_iterator iter = v.begin(); + for(iter; iter != v.end(); iter++) + outStrm.write(reinterpret_cast<const char*>(&(*iter)), sizeof(T)); + + if(outStrm.good()) + return false; + else + return true; +} + +template<class T> +bool VectorFromBinaryStream(istream& inStrm, vector<T>& v, const size_t maxSize = VECTORMAXSIZE) +//--------------------------------------------------------- +{ + if(!inStrm.good()) return true; + size_t size; + inStrm.read(reinterpret_cast<char*>(&size), sizeof(size)); + + if(size > maxSize) + return true; + + v.resize(size); + ASSERT(v.size() == size); + for(size_t i = 0; i<size; i++) + { + inStrm.read(reinterpret_cast<char*>(&v[i]), sizeof(T)); + } + if(inStrm.good()) + return false; + else + return true; +} + +bool StringToBinaryStream(ostream& outStream, const string& str); +bool StringFromBinaryStream(istream& inStrm, string& str, const size_t maxSize = STRINGMAXSIZE); + + +template<class T> +inline string Stringify(const T& x) +//-------------------------- +{ + std::ostringstream o; + if(!(o << x)) return "STRINGIFY() FAILURE"; + else return o.str(); +} + + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |