Fix for UBSan build
[platform/upstream/doxygen.git] / src / growbuf.h
1 #ifndef GROWBUF_H
2 #define GROWBUF_H
3
4 #include <stdlib.h>
5 #include <string.h>
6
7 /** Class representing a string buffer optimised for growing. */
8 class GrowBuf
9 {
10   public:
11     GrowBuf() : str(0), pos(0), len(0) {}
12    ~GrowBuf()         { free(str); str=0; pos=0; len=0; }
13     void clear()      { pos=0; }
14     void addChar(char c)  { if (pos>=len) { len+=1024; str = (char*)realloc(str,len); } 
15                         str[pos++]=c; 
16                       }
17     void addStr(const char *s) {
18                         int l=strlen(s);
19                         if (pos+l>=len) { len+=l+1024; str = (char*)realloc(str,len); }
20                         strcpy(&str[pos],s);
21                         pos+=l;
22                       }
23     void addStr(const char *s,int n) {
24                         int l=strlen(s);
25                         if (n<l) l=n;
26                         if (pos+l>=len) { len+=l+1024; str = (char*)realloc(str,len); }
27                         strncpy(&str[pos],s,n);
28                         pos+=l;
29                       }
30     const char *get()     { return str; }
31     int getPos() const    { return pos; }
32     char at(int i) const  { return str[i]; }
33   private:
34     char *str;
35     int pos;
36     int len;
37 };
38
39 #endif