Doxygen
bufstr.h
浏览该文件的文档.
1 /******************************************************************************
2  *
3  *
4  *
5  *
6  * Copyright (C) 1997-2015 by Dimitri van Heesch.
7  *
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.
13  *
14  * Documents produced by Doxygen are derivative works derived from the
15  * input used in their production; they are not affected by this license.
16  *
17  */
18 #ifndef BUFSTR_H
19 #define BUFSTR_H
20 
21 #include <cstdlib>
22 #include "qcstring.h"
23 
24 /*! @brief Buffer used to store strings
25  *
26  * This buffer is used append characters and strings. It will automatically
27  * resize itself, yet provide efficient random access to the content.
28  */
29 class BufStr
30 {
31  public:
33  : m_size(size), m_writeOffset(0), m_spareRoom(10240), m_buf(0)
34  {
35  m_buf = (char *)calloc(size,1);
36  }
37  ~BufStr()
38  {
39  free(m_buf);
40  }
41  void addChar(char c)
42  {
43  makeRoomFor(1);
44  m_buf[m_writeOffset++]=c;
45  }
46  void addArray(const char *a,uint len)
47  {
49  memcpy(m_buf+m_writeOffset,a,len);
50  m_writeOffset+=len;
51  }
52  void skip(uint s)
53  {
54  makeRoomFor(s);
55  m_writeOffset+=s;
56  }
57  void shrink( uint newlen )
58  {
59  m_writeOffset=newlen;
60  resize(newlen);
61  }
62  void resize( uint newlen )
63  {
64  uint oldsize = m_size;
65  m_size=newlen;
66  if (m_writeOffset>=m_size) // offset out of range -> enlarge
67  {
69  }
70  m_buf = (char *)realloc(m_buf,m_size);
71  if (m_size>oldsize)
72  {
73  memset(m_buf+oldsize,0,m_size-oldsize);
74  }
75  }
76  uint size() const
77  {
78  return m_size;
79  }
80  char *data() const
81  {
82  return m_buf;
83  }
84  char &at(uint i) const
85  {
86  return m_buf[i];
87  }
88  bool isEmpty() const
89  {
90  return m_writeOffset==0;
91  }
92  operator const char *() const
93  {
94  return m_buf;
95  }
96  uint curPos() const
97  {
98  return m_writeOffset;
99  }
100  void dropFromStart(uint bytes)
101  {
102  if (bytes>m_size) bytes=m_size;
103  if (bytes>0) qmemmove(m_buf,m_buf+bytes,m_size-bytes);
104  m_size-=bytes;
105  m_writeOffset-=bytes;
106  }
107  private:
109  {
111  {
113  }
114  }
115  uint m_size;
117  const uint m_spareRoom; // 10Kb extra room to avoid frequent resizing
118  char *m_buf;
119 };
120 
121 
122 #endif
BufStr
Buffer used to store strings
Definition: bufstr.h:29
BufStr::m_spareRoom
const uint m_spareRoom
Definition: bufstr.h:133
BufStr::at
char & at(uint i) const
Definition: bufstr.h:100
BufStr::data
char * data() const
Definition: bufstr.h:96
BufStr::shrink
void shrink(uint newlen)
Definition: bufstr.h:73
BufStr::m_buf
char * m_buf
Definition: bufstr.h:134
BufStr::dropFromStart
void dropFromStart(uint bytes)
Definition: bufstr.h:116
BufStr::BufStr
BufStr(uint size)
Definition: bufstr.h:48
qcstring.h
uint
unsigned uint
Definition: qcstring.h:40
BufStr::size
uint size() const
Definition: bufstr.h:92
BufStr::skip
void skip(uint s)
Definition: bufstr.h:68
BufStr::addArray
void addArray(const char *a, uint len)
Definition: bufstr.h:62
BufStr::makeRoomFor
void makeRoomFor(uint size)
Definition: bufstr.h:124
BufStr::~BufStr
~BufStr()
Definition: bufstr.h:53
BufStr::curPos
uint curPos() const
Definition: bufstr.h:112
BufStr::isEmpty
bool isEmpty() const
Definition: bufstr.h:104
BufStr::m_size
uint m_size
Definition: bufstr.h:131
BufStr::m_writeOffset
uint m_writeOffset
Definition: bufstr.h:132
BufStr::resize
void resize(uint newlen)
Definition: bufstr.h:78
BufStr::addChar
void addChar(char c)
Definition: bufstr.h:57
qmemmove
void * qmemmove(void *dst, const void *src, size_t len)
Definition: qcstring.cpp:397