1 /******************************************************************************
3 * $Id: bufstr.h,v 1.85 2000/11/18 12:58:19 root Exp $
6 * Copyright (C) 1997-2012 by Dimitri van Heesch.
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation under the terms of the GNU General Public License is hereby
10 * granted. No representations are made about the suitability of this software
11 * for any purpose. It is provided "as is" without express or implied warranty.
12 * See the GNU General Public License for more details.
14 * Documents produced by Doxygen are derivative works derived from the
15 * input used in their production; they are not affected by this license.
25 /*! @brief Buffer used to store strings
27 * This buffer is used append characters and strings. It will automatically
28 * resize itself, yet provide efficient random access to the content.
34 : m_size(size), m_writeOffset(0), m_spareRoom(10240), m_buf(0)
36 m_buf = (char *)malloc(size);
45 m_buf[m_writeOffset++]=c;
47 void addArray(const char *a,int len)
50 memcpy(m_buf+m_writeOffset,a,len);
58 void shrink( uint newlen )
63 void resize( uint newlen )
66 if (m_writeOffset>=m_size) // offset out of range -> enlarge
68 m_size=m_writeOffset+m_spareRoom;
70 m_buf = (char *)realloc(m_buf,m_size);
80 char &at(uint i) const
86 return m_writeOffset==0;
88 operator const char *() const
96 void dropFromStart(uint bytes)
98 if (bytes>m_size) bytes=m_size;
99 if (bytes>0) qmemmove(m_buf,m_buf+bytes,m_size-bytes);
101 m_writeOffset-=bytes;
104 void makeRoomFor(uint size)
106 if (m_writeOffset+size>=m_size)
108 resize(m_size+size+m_spareRoom);
113 const int m_spareRoom; // 10Kb extra room to avoid frequent resizing