From: <rel...@us...> - 2013-03-15 20:01:30
|
Revision: 1571 http://sourceforge.net/p/modplug/code/1571 Author: relabsoluness Date: 2013-03-15 20:01:21 +0000 (Fri, 15 Mar 2013) Log Message: ----------- [Ref] Introduced new string class to allow easier replacement of CString-objects in soundlib and made some related changes. Modified Paths: -------------- trunk/OpenMPT/common/misc_util.cpp trunk/OpenMPT/common/misc_util.h trunk/OpenMPT/mptrack/MIDIMapping.cpp trunk/OpenMPT/mptrack/MIDIMapping.h trunk/OpenMPT/mptrack/Vstplug.cpp trunk/OpenMPT/soundlib/Load_xm.cpp trunk/OpenMPT/soundlib/Message.cpp trunk/OpenMPT/soundlib/ModSequence.cpp trunk/OpenMPT/soundlib/ModSequence.h trunk/OpenMPT/soundlib/Snd_defs.h trunk/OpenMPT/soundlib/Sndfile.cpp trunk/OpenMPT/soundlib/Sndfile.h trunk/OpenMPT/soundlib/pattern.h trunk/OpenMPT/soundlib/patternContainer.cpp trunk/OpenMPT/soundlib/plugins/PlugInterface.h Modified: trunk/OpenMPT/common/misc_util.cpp =================================================================== --- trunk/OpenMPT/common/misc_util.cpp 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/common/misc_util.cpp 2013-03-15 20:01:21 UTC (rev 1571) @@ -44,8 +44,8 @@ // Returns WinAPI error message corresponding to error code returned by GetLastError(). -CString GetErrorMessage(DWORD nErrorCode) -//--------------------------------------- +std::string GetErrorMessage(DWORD nErrorCode) +//------------------------------------------- { LPVOID lpMsgBuf; @@ -57,7 +57,7 @@ 0, NULL ); - CString msg = (LPTSTR)lpMsgBuf; + std::string msg = (LPTSTR)lpMsgBuf; LocalFree(lpMsgBuf); return msg; Modified: trunk/OpenMPT/common/misc_util.h =================================================================== --- trunk/OpenMPT/common/misc_util.h 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/common/misc_util.h 2013-03-15 20:01:21 UTC (rev 1571) @@ -141,7 +141,7 @@ #ifdef MODPLUG_TRACKER LPCCH LoadResource(LPCTSTR lpName, LPCTSTR lpType, LPCCH& pData, size_t& nSize, HGLOBAL& hglob); -CString GetErrorMessage(DWORD nErrorCode); +std::string GetErrorMessage(DWORD nErrorCode); #endif // MODPLUG_TRACKER namespace utilImpl Modified: trunk/OpenMPT/mptrack/MIDIMapping.cpp =================================================================== --- trunk/OpenMPT/mptrack/MIDIMapping.cpp 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/mptrack/MIDIMapping.cpp 2013-03-15 20:01:21 UTC (rev 1571) @@ -14,16 +14,15 @@ #include "Mainfrm.h" -CString CMIDIMappingDirective::ToString() const -//--------------------------------------------- +mpt::String CMIDIMappingDirective::ToString() const +//------------------------------------------------- { - CString str; str.Preallocate(20); + mpt::String str; char flags[4] = "000"; if(m_Active) flags[0] = '1'; if(m_CaptureMIDI) flags[1] = '1'; if(m_AllowPatternEdit) flags[2] = '1'; str.Format("%s:%d:%x:%d:%d:%d", flags, (int)GetChannel(), (int)GetEvent(), (int)GetController(), (int)m_PluginIndex, m_Parameter); - str.Trim(); return str; } Modified: trunk/OpenMPT/mptrack/MIDIMapping.h =================================================================== --- trunk/OpenMPT/mptrack/MIDIMapping.h 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/mptrack/MIDIMapping.h 2013-03-15 20:01:21 UTC (rev 1571) @@ -14,7 +14,9 @@ #include <algorithm> using std::vector; +namespace mpt { class String; } + //========================= class CMIDIMappingDirective //========================= @@ -57,7 +59,7 @@ bool operator==(const CMIDIMappingDirective& d) const {return memcmp(this, &d, sizeof(CMIDIMappingDirective)) == 0;} - CString ToString() const; + mpt::String ToString() const; BYTE GetChnEvent() const {return m_ChnEvent;} Modified: trunk/OpenMPT/mptrack/Vstplug.cpp =================================================================== --- trunk/OpenMPT/mptrack/Vstplug.cpp 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/mptrack/Vstplug.cpp 2013-03-15 20:01:21 UTC (rev 1571) @@ -201,7 +201,7 @@ if(error != ERROR_MOD_NOT_FOUND) // "File not found errors" are annoying. { TCHAR szBuf[256]; - wsprintf(szBuf, "Warning: encountered problem when loading plugin dll. Error %d: %s", error, (LPCTSTR)GetErrorMessage(error)); + wsprintf(szBuf, "Warning: encountered problem when loading plugin dll. Error %d: %s", error, GetErrorMessage(error).c_str()); Reporting::Error(szBuf, "DEBUG: Error when loading plugin dll"); } #endif //_DEBUG @@ -3545,7 +3545,7 @@ #endif // NO_VST -CString SNDMIXPLUGIN::GetParamName(PlugParamIndex index) const +mpt::String SNDMIXPLUGIN::GetParamName(PlugParamIndex index) const //------------------------------------------------------------ { CVstPlugin *vstPlug = dynamic_cast<CVstPlugin *>(pMixPlugin); @@ -3554,6 +3554,6 @@ return vstPlug->GetParamName(index); } else { - return CString(); + return mpt::String(); } } Modified: trunk/OpenMPT/soundlib/Load_xm.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_xm.cpp 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/Load_xm.cpp 2013-03-15 20:01:21 UTC (rev 1571) @@ -557,7 +557,7 @@ \ if((len > uint16_max - (UINT)x) && GetpModDoc()) /*Reaching the limits of file format?*/ \ { \ - CString str; str.Format("%s (%s %u)\n", str_tooMuchPatternData, str_pattern, pat); \ + mpt::String str; str.Format("%s (%s %u)\n", str_tooMuchPatternData, str_pattern, pat); \ GetpModDoc()->AddToLog(str); \ break; \ } Modified: trunk/OpenMPT/soundlib/Message.cpp =================================================================== --- trunk/OpenMPT/soundlib/Message.cpp 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/Message.cpp 2013-03-15 20:01:21 UTC (rev 1571) @@ -194,10 +194,10 @@ // [in] lineEnding: line ending formatting of the text in memory. // [in] pTextConverter: Pointer to a callback function which can be used to post-process the written characters, if necessary (nullptr otherwise). // [out] returns formatted song message. -CString CSoundFile::GetSongMessage(const enmLineEndings lineEnding, void (*pTextConverter)(char &)) const +mpt::String CSoundFile::GetSongMessage(const enmLineEndings lineEnding, void (*pTextConverter)(char &)) const //------------------------------------------------------------------------------------------------------- { - CString comments; + mpt::String comments; if(m_lpszSongComments == nullptr) { @@ -205,7 +205,7 @@ } const size_t len = strlen(m_lpszSongComments); - comments.Preallocate(len); + comments.Reserve(len); for(size_t i = 0; i < len; i++) { Modified: trunk/OpenMPT/soundlib/ModSequence.cpp =================================================================== --- trunk/OpenMPT/soundlib/ModSequence.cpp 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/ModSequence.cpp 2013-03-15 20:01:21 UTC (rev 1571) @@ -717,7 +717,7 @@ if(size > ModSpecs::mptm.ordersMax) { // Hack: Show message here if trying to load longer sequence than what is supported. - CString str; str.Format(str_SequenceTruncationNote, size, ModSpecs::mptm.ordersMax); + mpt::String str; str.Format(str_SequenceTruncationNote, size, ModSpecs::mptm.ordersMax); Reporting::Warning(str); size = ModSpecs::mptm.ordersMax; } @@ -753,7 +753,7 @@ { srlztn::Ssb ssb(oStrm); ssb.BeginWrite(FileIdSequence, MptVersion::num); - ssb.WriteItem((LPCSTR)seq.m_sName, "n"); + ssb.WriteItem((const char*)seq.m_sName, "n"); const uint16 nLength = seq.GetLengthTailTrimmed(); ssb.WriteItem<uint16>(nLength, "l"); ssb.WriteItem(seq.m_pArray, "a", 1, srlztn::ArrayWriter<uint16>(nLength)); Modified: trunk/OpenMPT/soundlib/ModSequence.h =================================================================== --- trunk/OpenMPT/soundlib/ModSequence.h 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/ModSequence.h 2013-03-15 20:01:21 UTC (rev 1571) @@ -121,7 +121,7 @@ const_iterator end() const {return m_pArray + m_nSize;} public: - CString m_sName; // Sequence name. + mpt::String m_sName; // Sequence name. protected: PATTERNINDEX* m_pArray; // Pointer to sequence array. Modified: trunk/OpenMPT/soundlib/Snd_defs.h =================================================================== --- trunk/OpenMPT/soundlib/Snd_defs.h 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/Snd_defs.h 2013-03-15 20:01:21 UTC (rev 1571) @@ -380,4 +380,93 @@ VIB_RAMP_UP, VIB_RAMP_DOWN, VIB_RANDOM -}; \ No newline at end of file +}; + +namespace mpt +{ + // Quick and incomplete string class to be used as a replacement for + // CString and std::string in soundlib. + class String : public std::string + { + public: + typedef std::string BaseClass; + typedef char CharT; + + String() {} + String(const CharT* psz) : BaseClass(psz) {} + String(const std::string& other) : BaseClass(other) {} + + // Move constructors and move assignments. + #if (_MSC_VER >= MSVC_VER_2010) + String(BaseClass&& str) : BaseClass(std::move(str)) {} + String(String&& other) : BaseClass(std::move(static_cast<BaseClass&>(other))) {} + String& operator=(BaseClass&& str) {BaseClass::operator=(std::move(str)); return *this;} + String& operator=(String&& other) {BaseClass::operator=(std::move(static_cast<BaseClass&>(other))); return *this;} + #endif + + String& operator=(const CharT* psz) {BaseClass::operator=(psz); return *this;} + + #ifdef MODPLUG_TRACKER + String(const CString& str) {(*this) = str.GetString();} + String& operator=(const CString& str) {return operator=(str.GetString());} + #endif // MODPLUG_TRACKER + + // To allow easy assignment to CString in GUI side. + operator const CharT*() const {return c_str();} + + // Clears string. + void Empty() {clear();} + + // Tests whether string is empty. + bool IsEmpty() const {return empty();} + + // Compares this and other string. + bool Compare(const CharT* psz) const {return ((*this) == psz);} + + // Equivalent to Empty(); Append(psz, nCount); + void SetString(const CharT* psz, const size_t nCount) + { + Empty(); + Append(psz, nCount); + } + + // Append string to this. + void Append(const CharT* psz) {append(psz);} + + // Appends given string to this. Stops after 'nCount' chars if null terminator + // hasn't been encountered before that. + void Append(const CharT* psz, const size_t nCount) {append(psz, nCount);} + + // Appends single character to string. + void AppendChar(const CharT c) {append(1, c);} + + // Difference between Append and AppendChars is that latter can be used to add + // potentially non-null terminated char array. + template <size_t N> + void AppendChars(const CharT (&arr)[N]) + { + append(arr, arr + N); + } + + // Reserves store for the string without resizing. + void Reserve(size_t nSize) {reserve(nSize);} + + // Formats this string, like CString::Format. + void Format(const CharT* pszFormat, ...) + { + va_list argList; + va_start( argList, pszFormat ); + + // Count the needed array size. + const size_t nCount = _vscprintf(pszFormat, argList); // null character not included. + resize(nCount + 1); // + 1 is for null terminator. + + // Hack: directly modify the std::string's string. + // In C++11 std::string is guaranteed to be contiguous. + const int nCount2 = vsprintf_s(&*begin(), size(), pszFormat, argList); + resize(nCount2); // Removes the null character that vsprintf_s adds. + + va_end( argList ); + } + }; +} Modified: trunk/OpenMPT/soundlib/Sndfile.cpp =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.cpp 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/Sndfile.cpp 2013-03-15 20:01:21 UTC (rev 1571) @@ -1288,19 +1288,15 @@ } -CString CSoundFile::GetInstrumentName(UINT nInstr) const +mpt::String CSoundFile::GetInstrumentName(UINT nInstr) const //------------------------------------------------------ { if ((nInstr >= MAX_INSTRUMENTS) || (!Instruments[nInstr])) return TEXT(""); ASSERT(nInstr <= GetNumInstruments()); - const size_t nSize = CountOf(Instruments[nInstr]->name); - CString str; - LPTSTR p = str.GetBuffer(nSize + 1); - ArrayCopy(p, Instruments[nInstr]->name, nSize); - p[nSize] = 0; - str.ReleaseBuffer(); + mpt::String str; + str.AppendChars(Instruments[nInstr]->name); return str; } @@ -1625,8 +1621,8 @@ } // Load local tunings. - CString sPath; - sPath.Format(TEXT("%slocal_tunings%s"), TrackerSettings::Instance().GetDefaultDirectory(DIR_TUNING), CTuningCollection::s_FileExtension); + mpt::String sPath; + sPath.Format("%slocal_tunings%s", TrackerSettings::Instance().GetDefaultDirectory(DIR_TUNING), CTuningCollection::s_FileExtension); s_pTuningsSharedLocal->SetSavefilePath(sPath); s_pTuningsSharedLocal->Deserialize(); Modified: trunk/OpenMPT/soundlib/Sndfile.h =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.h 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/Sndfile.h 2013-03-15 20:01:21 UTC (rev 1571) @@ -359,7 +359,7 @@ void SetCurrentOrder(ORDERINDEX nOrder); LPCSTR GetTitle() const { return m_szNames[0]; } LPCTSTR GetSampleName(UINT nSample) const; - CString GetInstrumentName(UINT nInstr) const; + mpt::String GetInstrumentName(UINT nInstr) const; UINT GetMusicSpeed() const { return m_nMusicSpeed; } UINT GetMusicTempo() const { return m_nMusicTempo; } @@ -696,7 +696,7 @@ // [in] lineEnding: line ending formatting of the text in memory. // [in] pTextConverter: Pointer to a callback function which can be used to post-process the written characters, if necessary (nullptr otherwise). // [out] returns formatted song message. - CString GetSongMessage(const enmLineEndings lineEnding, void (*pTextConverter)(char &) = nullptr) const; + mpt::String GetSongMessage(const enmLineEndings lineEnding, void (*pTextConverter)(char &) = nullptr) const; protected: // Read song message from a mapped file. Modified: trunk/OpenMPT/soundlib/pattern.h =================================================================== --- trunk/OpenMPT/soundlib/pattern.h 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/pattern.h 2013-03-15 20:01:21 UTC (rev 1571) @@ -90,14 +90,13 @@ bool SetSignature(const ROWINDEX rowsPerBeat, const ROWINDEX rowsPerMeasure); void RemoveSignature() { m_RowsPerBeat = m_RowsPerMeasure = 0; } - // Patter name functions (for both CString and char[] arrays) - bool functions return true on success. + // Pattern name functions - bool functions return true on success. bool SetName(const char *newName, size_t maxChars = MAX_PATTERNNAME); template<size_t bufferSize> bool SetName(const char (&buffer)[bufferSize]) { return SetName(buffer, bufferSize); } - bool SetName(const CString newName) { m_PatternName = newName; return true; }; template<size_t bufferSize> bool GetName(char (&buffer)[bufferSize]) const @@ -105,7 +104,7 @@ return GetName(buffer, bufferSize); } bool GetName(char *buffer, size_t maxChars) const; - CString GetName() const { return m_PatternName; }; + const mpt::String& GetName() const { return m_PatternName; }; // Double number of rows bool Expand(); @@ -149,7 +148,7 @@ ROWINDEX m_Rows; ROWINDEX m_RowsPerBeat; // patterns-specific time signature. if != 0, this is implicitely set. ROWINDEX m_RowsPerMeasure; // dito - CString m_PatternName; + mpt::String m_PatternName; CPatternContainer& m_rPatternContainer; //END: DATA }; Modified: trunk/OpenMPT/soundlib/patternContainer.cpp =================================================================== --- trunk/OpenMPT/soundlib/patternContainer.cpp 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/patternContainer.cpp 2013-03-15 20:01:21 UTC (rev 1571) @@ -190,7 +190,7 @@ } for(PATTERNINDEX nPat = Size(); nPat > 0; nPat--) { - if(m_Patterns[nPat - 1].GetName() != "") + if(!m_Patterns[nPat - 1].GetName().IsEmpty()) { return nPat; } Modified: trunk/OpenMPT/soundlib/plugins/PlugInterface.h =================================================================== --- trunk/OpenMPT/soundlib/plugins/PlugInterface.h 2013-03-14 00:42:01 UTC (rev 1570) +++ trunk/OpenMPT/soundlib/plugins/PlugInterface.h 2013-03-15 20:01:21 UTC (rev 1571) @@ -151,7 +151,7 @@ { return Info.szName; } const char *GetLibraryName() const { return Info.szLibraryName; } - CString GetParamName(PlugParamIndex index) const; + mpt::String GetParamName(PlugParamIndex index) const; // Check if a plugin is loaded into this slot (also returns true if the plugin in this slot has not been found) bool IsValidPlugin() const { return (Info.dwPluginId1 | Info.dwPluginId2) != 0; }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |