Commit f3c70b58 authored by Kevin J. McCarthy's avatar Kevin J. McCarthy

Break buffer functions into separate source file.

parent caeda713
......@@ -26,7 +26,7 @@ BUILT_SOURCES = keymap_defs.h patchlist.c reldate.h conststrings.c $(HCVERSION)
bin_PROGRAMS = mutt $(DOTLOCK_TARGET) $(PGPAUX_TARGET)
mutt_SOURCES = \
addrbook.c alias.c attach.c base64.c browser.c buffy.c color.c \
addrbook.c alias.c attach.c base64.c browser.c buffer.c buffy.c color.c \
crypt.c cryptglue.c \
commands.c complete.c compose.c copy.c curs_lib.c curs_main.c date.c \
edit.c enter.c flags.c init.c filter.c from.c \
......@@ -65,7 +65,8 @@ EXTRA_mutt_SOURCES = account.c bcache.c compress.c crypt-gpgme.c crypt-mod-pgp-c
EXTRA_DIST = COPYRIGHT GPL OPS OPS.PGP OPS.CRYPT OPS.SMIME TODO UPDATING \
configure account.h \
attach.h buffy.h charset.h compress.h copy.h crypthash.h dotlock.h functions.h gen_defs \
attach.h buffer.h buffy.h charset.h compress.h copy.h crypthash.h \
dotlock.h functions.h gen_defs \
globals.h hash.h history.h init.h keymap.h mutt_crypt.h \
mailbox.h mapping.h md5.h mime.h mutt.h mutt_curses.h mutt_menu.h \
mutt_regex.h mutt_sasl.h mutt_socket.h mutt_ssl.h mutt_tunnel.h \
......
/*
* Copyright (C) 1996-2002,2010,2013 Michael R. Elkins <me@mutt.org>
* Copyright (C) 2004 g10 Code GmbH
* Copyright (C) 2018 Kevin J. McCarthy <kevin@8t8.us>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "mutt.h"
#include "buffer.h"
/* creates and initializes a BUFFER */
BUFFER *mutt_buffer_new(void) {
BUFFER *b;
b = safe_malloc(sizeof(BUFFER));
mutt_buffer_init(b);
return b;
}
/* initialize a new BUFFER */
BUFFER *mutt_buffer_init (BUFFER *b) {
memset(b, 0, sizeof(BUFFER));
return b;
}
/* Increases the allocated size of the buffer */
void mutt_buffer_increase_size (BUFFER *buf, size_t new_size)
{
size_t offset;
if (buf->dsize < new_size)
{
offset = buf->dptr - buf->data;
buf->dsize = new_size;
safe_realloc (&buf->data, buf->dsize);
buf->dptr = buf->data + offset;
}
}
/*
* Creates and initializes a BUFFER*. If passed an existing BUFFER*,
* just initializes. Frees anything already in the buffer. Copies in
* the seed string.
*
* Disregards the 'destroy' flag, which seems reserved for caller.
* This is bad, but there's no apparent protocol for it.
*/
BUFFER *mutt_buffer_from (char *seed) {
BUFFER *b;
if (!seed)
return NULL;
b = mutt_buffer_new ();
b->data = safe_strdup(seed);
b->dsize = mutt_strlen(seed);
b->dptr = (char *) b->data + b->dsize;
return b;
}
int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
{
va_list ap, ap_retry;
int len, blen, doff;
va_start (ap, fmt);
va_copy (ap_retry, ap);
if (!buf->dptr)
buf->dptr = buf->data;
doff = buf->dptr - buf->data;
blen = buf->dsize - doff;
/* solaris 9 vsnprintf barfs when blen is 0 */
if (!blen)
{
blen = 128;
buf->dsize += blen;
safe_realloc (&buf->data, buf->dsize);
buf->dptr = buf->data + doff;
}
if ((len = vsnprintf (buf->dptr, blen, fmt, ap)) >= blen)
{
blen = ++len - blen;
if (blen < 128)
blen = 128;
buf->dsize += blen;
safe_realloc (&buf->data, buf->dsize);
buf->dptr = buf->data + doff;
len = vsnprintf (buf->dptr, len, fmt, ap_retry);
}
if (len > 0)
buf->dptr += len;
va_end (ap);
va_end (ap_retry);
return len;
}
void mutt_buffer_addstr (BUFFER* buf, const char* s)
{
mutt_buffer_add (buf, s, mutt_strlen (s));
}
void mutt_buffer_addch (BUFFER* buf, char c)
{
mutt_buffer_add (buf, &c, 1);
}
void mutt_buffer_free (BUFFER **p)
{
if (!p || !*p)
return;
FREE(&(*p)->data);
/* dptr is just an offset to data and shouldn't be freed */
FREE(p); /* __FREE_CHECKED__ */
}
/* dynamically grows a BUFFER to accommodate s, in increments of 128 bytes.
* Always one byte bigger than necessary for the null terminator, and
* the buffer is always null-terminated */
void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
{
size_t offset;
if (buf->dptr + len + 1 > buf->data + buf->dsize)
{
offset = buf->dptr - buf->data;
buf->dsize += len < 128 ? 128 : len + 1;
/* suppress compiler aliasing warning */
safe_realloc ((void**) (void*) &buf->data, buf->dsize);
buf->dptr = buf->data + offset;
}
memcpy (buf->dptr, s, len);
buf->dptr += len;
*(buf->dptr) = '\0';
}
/*
* Copyright (C) 1996-2002,2010,2013 Michael R. Elkins <me@mutt.org>
* Copyright (C) 2004 g10 Code GmbH
* Copyright (C) 2018 Kevin J. McCarthy <kevin@8t8.us>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _BUFFER_H
#define _BUFFER_H
typedef struct
{
char *data; /* pointer to data */
char *dptr; /* current read/write position */
size_t dsize; /* length of data */
int destroy; /* destroy `data' when done? */
} BUFFER;
BUFFER *mutt_buffer_new (void);
BUFFER * mutt_buffer_init (BUFFER *);
void mutt_buffer_increase_size (BUFFER *, size_t);
BUFFER * mutt_buffer_from (char *);
void mutt_buffer_free(BUFFER **);
int mutt_buffer_printf (BUFFER*, const char*, ...);
void mutt_buffer_add (BUFFER*, const char*, size_t);
void mutt_buffer_addstr (BUFFER*, const char*);
void mutt_buffer_addch (BUFFER*, char);
#endif
/*
* Copyright (C) 1996-2002,2010,2013 Michael R. Elkins <me@mutt.org>
* Copyright (C) 2004 g10 Code GmbH
......@@ -63,6 +62,7 @@
#include "rfc822.h"
#include "hash.h"
#include "charset.h"
#include "buffer.h"
#ifndef HAVE_WC_FUNCS
# ifdef MB_LEN_MAX
......@@ -126,14 +126,6 @@ struct timespec
#define MUTT_TOKEN_COMMENT (1<<5) /* don't reap comments */
#define MUTT_TOKEN_SEMICOLON (1<<6) /* don't treat ; as special */
typedef struct
{
char *data; /* pointer to data */
char *dptr; /* current read/write position */
size_t dsize; /* length of data */
int destroy; /* destroy `data' when done? */
} BUFFER;
typedef struct
{
int ch; /* raw key pressed */
......
......@@ -1788,138 +1788,6 @@ void mutt_sleep (short s)
sleep(s);
}
/* creates and initializes a BUFFER */
BUFFER *mutt_buffer_new(void) {
BUFFER *b;
b = safe_malloc(sizeof(BUFFER));
mutt_buffer_init(b);
return b;
}
/* initialize a new BUFFER */
BUFFER *mutt_buffer_init (BUFFER *b) {
memset(b, 0, sizeof(BUFFER));
return b;
}
/* Increases the allocated size of the buffer */
void mutt_buffer_increase_size (BUFFER *buf, size_t new_size)
{
size_t offset;
if (buf->dsize < new_size)
{
offset = buf->dptr - buf->data;
buf->dsize = new_size;
safe_realloc (&buf->data, buf->dsize);
buf->dptr = buf->data + offset;
}
}
/*
* Creates and initializes a BUFFER*. If passed an existing BUFFER*,
* just initializes. Frees anything already in the buffer. Copies in
* the seed string.
*
* Disregards the 'destroy' flag, which seems reserved for caller.
* This is bad, but there's no apparent protocol for it.
*/
BUFFER *mutt_buffer_from (char *seed) {
BUFFER *b;
if (!seed)
return NULL;
b = mutt_buffer_new ();
b->data = safe_strdup(seed);
b->dsize = mutt_strlen(seed);
b->dptr = (char *) b->data + b->dsize;
return b;
}
int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
{
va_list ap, ap_retry;
int len, blen, doff;
va_start (ap, fmt);
va_copy (ap_retry, ap);
if (!buf->dptr)
buf->dptr = buf->data;
doff = buf->dptr - buf->data;
blen = buf->dsize - doff;
/* solaris 9 vsnprintf barfs when blen is 0 */
if (!blen)
{
blen = 128;
buf->dsize += blen;
safe_realloc (&buf->data, buf->dsize);
buf->dptr = buf->data + doff;
}
if ((len = vsnprintf (buf->dptr, blen, fmt, ap)) >= blen)
{
blen = ++len - blen;
if (blen < 128)
blen = 128;
buf->dsize += blen;
safe_realloc (&buf->data, buf->dsize);
buf->dptr = buf->data + doff;
len = vsnprintf (buf->dptr, len, fmt, ap_retry);
}
if (len > 0)
buf->dptr += len;
va_end (ap);
va_end (ap_retry);
return len;
}
void mutt_buffer_addstr (BUFFER* buf, const char* s)
{
mutt_buffer_add (buf, s, mutt_strlen (s));
}
void mutt_buffer_addch (BUFFER* buf, char c)
{
mutt_buffer_add (buf, &c, 1);
}
void mutt_buffer_free (BUFFER **p)
{
if (!p || !*p)
return;
FREE(&(*p)->data);
/* dptr is just an offset to data and shouldn't be freed */
FREE(p); /* __FREE_CHECKED__ */
}
/* dynamically grows a BUFFER to accommodate s, in increments of 128 bytes.
* Always one byte bigger than necessary for the null terminator, and
* the buffer is always null-terminated */
void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
{
size_t offset;
if (buf->dptr + len + 1 > buf->data + buf->dsize)
{
offset = buf->dptr - buf->data;
buf->dsize += len < 128 ? 128 : len + 1;
/* suppress compiler aliasing warning */
safe_realloc ((void**) (void*) &buf->data, buf->dsize);
buf->dptr = buf->data + offset;
}
memcpy (buf->dptr, s, len);
buf->dptr += len;
*(buf->dptr) = '\0';
}
/* Decrease a file's modification time by 1 second */
time_t mutt_decrease_mtime (const char *f, struct stat *st)
......
......@@ -39,15 +39,6 @@ struct hdr_format_info
void mutt_make_string_info (char *, size_t, int, const char *, struct hdr_format_info *, format_flag);
int mutt_extract_token (BUFFER *, BUFFER *, int);
BUFFER *mutt_buffer_new (void);
BUFFER * mutt_buffer_init (BUFFER *);
void mutt_buffer_increase_size (BUFFER *, size_t);
BUFFER * mutt_buffer_from (char *);
void mutt_buffer_free(BUFFER **);
int mutt_buffer_printf (BUFFER*, const char*, ...);
void mutt_buffer_add (BUFFER*, const char*, size_t);
void mutt_buffer_addstr (BUFFER*, const char*);
void mutt_buffer_addch (BUFFER*, char);
void mutt_free_opts (void);
#define mutt_system(x) _mutt_system(x,0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment