sort out some prototypes, put them where they belong.
[apps/madmutt.git] / lib-lib / buffer.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 /*
21  * Copyright notice from original mutt:
22  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
23  *
24  * This file is part of mutt-ng, see http://www.muttng.org/.
25  * It's licensed under the GNU General Public License,
26  * please see the file GPL in the top level source directory.
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32
33 #include "mem.h"
34 #include "str.h"
35 #include "ascii.h"
36 #include "buffer.h"
37 #include "file.h"
38
39 /*
40  * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
41  * just initializes. Frees anything already in the buffer.
42  *
43  * Disregards the 'destroy' flag, which seems reserved for caller.
44  * This is bad, but there's no apparent protocol for it.
45  */
46 BUFFER *mutt_buffer_init(BUFFER *b)
47 {
48     if (!b) {
49         b = p_new(BUFFER, 1);
50     }
51     p_delete(&b->data);
52     p_clear(b, 1);
53     return b;
54 }
55
56 /*
57  * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
58  * just initializes. Frees anything already in the buffer. Copies in
59  * the seed string.
60  *
61  * Disregards the 'destroy' flag, which seems reserved for caller.
62  * This is bad, but there's no apparent protocol for it.
63  */
64 BUFFER *mutt_buffer_from(BUFFER * b, const char *seed)
65 {
66     if (!seed)
67         return NULL;
68
69     b = mutt_buffer_init(b);
70     b->dsize = m_strlen(seed);
71     b->data  = m_strdup(seed);
72     b->dptr  = (char *)b->data + b->dsize;
73     return b;
74 }
75
76 void mutt_buffer_free(BUFFER **p)
77 {
78     if (p && *p) {
79         p_delete(&(*p)->data);
80         p_delete(p);
81     }
82 }
83
84 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
85  * Always one byte bigger than necessary for the null terminator, and
86  * the buffer is always null-terminated */
87 void mutt_buffer_add(BUFFER *buf, const char *s, ssize_t len)
88 {
89     size_t offset;
90
91     if (buf->dptr + len + 1 > buf->data + buf->dsize) {
92         offset = buf->dptr - buf->data;
93         buf->dsize += ((len + 1 + 127) & ~127);
94         p_realloc(&buf->data, buf->dsize);
95         buf->dptr = buf->data + offset;
96     }
97     memcpy(buf->dptr, s, len);
98     buf->dptr += len;
99     *buf->dptr = '\0';
100 }
101