summaryrefslogtreecommitdiff
path: root/src/bufstr.h
blob: bc0ef60cbc486b5b07a5ce5b61c854f64ee3833f (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/******************************************************************************
 *
 *
 *
 *
 * Copyright (C) 1997-2015 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 <cstdlib>
#include "qcstring.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(size_t size)
      : m_size(size), m_writeOffset(0), m_spareRoom(10240), m_buf(0)
    {
      m_buf = static_cast<char *>(calloc(size,1));
    }
    ~BufStr()
    {
      free(m_buf);
    }
    void addChar(char c)
    {
      makeRoomFor(1);
      m_buf[m_writeOffset++]=c;
    }
    void addArray(const char *a,size_t len)
    {
      makeRoomFor(len);
      memcpy(m_buf+m_writeOffset,a,len);
      m_writeOffset+=len;
    }
    void skip(size_t s)
    {
      makeRoomFor(s);
      m_writeOffset+=s;
    }
    void shrink( size_t newlen )
    {
      m_writeOffset=newlen;
      resize(newlen);
    }
    void resize( size_t newlen )
    {
      size_t oldsize = m_size;
      m_size=newlen;
      if (m_writeOffset>=m_size) // offset out of range -> enlarge
      {
        m_size=m_writeOffset+m_spareRoom;
      }
      m_buf = static_cast<char *>(realloc(m_buf,m_size));
      if (m_size>oldsize)
      {
        memset(m_buf+oldsize,0,m_size-oldsize);
      }
    }
    size_t size() const
    {
      return m_size;
    }
    char *data() const
    {
      return m_buf;
    }
    char &at(size_t i) const
    {
      return m_buf[i];
    }
    bool isEmpty() const
    {
      return m_writeOffset==0;
    }
    operator const char *() const
    {
      return m_buf;
    }
    size_t curPos() const
    {
      return m_writeOffset;
    }
    void dropFromStart(size_t 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;
    }
    void addTerminalCharIfMissing(char c)
    {
      if (m_buf && m_writeOffset>1 && m_buf[m_writeOffset-2]!=c && m_buf[m_writeOffset-1]=='\0')
      {
        // add missing terminal character and 0 terminator
        m_buf[m_writeOffset-1] = '\n';
        addChar('\0');
      }
      else if (m_buf && m_writeOffset>0 && m_buf[m_writeOffset-1]!='\0' && m_buf[m_writeOffset-1]!=c)
      {
        // add missing terminal character without 0 terminator
        addChar(c);
      }
    }
  private:
    void makeRoomFor(size_t size)
    {
      if (m_writeOffset+size>=m_size)
      {
        resize(m_size+size+m_spareRoom);
      }
    }
    size_t m_size;
    size_t m_writeOffset;
    const size_t m_spareRoom; // 10Kb extra room to avoid frequent resizing
    char *m_buf;
};


#endif