libdatatypes 0.3.2
Abstract datatypes for C.
Loading...
Searching...
No Matches
buffer.c File Reference

Byte buffer. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <assert.h>
#include "buffer.h"

Go to the source code of this file.

Functions

Bufferbuffer_new (size_t max_size)
 
void buffer_init (Buffer *buf, size_t max_size)
 
void buffer_free (Buffer *buf)
 
void buffer_destroy (Buffer *buf)
 
void buffer_clear (Buffer *buf)
 
size_t buffer_len (const Buffer *buf)
 
bool buffer_is_valid (const Buffer *buf)
 
bool buffer_is_empty (const Buffer *buf)
 
bool buffer_fill (Buffer *buf, const char *data, size_t len)
 
ssize_t buffer_fill_from_fd (Buffer *buf, int fd, size_t count)
 
bool buffer_read_line (Buffer *buf, char **dst, size_t *len)
 
bool buffer_flush (const Buffer *buf, char **dst, size_t *len)
 
char * buffer_to_string (const Buffer *buf)
 

Detailed Description

Byte buffer.

Author
Sebastian Fedrau sebas.nosp@m.tian.nosp@m..fedr.nosp@m.au@g.nosp@m.mail..nosp@m.com

Definition in file buffer.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 22 of file buffer.c.

Function Documentation

◆ buffer_clear()

void buffer_clear ( Buffer buf)
Parameters
bufBuffer to clear

Clears a buffer. The buffer becomes valid again.

Definition at line 96 of file buffer.c.

◆ buffer_destroy()

void buffer_destroy ( Buffer buf)
Parameters
bufBuffer to free

Frees a buffer and the buf pointer.

Definition at line 87 of file buffer.c.

◆ buffer_fill()

bool buffer_fill ( Buffer buf,
const char *  data,
size_t  len 
)
Parameters
bufa Buffer
datadata to write to the buffer
lennumber of bytes to write
Returns
true on success

Writes bytes to a buffer.

Definition at line 153 of file buffer.c.

◆ buffer_fill_from_fd()

ssize_t buffer_fill_from_fd ( Buffer buf,
int  fd,
size_t  count 
)
Parameters
bufa Buffer
fda file descriptor
countbytes to read from the file descriptor
Returns
number of bytes read from the file descriptor and written to the buffer, -1 on failure

Reads bytes from a file and writes the data to the buffer.

Definition at line 189 of file buffer.c.

◆ buffer_flush()

bool buffer_flush ( const Buffer buf,
char **  dst,
size_t *  len 
)
Parameters
bufa Buffer
dstlocation to store buffer data
lenbuffer length
Returns
true on success

Copies data from the buffer to a string (if buffer is valid). dst will be resized automatically if necessary.

Definition at line 267 of file buffer.c.

◆ buffer_free()

void buffer_free ( Buffer buf)
Parameters
bufBuffer to free

Frees a buffer without freeing the buf pointer.

Definition at line 79 of file buffer.c.

◆ buffer_init()

void buffer_init ( Buffer buf,
size_t  max_size 
)
Parameters
bufa Buffer
max_sizemaximum buffer length

Initializes a buffer.

Definition at line 59 of file buffer.c.

◆ buffer_is_empty()

bool buffer_is_empty ( const Buffer buf)
Parameters
bufa Buffer
Returns
true if the buffer is empty

Checks if a buffer is empty.

Definition at line 123 of file buffer.c.

◆ buffer_is_valid()

bool buffer_is_valid ( const Buffer buf)
Parameters
bufa Buffer
Returns
true if the buffer is valid

Checks if a buffer is valid.

Definition at line 115 of file buffer.c.

◆ buffer_len()

size_t buffer_len ( const Buffer buf)
Parameters
bufa Buffer
Returns
length of the buffer

Gets the length of the buffer.

Definition at line 105 of file buffer.c.

◆ buffer_new()

Buffer * buffer_new ( size_t  max_size)
Parameters
max_sizemaximum buffer length
Returns
a new Buffer

Creates a new buffer.

Definition at line 41 of file buffer.c.

◆ buffer_read_line()

bool buffer_read_line ( Buffer buf,
char **  dst,
size_t *  len 
)
Parameters
bufa Buffer
dstlocation to store read string
lenlength of dst
Returns
true if a line could be read from the buffer

Tries to read a line from the buffer. dst will be resized automatically if necessary.

Definition at line 240 of file buffer.c.

◆ buffer_to_string()

char * buffer_to_string ( const Buffer buf)
Parameters
bufa Buffer
Returns
a newly-allocated string

Converts the data from the buffer to a new string. Returns NULL if buffer is invalid.

Definition at line 287 of file buffer.c.