General purpose object for storing and parsing strings. More...
#include "config.h"
#include <errno.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"
Go to the source code of this file.
Functions | |
struct Buffer * | buf_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 Buffer * | buf_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 Buffer * | buf_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. | |
General purpose object for storing and parsing strings.
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.
void buf_reset | ( | struct Buffer * | buf | ) |
Reset an existing Buffer.
buf | Buffer to reset |
This can be called on a Buffer to reset the pointers, effectively emptying it.
Definition at line 76 of file buffer.c.
size_t buf_addstr_n | ( | struct Buffer * | buf, |
const char * | s, | ||
size_t | len | ||
) |
Add a string to a Buffer, expanding it if necessary.
buf | Buffer to add to |
s | String to add |
len | Length of the string |
num | Bytes written to Buffer |
0 | Error |
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 96 of file buffer.c.
|
static |
Format a string into a Buffer.
buf | Buffer |
fmt | printf-style format string |
ap | Arguments to be formatted |
num | Characters written |
0 | Error |
Definition at line 126 of file buffer.c.
int buf_printf | ( | struct Buffer * | buf, |
const char * | fmt, | ||
... | |||
) |
Format a string overwriting a Buffer.
buf | Buffer |
fmt | printf-style format string |
... | Arguments to be formatted |
num | Characters written |
-1 | Error |
Definition at line 161 of file buffer.c.
void buf_fix_dptr | ( | struct Buffer * | buf | ) |
Move the dptr to end of the Buffer.
buf | Buffer to alter |
Ensure buffer->dptr points to the end of the buffer.
Definition at line 182 of file buffer.c.
int buf_add_printf | ( | struct Buffer * | buf, |
const char * | fmt, | ||
... | |||
) |
Format a string appending a Buffer.
buf | Buffer |
fmt | printf-style format string |
... | Arguments to be formatted |
num | Characters written |
-1 | Error |
Definition at line 204 of file buffer.c.
size_t buf_addstr | ( | struct Buffer * | buf, |
const char * | s | ||
) |
Add a string to a Buffer.
buf | Buffer to add to |
s | String to add |
num | Bytes written to Buffer |
If necessary, the Buffer will be expanded.
Definition at line 226 of file buffer.c.
size_t buf_addch | ( | struct Buffer * | buf, |
char | c | ||
) |
size_t buf_insert | ( | struct Buffer * | buf, |
size_t | offset, | ||
const char * | s | ||
) |
Add a string in the middle of a buffer.
buf | Buffer |
offset | Position for the insertion |
s | String to insert |
num | Characters written |
-1 | Error |
Definition at line 256 of file buffer.c.
bool buf_is_empty | ( | const struct Buffer * | buf | ) |
struct Buffer * buf_new | ( | const char * | str | ) |
Allocate a new Buffer.
str | String to initialise the buffer with, can be NULL |
ptr | Pointer to new buffer |
Definition at line 304 of file buffer.c.
void buf_free | ( | struct Buffer ** | ptr | ) |
Deallocates a buffer.
ptr | Buffer to free |
Definition at line 319 of file buffer.c.
void buf_alloc | ( | struct Buffer * | buf, |
size_t | new_size | ||
) |
Make sure a buffer can store at least new_size bytes.
buf | Buffer to change |
new_size | New size |
Definition at line 337 of file buffer.c.
void buf_dealloc | ( | struct Buffer * | buf | ) |
size_t buf_strcpy | ( | struct Buffer * | buf, |
const char * | s | ||
) |
Copy a string into a Buffer.
buf | Buffer to overwrite |
s | String to copy |
num | Bytes written to Buffer |
Overwrites any existing content.
Definition at line 395 of file buffer.c.
size_t buf_strcpy_n | ( | struct Buffer * | buf, |
const char * | s, | ||
size_t | len | ||
) |
Copy a string into a Buffer.
buf | Buffer to overwrite |
s | String to copy |
len | Length of string to copy |
num | Bytes written to Buffer |
Overwrites any existing content.
Definition at line 416 of file buffer.c.
void buf_dequote_comment | ( | struct Buffer * | buf | ) |
Un-escape characters in an email address comment.
buf | Buffer to be un-escaped |
Definition at line 434 of file buffer.c.
size_t buf_substrcpy | ( | struct Buffer * | buf, |
const char * | beg, | ||
const char * | end | ||
) |
Copy a partial string into a Buffer.
buf | Buffer to overwrite |
beg | Start of string to copy |
end | End of string to copy |
num | Bytes written to Buffer |
Overwrites any existing content.
Definition at line 471 of file buffer.c.
size_t buf_len | ( | const struct Buffer * | buf | ) |
size_t buf_concat_path | ( | struct Buffer * | buf, |
const char * | dir, | ||
const char * | fname | ||
) |
Join a directory name and a filename.
buf | Buffer to add to |
dir | Directory name |
fname | File name |
num | Bytes 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 509 of file buffer.c.
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.
buf | Buffer for the result |
dir | Directory name |
dirlen | Directory name |
fname | File name |
fnamelen | File name |
num | Size 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 546 of file buffer.c.
char * buf_strdup | ( | const struct Buffer * | buf | ) |
Copy a Buffer into a new allocated buffer.
buf | Buffer to copy |
buf | New allocated copy of buffer |
Definition at line 586 of file buffer.c.
void buf_seek | ( | struct Buffer * | buf, |
size_t | offset | ||
) |
Set current read/write position to offset from beginning.
buf | Buffer to use |
offset | Distance 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 622 of file buffer.c.
const char * buf_find_string | ( | const struct Buffer * | buf, |
const char * | s | ||
) |
const char * buf_find_char | ( | const struct Buffer * | buf, |
const char | c | ||
) |
char buf_at | ( | const struct Buffer * | buf, |
size_t | offset | ||
) |
Return the character at the given offset.
buf | Buffer to search |
offset | Offset to get |
NUL | Offset out of bounds |
Definition at line 670 of file buffer.c.
Return if two buffers are equal.
true | Strings are equal |
false | String are not equal |
Definition at line 685 of file buffer.c.
Return if two buffers are equal, case insensitive.
a | - First buffer to compare |
b | - Second buffer to compare |
true | Strings are equal |
false | String are not equal |
Definition at line 697 of file buffer.c.
size_t buf_startswith | ( | const struct Buffer * | buf, |
const char * | prefix | ||
) |
Check whether a buffer starts with a prefix.
buf | Buffer to check |
prefix | Prefix to match |
num | Length of prefix if str starts with prefix |
0 | str does not start with prefix |
Definition at line 709 of file buffer.c.
Collate two strings (compare using locale)
a | First buffer to compare |
b | Second buffer to compare |
<0 | a precedes b |
0 | a and b are identical |
>0 | b precedes a |
Definition at line 725 of file buffer.c.
void buf_lower | ( | struct Buffer * | buf | ) |
Sets a buffer to lowercase.
[out] | buf | Buffer to transform to lowercase |
Definition at line 736 of file buffer.c.
void buf_join_str | ( | struct Buffer * | buf, |
const char * | str, | ||
char | sep | ||
) |
Join a buffer with a string separated by sep.
buf | Buffer to append to |
str | String to append |
sep | separator between string item |
Definition at line 750 of file buffer.c.
void buf_inline_replace | ( | struct Buffer * | buf, |
size_t | pos, | ||
size_t | len, | ||
const char * | str | ||
) |
Definition at line 770 of file buffer.c.
const char * buf_rfind | ( | const struct Buffer * | buf, |
const char * | str | ||
) |
Find last instance of a substring.
buf | Buffer to search through |
str | String to find |
NULL | String not found |
ptr | Location of string |
Return the last instance of str in the buffer, or NULL.
Definition at line 797 of file buffer.c.