From: <sag...@us...> - 2010-08-12 16:12:55
|
Revision: 686 http://modplug.svn.sourceforge.net/modplug/?rev=686&view=rev Author: saga-games Date: 2010-08-12 16:12:48 +0000 (Thu, 12 Aug 2010) Log Message: ----------- [Imp] Mod Savers: Garbage characters following the trailing null char in sample/instrument/song names are now removed before saving, so they don't turn up in the saved modules anymore (happened sometimes). Modified Paths: -------------- trunk/OpenMPT/mptrack/Moddoc.cpp trunk/OpenMPT/mptrack/Moddoc.h trunk/OpenMPT/mptrack/misc_util.h Modified: trunk/OpenMPT/mptrack/Moddoc.cpp =================================================================== --- trunk/OpenMPT/mptrack/Moddoc.cpp 2010-08-12 12:39:51 UTC (rev 685) +++ trunk/OpenMPT/mptrack/Moddoc.cpp 2010-08-12 16:12:48 UTC (rev 686) @@ -93,7 +93,7 @@ CModDoc::CModDoc() //---------------- { - m_bHasValidPath=false; + m_bHasValidPath = false; m_bPaused = TRUE; m_lpszLog = NULL; m_hWndFollow = NULL; @@ -425,6 +425,7 @@ } BeginWaitCursor(); ClearLog(); + FixNullStrings(); switch(nType) { case MOD_TYPE_MOD: bOk = m_SndFile.SaveMod(lpszPathName, dwPacking); break; @@ -1760,6 +1761,7 @@ if(files.abort) return; ClearLog(); + FixNullStrings(); switch (type) { case MOD_TYPE_MOD: @@ -3707,4 +3709,43 @@ m_SndFile.ResetChannels(); m_SndFile.StopAllVsti(); END_CRITICAL(); +} + + +// Before saving, make sure that every char after the terminating null char is also null. +// Else, garbage might end up in various text strings that wasn't supposed to be there. +void CModDoc::FixNullStrings() +//---------------------------- +{ + // Song title + FixNullString(m_SndFile.m_szNames[0]); + + // Sample names + filenames + for(SAMPLEINDEX nSmp = 1; nSmp < m_SndFile.GetNumSamples(); nSmp++) + { + FixNullString(m_SndFile.m_szNames[nSmp]); + FixNullString(m_SndFile.Samples[nSmp].filename); + } + + // Instrument names + for(INSTRUMENTINDEX nIns = 1; nIns < m_SndFile.GetNumInstruments(); nIns++) + { + if(m_SndFile.Instruments[nIns] != nullptr) + { + FixNullString(m_SndFile.Instruments[nIns]->name); + FixNullString(m_SndFile.Instruments[nIns]->filename); + } + } + + // Channel names + for(CHANNELINDEX nChn = 0; nChn < m_SndFile.GetNumChannels(); nChn++) + { + FixNullString(m_SndFile.ChnSettings[nChn].szName); + } + + // Pattern names + // Halp, this is currently not possible. Is it even needed? + + // Sequence names. + // Not needed? } \ No newline at end of file Modified: trunk/OpenMPT/mptrack/Moddoc.h =================================================================== --- trunk/OpenMPT/mptrack/Moddoc.h 2010-08-12 12:39:51 UTC (rev 685) +++ trunk/OpenMPT/mptrack/Moddoc.h 2010-08-12 16:12:48 UTC (rev 686) @@ -291,6 +291,8 @@ bool HasMPTHacks(bool autofix = false); + void FixNullStrings(); + bool m_bHasValidPath; //becomes true if document is loaded or saved. // Fix: save pattern scrollbar position when switching to other tab CSize GetOldPatternScrollbarsPos() const { return m_szOldPatternScrollbarsPos; }; Modified: trunk/OpenMPT/mptrack/misc_util.h =================================================================== --- trunk/OpenMPT/mptrack/misc_util.h 2010-08-12 12:39:51 UTC (rev 685) +++ trunk/OpenMPT/mptrack/misc_util.h 2010-08-12 16:12:48 UTC (rev 686) @@ -103,13 +103,16 @@ // If the source and destination arrays overlap, behaviour is undefined. template <class T> void ArrayCopy(T* pDst, const T* pSrc, const size_t n) +//---------------------------------------------------- { utilImpl::ArrayCopyImpl<std::tr1::has_trivial_assign<T>::value>::Do(pDst, pSrc, n); } + // Sanitize a filename (remove special chars) template <size_t size> inline void SanitizeFilename(char (&buffer)[size]) +//------------------------------------------------ { STATIC_ASSERT(size > 0); for(size_t i = 0; i < size; i++) @@ -132,9 +135,11 @@ } } + // Convert a 0-terminated string to a space-padded string template <size_t size> void NullToSpaceString(char (&buffer)[size]) +//------------------------------------------ { STATIC_ASSERT(size > 0); size_t pos = size; @@ -143,9 +148,12 @@ buffer[pos] = 32; buffer[size - 1] = 0; } + + // Convert a space-padded string to a 0-terminated string template <size_t size> void SpaceToNullString(char (&buffer)[size]) +//------------------------------------------ { STATIC_ASSERT(size > 0); // First, remove any Nulls @@ -161,6 +169,24 @@ buffer[size - 1] = 0; } + +// Remove any chars after the first null char +template <size_t size> +void FixNullString(char (&buffer)[size]) +//-------------------------------------- +{ + STATIC_ASSERT(size > 0); + bool overwrite = false; + for(size_t pos = 0; pos < size; pos++) + { + if(overwrite) + buffer[pos] = 0; + else if(buffer[pos] == 0) + overwrite = true; + } +} + + // Convert a space-padded string to a 0-terminated string. // Additional parameter to specifify the max length of the final string, // not including null char (useful for e.g. mod loaders) @@ -178,4 +204,5 @@ buffer[pos] = 0; } } + #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |