Source

oberchatten / History.cpp

Full commit
/*
Copyright (C) 2001-2005 Daybo Logic.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
//---------------------------------------------------------------------------
#include "overchat_config.h"
#include <assert>
#include <deque>
#include <string>
#include <time>
#include <stdio>
#include <new>
#ifdef HDRSTOP
# pragma hdrstop
#endif //HDRSTOP

#include "master.h"
#include "History.h" // InfoBox()
//---------------------------------------------------------------------------
#pragma package(smart_init)

using namespace std;
using namespace Oberchatten;

#ifdef OVERCHAT_RCS_STAMPS
static char rcsid[] = "$Id: MsgUnit.cpp,v 0.6 2005/09/22 17:03:00 ddrp Exp $";
#endif /*OVERCHAT_RCS_STAMPS*/

static const string emptyString;
//---------------------------------------------------------------------------
Oberchatten::TMessageHistory::TMessageHistory()
        : pos(0U),
          limit(20U)
{
  #ifdef OVERCHAT_RCS_STAMPS
    blake_Unused(rcsid);
  #endif /*OVERCHAT_RCS_STAMPS*/

  history.size = sizeof(history);
  blake_InitLinkPtrs(&history, sizeof(string*));
}
//---------------------------------------------------------------------------
Oberchatten::TMessageHistory::~TMessageHistory()
{
  ClearHistory(); // Cleanup
}
//---------------------------------------------------------------------------
const string* Oberchatten::TMessageHistory::Previous()
{
  if ( history.Count(&history) ) {
    const string* ptr;
    history.Get(&history, pos, &ptr);
    if ( pos ) pos--;
    return ptr;
  }
  return &emptyString;
}
//---------------------------------------------------------------------------
const char* Oberchatten::TMessageHistory::PreviousCompat()
{
  const string* prev = Previous();
  return (( prev ) ? ( prev->c_str() ) : ( NULL ));
}
//---------------------------------------------------------------------------
const string* Oberchatten::TMessageHistory::Next()
{
  unsigned int historyItemCount = history.Count(&history);
  if ( historyItemCount ) {
    if ( (pos+1) < historyItemCount ) {
      const string* ptr = NULL;
      history.Get(&history, ++pos, &ptr);
    }
  }
  return &emptyString;
}
//---------------------------------------------------------------------------
const char* Oberchatten::TMessageHistory::NextCompat()
{
  const string* next = Next();
  return (( next ) ? ( next->c_str() ) : ( NULL ));
}
//---------------------------------------------------------------------------
void Oberchatten::TMessageHistory::ClearHistory()
{
  while ( history.Count(&history) ) {
    string* ptr;
    unsigned int pos2kill = history.Count(&history);
    if ( pos2kill ) {
      history.Get(&history, --pos2kill, &ptr);
      if ( ptr )
        delete ptr;

      history.DelByPos(&history, pos2kill);
    }
  }
}
//---------------------------------------------------------------------------
void Oberchatten::TMessageHistory::GoToEnd()
{
  pos = history.Count(&history);
  if ( pos ) pos--;
}
//---------------------------------------------------------------------------
bool Oberchatten::TMessageHistory::AddString(const string& Str)
{
  return AddString(&Str);
}
//---------------------------------------------------------------------------
bool Oberchatten::TMessageHistory::AddString(const string* PStr)
{
  if ( PStr ) {
    if ( PStr->length() ) {
      string* newStr;
      try {
        newStr = new string;
      }
      catch ( bad_alloc ) {
        newStr = NULL;
      }
      if ( newStr ) {
        *newStr = *PStr;
        history.AddToHead(&history, &newStr);
        EnforceBufferLimit();
        GoToEnd();
        return true;
      }
    }
  }
  return false;
}
//---------------------------------------------------------------------------
bool Oberchatten::TMessageHistory::AddString(const char* Str)
{
  assert(Str);
  if ( Str && Str[0] ) {
    string string2Add(Str);
    return AddString(string2Add);
  }
  return false;
}
//---------------------------------------------------------------------------
void Oberchatten::TMessageHistory::SetBufferLimit(const unsigned int Limit)
{
  if ( Limit != limit ) {
    limit = Limit;
    EnforceBufferLimit();
  }
}
//---------------------------------------------------------------------------
void Oberchatten::TMessageHistory::EnforceBufferLimit()
{
  while ( history.Count(&history) > limit ) {
    string* ptr;
    unsigned int pos2kill = history.Count(&history);
    if ( pos2kill ) {
      history.Get(&history, --pos2kill, &ptr);
      if ( ptr )
        delete ptr;

      history.DelByPos(&history, pos2kill);
    }
  }
}
//---------------------------------------------------------------------------