Public Member Functions | Private Attributes | List of all members
QGString Class Reference

#include <qgstring.h>

Public Member Functions

 QGString ()
 
 QGString (uint size)
 
 QGString (const QGString &s)
 
 QGString (const char *str)
 
 ~QGString ()
 
bool resize (uint newlen)
 
bool enlarge (uint newlen)
 
void setLen (uint newlen)
 
QGStringoperator= (const QGString &s)
 
QGStringoperator= (const char *str)
 
QGStringoperator+= (const QGString &s)
 
QGStringoperator+= (const char *str)
 
QGStringoperator+= (char c)
 
bool isNull () const
 
bool isEmpty () const
 
uint length () const
 
uint size () const
 
char * data () const
 
bool truncate (uint pos)
 
 operator const char * () const
 
char & at (uint index) const
 
char & operator[] (int i) const
 

Private Attributes

char * m_data
 
uint m_len
 
uint m_memSize
 

Detailed Description

This is an alternative implementation of QCString.

Definition at line 19 of file qgstring.h.

Constructor & Destructor Documentation

QGString::QGString ( )

Definition at line 27 of file qgstring.cpp.

28  : m_data(0), m_len(0), m_memSize(0)
29 {
30  DBG_STR(("%p: QGString::QGString() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
31 }
uint m_memSize
Definition: qgstring.h:51
uint m_len
Definition: qgstring.h:50
#define DBG_STR(x)
Definition: qgstring.cpp:24
char * m_data
Definition: qgstring.h:49
QGString::QGString ( uint  size)

Definition at line 33 of file qgstring.cpp.

34 {
35  if (size==0)
36  {
37  m_data=0;
38  m_len=0;
39  }
40  else
41  {
43  m_data = (char*)malloc(m_memSize);
44  memset(m_data,' ',size);
45  m_data[size]='\0';
46  m_len=size;
47  }
48  DBG_STR(("%p: QGString::QGString(uint size=%d) %d:%s\n",
49  this,size,m_len,m_data?m_data:"<none>"));
50 }
uint m_memSize
Definition: qgstring.h:51
uint m_len
Definition: qgstring.h:50
uint size() const
Definition: qgstring.h:41
#define ROUND_SIZE(x)
Definition: qgstring.cpp:22
#define DBG_STR(x)
Definition: qgstring.cpp:24
char * m_data
Definition: qgstring.h:49
QGString::QGString ( const QGString s)

Definition at line 52 of file qgstring.cpp.

53 {
54  if (s.m_memSize==0)
55  {
56  m_data = 0;
57  m_len = 0;
58  m_memSize = 0;
59  }
60  else
61  {
62  m_data = (char *)malloc(s.m_memSize);
63  m_len = s.m_len;
64  m_memSize = s.m_memSize;
66  }
67  DBG_STR(("%p: QGString::QGString(const QGString &) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
68 }
uint m_memSize
Definition: qgstring.h:51
uint m_len
Definition: qgstring.h:50
#define DBG_STR(x)
Definition: qgstring.cpp:24
Q_EXPORT char * qstrcpy(char *dst, const char *src)
Definition: qcstring.h:87
char * m_data
Definition: qgstring.h:49
QGString::QGString ( const char *  str)

Definition at line 70 of file qgstring.cpp.

71 {
72  if (str==0)
73  {
74  m_data=0;
75  m_len=0;
76  m_memSize=0;
77  }
78  else
79  {
80  m_len = qstrlen(str);
82  assert(m_memSize>=m_len+1);
83  m_data = (char *)malloc(m_memSize);
85  }
86  DBG_STR(("%p: QGString::QGString(const char *) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
87 }
uint m_memSize
Definition: qgstring.h:51
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
uint m_len
Definition: qgstring.h:50
#define ROUND_SIZE(x)
Definition: qgstring.cpp:22
#define DBG_STR(x)
Definition: qgstring.cpp:24
Q_EXPORT char * qstrcpy(char *dst, const char *src)
Definition: qcstring.h:87
char * m_data
Definition: qgstring.h:49
static QCString str
QGString::~QGString ( )

Definition at line 89 of file qgstring.cpp.

90 {
91  free(m_data);
92  m_data=0;
93  DBG_STR(("%p: QGString::~QGString() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
94 }
uint m_len
Definition: qgstring.h:50
#define DBG_STR(x)
Definition: qgstring.cpp:24
char * m_data
Definition: qgstring.h:49

Member Function Documentation

char& QGString::at ( uint  index) const
inline

Definition at line 45 of file qgstring.h.

45 { return m_data[index]; }
char * m_data
Definition: qgstring.h:49
char* QGString::data ( ) const
inline

Definition at line 42 of file qgstring.h.

42 { return m_data; }
char * m_data
Definition: qgstring.h:49
bool QGString::enlarge ( uint  newlen)

Definition at line 127 of file qgstring.cpp.

128 {
129  if (newlen==0)
130  {
131  if (m_data) { free(m_data); m_data=0; }
132  m_memSize=0;
133  m_len=0;
134  return TRUE;
135  }
136  uint newMemSize = ROUND_SIZE(newlen+1);
137  if (newMemSize==m_memSize) return TRUE;
138  m_memSize = newMemSize;
139  if (m_data==0)
140  {
141  m_data = (char *)malloc(m_memSize);
142  }
143  else
144  {
145  m_data = (char *)realloc(m_data,m_memSize);
146  }
147  if (m_data==0)
148  {
149  return FALSE;
150  }
151  m_data[newlen-1]='\0';
152  if (m_len>newlen) m_len=newlen;
153  return TRUE;
154 }
const bool FALSE
Definition: qglobal.h:370
uint m_memSize
Definition: qgstring.h:51
uint m_len
Definition: qgstring.h:50
#define ROUND_SIZE(x)
Definition: qgstring.cpp:22
unsigned uint
Definition: qglobal.h:351
char * m_data
Definition: qgstring.h:49
const bool TRUE
Definition: qglobal.h:371
bool QGString::isEmpty ( ) const
inline

Definition at line 39 of file qgstring.h.

39 { return m_len==0; }
uint m_len
Definition: qgstring.h:50
bool QGString::isNull ( ) const
inline

Definition at line 38 of file qgstring.h.

38 { return m_data==0; }
char * m_data
Definition: qgstring.h:49
uint QGString::length ( ) const
inline

Definition at line 40 of file qgstring.h.

40 { return m_len; }
uint m_len
Definition: qgstring.h:50
QGString::operator const char * ( ) const
inline

Definition at line 44 of file qgstring.h.

44 { return (const char *)data(); }
char * data() const
Definition: qgstring.h:42
QGString & QGString::operator+= ( const QGString s)

Definition at line 203 of file qgstring.cpp.

204 {
205  if (s.m_memSize==0) return *this;
206  uint len1 = length();
207  uint len2 = s.length();
208  uint memSize = ROUND_SIZE(len1 + len2 + 1);
209  assert(memSize>=len1+len2+1);
210  char *newData = memSize!=m_memSize ? (char*)realloc( m_data, memSize ) : m_data;
211  m_memSize = memSize;
212  if (newData)
213  {
214  m_data = newData;
215  memcpy( m_data + len1, s, len2 + 1 );
216  }
217  m_len = len1+len2;
218  DBG_STR(("%p: QGString::operator+=(const QGString &) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
219  return *this;
220 }
uint length() const
Definition: qgstring.h:40
uint m_memSize
Definition: qgstring.h:51
uint m_len
Definition: qgstring.h:50
#define ROUND_SIZE(x)
Definition: qgstring.cpp:22
#define DBG_STR(x)
Definition: qgstring.cpp:24
unsigned uint
Definition: qglobal.h:351
char * m_data
Definition: qgstring.h:49
QGString & QGString::operator+= ( const char *  str)

Definition at line 222 of file qgstring.cpp.

223 {
224  if (!str) return *this;
225  uint len1 = length();
226  uint len2 = qstrlen(str);
227  uint memSize = ROUND_SIZE(len1 + len2 + 1);
228  assert(memSize>=len1+len2+1);
229  char *newData = memSize!=m_memSize ? (char *)realloc( m_data, memSize ) : m_data;
230  m_memSize = memSize;
231  if (newData)
232  {
233  m_data = newData;
234  memcpy( m_data + len1, str, len2 + 1 );
235  }
236  m_len+=len2;
237  DBG_STR(("%p: QGString::operator+=(const char *) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
238  return *this;
239 }
uint length() const
Definition: qgstring.h:40
uint m_memSize
Definition: qgstring.h:51
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
uint m_len
Definition: qgstring.h:50
#define ROUND_SIZE(x)
Definition: qgstring.cpp:22
#define DBG_STR(x)
Definition: qgstring.cpp:24
unsigned uint
Definition: qglobal.h:351
char * m_data
Definition: qgstring.h:49
static QCString str
QGString & QGString::operator+= ( char  c)

Definition at line 241 of file qgstring.cpp.

242 {
243  uint len = m_len;
244  uint memSize = ROUND_SIZE(len+2);
245  assert(memSize>=len+2);
246  char *newData = memSize!=m_memSize ? (char *)realloc( m_data, memSize ) : m_data;
247  m_memSize = memSize;
248  if (newData)
249  {
250  m_data = newData;
251  m_data[len] = c;
252  m_data[len+1] = '\0';
253  }
254  m_len++;
255  DBG_STR(("%p: QGString::operator+=(char s) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
256  return *this;
257 }
uint m_memSize
Definition: qgstring.h:51
uint m_len
Definition: qgstring.h:50
#define ROUND_SIZE(x)
Definition: qgstring.cpp:22
#define DBG_STR(x)
Definition: qgstring.cpp:24
unsigned uint
Definition: qglobal.h:351
char * m_data
Definition: qgstring.h:49
QGString & QGString::operator= ( const QGString s)

Definition at line 161 of file qgstring.cpp.

162 {
163  if (m_data) free(m_data);
164  if (s.m_memSize==0) // null string
165  {
166  m_data = 0;
167  m_len = 0;
168  m_memSize = 0;
169  }
170  else
171  {
172  m_len = s.m_len;
173  m_memSize = s.m_memSize;
174  m_data = (char*)malloc(m_memSize);
175  qstrcpy(m_data,s.m_data);
176  }
177  DBG_STR(("%p: QGString::operator=(const QGString &%p) %d:%s\n",
178  this,&s,m_len,m_data?m_data:"<none>"));
179  return *this;
180 }
uint m_memSize
Definition: qgstring.h:51
uint m_len
Definition: qgstring.h:50
#define DBG_STR(x)
Definition: qgstring.cpp:24
Q_EXPORT char * qstrcpy(char *dst, const char *src)
Definition: qcstring.h:87
char * m_data
Definition: qgstring.h:49
QGString & QGString::operator= ( const char *  str)

Definition at line 182 of file qgstring.cpp.

183 {
184  if (m_data) free(m_data);
185  if (str==0) // null string
186  {
187  m_data = 0;
188  m_len = 0;
189  m_memSize = 0;
190  }
191  else
192  {
193  m_len = qstrlen(str);
195  assert(m_memSize>=m_len+1);
196  m_data = (char*)malloc(m_memSize);
197  qstrcpy(m_data,str);
198  }
199  DBG_STR(("%p: QGString::operator=(const char *) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
200  return *this;
201 }
uint m_memSize
Definition: qgstring.h:51
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
uint m_len
Definition: qgstring.h:50
#define ROUND_SIZE(x)
Definition: qgstring.cpp:22
#define DBG_STR(x)
Definition: qgstring.cpp:24
Q_EXPORT char * qstrcpy(char *dst, const char *src)
Definition: qcstring.h:87
char * m_data
Definition: qgstring.h:49
static QCString str
char& QGString::operator[] ( int  i) const
inline

Definition at line 46 of file qgstring.h.

46 { return at(i); }
char & at(uint index) const
Definition: qgstring.h:45
bool QGString::resize ( uint  newlen)

Definition at line 96 of file qgstring.cpp.

97 {
98  m_len = 0;
99  if (newlen==0)
100  {
101  if (m_data) { free(m_data); m_data=0; }
102  m_memSize=0;
103  DBG_STR(("%p: 1.QGString::resize() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
104  return TRUE;
105  }
106  m_memSize = ROUND_SIZE(newlen+1);
107  assert(m_memSize>=newlen+1);
108  if (m_data==0)
109  {
110  m_data = (char *)malloc(m_memSize);
111  }
112  else
113  {
114  m_data = (char *)realloc(m_data,m_memSize);
115  }
116  if (m_data==0)
117  {
118  DBG_STR(("%p: 2.QGString::resize() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
119  return FALSE;
120  }
121  m_data[newlen]='\0';
122  m_len = qstrlen(m_data);
123  DBG_STR(("%p: 3.QGString::resize() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
124  return TRUE;
125 }
const bool FALSE
Definition: qglobal.h:370
uint m_memSize
Definition: qgstring.h:51
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
uint m_len
Definition: qgstring.h:50
#define ROUND_SIZE(x)
Definition: qgstring.cpp:22
#define DBG_STR(x)
Definition: qgstring.cpp:24
char * m_data
Definition: qgstring.h:49
const bool TRUE
Definition: qglobal.h:371
void QGString::setLen ( uint  newlen)

Definition at line 156 of file qgstring.cpp.

157 {
158  m_len = newlen<=m_memSize ? newlen : m_memSize;
159 }
uint m_memSize
Definition: qgstring.h:51
uint m_len
Definition: qgstring.h:50
uint QGString::size ( ) const
inline

Definition at line 41 of file qgstring.h.

41 { return m_memSize; }
uint m_memSize
Definition: qgstring.h:51
bool QGString::truncate ( uint  pos)
inline

Definition at line 43 of file qgstring.h.

43 { return resize(pos+1); }
bool resize(uint newlen)
Definition: qgstring.cpp:96

Member Data Documentation

char* QGString::m_data
private

Definition at line 49 of file qgstring.h.

uint QGString::m_len
private

Definition at line 50 of file qgstring.h.

uint QGString::m_memSize
private

Definition at line 51 of file qgstring.h.


The documentation for this class was generated from the following files: