|
1 /****************************************************************************** |
|
2 * |
|
3 * |
|
4 * |
|
5 * |
|
6 * Copyright (C) 1997-2008 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 "qtbc.h" |
|
22 #include <stdio.h> |
|
23 #include <stdlib.h> |
|
24 |
|
25 /*! @brief Buffer used to store strings |
|
26 * |
|
27 * This buffer is used append characters and strings. It will automatically |
|
28 * resize itself, yet provide efficient random access to the content. |
|
29 */ |
|
30 class BufStr |
|
31 { |
|
32 public: |
|
33 BufStr(int size) |
|
34 : m_size(size), m_writeOffset(0), m_spareRoom(10240), m_buf(0) |
|
35 { |
|
36 m_buf = (char *)malloc(size); |
|
37 } |
|
38 ~BufStr() |
|
39 { |
|
40 free(m_buf); |
|
41 } |
|
42 void addChar(char c) |
|
43 { |
|
44 makeRoomFor(1); |
|
45 m_buf[m_writeOffset++]=c; |
|
46 } |
|
47 void addArray(const char *a,int len) |
|
48 { |
|
49 makeRoomFor(len); |
|
50 memcpy(m_buf+m_writeOffset,a,len); |
|
51 m_writeOffset+=len; |
|
52 } |
|
53 void skip(uint s) |
|
54 { |
|
55 makeRoomFor(s); |
|
56 m_writeOffset+=s; |
|
57 } |
|
58 void shrink( uint newlen ) |
|
59 { |
|
60 m_writeOffset=newlen; |
|
61 resize(newlen); |
|
62 } |
|
63 void resize( uint newlen ) |
|
64 { |
|
65 m_size=newlen; |
|
66 if (m_writeOffset>=m_size) // offset out of range -> enlarge |
|
67 { |
|
68 m_size=m_writeOffset+m_spareRoom; |
|
69 } |
|
70 m_buf = (char *)realloc(m_buf,m_size); |
|
71 } |
|
72 int size() const |
|
73 { |
|
74 return m_size; |
|
75 } |
|
76 char *data() const |
|
77 { |
|
78 return m_buf; |
|
79 } |
|
80 char &at(uint i) const |
|
81 { |
|
82 return m_buf[i]; |
|
83 } |
|
84 bool isEmpty() const |
|
85 { |
|
86 return m_writeOffset==0; |
|
87 } |
|
88 operator const char *() const |
|
89 { |
|
90 return m_buf; |
|
91 } |
|
92 uint curPos() const |
|
93 { |
|
94 return m_writeOffset; |
|
95 } |
|
96 void dropFromStart(uint bytes) |
|
97 { |
|
98 if (bytes>m_size) bytes=m_size; |
|
99 if (bytes>0) qmemmove(m_buf,m_buf+bytes,m_size-bytes); |
|
100 m_size-=bytes; |
|
101 m_writeOffset-=bytes; |
|
102 } |
|
103 private: |
|
104 void makeRoomFor(uint size) |
|
105 { |
|
106 if (m_writeOffset+size>=m_size) |
|
107 { |
|
108 resize(m_size+size+m_spareRoom); |
|
109 } |
|
110 } |
|
111 uint m_size; |
|
112 uint m_writeOffset; |
|
113 const int m_spareRoom; // 10Kb extra room to avoid frequent resizing |
|
114 char *m_buf; |
|
115 }; |
|
116 |
|
117 |
|
118 #endif |