From b1da11f102f7f82b6519b1c76da50dddcd9fb447 Mon Sep 17 00:00:00 2001 From: cflakes Date: Wed, 28 Apr 2010 21:34:09 +0000 Subject: [PATCH] Added the ability to sort template loop data based on a key. e.g. or Patch by BrianC, thanks again. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1948 726aef4b-f618-498e-8847-2d620e286838 --- Template.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Template.cpp b/Template.cpp index 80a0c855..41ff972b 100644 --- a/Template.cpp +++ b/Template.cpp @@ -9,6 +9,7 @@ #include "Template.h" #include "FileUtils.h" #include +#include using std::stringstream; @@ -201,6 +202,15 @@ bool CTemplate::SetFile(const CString& sFileName) { return true; } +class CLoopSorter { + CString m_sType; +public: + CLoopSorter(const CString& sType) : m_sType(sType) {} + bool operator()(CTemplate* pTemplate1, CTemplate* pTemplate2) { + return (pTemplate1->GetValue(m_sType, false) < pTemplate2->GetValue(m_sType, false)); + } +}; + CTemplate& CTemplate::AddRow(const CString& sName) { CTemplate* pTmpl = new CTemplate(m_spOptions, this); m_mvLoops[sName].push_back(pTmpl); @@ -429,8 +439,24 @@ bool CTemplate::Print(const CString& sFileName, ostream& oOut) { CString sLoopName = sArgs.Token(0); bool bReverse = (sArgs.Token(1).Equals("REVERSE")); + bool bSort = (sArgs.Token(1).Left(4).Equals("SORT")); vector* pvLoop = GetLoop(sLoopName); + if (bSort) { + CString sKey; + + if(sArgs.Token(1).TrimPrefix_n("SORT").Left(4).Equals("ASC=")) { + sKey = sArgs.Token(1).TrimPrefix_n("SORTASC="); + } else if(sArgs.Token(1).TrimPrefix_n("SORT").Left(5).Equals("DESC=")) { + sKey = sArgs.Token(1).TrimPrefix_n("SORTDESC="); + bReverse = true; + } + + if (!sKey.empty()) { + std::sort(pvLoop->begin(), pvLoop->end(), CLoopSorter(sKey)); + } + } + if (pvLoop) { // If we found data for this loop, add it to our context vector //unsigned long uBeforeLoopTag = uCurPos - iPos2 - 4;