summaryrefslogtreecommitdiff
path: root/src/bufstr.h
blob: 117469aa2897ba46bff892a9462da4ab81a7a259 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/******************************************************************************
 *
 * $Id: bufstr.h,v 1.85 2000/11/18 12:58:19 root Exp $
 *
 *
 * Copyright (C) 1997-2012 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 */
#ifndef _BUFSTR_H
#define _BUFSTR_H

#include "qtbc.h"
#include <stdio.h>
#include <stdlib.h>

/*! @brief Buffer used to store strings
 *  
 *  This buffer is used append characters and strings. It will automatically
 *  resize itself, yet provide efficient random access to the content.
 */
class BufStr 
{
  public:
    BufStr(int size) 
      : m_size(size), m_writeOffset(0), m_spareRoom(10240), m_buf(0) 
    {
      m_buf = (char *)malloc(size);
    }
    ~BufStr()
    {
      free(m_buf);
    }
    void addChar(char c)
    {
      makeRoomFor(1);
      m_buf[m_writeOffset++]=c;
    }
    void addArray(const char *a,int len)
    {
      makeRoomFor(len);
      memcpy(m_buf+m_writeOffset,a,len);
      m_writeOffset+=len;
    }
    void skip(uint s)
    {
      makeRoomFor(s);
      m_writeOffset+=s;
    }
    void shrink( uint newlen )
    {
      m_writeOffset=newlen;
      resize(newlen);
    }
    void resize( uint newlen )
    {
      m_size=newlen;
      if (m_writeOffset>=m_size) // offset out of range -> enlarge
      {
        m_size=m_writeOffset+m_spareRoom;
      }
      m_buf = (char *)realloc(m_buf,m_size);
    }
    int size() const
    {
      return m_size;
    }
    char *data() const
    {
      return m_buf;
    }
    char &at(uint i) const
    {
      return m_buf[i];
    }
    bool isEmpty() const
    {
      return m_writeOffset==0;
    }
    operator const char *() const
    {
      return m_buf;
    }
    uint curPos() const
    { 
      return m_writeOffset; 
    }
    void dropFromStart(uint bytes)
    {
      if (bytes>m_size) bytes=m_size;
      if (bytes>0) qmemmove(m_buf,m_buf+bytes,m_size-bytes);
      m_size-=bytes;
      m_writeOffset-=bytes;
    }
  private:
    void makeRoomFor(uint size)
    {
      if (m_writeOffset+size>=m_size) 
      {
        resize(m_size+size+m_spareRoom);
      }
    }
    uint m_size;
    uint m_writeOffset;
    const int m_spareRoom; // 10Kb extra room to avoid frequent resizing
    char *m_buf;
};


#endif