NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
buffer.c File Reference

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

#include "config.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "buffer.h"
#include "exit.h"
#include "logging2.h"
#include "memory.h"
#include "message.h"
#include "string2.h"
+ Include dependency graph for buffer.c:

Go to the source code of this file.

Functions

struct Bufferbuf_init (struct Buffer *buf)
 Initialise a new Buffer.
 
void buf_reset (struct Buffer *buf)
 Reset an existing 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.
 
static int buf_vaprintf (struct Buffer *buf, const char *fmt, va_list ap)
 Format a string into a Buffer.
 
int buf_printf (struct Buffer *buf, const char *fmt,...)
 Format a string overwriting a Buffer.
 
void buf_fix_dptr (struct Buffer *buf)
 Move the dptr to end of the Buffer.
 
int buf_add_printf (struct Buffer *buf, const char *fmt,...)
 Format a string appending a Buffer.
 
size_t buf_addstr (struct Buffer *buf, const char *s)
 Add a string to a Buffer.
 
size_t buf_addch (struct Buffer *buf, char c)
 Add a single character to a Buffer.
 
size_t buf_insert (struct Buffer *buf, size_t offset, const char *s)
 Add a string in the middle of a buffer.
 
bool buf_is_empty (const struct Buffer *buf)
 Is the Buffer empty?
 
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 new_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.
 
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.
 
void buf_dequote_comment (struct Buffer *buf)
 Un-escape characters in an email address comment.
 
size_t buf_substrcpy (struct Buffer *buf, const char *beg, const char *end)
 Copy a partial string into a Buffer.
 
size_t buf_len (const struct Buffer *buf)
 Calculate the length 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 *buf, const char *dir, size_t dirlen, const char *fname, size_t fnamelen)
 Join a directory name and a filename.
 
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.
 
size_t buf_copy (struct Buffer *dst, const struct Buffer *src)
 Copy a Buffer's contents to another 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.
 
size_t buf_startswith (const struct Buffer *buf, const char *prefix)
 Check whether a buffer starts with a prefix.
 
int buf_coll (const struct Buffer *a, const struct Buffer *b)
 Collate two strings (compare using locale)
 
void buf_lower (struct Buffer *buf)
 Sets a buffer to lowercase.
 
void buf_join_str (struct Buffer *buf, const char *str, char sep)
 Join a buffer with a string separated by sep.
 
void buf_inline_replace (struct Buffer *buf, size_t pos, size_t len, const char *str)
 
const char * buf_rfind (const struct Buffer *buf, const char *str)
 Find last instance of a substring.
 

Variables

static const int BufferStepSize = 128
 When increasing the size of a Buffer, add this much extra space.
 

Detailed Description

General purpose object for storing and parsing strings.

Authors
  • Ian Zimmerman
  • Richard Russon
  • Pietro Cerutti
  • Anna Figueiredo Gomes
  • Simon Reichel
  • Dennis Schön

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.c.

Function Documentation

◆ 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}
String manipulation buffer.
Definition: buffer.h:36
+ 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 75 of file buffer.c.

76{
77 if (!buf || !buf->data || (buf->dsize == 0))
78 return;
79 memset(buf->data, 0, buf->dsize);
80 buf_seek(buf, 0);
81}
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:621
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:

◆ 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 95 of file buffer.c.

96{
97 if (!buf || !s)
98 return 0;
99
100 if (len > (SIZE_MAX - BufferStepSize))
101 {
102 // LCOV_EXCL_START
103 mutt_error(_("Out of memory"));
104 mutt_exit(1);
105 // LCOV_EXCL_STOP
106 }
107
108 if (!buf->data || !buf->dptr || ((buf->dptr + len + 1) > (buf->data + buf->dsize)))
109 buf_alloc(buf, buf->dsize + MAX(BufferStepSize, len + 1));
110
111 memcpy(buf->dptr, s, len);
112 buf->dptr += len;
113 *(buf->dptr) = '\0';
114 return len;
115}
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:336
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:268
#define MAX(a, b)
Definition: memory.h:31
#define _(a)
Definition: message.h:28
char * dptr
Current read/write position.
Definition: buffer.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_vaprintf()

static int buf_vaprintf ( struct Buffer buf,
const char *  fmt,
va_list  ap 
)
static

Format a string into a Buffer.

Parameters
bufBuffer
fmtprintf-style format string
apArguments to be formatted
Return values
numCharacters written
0Error

Definition at line 125 of file buffer.c.

126{
127 if (!buf || !fmt)
128 return 0; /* LCOV_EXCL_LINE */
129
130 buf_alloc(buf, 128);
131
132 int doff = buf->dptr - buf->data;
133 int blen = buf->dsize - doff;
134
135 va_list ap_retry;
136 va_copy(ap_retry, ap);
137
138 int len = vsnprintf(buf->dptr, blen, fmt, ap);
139 if (len >= blen)
140 {
141 buf_alloc(buf, buf->dsize + len - blen + 1);
142 len = vsnprintf(buf->dptr, len + 1, fmt, ap_retry);
143 }
144 if (len > 0)
145 buf->dptr += len;
146
147 va_end(ap_retry);
148
149 return len;
150}
+ 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,
  ... 
)

Format a string overwriting a Buffer.

Parameters
bufBuffer
fmtprintf-style format string
...Arguments to be formatted
Return values
numCharacters written
-1Error

Definition at line 160 of file buffer.c.

161{
162 if (!buf || !fmt)
163 return -1;
164
165 va_list ap;
166
167 va_start(ap, fmt);
168 buf_reset(buf);
169 int len = buf_vaprintf(buf, fmt, ap);
170 va_end(ap);
171
172 return len;
173}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:75
static int buf_vaprintf(struct Buffer *buf, const char *fmt, va_list ap)
Format a string into a Buffer.
Definition: buffer.c:125
+ Here is the call 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 181 of file buffer.c.

182{
183 if (!buf)
184 return;
185
186 buf_seek(buf, 0);
187
188 if (buf->data && (buf->dsize > 0))
189 {
190 buf->data[buf->dsize - 1] = '\0';
191 buf->dptr = strchr(buf->data, '\0');
192 }
193}
+ 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,
  ... 
)

Format a string appending a Buffer.

Parameters
bufBuffer
fmtprintf-style format string
...Arguments to be formatted
Return values
numCharacters written
-1Error

Definition at line 203 of file buffer.c.

204{
205 if (!buf || !fmt)
206 return -1;
207
208 va_list ap;
209
210 va_start(ap, fmt);
211 int len = buf_vaprintf(buf, fmt, ap);
212 va_end(ap);
213
214 return len;
215}
+ Here is the call graph for this function:
+ Here is the caller 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 225 of file buffer.c.

226{
227 if (!buf || !s)
228 return 0;
229 return buf_addstr_n(buf, s, mutt_str_len(s));
230}
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:95
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:490
+ Here is the call 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 240 of file buffer.c.

241{
242 if (!buf)
243 return 0;
244 return buf_addstr_n(buf, &c, 1);
245}
+ Here is the call 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 255 of file buffer.c.

256{
257 if (!buf || !s || (*s == '\0'))
258 {
259 return -1;
260 }
261
262 const size_t slen = mutt_str_len(s);
263 const size_t curlen = buf_len(buf);
264 buf_alloc(buf, curlen + slen + 1);
265
266 if (offset > curlen)
267 {
268 for (size_t i = curlen; i < offset; ++i)
269 {
270 buf_addch(buf, ' ');
271 }
272 buf_addstr(buf, s);
273 }
274 else
275 {
276 memmove(buf->data + offset + slen, buf->data + offset, curlen - offset);
277 memcpy(buf->data + offset, s, slen);
278 buf->data[curlen + slen] = '\0';
279 buf->dptr = buf->data + curlen + slen;
280 }
281
282 return buf_len(buf) - curlen;
283}
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:490
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
+ Here is the call graph for this function:
+ 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 290 of file buffer.c.

291{
292 if (!buf || !buf->data)
293 return true;
294
295 return (buf->data[0] == '\0');
296}

◆ 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 303 of file buffer.c.

304{
305 struct Buffer *buf = mutt_mem_calloc(1, sizeof(struct Buffer));
306
307 if (str)
308 buf_addstr(buf, str);
309 else
310 buf_alloc(buf, 1);
311 return buf;
312}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
+ 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 318 of file buffer.c.

319{
320 if (!ptr || !*ptr)
321 return;
322
323 struct Buffer *buf = *ptr;
324 buf_dealloc(buf);
325
326 FREE(ptr);
327}
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:376
#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 336 of file buffer.c.

337{
338 if (!buf)
339 return;
340
341 if (buf->data && (new_size <= buf->dsize))
342 {
343 // Extra sanity-checking
344 if (!buf->dptr || (buf->dptr < buf->data) || (buf->dptr > (buf->data + buf->dsize)))
345 buf->dptr = buf->data; // LCOV_EXCL_LINE
346 return;
347 }
348
349 if (new_size > (SIZE_MAX - BufferStepSize))
350 {
351 // LCOV_EXCL_START
352 mutt_error(_("Out of memory"));
353 mutt_exit(1);
354 // LCOV_EXCL_STOP
355 }
356
357 const bool was_empty = (buf->dptr == NULL);
358 const size_t offset = (buf->dptr && buf->data) ? (buf->dptr - buf->data) : 0;
359
360 buf->dsize = ROUND_UP(new_size + 1, BufferStepSize);
361
362 mutt_mem_realloc(&buf->data, buf->dsize);
363 buf->dptr = buf->data + offset;
364
365 // Ensures that initially NULL buf->data is properly terminated
366 if (was_empty)
367 {
368 *buf->dptr = '\0';
369 }
370}
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
+ 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 376 of file buffer.c.

377{
378 if (!buf || !buf->data)
379 return;
380
381 buf->dptr = NULL;
382 buf->dsize = 0;
383 FREE(&buf->data);
384}
+ Here is the caller graph for this function:

◆ buf_strcpy()

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 394 of file buffer.c.

395{
396 if (!buf)
397 return 0;
398
399 buf_reset(buf);
400 if (!s)
401 return 0;
402
403 return buf_addstr(buf, s);
404}
+ 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 415 of file buffer.c.

416{
417 if (!buf)
418 return 0;
419
420 buf_reset(buf);
421 if (!s)
422 return 0;
423
424 return buf_addstr_n(buf, s, len);
425}
+ 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 433 of file buffer.c.

434{
435 if (!buf || !buf->data || (buf->dsize == 0))
436 return;
437
438 buf_seek(buf, 0);
439
440 char *s = buf->data;
441 for (; *buf->dptr; buf->dptr++)
442 {
443 if (*buf->dptr == '\\')
444 {
445 if (!*++buf->dptr)
446 break; /* error? */
447 *s++ = *buf->dptr;
448 }
449 else if (*buf->dptr != '\"')
450 {
451 if (s != buf->dptr)
452 *s = *buf->dptr;
453 s++;
454 }
455 }
456 *s = '\0';
457
458 buf_fix_dptr(buf);
459}
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:181
+ 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 470 of file buffer.c.

471{
472 if (!buf)
473 return 0;
474
475 buf_reset(buf);
476 if (!beg || !end)
477 return 0;
478
479 if (end <= beg)
480 return 0;
481
482 return buf_strcpy_n(buf, beg, end - beg);
483}
size_t buf_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition: buffer.c:415
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 490 of file buffer.c.

491{
492 if (!buf || !buf->data || !buf->dptr)
493 return 0;
494
495 return buf->dptr - buf->data;
496}
+ 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 508 of file buffer.c.

509{
510 if (!buf)
511 return 0;
512
513 if (!dir)
514 dir = "";
515 if (!fname)
516 fname = "";
517
518 const bool d_set = (dir[0] != '\0');
519 const bool f_set = (fname[0] != '\0');
520 if (!d_set && !f_set)
521 return 0;
522
523 const int d_len = strlen(dir);
524 const bool slash = d_set && (dir[d_len - 1] == '/');
525
526 const char *fmt = "%s/%s";
527 if (!f_set || !d_set || slash)
528 fmt = "%s%s";
529
530 return buf_printf(buf, fmt, dir, fname);
531}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
+ 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 545 of file buffer.c.

547{
548 if (!buf)
549 return 0;
550
551 buf_reset(buf);
552
553 size_t len = 0;
554 if (dirlen != 0)
555 len += buf_addstr_n(buf, dir, dirlen);
556 if ((dirlen != 0) && (fnamelen != 0))
557 len += buf_addch(buf, '/');
558 if (fnamelen != 0)
559 len += buf_addstr_n(buf, fname, fnamelen);
560 return len;
561}
+ Here is the call graph for this function:
+ Here is the caller 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 570 of file buffer.c.

571{
572 if (!buf)
573 return NULL;
574
575 return mutt_str_dup(buf->data);
576}
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 585 of file buffer.c.

586{
587 if (!buf)
588 return NULL;
589
590 return buf_new(buf_string(buf));
591}
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:303
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
+ 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 600 of file buffer.c.

601{
602 if (!dst)
603 return 0;
604
605 buf_reset(dst);
606 if (!src || !src->data)
607 return 0;
608
609 return buf_addstr_n(dst, src->data, buf_len(src));
610}
+ 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 621 of file buffer.c.

622{
623 if (!buf)
624 return;
625
626 if ((offset < buf_len(buf)))
627 {
628 buf->dptr = buf->data ? buf->data + offset : NULL;
629 }
630}
+ 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 639 of file buffer.c.

640{
641 if (!buf || !s)
642 return NULL;
643
644 return strstr(buf->data, s);
645}
+ 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 654 of file buffer.c.

655{
656 if (!buf)
657 return NULL;
658
659 return strchr(buf->data, c);
660}
+ 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 669 of file buffer.c.

670{
671 if (!buf || (offset > buf_len(buf)))
672 return '\0';
673
674 return buf->data[offset];
675}
+ 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 684 of file buffer.c.

685{
686 return mutt_str_equal(buf_string(a), buf_string(b));
687}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:654
+ 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 696 of file buffer.c.

697{
699}
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:666
+ 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 708 of file buffer.c.

709{
710 if (!buf || !prefix)
711 return 0;
712
713 return mutt_str_startswith(buf_string(buf), prefix);
714}
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_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 724 of file buffer.c.

725{
726 return mutt_str_coll(buf_string(a), buf_string(b));
727}
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:503
+ 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 735 of file buffer.c.

736{
737 if (!buf)
738 return;
739
740 mutt_str_lower(buf->data);
741}
char * mutt_str_lower(char *str)
Convert all characters in the string to lowercase.
Definition: string.c:307
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_join_str()

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 749 of file buffer.c.

750{
751 if (!buf || !str)
752 return;
753
754 if (!buf_is_empty(buf) && mutt_str_len(str))
755 buf_addch(buf, sep);
756
757 buf_addstr(buf, str);
758}
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:290
+ 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 769 of file buffer.c.

770{
771 if (!buf || !str)
772 return;
773
774 size_t olen = buf->dsize;
775 size_t rlen = mutt_str_len(str);
776
777 size_t new_size = buf->dsize - len + rlen + 1;
778 if (new_size > buf->dsize)
779 buf_alloc(buf, new_size);
780
781 memmove(buf->data + pos + rlen, buf->data + pos + len, olen - pos - len);
782 memmove(buf->data + pos, str, rlen);
783
784 buf_fix_dptr(buf);
785}
+ 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 796 of file buffer.c.

797{
798 if (buf_is_empty(buf) || !str)
799 return NULL;
800
801 int len = strlen(str);
802 const char *end = buf->data + buf->dsize - len;
803
804 for (const char *p = end; p >= buf->data; --p)
805 {
806 for (size_t i = 0; i < len; i++)
807 {
808 if (p[i] != str[i])
809 goto next;
810 }
811 return p;
812
813 next:;
814 }
815 return NULL;
816}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ BufferStepSize

const int BufferStepSize = 128
static

When increasing the size of a Buffer, add this much extra space.

Definition at line 51 of file buffer.c.