Permalink
Browse files

Slight refactor of CBuffer & CBufLine.

This is in preparation of adding more attributes to a CBufLine. Going
forward, at least savebuf will need access to all of these to properly
serialize buffers.

Basically, instead of relying on `GetLine()` to return `false`, the
caller is now expected to check bounds himself using `Size()`.
  • Loading branch information...
1 parent cb4131e commit c3d677a4d849d6ce74afbdd020269033232506a3 @stephank stephank committed Oct 16, 2011
Showing with 31 additions and 51 deletions.
  1. +4 −4 include/znc/Buffer.h
  2. +4 −3 modules/savebuff.cpp
  3. +3 −4 modules/watch.cpp
  4. +6 −28 src/Buffer.cpp
  5. +3 −3 src/Chan.cpp
  6. +11 −9 src/IRCNetwork.cpp
View
@@ -21,7 +21,7 @@ class CBufLine {
~CBufLine();
const CString& GetFormat() const { return m_sFormat; }
void SetFormat(const CString& sFormat) { m_sFormat = sFormat; }
- void GetLine(CString& sRet, const MCString& msParams) const;
+ CString GetLine(const MCString& msParams) const;
private:
protected:
@@ -38,9 +38,9 @@ class CBuffer : private deque<CBufLine> {
int UpdateLine(const CString& sMatch, const CString& sFormat);
/// Same as UpdateLine, but does nothing if this exact line already exists.
int UpdateExactLine(const CString& sFormat);
- bool GetNextLine(CString& sRet, const MCString& msParams = MCString::EmptyMap);
- bool GetLineFormat(unsigned int uIdx, CString& sRet) const;
- bool GetLine(unsigned int uIdx, CString& sRet, const MCString& msParams = MCString::EmptyMap) const;
+ const CBufLine& GetBufLine(unsigned int uIdx) const;
+ CString GetLine(unsigned int uIdx, const MCString& msParams = MCString::EmptyMap) const;
+ unsigned int Size() const { return size(); }
bool IsEmpty() const { return empty(); }
void Clear() { clear(); }
View
@@ -148,9 +148,10 @@ class CSaveBuff : public CModule
CString sFile = CRYPT_VERIFICATION_TOKEN;
- unsigned int uIdx = 0;
- while (Buffer.GetLineFormat(uIdx++, sLine)) {
- sFile += sLine + "\n";
+ unsigned int uSize = Buffer.Size();
+ for (unsigned int uIdx = 0; uIdx < uSize; uIdx++) {
+ const CBufLine& Line = Buffer.GetBufLine(uIdx);
+ sFile += Line.GetFormat() + "\n";
}
CBlowfish c(m_sPassword, BF_ENCRYPT);
View
@@ -170,11 +170,10 @@ class CWatcherMod : public CModule {
MCString msParams;
msParams["target"] = m_pNetwork->GetCurNick();
- CString sBufLine;
- while (m_Buffer.GetNextLine(sBufLine, msParams)) {
- PutUser(sBufLine);
+ unsigned int uSize = m_Buffer.Size();
+ for (unsigned int uIdx = 0; uIdx < uSize; uIdx++) {
+ PutUser(m_Buffer.GetLine(uIdx, msParams));
}
-
m_Buffer.Clear();
}
View
@@ -15,8 +15,8 @@ CBufLine::CBufLine(const CString& sFormat) {
CBufLine::~CBufLine() {}
-void CBufLine::GetLine(CString& sRet, const MCString& msParams) const {
- sRet = CString::NamedFormat(m_sFormat, msParams);
+CString CBufLine::GetLine(const MCString& msParams) const {
+ return CString::NamedFormat(m_sFormat, msParams);
}
CBuffer::CBuffer(unsigned int uLineCount) {
@@ -59,34 +59,12 @@ int CBuffer::UpdateExactLine(const CString& sFormat) {
return AddLine(sFormat);
}
-bool CBuffer::GetLineFormat(unsigned int uIdx, CString& sRet) const {
- if (uIdx >= size()) {
- return false;
- }
-
- sRet = (*this)[uIdx].GetFormat();
- return true;
+const CBufLine& CBuffer::GetBufLine(unsigned int uIdx) const {
+ return (*this)[uIdx];
}
-bool CBuffer::GetLine(unsigned int uIdx, CString& sRet, const MCString& msParams) const {
- if (uIdx >= size()) {
- return false;
- }
-
- (*this)[uIdx].GetLine(sRet, msParams);
- return true;
-}
-
-bool CBuffer::GetNextLine(CString& sRet, const MCString& msParams) {
- sRet = "";
-
- if (!size()) {
- return false;
- }
-
- begin()->GetLine(sRet, msParams);
- erase(begin());
- return true;
+CString CBuffer::GetLine(unsigned int uIdx, const MCString& msParams) const {
+ return (*this)[uIdx].GetLine(msParams);
}
bool CBuffer::SetLineCount(unsigned int u, bool bForce) {
View
@@ -536,9 +536,9 @@ void CChan::SendBuffer(CClient* pClient) {
m_pNetwork->PutUser(":***!znc@znc.in PRIVMSG " + GetName() + " :Buffer Playback...", pUseClient);
}
- CString sLine;
- unsigned int uIdx = 0;
- while (m_Buffer.GetLine(uIdx++, sLine)) {
+ unsigned int uSize = m_Buffer.Size();
+ for (unsigned int uIdx = 0; uIdx < uSize; uIdx++) {
+ CString sLine = m_Buffer.GetLine(uIdx);
NETWORKMODULECALL(OnChanBufferPlayLine(*this, *pUseClient, sLine), m_pNetwork->GetUser(), m_pNetwork, NULL, continue);
m_pNetwork->PutUser(sLine, pUseClient);
}
View
@@ -344,27 +344,26 @@ void CIRCNetwork::ClientConnected(CClient *pClient) {
m_vClients.push_back(pClient);
- unsigned int uIdx;
- CString sLine;
+ unsigned int uIdx, uSize;
MCString msParams;
msParams["target"] = GetIRCNick().GetNick();
if (m_RawBuffer.IsEmpty()) {
pClient->PutClient(":irc.znc.in 001 " + pClient->GetNick() + " :- Welcome to ZNC -");
} else {
- uIdx = 0;
- while (m_RawBuffer.GetLine(uIdx++, sLine, msParams)) {
- pClient->PutClient(sLine);
+ uSize = m_RawBuffer.Size();
+ for (uIdx = 0; uIdx < uSize; uIdx++) {
+ pClient->PutClient(m_RawBuffer.GetLine(uIdx, msParams));
}
// The assumption is that the client got this nick from the 001 reply
pClient->SetNick(GetIRCNick().GetNick());
}
// Send the cached MOTD
- uIdx = 0;
- while (m_MotdBuffer.GetLine(uIdx++, sLine, msParams)) {
- pClient->PutClient(sLine);
+ uSize = m_MotdBuffer.Size();
+ for (uIdx = 0; uIdx < uSize; uIdx++) {
+ pClient->PutClient(m_MotdBuffer.GetLine(uIdx, msParams));
}
if (GetIRCSock() != NULL) {
@@ -392,10 +391,13 @@ void CIRCNetwork::ClientConnected(CClient *pClient) {
}
}
- while (m_QueryBuffer.GetNextLine(sLine, msParams)) {
+ uSize = m_QueryBuffer.Size();
+ for (uIdx = 0; uIdx < uSize; uIdx++) {
+ CString sLine = m_QueryBuffer.GetLine(uIdx, msParams);
NETWORKMODULECALL(OnPrivBufferPlayLine(*pClient, sLine), m_pUser, this, NULL, continue);
pClient->PutClient(sLine);
}
+ m_QueryBuffer.Clear();
// Tell them why they won't connect
if (!m_pUser->GetIRCConnectEnabled())

0 comments on commit c3d677a

Please sign in to comment.