Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
LISTS: generic type list

Data Structures

struct  LIST
 Structure of a generic LIST container. More...
struct  LIST_NODE
 Structure of a generic list node. More...

Macros

#define link_node(__NODE_1, __NODE_2)
 Macro helper for linking two nodes, with NULL safety.
#define list_foreach(__ITEM_, __LIST_)
 ForEach macro helper, safe for node removal during iteration.
#define list_pop(__LIST_, __TYPE_)
 Pop macro helper for void pointer casting.
#define list_shift(__LIST_, __TYPE_)
 Shift macro helper for void pointer casting.
#define MAX_LIST_ITEMS   SIZE_MAX
 flag to pass to new_generic_list for the maximum possible number of item in a list
#define remove_list_node(__LIST_, __NODE_, __TYPE_)
 Remove macro helper for void pointer casting.
#define UNLIMITED_LIST_ITEMS   0
 flag to pass to new_generic_list for an unlimited number of item in the list.

Functions

int list_destroy (LIST **list)
 free the list
int list_empty (LIST *list)
 empty the list
int list_empty_with_f (LIST *list, void(*free_fnct)(void *ptr))
 empty the list with a custom free function
LIST_NODE * list_node_pop (LIST *list)
 pop a node from the end of the list
int list_node_push (LIST *list, LIST_NODE *node)
 push a node at the end of the list
LIST_NODE * list_node_shift (LIST *list)
 shift a node from the beginning of the list
int list_node_unshift (LIST *list, LIST_NODE *node)
 unshift a node at the beginning of the list
void * list_pop_f (LIST *list)
 get last ptr from list
int list_push (LIST *list, void *ptr, void(*destructor)(void *ptr))
 add a pointer at the end of the list
int list_push_sorted (LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
 add a pointer sorted via comparator from the end of the list
LIST_NODE * list_search (LIST *list, const void *ptr)
 search ptr in list
LIST_NODE * list_search_with_f (LIST *list, int(*checkfunk)(void *ptr))
 search for data in list
void * list_shift_f (LIST *list, char *file, size_t line)
 get first ptr from list
int list_unshift (LIST *list, void *ptr, void(*destructor)(void *ptr))
 put a pointer at the beginning of list
int list_unshift_sorted (LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
 put a pointer sorted via comparator from the start to the end
LIST * new_generic_list (size_t max_items)
 initialize a list
LIST_NODE * new_list_node (void *ptr, void(*destructor)(void *ptr))
 create a new node
void * remove_list_node_f (LIST *list, LIST_NODE *node)
 remove a node

Detailed Description


Data Structure Documentation

◆ LIST

struct LIST
Collaboration diagram for LIST:
Data Fields
LIST_NODE * end pointer to the end of the list
size_t nb_items number of item currently in the list
size_t nb_max_items Maximum number of items in the list.

0 means unlimited

LIST_NODE * start pointer to the start of the list

◆ LIST_NODE

struct LIST_NODE

Structure of a generic list node.

Examples
ex_git.c, ex_html.c, ex_list.c, and ex_network_reactor.c.

Definition at line 44 of file n_list.h.

Collaboration diagram for LIST_NODE:
Data Fields
void(*)(void *ptr) destroy_func pointer to destructor function if any, else NULL
struct LIST_NODE * next pointer to the next node
struct LIST_NODE * prev pointer to the previous node
void * ptr void pointer to store

Macro Definition Documentation

◆ link_node

#define link_node ( __NODE_1,
__NODE_2 )
Value:
do { \
if (!(__NODE_1) || !(__NODE_2)) { \
n_log(LOG_ERR, "link_node: NULL argument (%s=%p, %s=%p)", #__NODE_1, (void*)(__NODE_1), #__NODE_2, (void*)(__NODE_2)); \
} else { \
(__NODE_2)->prev = (__NODE_1); \
(__NODE_1)->next = (__NODE_2); \
} \
} while (0)
#define LOG_ERR
error conditions
Definition n_log.h:100

Macro helper for linking two nodes, with NULL safety.

Definition at line 78 of file n_list.h.

Referenced by list_node_unshift(), list_push_sorted(), list_unshift(), and list_unshift_sorted().

◆ list_foreach

#define list_foreach ( __ITEM_,
__LIST_ )
Value:
for (LIST_NODE* __ITEM_ = (__LIST_) ? (__LIST_)->start : NULL, *__next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL; __ITEM_; __ITEM_ = __next_##__ITEM_, \
__next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL)
struct LIST_NODE * next
pointer to the next node
Definition n_list.h:52
Structure of a generic list node.
Definition n_list.h:44

ForEach macro helper, safe for node removal during iteration.

Examples
ex_diff.c, ex_file.c, ex_git.c, ex_gui_dictionary.c, ex_gui_network.c, ex_hash.c, ex_html.c, ex_kafka.c, ex_list.c, ex_network_ssl.c, and ex_network_ssl_hardened.c.

Definition at line 89 of file n_list.h.

Referenced by _button_neighbour(), _compute_gui_bounds(), _datagrid_resize_hover(), _draw_window(), _find_focused_window(), _find_widget_const(), _find_widget_window(), _find_window_node(), _ht_get_node(), _ht_remove(), _n_gui_window_capture_normalized(), _n_html_attr_lookup(), _n_html_push_js_url(), _pointer_over_window(), _sort_windows_by_zorder(), _splitpane_hit(), _tooltip_widget_at(), _topmost_window_at(), _window_update_content_size(), destroy_threaded_pool(), dump_nodup_log(), find_link(), get_config_section_value(), get_nb_config_file_sections(), get_nb_config_file_sections_entries(), handle_request(), ht_get_node_ex(), ht_put_ptr_ex(), ht_remove_ex(), list_count(), list_search(), list_search_with_f(), main(), main(), main(), mp_emit(), n_cookiejar_add_from_header(), n_cookiejar_build_header(), n_cookiejar_remove(), n_diff_to_unified(), n_fluid_simulate_threaded(), n_gui_apply_adaptive_resize(), n_gui_close_window(), n_gui_count_detached(), n_gui_destroy_ctx(), n_gui_draw(), n_gui_draw_detached(), n_gui_focus_window(), n_gui_hovered_widget(), n_gui_load_layout_json(), n_gui_load_theme_json(), n_gui_process_event(), n_gui_reset_all_widget_themes(), n_gui_save_layout_json(), n_gui_set_default_font(), n_gui_set_focus(), n_gui_set_resize_mode(), n_gui_wants_mouse(), n_gui_widget_set_rect(), n_gui_window_apply_autofit(), n_gui_window_attach(), n_gui_window_autosize(), n_gui_window_detach(), n_gui_window_find_button(), n_gui_window_from_display(), n_gui_window_set_rect_axes(), n_html_extract_forms(), n_html_extract_links(), n_kafka_dump_unprocessed(), netw_close(), test_scripts(), update_completion(), update_definitions(), userlist_send_waiting_msgs(), and userlist_user_send_waiting_msgs().

◆ list_pop

#define list_pop ( __LIST_,
__TYPE_ )
Value:
(__TYPE_*)list_pop_f(__LIST_)
void * list_pop_f(LIST *list)
Get a pointer from the end of the list.
Definition n_list.c:403

Pop macro helper for void pointer casting.

Examples
ex_list.c.

Definition at line 94 of file n_list.h.

Referenced by main().

◆ list_shift

#define list_shift ( __LIST_,
__TYPE_ )
Value:
(__TYPE_*)list_shift_f(__LIST_, __FILE__, __LINE__)
void * list_shift_f(LIST *list, char *file, size_t line)
Get a pointer from the start of the list.
Definition n_list.c:436

Shift macro helper for void pointer casting.

Examples
ex_list.c.

Definition at line 96 of file n_list.h.

Referenced by get_bytes_from_msg(), get_int_from_msg(), get_nb_from_msg(), get_nstr_from_msg(), get_str_from_msg(), main(), netw_get_msg(), and netw_send_func().

◆ MAX_LIST_ITEMS

◆ remove_list_node

#define remove_list_node ( __LIST_,
__NODE_,
__TYPE_ )
Value:
(__TYPE_*)remove_list_node_f(__LIST_, __NODE_)
void * remove_list_node_f(LIST *list, LIST_NODE *node)
Internal function called each time we need to get a node out of a list.
Definition n_list.c:76

Remove macro helper for void pointer casting.

Examples
ex_list.c.

Definition at line 98 of file n_list.h.

Referenced by _empty_ht(), _ht_remove(), free_emitters(), free_particle_system(), ht_remove_ex(), main(), manage_particle_ex(), n_kafka_get_event(), n_kafka_poll(), netw_pool_remove(), refresh_thread_pool(), and remove_emitter().

◆ UNLIMITED_LIST_ITEMS

#define UNLIMITED_LIST_ITEMS   0

flag to pass to new_generic_list for an unlimited number of item in the list.

Can be dangerous !

Examples
ex_gui_network.c, and ex_list.c.

Definition at line 73 of file n_list.h.

Referenced by init_particle_system(), main(), n_git_list_branches(), n_git_log(), n_git_status(), n_gui_add_window(), and n_gui_new_ctx().

Function Documentation

◆ list_destroy()

int list_destroy ( LIST ** list)

◆ list_empty()

int list_empty ( LIST * list)

empty the list

empty the list

Parameters
listThe list to empty. An already empty or null list will cause an error and a _log message.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 501 of file n_list.c.

References __n_assert, LIST_NODE::destroy_func, LIST::end, Free, LOG_ERR, n_log, LIST::nb_items, LIST_NODE::next, LIST_NODE::ptr, and LIST::start.

Referenced by empty_msg(), list_destroy(), main(), netw_set(), userlist_del_user(), userlist_send_waiting_msgs(), and userlist_user_send_waiting_msgs().

Here is the caller graph for this function:

◆ list_empty_with_f()

int list_empty_with_f ( LIST * list,
void(* free_fnct )(void *ptr) )

empty the list with a custom free function

empty the list with a custom free function

Parameters
listThe list to empty. An already empty or null list will cause an error and a _log message.
free_fncta function ptr to a void func( void *ptr )
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 526 of file n_list.c.

References __n_assert, LIST::end, Free, LOG_ERR, n_log, LIST::nb_items, LIST_NODE::next, LIST_NODE::ptr, and LIST::start.

◆ list_node_pop()

LIST_NODE * list_node_pop ( LIST * list)

pop a node from the end of the list

pop a node from the end of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A LIST_NODE *node or NULL. Check error messages in DEBUG mode.

Definition at line 143 of file n_list.c.

References __n_assert, LIST::end, LOG_ERR, n_log, LIST::nb_items, LIST_NODE::next, LIST_NODE::prev, and LIST::start.

Referenced by main().

Here is the caller graph for this function:

◆ list_node_push()

int list_node_push ( LIST * list,
LIST_NODE * node )

push a node at the end of the list

push a node at the end of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
nodeThe node pointer you want to put in the list. A null value will cause an error and a _log message.
Returns
TRUE or FALSE

Definition at line 117 of file n_list.c.

References __n_assert, LIST::end, LOG_ERR, n_log, LIST::nb_items, LIST::nb_max_items, LIST_NODE::next, LIST_NODE::prev, and LIST::start.

Referenced by ht_resize(), main(), and tree_insert_child().

Here is the caller graph for this function:

◆ list_node_shift()

LIST_NODE * list_node_shift ( LIST * list)

shift a node from the beginning of the list

shift a node from the beginning of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A LIST_NODE *node or NULL. Check error messages in DEBUG mode.

Definition at line 170 of file n_list.c.

References __n_assert, LIST::end, LOG_ERR, n_log, LIST::nb_items, LIST_NODE::next, LIST_NODE::prev, and LIST::start.

Referenced by ht_resize(), and main().

Here is the caller graph for this function:

◆ list_node_unshift()

int list_node_unshift ( LIST * list,
LIST_NODE * node )

unshift a node at the beginning of the list

unshift a node at the beginning of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
nodeThe node pointer you wan to put in the list. A null value will cause an error and a _log message.
Returns
TRUE or FALSE

Definition at line 200 of file n_list.c.

References __n_assert, LIST::end, link_node, LOG_ERR, n_log, LIST::nb_items, LIST::nb_max_items, LIST_NODE::next, LIST_NODE::prev, and LIST::start.

Referenced by main().

Here is the caller graph for this function:

◆ list_pop_f()

void * list_pop_f ( LIST * list)

get last ptr from list

get last ptr from list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
Returns
A void *ptr or NULL. Check error messages in DEBUG mode.

Definition at line 403 of file n_list.c.

References __n_assert, LIST::end, Free, LOG_ERR, n_log, LIST::nb_items, LIST_NODE::next, LIST_NODE::prev, LIST_NODE::ptr, and LIST::start.

◆ list_push()

int list_push ( LIST * list,
void * ptr,
void(* destructor )(void *ptr) )

add a pointer at the end of the list

add a pointer at the end of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 228 of file n_list.c.

References __n_assert, LIST::end, LOG_ERR, n_log, LIST::nb_items, LIST::nb_max_items, new_list_node(), LIST_NODE::next, LIST_NODE::prev, and LIST::start.

Referenced by _ht_depth_first_search(), _ht_put_double(), _ht_put_int(), _ht_put_ptr(), _ht_put_string(), _ht_put_string_ptr(), _ht_search(), _ht_search_trie_helper(), _n_html_parse_tag(), _n_html_push_link(), _n_mock_parse_request(), _sort_windows_by_zorder(), add_bytes_to_msg(), add_emitter(), add_int_to_msg(), add_nb_to_msg(), add_nstrdup_to_msg(), add_nstrptr_to_msg(), add_particle(), add_particle_at(), add_strdup_to_msg(), add_threaded_process(), ht_get_completion_list(), ht_put_ptr_ex(), list_push_sorted(), list_unshift_sorted(), load_config_file(), main(), main(), n_cookiejar_add_from_header(), n_git_list_branches(), n_git_log(), n_git_status(), n_gui_add_button(), n_gui_add_checkbox(), n_gui_add_combobox(), n_gui_add_custom(), n_gui_add_datagrid(), n_gui_add_dropmenu(), n_gui_add_hexview(), n_gui_add_image(), n_gui_add_label(), n_gui_add_listbox(), n_gui_add_markdownview(), n_gui_add_progressbar(), n_gui_add_radiolist(), n_gui_add_scrollbar(), n_gui_add_slider(), n_gui_add_splitpane(), n_gui_add_syntaxview(), n_gui_add_textarea(), n_gui_add_window(), n_gui_raise_window(), n_html_extract_forms(), n_html_extract_scripts(), n_http_parse_multipart(), n_kafka_poll(), n_kafka_produce(), n_sitemap_extract_urls(), netw_add_msg(), netw_add_msg_ex(), netw_pool_add(), netw_recv_func(), new_n_fluid(), process_args(), run_server(), scan_dir_ex(), and userlist_user_add_waiting_msg().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_push_sorted()

int list_push_sorted ( LIST * list,
void * ptr,
int(* comparator )(const void *a, const void *b),
void(* destructor )(void *ptr) )

add a pointer sorted via comparator from the end of the list

add a pointer sorted via comparator from the end of the list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
comparatorA pointer to a function which take two void * pointers and return an int.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 261 of file n_list.c.

References __n_assert, LIST::end, link_node, list_push(), list_unshift(), LOG_ERR, n_log, LIST::nb_items, LIST::nb_max_items, new_list_node(), LIST_NODE::next, LIST_NODE::prev, and LIST_NODE::ptr.

Referenced by main(), and n_scan_dir().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_search()

LIST_NODE * list_search ( LIST * list,
const void * ptr )

search ptr in list

Parameters
listThe target list
ptrSearched pointer
Returns
The LIST_NODE *node or NULL

Definition at line 469 of file n_list.c.

References __n_assert, and list_foreach.

Referenced by main(), and netw_pool_remove().

Here is the caller graph for this function:

◆ list_search_with_f()

LIST_NODE * list_search_with_f ( LIST * list,
int(* checkfunk )(void *ptr) )

search for data in list

search for data in list

Parameters
listThe target list
checkfunkFunction pointer who should return 1 if it matches or 0 if it's not
Returns
The LIST_NODE *node or NULL

Definition at line 485 of file n_list.c.

References __n_assert, and list_foreach.

◆ list_shift_f()

void * list_shift_f ( LIST * list,
char * file,
size_t line )

get first ptr from list

get first ptr from list

Helper function for the list_shift macro.

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
fileholder for FILE
lineholder for LINE
Returns
A void *ptr or NULL. Check error messages in DEBUG mode.

Definition at line 436 of file n_list.c.

References __n_assert, LIST::end, Free, line(), LOG_ERR, n_log, LIST::nb_items, LIST_NODE::next, LIST_NODE::prev, LIST_NODE::ptr, and LIST::start.

Here is the call graph for this function:

◆ list_unshift()

int list_unshift ( LIST * list,
void * ptr,
void(* destructor )(void *ptr) )

put a pointer at the beginning of list

put a pointer at the beginning of list

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
A void *ptr or NULL. Check error messages in DEBUG mode.

Definition at line 317 of file n_list.c.

References __n_assert, LIST::end, link_node, LOG_ERR, n_log, LIST::nb_items, LIST::nb_max_items, new_list_node(), and LIST::start.

Referenced by _emit(), list_push_sorted(), list_unshift_sorted(), main(), and n_gui_lower_window().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_unshift_sorted()

int list_unshift_sorted ( LIST * list,
void * ptr,
int(* comparator )(const void *a, const void *b),
void(* destructor )(void *ptr) )

put a pointer sorted via comparator from the start to the end

put a pointer sorted via comparator from the start to the end

Parameters
listAn initilized list container. A null value will cause an error and a _log message.
ptrThe pointer you wan to put in the list. A null value will cause an error and a _log message.
comparatorA pointer to a function which take two void * pointers and return an int.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
TRUE or FALSE. Check error messages in DEBUG mode.

Definition at line 349 of file n_list.c.

References __n_assert, link_node, list_push(), list_unshift(), LOG_ERR, n_log, LIST::nb_items, LIST::nb_max_items, new_list_node(), LIST_NODE::next, LIST_NODE::prev, and LIST::start.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ new_generic_list()

◆ new_list_node()

LIST_NODE * new_list_node ( void * ptr,
void(* destructor )(void *ptr) )

create a new node

create a new node

Parameters
ptrThe pointer you want to put in the node.
destructorPointer to the ptr type destructor function. Leave to NULL if there isn't.
Returns
A new node or NULL on error. Check error messages in DEBUG mode.

Definition at line 57 of file n_list.c.

References __n_assert, LIST_NODE::destroy_func, LOG_ERR, Malloc, n_log, LIST_NODE::next, LIST_NODE::prev, and LIST_NODE::ptr.

Referenced by list_push(), list_push_sorted(), list_unshift(), list_unshift_sorted(), main(), and tree_insert_child().

Here is the caller graph for this function:

◆ remove_list_node_f()

void * remove_list_node_f ( LIST * list,
LIST_NODE * node )

remove a node

remove a node

Parameters
listThe list to pick in
nodeThe node to remove in the list
Returns
The node holded pointer or NULL

Definition at line 76 of file n_list.c.

References __n_assert, LIST::end, Free, LOG_ERR, n_log, LIST::nb_items, LIST_NODE::next, LIST_NODE::prev, LIST_NODE::ptr, and LIST::start.

Referenced by _sort_windows_by_zorder(), n_cookiejar_clear(), n_cookiejar_remove(), n_gui_lower_window(), n_gui_raise_window(), run_server(), and tree_delete_node().

Here is the caller graph for this function: