NeoMutt  2024-02-01-35-geee02f
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
buffer.h File Reference

General purpose object for storing and parsing strings. More...

#include <stddef.h>
#include <stdbool.h>
+ Include dependency graph for buffer.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Buffer
 String manipulation buffer. More...
 

Functions

struct Bufferbuf_new (const char *str)
 Allocate a new Buffer.
 
void buf_free (struct Buffer **ptr)
 Deallocates a buffer.
 
void buf_alloc (struct Buffer *buf, size_t size)
 Make sure a buffer can store at least new_size bytes.
 
void buf_dealloc (struct Buffer *buf)
 Release the memory allocated by a buffer.
 
void buf_fix_dptr (struct Buffer *buf)
 Move the dptr to end of the Buffer.
 
struct Bufferbuf_init (struct Buffer *buf)
 Initialise a new Buffer.
 
bool buf_is_empty (const struct Buffer *buf)
 Is the Buffer empty?
 
size_t buf_len (const struct Buffer *buf)
 Calculate the length of a Buffer.
 
struct Buffer buf_make (size_t size)
 Make a new buffer on the stack.
 
void buf_reset (struct Buffer *buf)
 Reset an existing Buffer.
 
char * buf_strdup (const struct Buffer *buf)
 Copy a Buffer's string.
 
struct Bufferbuf_dup (const struct Buffer *buf)
 Copy a Buffer into a new allocated buffer.
 
void buf_seek (struct Buffer *buf, size_t offset)
 Set current read/write position to offset from beginning.
 
const char * buf_find_string (const struct Buffer *buf, const char *s)
 Return a pointer to a substring found in the buffer.
 
const char * buf_find_char (const struct Buffer *buf, const char c)
 Return a pointer to a char found in the buffer.
 
char buf_at (const struct Buffer *buf, size_t offset)
 Return the character at the given offset.
 
bool buf_str_equal (const struct Buffer *a, const struct Buffer *b)
 Return if two buffers are equal.
 
bool buf_istr_equal (const struct Buffer *a, const struct Buffer *b)
 Return if two buffers are equal, case insensitive.
 
int buf_coll (const struct Buffer *a, const struct Buffer *b)
 Collate two strings (compare using locale)
 
size_t buf_startswith (const struct Buffer *buf, const char *prefix)
 Check whether a buffer starts with a prefix.
 
const char * buf_rfind (const struct Buffer *buf, const char *str)
 Find last instance of a substring.
 
size_t buf_addch (struct Buffer *buf, char c)
 Add a single character to a Buffer.
 
size_t buf_addstr (struct Buffer *buf, const char *s)
 Add a string to a Buffer.
 
size_t buf_addstr_n (struct Buffer *buf, const char *s, size_t len)
 Add a string to a Buffer, expanding it if necessary.
 
int buf_add_printf (struct Buffer *buf, const char *fmt,...) __attribute__((__format__(__printf__
 
int void buf_join_str (struct Buffer *str, const char *item, char sep)
 Join a buffer with a string separated by sep.
 
size_t buf_insert (struct Buffer *buf, size_t offset, const char *s)
 Add a string in the middle of a buffer.
 
size_t buf_concat_path (struct Buffer *buf, const char *dir, const char *fname)
 Join a directory name and a filename.
 
size_t buf_concatn_path (struct Buffer *dst, const char *dir, size_t dirlen, const char *fname, size_t fnamelen)
 Join a directory name and a filename.
 
size_t buf_copy (struct Buffer *dst, const struct Buffer *src)
 Copy a Buffer's contents to another Buffer.
 
int buf_printf (struct Buffer *buf, const char *fmt,...) __attribute__((__format__(__printf__
 
int size_t buf_strcpy (struct Buffer *buf, const char *s)
 Copy a string into a Buffer.
 
size_t buf_strcpy_n (struct Buffer *buf, const char *s, size_t len)
 Copy a string into a Buffer.
 
size_t buf_substrcpy (struct Buffer *buf, const char *beg, const char *end)
 Copy a partial string into a Buffer.
 
void buf_dequote_comment (struct Buffer *buf)
 Un-escape characters in an email address comment.
 
void buf_lower (struct Buffer *buf)
 Sets a buffer to lowercase.
 
void buf_inline_replace (struct Buffer *buf, size_t pos, size_t len, const char *str)
 
static const char * buf_string (const struct Buffer *buf)
 Convert a buffer to a const char * "string".
 

Detailed Description

General purpose object for storing and parsing strings.

Authors
  • Ian Zimmerman
  • Richard Russon
  • Anna Figueiredo Gomes
  • Simon Reichel

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, see http://www.gnu.org/licenses/.

Definition in file buffer.h.

Function Documentation

◆ buf_new()

struct Buffer * buf_new ( const char *  str)

Allocate a new Buffer.

Parameters
strString to initialise the buffer with, can be NULL
Return values
ptrPointer to new buffer

Definition at line 321 of file buffer.c.

322{
323 struct Buffer *buf = mutt_mem_calloc(1, sizeof(struct Buffer));
324
325 if (str)
326 buf_addstr(buf, str);
327 else
328 buf_alloc(buf, 1);
329 return buf;
330}
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:243
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:354
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
String manipulation buffer.
Definition: buffer.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_free()

void buf_free ( struct Buffer **  ptr)

Deallocates a buffer.

Parameters
ptrBuffer to free

Definition at line 336 of file buffer.c.

337{
338 if (!ptr || !*ptr)
339 return;
340
341 struct Buffer *buf = *ptr;
342 buf_dealloc(buf);
343
344 FREE(ptr);
345}
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:394
#define FREE(x)
Definition: memory.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_alloc()

void buf_alloc ( struct Buffer buf,
size_t  new_size 
)

Make sure a buffer can store at least new_size bytes.

Parameters
bufBuffer to change
new_sizeNew size
Note
new_size will be rounded up to BufferStepSize

Definition at line 354 of file buffer.c.

355{
356 if (!buf)
357 return;
358
359 if (buf->data && (new_size <= buf->dsize))
360 {
361 // Extra sanity-checking
362 if (!buf->dptr || (buf->dptr < buf->data) || (buf->dptr > (buf->data + buf->dsize)))
363 buf->dptr = buf->data; // LCOV_EXCL_LINE
364 return;
365 }
366
367 if (new_size > (SIZE_MAX - BufferStepSize))
368 {
369 // LCOV_EXCL_START
370 mutt_error(_("Out of memory"));
371 mutt_exit(1);
372 // LCOV_EXCL_STOP
373 }
374
375 const bool was_empty = (buf->dptr == NULL);
376 const size_t offset = (buf->dptr && buf->data) ? (buf->dptr - buf->data) : 0;
377
378 buf->dsize = ROUND_UP(new_size + 1, BufferStepSize);
379
380 mutt_mem_realloc(&buf->data, buf->dsize);
381 buf->dptr = buf->data + offset;
382
383 // Ensures that initially NULL buf->data is properly terminated
384 if (was_empty)
385 {
386 *buf->dptr = '\0';
387 }
388}
static const int BufferStepSize
When increasing the size of a Buffer, add this much extra space.
Definition: buffer.c:51
#define mutt_error(...)
Definition: logging2.h:92
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:231
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define ROUND_UP(NUM, STEP)
Definition: memory.h:36
#define _(a)
Definition: message.h:28
char * dptr
Current read/write position.
Definition: buffer.h:38
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_dealloc()

void buf_dealloc ( struct Buffer buf)

Release the memory allocated by a buffer.

Parameters
bufBuffer to change

Definition at line 394 of file buffer.c.

395{
396 if (!buf || !buf->data)
397 return;
398
399 buf->dptr = NULL;
400 buf->dsize = 0;
401 FREE(&buf->data);
402}
+ Here is the caller graph for this function:

◆ buf_fix_dptr()

void buf_fix_dptr ( struct Buffer buf)

Move the dptr to end of the Buffer.

Parameters
bufBuffer to alter

Ensure buffer->dptr points to the end of the buffer.

Definition at line 199 of file buffer.c.

200{
201 if (!buf)
202 return;
203
204 buf_seek(buf, 0);
205
206 if (buf->data && (buf->dsize > 0))
207 {
208 buf->data[buf->dsize - 1] = '\0';
209 buf->dptr = strchr(buf->data, '\0');
210 }
211}
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:639
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_init()

struct Buffer * buf_init ( struct Buffer buf)

Initialise a new Buffer.

Parameters
bufBuffer to initialise
Return values
ptrInitialised Buffer

This must not be called on a Buffer that already contains data.

Definition at line 60 of file buffer.c.

61{
62 if (!buf)
63 return NULL;
64 memset(buf, 0, sizeof(struct Buffer));
65 return buf;
66}
+ Here is the caller graph for this function:

◆ buf_is_empty()

bool buf_is_empty ( const struct Buffer buf)

Is the Buffer empty?

Parameters
bufBuffer to inspect
Return values
trueBuffer is empty

Definition at line 308 of file buffer.c.

309{
310 if (!buf || !buf->data)
311 return true;
312
313 return (buf->data[0] == '\0');
314}

◆ buf_len()

size_t buf_len ( const struct Buffer buf)

Calculate the length of a Buffer.

Parameters
bufBuffer
Return values
numSize of buffer

Definition at line 508 of file buffer.c.

509{
510 if (!buf || !buf->data || !buf->dptr)
511 return 0;
512
513 return buf->dptr - buf->data;
514}
+ Here is the caller graph for this function:

◆ buf_make()

struct Buffer buf_make ( size_t  size)

Make a new buffer on the stack.

Parameters
sizeInitial size
Return values
objInitialized buffer

The buffer must be released using buf_dealloc

Definition at line 75 of file buffer.c.

76{
77 struct Buffer buf = { 0 };
78 if (size != 0)
79 {
80 buf.dptr = buf.data = mutt_mem_calloc(1, size);
81 buf.dsize = size;
82 }
83 return buf;
84}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_reset()

void buf_reset ( struct Buffer buf)

Reset an existing Buffer.

Parameters
bufBuffer to reset

This can be called on a Buffer to reset the pointers, effectively emptying it.

Definition at line 93 of file buffer.c.

94{
95 if (!buf || !buf->data || (buf->dsize == 0))
96 return;
97 memset(buf->data, 0, buf->dsize);
98 buf_seek(buf, 0);
99}
+ Here is the call graph for this function:

◆ buf_strdup()

char * buf_strdup ( const struct Buffer buf)

Copy a Buffer's string.

Parameters
bufBuffer to copy
Return values
ptrCopy of string
Note
Caller must free the returned string

Definition at line 588 of file buffer.c.

589{
590 if (!buf)
591 return NULL;
592
593 return mutt_str_dup(buf->data);
594}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
+ Here is the call graph for this function:

◆ buf_dup()

struct Buffer * buf_dup ( const struct Buffer buf)

Copy a Buffer into a new allocated buffer.

Parameters
bufBuffer to copy
Return values
bufNew allocated copy of buffer
Note
Caller must free the returned buffer

Definition at line 603 of file buffer.c.

604{
605 if (!buf)
606 return NULL;
607
608 return buf_new(buf_string(buf));
609}
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:321
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:97
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_seek()

void buf_seek ( struct Buffer buf,
size_t  offset 
)

Set current read/write position to offset from beginning.

Parameters
bufBuffer to use
offsetDistance from the beginning

This is used for cases where the buffer is read from A value is placed in the buffer, and then b->dptr is set back to the beginning as a read marker instead of write marker.

Definition at line 639 of file buffer.c.

640{
641 if (!buf)
642 return;
643
644 if ((offset < buf_len(buf)))
645 {
646 buf->dptr = buf->data ? buf->data + offset : NULL;
647 }
648}
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:508
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_find_string()

const char * buf_find_string ( const struct Buffer buf,
const char *  s 
)

Return a pointer to a substring found in the buffer.

Parameters
bufBuffer to search
sSubstring to find
Return values
NULLsubstring not found
nPointer to the beginning of the found substring

Definition at line 657 of file buffer.c.

658{
659 if (!buf || !s)
660 return NULL;
661
662 return strstr(buf->data, s);
663}
+ Here is the caller graph for this function:

◆ buf_find_char()

const char * buf_find_char ( const struct Buffer buf,
const char  c 
)

Return a pointer to a char found in the buffer.

Parameters
bufBuffer to search
cChar to find
Return values
NULLchar not found
ptrPointer to the found char

Definition at line 672 of file buffer.c.

673{
674 if (!buf)
675 return NULL;
676
677 return strchr(buf->data, c);
678}
+ Here is the caller graph for this function:

◆ buf_at()

char buf_at ( const struct Buffer buf,
size_t  offset 
)

Return the character at the given offset.

Parameters
bufBuffer to search
offsetOffset to get
Return values
NULOffset out of bounds
Returns
n The char at the offset

Definition at line 687 of file buffer.c.

688{
689 if (!buf || (offset > buf_len(buf)))
690 return '\0';
691
692 return buf->data[offset];
693}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_str_equal()

bool buf_str_equal ( const struct Buffer a,
const struct Buffer b 
)

Return if two buffers are equal.

Parameters
a- Buffer to compare
b- Buffer to compare
Return values
trueStrings are equal
falseString are not equal

Definition at line 702 of file buffer.c.

703{
704 return mutt_str_equal(buf_string(a), buf_string(b));
705}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:709
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_istr_equal()

bool buf_istr_equal ( const struct Buffer a,
const struct Buffer b 
)

Return if two buffers are equal, case insensitive.

Parameters
a- First buffer to compare
b- Second buffer to compare
Return values
trueStrings are equal
falseString are not equal

Definition at line 714 of file buffer.c.

715{
717}
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:721
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_coll()

int buf_coll ( const struct Buffer a,
const struct Buffer b 
)

Collate two strings (compare using locale)

Parameters
aFirst buffer to compare
bSecond buffer to compare
Return values
<0a precedes b
0a and b are identical
>0b precedes a

Definition at line 742 of file buffer.c.

743{
744 return mutt_str_coll(buf_string(a), buf_string(b));
745}
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:558
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_startswith()

size_t buf_startswith ( const struct Buffer buf,
const char *  prefix 
)

Check whether a buffer starts with a prefix.

Parameters
bufBuffer to check
prefixPrefix to match
Return values
numLength of prefix if str starts with prefix
0str does not start with prefix

Definition at line 726 of file buffer.c.

727{
728 if (!buf || !prefix)
729 return 0;
730
731 return mutt_str_startswith(buf_string(buf), prefix);
732}
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:230
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_rfind()

const char * buf_rfind ( const struct Buffer buf,
const char *  str 
)

Find last instance of a substring.

Parameters
bufBuffer to search through
strString to find
Return values
NULLString not found
ptrLocation of string

Return the last instance of str in the buffer, or NULL.

Definition at line 814 of file buffer.c.

815{
816 if (buf_is_empty(buf) || !str)
817 return NULL;
818
819 int len = strlen(str);
820 const char *end = buf->data + buf->dsize - len;
821
822 for (const char *p = end; p >= buf->data; --p)
823 {
824 for (size_t i = 0; i < len; i++)
825 {
826 if (p[i] != str[i])
827 goto next;
828 }
829 return p;
830
831 next:;
832 }
833 return NULL;
834}
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:308
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_addch()

size_t buf_addch ( struct Buffer buf,
char  c 
)

Add a single character to a Buffer.

Parameters
bufBuffer to add to
cCharacter to add
Return values
numBytes written to Buffer

If necessary, the Buffer will be expanded.

Definition at line 258 of file buffer.c.

259{
260 if (!buf)
261 return 0;
262 return buf_addstr_n(buf, &c, 1);
263}
size_t buf_addstr_n(struct Buffer *buf, const char *s, size_t len)
Add a string to a Buffer, expanding it if necessary.
Definition: buffer.c:113
+ Here is the call graph for this function:

◆ buf_addstr()

size_t buf_addstr ( struct Buffer buf,
const char *  s 
)

Add a string to a Buffer.

Parameters
bufBuffer to add to
sString to add
Return values
numBytes written to Buffer

If necessary, the Buffer will be expanded.

Definition at line 243 of file buffer.c.

244{
245 if (!buf || !s)
246 return 0;
247 return buf_addstr_n(buf, s, mutt_str_len(s));
248}
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:545
+ Here is the call graph for this function:

◆ buf_addstr_n()

size_t buf_addstr_n ( struct Buffer buf,
const char *  s,
size_t  len 
)

Add a string to a Buffer, expanding it if necessary.

Parameters
bufBuffer to add to
sString to add
lenLength of the string
Return values
numBytes written to Buffer
0Error

Dynamically grow 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 NUL-terminated

Definition at line 113 of file buffer.c.

114{
115 if (!buf || !s)
116 return 0;
117
118 if (len > (SIZE_MAX - BufferStepSize))
119 {
120 // LCOV_EXCL_START
121 mutt_error(_("Out of memory"));
122 mutt_exit(1);
123 // LCOV_EXCL_STOP
124 }
125
126 if (!buf->data || !buf->dptr || ((buf->dptr + len + 1) > (buf->data + buf->dsize)))
127 buf_alloc(buf, buf->dsize + MAX(BufferStepSize, len + 1));
128
129 memcpy(buf->dptr, s, len);
130 buf->dptr += len;
131 *(buf->dptr) = '\0';
132 return len;
133}
#define MAX(a, b)
Definition: memory.h:31
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_add_printf()

int buf_add_printf ( struct Buffer buf,
const char *  fmt,
  ... 
)

◆ buf_join_str()

int void buf_join_str ( struct Buffer buf,
const char *  str,
char  sep 
)

Join a buffer with a string separated by sep.

Parameters
bufBuffer to append to
strString to append
sepseparator between string item

Definition at line 767 of file buffer.c.

768{
769 if (!buf || !str)
770 return;
771
772 if (!buf_is_empty(buf) && mutt_str_len(str))
773 buf_addch(buf, sep);
774
775 buf_addstr(buf, str);
776}
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:258
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_insert()

size_t buf_insert ( struct Buffer buf,
size_t  offset,
const char *  s 
)

Add a string in the middle of a buffer.

Parameters
bufBuffer
offsetPosition for the insertion
sString to insert
Return values
numCharacters written
-1Error

Definition at line 273 of file buffer.c.

274{
275 if (!buf || !s || (*s == '\0'))
276 {
277 return -1;
278 }
279
280 const size_t slen = mutt_str_len(s);
281 const size_t curlen = buf_len(buf);
282 buf_alloc(buf, curlen + slen + 1);
283
284 if (offset > curlen)
285 {
286 for (size_t i = curlen; i < offset; ++i)
287 {
288 buf_addch(buf, ' ');
289 }
290 buf_addstr(buf, s);
291 }
292 else
293 {
294 memmove(buf->data + offset + slen, buf->data + offset, curlen - offset);
295 memcpy(buf->data + offset, s, slen);
296 buf->data[curlen + slen] = '\0';
297 buf->dptr = buf->data + curlen + slen;
298 }
299
300 return buf_len(buf) - curlen;
301}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_concat_path()

size_t buf_concat_path ( struct Buffer buf,
const char *  dir,
const char *  fname 
)

Join a directory name and a filename.

Parameters
bufBuffer to add to
dirDirectory name
fnameFile name
Return values
numBytes written to Buffer

If both dir and fname are supplied, they are separated with '/'. If either is missing, then the other will be copied exactly.

Definition at line 526 of file buffer.c.

527{
528 if (!buf)
529 return 0;
530
531 if (!dir)
532 dir = "";
533 if (!fname)
534 fname = "";
535
536 const bool d_set = (dir[0] != '\0');
537 const bool f_set = (fname[0] != '\0');
538 if (!d_set && !f_set)
539 return 0;
540
541 const int d_len = strlen(dir);
542 const bool slash = d_set && (dir[d_len - 1] == '/');
543
544 const char *fmt = "%s/%s";
545 if (!f_set || !d_set || slash)
546 fmt = "%s%s";
547
548 return buf_printf(buf, fmt, dir, fname);
549}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:178
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_concatn_path()

size_t buf_concatn_path ( struct Buffer buf,
const char *  dir,
size_t  dirlen,
const char *  fname,
size_t  fnamelen 
)

Join a directory name and a filename.

Parameters
bufBuffer for the result
dirDirectory name
dirlenDirectory name
fnameFile name
fnamelenFile name
Return values
numSize of buffer

If both dir and fname are supplied, they are separated with '/'. If either is missing, then the other will be copied exactly.

Definition at line 563 of file buffer.c.

565{
566 if (!buf)
567 return 0;
568
569 buf_reset(buf);
570
571 size_t len = 0;
572 if (dirlen != 0)
573 len += buf_addstr_n(buf, dir, dirlen);
574 if ((dirlen != 0) && (fnamelen != 0))
575 len += buf_addch(buf, '/');
576 if (fnamelen != 0)
577 len += buf_addstr_n(buf, fname, fnamelen);
578 return len;
579}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:93
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_copy()

size_t buf_copy ( struct Buffer dst,
const struct Buffer src 
)

Copy a Buffer's contents to another Buffer.

Parameters
dstBuffer for result
srcBuffer to copy
Return values
numBytes written to Buffer
0Error

Definition at line 618 of file buffer.c.

619{
620 if (!dst)
621 return 0;
622
623 buf_reset(dst);
624 if (!src || !src->data)
625 return 0;
626
627 return buf_addstr_n(dst, src->data, buf_len(src));
628}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_printf()

int buf_printf ( struct Buffer buf,
const char *  fmt,
  ... 
)

◆ buf_strcpy()

int size_t buf_strcpy ( struct Buffer buf,
const char *  s 
)

Copy a string into a Buffer.

Parameters
bufBuffer to overwrite
sString to copy
Return values
numBytes written to Buffer

Overwrites any existing content.

Definition at line 412 of file buffer.c.

413{
414 if (!buf)
415 return 0;
416
417 buf_reset(buf);
418 if (!s)
419 return 0;
420
421 return buf_addstr(buf, s);
422}
+ Here is the call graph for this function:

◆ buf_strcpy_n()

size_t buf_strcpy_n ( struct Buffer buf,
const char *  s,
size_t  len 
)

Copy a string into a Buffer.

Parameters
bufBuffer to overwrite
sString to copy
lenLength of string to copy
Return values
numBytes written to Buffer

Overwrites any existing content.

Definition at line 433 of file buffer.c.

434{
435 if (!buf)
436 return 0;
437
438 buf_reset(buf);
439 if (!s)
440 return 0;
441
442 return buf_addstr_n(buf, s, len);
443}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_substrcpy()

size_t buf_substrcpy ( struct Buffer buf,
const char *  beg,
const char *  end 
)

Copy a partial string into a Buffer.

Parameters
bufBuffer to overwrite
begStart of string to copy
endEnd of string to copy
Return values
numBytes written to Buffer

Overwrites any existing content.

Definition at line 488 of file buffer.c.

489{
490 if (!buf)
491 return 0;
492
493 buf_reset(buf);
494 if (!beg || !end)
495 return 0;
496
497 if (end <= beg)
498 return 0;
499
500 return buf_strcpy_n(buf, beg, end - beg);
501}
size_t buf_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition: buffer.c:433
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_dequote_comment()

void buf_dequote_comment ( struct Buffer buf)

Un-escape characters in an email address comment.

Parameters
bufBuffer to be un-escaped
Note
the buffer is modified

Definition at line 451 of file buffer.c.

452{
453 if (!buf || !buf->data || (buf->dsize == 0))
454 return;
455
456 buf_seek(buf, 0);
457
458 char *s = buf->data;
459 for (; *buf->dptr; buf->dptr++)
460 {
461 if (*buf->dptr == '\\')
462 {
463 if (!*++buf->dptr)
464 break; /* error? */
465 *s++ = *buf->dptr;
466 }
467 else if (*buf->dptr != '\"')
468 {
469 if (s != buf->dptr)
470 *s = *buf->dptr;
471 s++;
472 }
473 }
474 *s = '\0';
475
476 buf_fix_dptr(buf);
477}
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:199
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_lower()

void buf_lower ( struct Buffer buf)

Sets a buffer to lowercase.

Parameters
[out]bufBuffer to transform to lowercase
Note
Modifies the buffer

Definition at line 753 of file buffer.c.

754{
755 if (!buf)
756 return;
757
758 mutt_str_lower(buf->data);
759}
char * mutt_str_lower(char *str)
Convert all characters in the string to lowercase.
Definition: string.c:362
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_inline_replace()

void buf_inline_replace ( struct Buffer buf,
size_t  pos,
size_t  len,
const char *  str 
)

Definition at line 787 of file buffer.c.

788{
789 if (!buf || !str)
790 return;
791
792 size_t olen = buf->dsize;
793 size_t rlen = mutt_str_len(str);
794
795 size_t new_size = buf->dsize - len + rlen + 1;
796 if (new_size > buf->dsize)
797 buf_alloc(buf, new_size);
798
799 memmove(buf->data + pos + rlen, buf->data + pos + len, olen - pos - len);
800 memmove(buf->data + pos, str, rlen);
801
802 buf_fix_dptr(buf);
803}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_string()

static const char * buf_string ( const struct Buffer buf)
inlinestatic

Convert a buffer to a const char * "string".

Parameters
bufBuffer to that is to be converted
Return values
ptrString inside the Buffer

This method exposes Buffer's underlying data field

Note
Returns an empty string if Buffer isn't initialised

Definition at line 97 of file buffer.h.

98{
99 if (!buf || !buf->data)
100 return "";
101
102 return buf->data;
103}