growbuf.h
Go to the documentation of this file.
1 #ifndef GROWBUF_H
2 #define GROWBUF_H
3 
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #define GROW_AMOUNT 1024
8 
9 /** Class representing a string buffer optimised for growing. */
10 class GrowBuf
11 {
12  public:
13  GrowBuf() : str(0), pos(0), len(0) {}
14  ~GrowBuf() { free(str); str=0; pos=0; len=0; }
15  void clear() { pos=0; }
16  void addChar(char c) { if (pos>=len) { len+=GROW_AMOUNT; str = (char*)realloc(str,len); }
17  str[pos++]=c;
18  }
19  void addStr(const char *s) {
20  if (s)
21  {
22  int l=strlen(s);
23  if (pos+l>=len) { len+=l+GROW_AMOUNT; str = (char*)realloc(str,len); }
24  strcpy(&str[pos],s);
25  pos+=l;
26  }
27  }
28  void addStr(const char *s,int n) {
29  if (s)
30  {
31  int l=strlen(s);
32  if (n<l) l=n;
33  if (pos+l>=len) { len+=l+GROW_AMOUNT; str = (char*)realloc(str,len); }
34  strncpy(&str[pos],s,n);
35  pos+=l;
36  }
37  }
38  const char *get() { return str; }
39  int getPos() const { return pos; }
40  char at(int i) const { return str[i]; }
41  private:
42  char *str;
43  int pos;
44  int len;
45 };
46 
47 #endif
void clear()
Definition: growbuf.h:15
void addStr(const char *s)
Definition: growbuf.h:19
int pos
Definition: growbuf.h:43
static QStrList * l
Definition: config.cpp:1044
#define GROW_AMOUNT
Definition: growbuf.h:7
void addChar(char c)
Definition: growbuf.h:16
int len
Definition: growbuf.h:44
std::void_t< T > n
void addStr(const char *s, int n)
Definition: growbuf.h:28
char * str
Definition: growbuf.h:42
char at(int i) const
Definition: growbuf.h:40
int getPos() const
Definition: growbuf.h:39
GrowBuf()
Definition: growbuf.h:13
~GrowBuf()
Definition: growbuf.h:14
static QCString * s
Definition: config.cpp:1042