myMPD
Internal API documentation
Loading...
Searching...
No Matches
list.c File Reference

Linked list implementation. More...

#include "src/lib/list/list.h"
#include "src/lib/filehandler.h"
#include "src/lib/log.h"
#include "src/lib/mem.h"
#include "src/lib/sds/sds_extras.h"
#include <limits.h>
#include <string.h>
Include dependency graph for list.c:

Functions

struct t_listlist_new (void)
struct t_listlist_dup (struct t_list *l)
bool list_append (struct t_list *dst, struct t_list *src)
void list_init (struct t_list *l)
void list_clear (struct t_list *l)
void list_free (struct t_list *l)
void list_free_void (void *l)
void list_clear_user_data (struct t_list *l, user_data_callback free_cb)
void list_free_user_data (struct t_list *l, user_data_callback free_cb)
void list_free_cb_ignore_user_data (struct t_list_node *current)
void list_free_cb_sds_user_data (struct t_list_node *current)
void list_free_cb_ptr_user_data (struct t_list_node *current)
unsigned list_get_node_idx (const struct t_list *l, const char *key)
struct t_list_nodelist_get_node (const struct t_list *l, const char *key)
struct t_list_nodelist_node_at (const struct t_list *l, unsigned idx)
bool list_move_node (struct t_list *l, unsigned from, unsigned to)
bool list_push (struct t_list *l, const char *key, int64_t value_i, const char *value_p, void *user_data)
bool list_push_len (struct t_list *l, const char *key, size_t key_len, int64_t value_i, const char *value_p, size_t value_len, void *user_data)
bool list_insert (struct t_list *l, const char *key, int64_t value_i, const char *value_p, void *user_data)
bool list_replace (struct t_list *l, unsigned idx, const char *key, int64_t value_i, const char *value_p, void *user_data)
bool list_replace_len (struct t_list *l, unsigned idx, const char *key, size_t key_len, int64_t value_i, const char *value_p, size_t value_len, void *user_data)
bool list_replace_user_data (struct t_list *l, unsigned idx, const char *key, int64_t value_i, const char *value_p, void *user_data, user_data_callback free_cb)
bool list_replace_len_user_data (struct t_list *l, unsigned idx, const char *key, size_t key_len, int64_t value_i, const char *value_p, size_t value_len, void *user_data, user_data_callback free_cb)
void * list_node_free (struct t_list_node *n)
void * list_node_free_user_data (struct t_list_node *n, user_data_callback free_cb)
bool list_remove_node (struct t_list *l, unsigned idx)
bool list_remove_node_user_data (struct t_list *l, unsigned idx, user_data_callback free_cb)
bool list_remove_node_by_key (struct t_list *l, const char *key)
bool list_remove_node_by_key_user_data (struct t_list *l, const char *key, user_data_callback free_cb)
struct t_list_nodelist_shift_first (struct t_list *l)
struct t_list_nodelist_node_extract_at (struct t_list *l, unsigned idx)
struct t_list_nodelist_node_extract (struct t_list *l, struct t_list_node *node)
bool list_write_to_disk (sds filepath, struct t_list *l, list_node_to_line_callback node_to_line_cb)
void list_crop (struct t_list *l, unsigned length, user_data_callback free_cb)

Detailed Description

Linked list implementation.

Function Documentation

◆ list_append()

bool list_append ( struct t_list * dst,
struct t_list * src )

Appends a list to another list Leaves user_data pointer in place.

Parameters
dstlist to that was append
srclist that was append
Returns
duplicated list or NULL on error

◆ list_clear()

void list_clear ( struct t_list * l)

Clears the list and frees all nodes, ignores user_data

Parameters
lpointer to list

◆ list_clear_user_data()

void list_clear_user_data ( struct t_list * l,
user_data_callback free_cb )

Clears the list and frees all nodes and there values, calls a function to free user_data, set free_cb to NULL, to free a generic pointer.

Parameters
lpointer to list
free_cb

◆ list_crop()

void list_crop ( struct t_list * l,
unsigned length,
user_data_callback free_cb )

Crops the list to the defined length

Parameters
lpointer to list to crop
lengthmax length
free_cbcallback function to free user_data pointer

◆ list_dup()

struct t_list * list_dup ( struct t_list * l)

Duplicates a list. Leaves user_data pointer in place.

Parameters
llist to duplicate
Returns
duplicated list or NULL on error

◆ list_free()

void list_free ( struct t_list * l)

Clears the list, frees all nodes and the list itself, ignores user_data

Parameters
lpointer to list

◆ list_free_cb_ignore_user_data()

void list_free_cb_ignore_user_data ( struct t_list_node * current)

Callback function to not free user_data.

Parameters
currentlist node

◆ list_free_cb_ptr_user_data()

void list_free_cb_ptr_user_data ( struct t_list_node * current)

Callback function to free user_data of generic pointer.

Parameters
currentlist node

◆ list_free_cb_sds_user_data()

void list_free_cb_sds_user_data ( struct t_list_node * current)

Callback function to free user_data of type sds.

Parameters
currentlist node

◆ list_free_user_data()

void list_free_user_data ( struct t_list * l,
user_data_callback free_cb )

Clears the list and frees all nodes and there values, calls a function to free user_data, set free_cb to NULL, to free a generic pointer.

Parameters
lpointer to list
free_cbCallback to free the user data

◆ list_free_void()

void list_free_void ( void * l)

Clears the list, frees all nodes and the list itself, ignores user_data

Parameters
lpointer to list

◆ list_get_node()

struct t_list_node * list_get_node ( const struct t_list * l,
const char * key )

Gets a list node by key.

Parameters
llist
keykey to get
Returns
pointer to list node

◆ list_get_node_idx()

unsigned list_get_node_idx ( const struct t_list * l,
const char * key )

Gets a list node index by key.

Parameters
llist
keykey to get
Returns
int index of the key, UINT_MAX if not found

◆ list_init()

void list_init ( struct t_list * l)

Inits a already allocated list

Parameters
lpointer to list

◆ list_insert()

bool list_insert ( struct t_list * l,
const char * key,
int64_t value_i,
const char * value_p,
void * user_data )

Creates and inserts a node at the beginning of the list.

Parameters
llist
keykey value
value_iint64_t value
value_psds value
user_datapointer to user_data
Returns
true on success, else false

◆ list_move_node()

bool list_move_node ( struct t_list * l,
unsigned from,
unsigned to )

Moves a node in the list to another position.

Parameters
llist
fromfrom pos
toto pos
Returns
true on success, else false

◆ list_new()

struct t_list * list_new ( void )

Mallocs a new list and inits it.

Returns
allocated empty list

◆ list_node_at()

struct t_list_node * list_node_at ( const struct t_list * l,
unsigned idx )

Gets the list node at idx and its previous node.

Parameters
llist
idxnode index to get
Returns
pointer to list node

◆ list_node_extract()

struct t_list_node * list_node_extract ( struct t_list * l,
struct t_list_node * node )

Removes a node directly from the list

Parameters
llist
nodenode to remove
Returns
pointer to extracted node

◆ list_node_extract_at()

struct t_list_node * list_node_extract_at ( struct t_list * l,
unsigned idx )

Removes the node at idx from the list and returns it.

Parameters
llist
idxnode index to remove
Returns
pointer to list node

◆ list_node_free()

void * list_node_free ( struct t_list_node * n)

Frees a list node, ignoring its user_data pointer.

Parameters
nnode to free
Returns
NULL

◆ list_node_free_user_data()

void * list_node_free_user_data ( struct t_list_node * n,
user_data_callback free_cb )

Frees a list node and its user_data pointer

Parameters
nnode to free
free_cbcallback function to free user_data pointer
Returns
NULL

◆ list_push()

bool list_push ( struct t_list * l,
const char * key,
int64_t value_i,
const char * value_p,
void * user_data )

Creates and appends a node to the end of the list.

Parameters
llist
keykey value
value_iint64_t value
value_psds value
user_datapointer to user_data
Returns
true on success, else false

◆ list_push_len()

bool list_push_len ( struct t_list * l,
const char * key,
size_t key_len,
int64_t value_i,
const char * value_p,
size_t value_len,
void * user_data )

Creates and appends a node to the end of the list. key and value_p len must be specified.

Parameters
llist
keykey value
key_lenkey length
value_iint64_t value
value_psds value
value_lenvalue_p length
user_datapointer to user_data
Returns
true on success, else false

◆ list_remove_node()

bool list_remove_node ( struct t_list * l,
unsigned idx )

Removes the node at idx from the list and frees it. Ignores the user_data pointer.

Parameters
llist
idxnode index to free
Returns
true on success, else false

◆ list_remove_node_by_key()

bool list_remove_node_by_key ( struct t_list * l,
const char * key )

Removes the node with key Ignores user_data pointer

Parameters
llist
keykey
Returns
bool true on success, else false

◆ list_remove_node_by_key_user_data()

bool list_remove_node_by_key_user_data ( struct t_list * l,
const char * key,
user_data_callback free_cb )

Removes the node with key

Parameters
llist
keykey
free_cbCallback to free the user data
Returns
bool true on success, else false

◆ list_remove_node_user_data()

bool list_remove_node_user_data ( struct t_list * l,
unsigned idx,
user_data_callback free_cb )

Removes the node at idx from the list and frees it and frees the user_data pointer

Parameters
llist
idxnode index to free
free_cbcallback function to free user_data pointer
Returns
true on success, else false

◆ list_replace()

bool list_replace ( struct t_list * l,
unsigned idx,
const char * key,
int64_t value_i,
const char * value_p,
void * user_data )

Replaces a list nodes values at pos. Ignores the old user_data pointer.

Parameters
llist
idxindex of node to change
keynew key value
value_inew int64_t value
value_pnew sds value
user_datanew user_data pointer
Returns
true on success, else false

◆ list_replace_len()

bool list_replace_len ( struct t_list * l,
unsigned idx,
const char * key,
size_t key_len,
int64_t value_i,
const char * value_p,
size_t value_len,
void * user_data )

Replaces a list nodes values at pos. Ignores the old user_data pointer.

Parameters
llist
idxindex of node to change
keynew key value
key_lennew key value length
value_inew int64_t value
value_pnew sds value
value_lennew sds value len
user_datanew user_data pointer
Returns
true on success, else false

◆ list_replace_len_user_data()

bool list_replace_len_user_data ( struct t_list * l,
unsigned idx,
const char * key,
size_t key_len,
int64_t value_i,
const char * value_p,
size_t value_len,
void * user_data,
user_data_callback free_cb )

Replaces a list nodes values at pos. Frees the old user_data pointer.

Parameters
llist
idxindex of node to change
keynew key value
key_lennew key value length
value_inew int64_t value
value_pnew sds value
value_lennew sds value len
user_datanew user_data pointer
free_cbcallback function to free old user_data pointer
Returns
true on success, else false

◆ list_replace_user_data()

bool list_replace_user_data ( struct t_list * l,
unsigned idx,
const char * key,
int64_t value_i,
const char * value_p,
void * user_data,
user_data_callback free_cb )

Replaces a list nodes values at pos. Frees the old user_data pointer.

Parameters
llist
idxindex of node to change
keynew key value
value_inew int64_t value
value_pnew sds value
user_datanew user_data pointer
free_cbcallback function to free old user_data pointer
Returns
true on success, else false

◆ list_shift_first()

struct t_list_node * list_shift_first ( struct t_list * l)

Removes the first node from the list and returns it. This is only a shortcut for list_node_extract.

Parameters
llist
Returns
pointer to list node

◆ list_write_to_disk()

bool list_write_to_disk ( sds filepath,
struct t_list * l,
list_node_to_line_callback node_to_line_cb )

Saves the list to disk

Parameters
filepathfilepath to write the list
llist to save
node_to_line_cbcallback function to write a list node
Returns
true on success, else false