From: <man...@us...> - 2014-10-09 08:13:27
|
Revision: 4407 http://sourceforge.net/p/modplug/code/4407 Author: manxorist Date: 2014-10-09 08:13:13 +0000 (Thu, 09 Oct 2014) Log Message: ----------- [Ref] Rename srlztn::StringFromBinaryStream to mpt::IO::ReadSizedStringLE and srlztn::StringToBinaryStream to mpt::IO::WriteSizedStringLE, and invert the logic to actually return true on success. Modified Paths: -------------- trunk/OpenMPT/common/mptIO.h trunk/OpenMPT/common/serialization_utils.h trunk/OpenMPT/soundlib/Load_it.cpp trunk/OpenMPT/soundlib/tuning.cpp trunk/OpenMPT/soundlib/tuningCollection.cpp trunk/OpenMPT/soundlib/tuningbase.cpp Modified: trunk/OpenMPT/common/mptIO.h =================================================================== --- trunk/OpenMPT/common/mptIO.h 2014-10-08 22:27:31 UTC (rev 4406) +++ trunk/OpenMPT/common/mptIO.h 2014-10-09 08:13:13 UTC (rev 4407) @@ -239,6 +239,33 @@ return result; } +template <typename Tsize, typename Tfile> +inline bool ReadSizedStringLE(Tfile & f, std::string & str, Tsize maxSize = std::numeric_limits<Tsize>::max()) +{ + STATIC_ASSERT(std::numeric_limits<Tsize>::is_integer); + str.clear(); + Tsize size = 0; + if(!mpt::IO::ReadIntLE(f, size)) + { + return false; + } + if(size > maxSize) + { + return false; + } + for(Tsize i = 0; i != size; ++i) + { + char c = '\0'; + if(!mpt::IO::ReadIntLE(f, c)) + { + return false; + } + str.push_back(c); + } + return true; +} + + template <typename T, typename Tfile> inline bool WriteIntLE(Tfile & f, const T & v) { @@ -333,6 +360,26 @@ } } +template <typename Tsize, typename Tfile> +inline bool WriteSizedStringLE(Tfile & f, const std::string & str) +{ + STATIC_ASSERT(std::numeric_limits<Tsize>::is_integer); + if(str.size() > std::numeric_limits<Tsize>::max()) + { + return false; + } + Tsize size = str.size(); + if(!mpt::IO::WriteIntLE(f, size)) + { + return false; + } + if(!mpt::IO::WriteRaw(f, str.data(), str.size())) + { + return false; + } + return true; +} + template <typename T, typename Tfile> inline bool WriteConvertEndianness(Tfile & f, T & v) { Modified: trunk/OpenMPT/common/serialization_utils.h =================================================================== --- trunk/OpenMPT/common/serialization_utils.h 2014-10-08 22:27:31 UTC (rev 4406) +++ trunk/OpenMPT/common/serialization_utils.h 2014-10-09 08:13:13 UTC (rev 4407) @@ -16,12 +16,15 @@ #include <algorithm> #include <bitset> -#include <istream> +#include <ios> +#include <iosfwd> #include <limits> -#include <ostream> #include <string> #include <vector> +#include <istream> +#include <ostream> + #ifdef HAS_TYPE_TRAITS #include <type_traits> #endif @@ -506,37 +509,8 @@ size_t m_nCount; }; -template<class SIZETYPE> -bool StringToBinaryStream(std::ostream& oStrm, const std::string& str) -//-------------------------------------------------------------------- -{ - if(!oStrm.good()) return true; - if((std::numeric_limits<SIZETYPE>::max)() < str.size()) return true; - SIZETYPE size = static_cast<SIZETYPE>(str.size()); - Binarywrite(oStrm, size); - oStrm.write(str.c_str(), size); - if(oStrm.good()) return false; - else return true; -} -template<class SIZETYPE> -bool StringFromBinaryStream(std::istream& iStrm, std::string& str, const SIZETYPE maxSize = (std::numeric_limits<SIZETYPE>::max)()) -//--------------------------------------------------------------------------------------------------------------------------------- -{ - if(!iStrm.good()) return true; - SIZETYPE strSize; - Binaryread(iStrm, strSize); - if(strSize > maxSize) - return true; - str.resize(strSize); - for(SIZETYPE i = 0; i<strSize; i++) - iStrm.read(&str[i], 1); - if(iStrm.good()) return false; - else return true; -} - - } //namespace srlztn. Modified: trunk/OpenMPT/soundlib/Load_it.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_it.cpp 2014-10-08 22:27:31 UTC (rev 4406) +++ trunk/OpenMPT/soundlib/Load_it.cpp 2014-10-09 08:13:13 UTC (rev 4407) @@ -105,9 +105,9 @@ for(TNTS_MAP_ITER iter = tNameToShort_Map.begin(); iter != tNameToShort_Map.end(); iter++) { if(iter->first) - srlztn::StringToBinaryStream<uint8>(oStrm, iter->first->GetName()); + mpt::IO::WriteSizedStringLE<uint8>(oStrm, iter->first->GetName()); else //Case: Using original IT tuning. - srlztn::StringToBinaryStream<uint8>(oStrm, "->MPT_ORIGINAL_IT<-"); + mpt::IO::WriteSizedStringLE<uint8>(oStrm, "->MPT_ORIGINAL_IT<-"); mpt::IO::WriteIntLE<uint16>(oStrm, iter->second); } @@ -144,7 +144,7 @@ { std::string temp; uint16 ui; - if(srlztn::StringFromBinaryStream<STRSIZETYPE>(iStrm, temp, 255)) + if(!mpt::IO::ReadSizedStringLE<STRSIZETYPE>(iStrm, temp, 255)) return true; iStrm.read(reinterpret_cast<char*>(&ui), sizeof(ui)); Modified: trunk/OpenMPT/soundlib/tuning.cpp =================================================================== --- trunk/OpenMPT/soundlib/tuning.cpp 2014-10-08 22:27:31 UTC (rev 4406) +++ trunk/OpenMPT/soundlib/tuning.cpp 2014-10-09 08:13:13 UTC (rev 4407) @@ -11,6 +11,7 @@ #include "stdafx.h" #include "tuning.h" +#include "../common/mptIO.h" #include "../common/serialization_utils.h" #ifdef MODPLUG_TRACKER #include "../mptrack/Reporting.h" @@ -533,7 +534,7 @@ int16 key; mpt::IO::ReadIntLE<int16>(iStrm, key); std::string str; - srlztn::StringFromBinaryStream<uint8>(iStrm, str); + mpt::IO::ReadSizedStringLE<uint8>(iStrm, str); m[key] = str; } } @@ -575,7 +576,7 @@ for(; iter != end; iter++) { mpt::IO::WriteIntLE<int16>(oStrm, iter->first); - srlztn::StringToBinaryStream<uint8>(oStrm, iter->second); + mpt::IO::WriteSizedStringLE<uint8>(oStrm, iter->second); } } Modified: trunk/OpenMPT/soundlib/tuningCollection.cpp =================================================================== --- trunk/OpenMPT/soundlib/tuningCollection.cpp 2014-10-08 22:27:31 UTC (rev 4406) +++ trunk/OpenMPT/soundlib/tuningCollection.cpp 2014-10-09 08:13:13 UTC (rev 4407) @@ -10,6 +10,7 @@ #include "stdafx.h" #include "tuningcollection.h" +#include "../common/mptIO.h" #include "../common/serialization_utils.h" #include <algorithm> #include <bitset> @@ -221,12 +222,12 @@ //3. Name if(version < 2) { - if(srlztn::StringFromBinaryStream<uint32>(inStrm, m_Name, 256)) + if(!mpt::IO::ReadSizedStringLE<uint32>(inStrm, m_Name, 256)) return false; } else { - if(srlztn::StringFromBinaryStream<uint8>(inStrm, m_Name)) + if(!mpt::IO::ReadSizedStringLE<uint8>(inStrm, m_Name)) return false; } Modified: trunk/OpenMPT/soundlib/tuningbase.cpp =================================================================== --- trunk/OpenMPT/soundlib/tuningbase.cpp 2014-10-08 22:27:31 UTC (rev 4406) +++ trunk/OpenMPT/soundlib/tuningbase.cpp 2014-10-09 08:13:13 UTC (rev 4407) @@ -10,6 +10,7 @@ #include "stdafx.h" #include "tuningbase.h" +#include "../common/mptIO.h" #include "../common/serialization_utils.h" #include <istream> @@ -440,7 +441,7 @@ if(version != 4) return SERIALIZATION_FAILURE; //Tuning name - if(srlztn::StringFromBinaryStream<uint8>(inStrm, m_TuningName)) + if(!mpt::IO::ReadSizedStringLE<uint8>(inStrm, m_TuningName)) return SERIALIZATION_FAILURE; //Const mask @@ -461,7 +462,7 @@ NOTEINDEXTYPE n; std::string str; inStrm.read(reinterpret_cast<char*>(&n), sizeof(n)); - if(srlztn::StringFromBinaryStream<uint8>(inStrm, str)) + if(!mpt::IO::ReadSizedStringLE<uint8>(inStrm, str)) return SERIALIZATION_FAILURE; m_NoteNameMap[n] = str; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |