~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/jsparse/textbuf.h

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef TEXTBUF_H
2
 
#define TEXTBUF_H
3
 
struct text_buffer {
4
 
        char *data;
5
 
        size_t pos;
6
 
        size_t capacity;
7
 
};
8
 
 
9
 
static inline int textbuffer_ensure_capacity(struct text_buffer *txtbuf, size_t len)
10
 
{
11
 
        if (txtbuf->pos + len > txtbuf->capacity) {
12
 
                char *d;
13
 
                unsigned capacity = MAX(txtbuf->pos + len, txtbuf->capacity + 4096);
14
 
                d = cli_realloc(txtbuf->data, capacity);
15
 
                if(!d)
16
 
                        return -1;
17
 
                txtbuf->capacity = capacity;
18
 
                txtbuf->data = d;
19
 
        }
20
 
        return 0;
21
 
}
22
 
 
23
 
static inline int textbuffer_append_len(struct text_buffer *txtbuf, const char *s, size_t len)
24
 
{
25
 
        if(textbuffer_ensure_capacity(txtbuf, len) == -1)
26
 
                return -1;
27
 
        memcpy(&txtbuf->data[txtbuf->pos], s, len);
28
 
        txtbuf->pos += len;
29
 
        return 0;
30
 
}
31
 
 
32
 
 
33
 
static inline int textbuffer_append(struct text_buffer *txtbuf, const char *s)
34
 
{
35
 
        size_t len = strlen(s);
36
 
        return textbuffer_append_len(txtbuf, s, len);
37
 
}
38
 
 
39
 
static inline int textbuffer_putc(struct text_buffer *txtbuf, const char c)
40
 
{
41
 
        if(textbuffer_ensure_capacity(txtbuf, 1) == -1)
42
 
                return -1;
43
 
        txtbuf->data[txtbuf->pos++] = c;
44
 
        return 0;
45
 
}
46
 
#endif