Blame src/bufstr.h

Packit Service 50c9f2
/******************************************************************************
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * 
Packit Service 50c9f2
 *
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Copyright (C) 1997-2015 by Dimitri van Heesch.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Permission to use, copy, modify, and distribute this software and its
Packit Service 50c9f2
 * documentation under the terms of the GNU General Public License is hereby 
Packit Service 50c9f2
 * granted. No representations are made about the suitability of this software 
Packit Service 50c9f2
 * for any purpose. It is provided "as is" without express or implied warranty.
Packit Service 50c9f2
 * See the GNU General Public License for more details.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Documents produced by Doxygen are derivative works derived from the
Packit Service 50c9f2
 * input used in their production; they are not affected by this license.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 */
Packit Service 50c9f2
#ifndef _BUFSTR_H
Packit Service 50c9f2
#define _BUFSTR_H
Packit Service 50c9f2
Packit Service 50c9f2
#include <qglobal.h>
Packit Service 50c9f2
#include <qcstring.h>
Packit Service 50c9f2
#include <stdlib.h>
Packit Service 50c9f2
Packit Service 50c9f2
/*! @brief Buffer used to store strings
Packit Service 50c9f2
 *  
Packit Service 50c9f2
 *  This buffer is used append characters and strings. It will automatically
Packit Service 50c9f2
 *  resize itself, yet provide efficient random access to the content.
Packit Service 50c9f2
 */
Packit Service 50c9f2
class BufStr 
Packit Service 50c9f2
{
Packit Service 50c9f2
  public:
Packit Service 50c9f2
    BufStr(int size) 
Packit Service 50c9f2
      : m_size(size), m_writeOffset(0), m_spareRoom(10240), m_buf(0) 
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_buf = (char *)calloc(size,1);
Packit Service 50c9f2
    }
Packit Service 50c9f2
    ~BufStr()
Packit Service 50c9f2
    {
Packit Service 50c9f2
      free(m_buf);
Packit Service 50c9f2
    }
Packit Service 50c9f2
    void addChar(char c)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      makeRoomFor(1);
Packit Service 50c9f2
      m_buf[m_writeOffset++]=c;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    void addArray(const char *a,int len)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      makeRoomFor(len);
Packit Service 50c9f2
      memcpy(m_buf+m_writeOffset,a,len);
Packit Service 50c9f2
      m_writeOffset+=len;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    void skip(uint s)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      makeRoomFor(s);
Packit Service 50c9f2
      m_writeOffset+=s;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    void shrink( uint newlen )
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_writeOffset=newlen;
Packit Service 50c9f2
      resize(newlen);
Packit Service 50c9f2
    }
Packit Service 50c9f2
    void resize( uint newlen )
Packit Service 50c9f2
    {
Packit Service 50c9f2
      uint oldsize = m_size;
Packit Service 50c9f2
      m_size=newlen;
Packit Service 50c9f2
      if (m_writeOffset>=m_size) // offset out of range -> enlarge
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_size=m_writeOffset+m_spareRoom;
Packit Service 50c9f2
      }
Packit Service 50c9f2
      m_buf = (char *)realloc(m_buf,m_size);
Packit Service 50c9f2
      if (m_size>oldsize)
Packit Service 50c9f2
      {
Packit Service 50c9f2
        memset(m_buf+oldsize,0,m_size-oldsize);
Packit Service 50c9f2
      }
Packit Service 50c9f2
    }
Packit Service 50c9f2
    int size() const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_size;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    char *data() const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_buf;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    char &at(uint i) const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_buf[i];
Packit Service 50c9f2
    }
Packit Service 50c9f2
    bool isEmpty() const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_writeOffset==0;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    operator const char *() const
Packit Service 50c9f2
    {
Packit Service 50c9f2
      return m_buf;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    uint curPos() const
Packit Service 50c9f2
    { 
Packit Service 50c9f2
      return m_writeOffset; 
Packit Service 50c9f2
    }
Packit Service 50c9f2
    void dropFromStart(uint bytes)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      if (bytes>m_size) bytes=m_size;
Packit Service 50c9f2
      if (bytes>0) qmemmove(m_buf,m_buf+bytes,m_size-bytes);
Packit Service 50c9f2
      m_size-=bytes;
Packit Service 50c9f2
      m_writeOffset-=bytes;
Packit Service 50c9f2
    }
Packit Service 50c9f2
  private:
Packit Service 50c9f2
    void makeRoomFor(uint size)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      if (m_writeOffset+size>=m_size) 
Packit Service 50c9f2
      {
Packit Service 50c9f2
        resize(m_size+size+m_spareRoom);
Packit Service 50c9f2
      }
Packit Service 50c9f2
    }
Packit Service 50c9f2
    uint m_size;
Packit Service 50c9f2
    uint m_writeOffset;
Packit Service 50c9f2
    const int m_spareRoom; // 10Kb extra room to avoid frequent resizing
Packit Service 50c9f2
    char *m_buf;
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
#endif