1
0

patternContainer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * PatternContainer.cpp
  3. * --------------------
  4. * Purpose: Container class for managing patterns.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "patternContainer.h"
  11. #include "Sndfile.h"
  12. #include "mod_specifications.h"
  13. #include "../common/serialization_utils.h"
  14. #include "../common/version.h"
  15. OPENMPT_NAMESPACE_BEGIN
  16. void CPatternContainer::ClearPatterns()
  17. {
  18. DestroyPatterns();
  19. m_Patterns.assign(m_Patterns.size(), CPattern(*this));
  20. }
  21. void CPatternContainer::DestroyPatterns()
  22. {
  23. m_Patterns.clear();
  24. }
  25. PATTERNINDEX CPatternContainer::Duplicate(PATTERNINDEX from, bool respectQtyLimits)
  26. {
  27. if(!IsValidPat(from))
  28. {
  29. return PATTERNINDEX_INVALID;
  30. }
  31. PATTERNINDEX newPatIndex = InsertAny(m_Patterns[from].GetNumRows(), respectQtyLimits);
  32. if(newPatIndex != PATTERNINDEX_INVALID)
  33. {
  34. m_Patterns[newPatIndex] = m_Patterns[from];
  35. }
  36. return newPatIndex;
  37. }
  38. PATTERNINDEX CPatternContainer::InsertAny(const ROWINDEX rows, bool respectQtyLimits)
  39. {
  40. PATTERNINDEX i = 0;
  41. for(i = 0; i < m_Patterns.size(); i++)
  42. if(!m_Patterns[i].IsValid()) break;
  43. if(respectQtyLimits && i >= m_rSndFile.GetModSpecifications().patternsMax)
  44. return PATTERNINDEX_INVALID;
  45. if(!Insert(i, rows))
  46. return PATTERNINDEX_INVALID;
  47. else return i;
  48. }
  49. bool CPatternContainer::Insert(const PATTERNINDEX index, const ROWINDEX rows)
  50. {
  51. if(rows > MAX_PATTERN_ROWS || rows == 0 || index >= PATTERNINDEX_INVALID)
  52. return false;
  53. if(IsValidPat(index))
  54. return false;
  55. try
  56. {
  57. if(index >= m_Patterns.size())
  58. {
  59. m_Patterns.resize(index + 1, CPattern(*this));
  60. }
  61. m_Patterns[index].AllocatePattern(rows);
  62. m_Patterns[index].RemoveSignature();
  63. m_Patterns[index].SetName("");
  64. } catch(mpt::out_of_memory e)
  65. {
  66. mpt::delete_out_of_memory(e);
  67. return false;
  68. }
  69. return m_Patterns[index].IsValid();
  70. }
  71. void CPatternContainer::Remove(const PATTERNINDEX ipat)
  72. {
  73. if(ipat < m_Patterns.size()) m_Patterns[ipat].Deallocate();
  74. }
  75. bool CPatternContainer::IsPatternEmpty(const PATTERNINDEX nPat) const
  76. {
  77. if(!IsValidPat(nPat))
  78. return false;
  79. for(const auto &m : m_Patterns[nPat].m_ModCommands)
  80. {
  81. if(!m.IsEmpty())
  82. return false;
  83. }
  84. return true;
  85. }
  86. void CPatternContainer::ResizeArray(const PATTERNINDEX newSize)
  87. {
  88. m_Patterns.resize(newSize, CPattern(*this));
  89. }
  90. void CPatternContainer::OnModTypeChanged(const MODTYPE /*oldtype*/)
  91. {
  92. const CModSpecifications specs = m_rSndFile.GetModSpecifications();
  93. //if(specs.patternsMax < Size())
  94. // ResizeArray(specs.patternsMax);
  95. // remove pattern time signatures
  96. if(!specs.hasPatternSignatures)
  97. {
  98. for(PATTERNINDEX nPat = 0; nPat < m_Patterns.size(); nPat++)
  99. {
  100. m_Patterns[nPat].RemoveSignature();
  101. m_Patterns[nPat].RemoveTempoSwing();
  102. }
  103. }
  104. }
  105. PATTERNINDEX CPatternContainer::GetNumPatterns() const
  106. {
  107. for(PATTERNINDEX pat = Size(); pat > 0; pat--)
  108. {
  109. if(IsValidPat(pat - 1))
  110. {
  111. return pat;
  112. }
  113. }
  114. return 0;
  115. }
  116. PATTERNINDEX CPatternContainer::GetNumNamedPatterns() const
  117. {
  118. if(Size() == 0)
  119. {
  120. return 0;
  121. }
  122. for(PATTERNINDEX nPat = Size(); nPat > 0; nPat--)
  123. {
  124. if(!m_Patterns[nPat - 1].GetName().empty())
  125. {
  126. return nPat;
  127. }
  128. }
  129. return 0;
  130. }
  131. void WriteModPatterns(std::ostream& oStrm, const CPatternContainer& patc)
  132. {
  133. srlztn::SsbWrite ssb(oStrm);
  134. ssb.BeginWrite(FileIdPatterns, Version::Current().GetRawVersion());
  135. const PATTERNINDEX nPatterns = patc.Size();
  136. uint16 nCount = 0;
  137. for(uint16 i = 0; i < nPatterns; i++) if (patc[i].IsValid())
  138. {
  139. ssb.WriteItem(patc[i], srlztn::ID::FromInt<uint16>(i), &WriteModPattern);
  140. nCount = i + 1;
  141. }
  142. ssb.WriteItem<uint16>(nCount, "num"); // Index of last pattern + 1.
  143. ssb.FinishWrite();
  144. }
  145. void ReadModPatterns(std::istream& iStrm, CPatternContainer& patc, const size_t)
  146. {
  147. srlztn::SsbRead ssb(iStrm);
  148. ssb.BeginRead(FileIdPatterns, Version::Current().GetRawVersion());
  149. if ((ssb.GetStatus() & srlztn::SNT_FAILURE) != 0)
  150. return;
  151. PATTERNINDEX nPatterns = patc.Size();
  152. uint16 nCount = uint16_max;
  153. if (ssb.ReadItem(nCount, "num") != srlztn::SsbRead::EntryNotFound)
  154. nPatterns = nCount;
  155. LimitMax(nPatterns, ModSpecs::mptm.patternsMax);
  156. if (nPatterns > patc.Size())
  157. patc.ResizeArray(nPatterns);
  158. for(uint16 i = 0; i < nPatterns; i++)
  159. {
  160. ssb.ReadItem(patc[i], srlztn::ID::FromInt<uint16>(i), &ReadModPattern);
  161. }
  162. }
  163. OPENMPT_NAMESPACE_END