Skip to content

Commit

Permalink
Check in and expose a linked list.
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuDuponchelle committed Aug 18, 2018
1 parent b37bbe6 commit bb2116a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -42,6 +42,7 @@ set(LIBRARY_SOURCES
houdini_html_e.c
houdini_html_u.c
cmark_ctype.c
linked_list.c
${HEADERS}
)

Expand Down
40 changes: 40 additions & 0 deletions src/cmark.h
Expand Up @@ -106,6 +106,46 @@ typedef struct cmark_mem {
void (*free)(void *);
} cmark_mem;

/**
* ## Basic data structures
*
* To keep dependencies to the strict minimum, libcmark implements
* its own versions of "classic" data structures.
*/

/**
* ### Linked list
*/

/** A generic singly linked list.
*/
typedef struct _cmark_llist
{
struct _cmark_llist *next;
void *data;
} cmark_llist;

typedef void (*CMarkListFreeFunc)(void *data);

/** Append an element to the linked list, return the possibly modified
* head of the list.
*/
CMARK_EXPORT
cmark_llist * cmark_llist_append (cmark_llist * head,
void * data);

/** Free the list starting with 'head', calling 'free_func' with the
* data pointer of each of its elements
*/
CMARK_EXPORT
void cmark_llist_free_full (cmark_llist * head,
CMarkListFreeFunc free_func);

/** Free the list starting with 'head'
*/
CMARK_EXPORT
void cmark_llist_free (cmark_llist * head);

/**
* ## Creating and Destroying Nodes
*/
Expand Down
37 changes: 37 additions & 0 deletions src/linked_list.c
@@ -0,0 +1,37 @@
#include <stdlib.h>

#include "cmark.h"

cmark_llist *cmark_llist_append(cmark_llist *head, void *data) {
cmark_llist *tmp;
cmark_llist *new_node = malloc(sizeof(cmark_llist));

new_node->data = data;
new_node->next = NULL;

if (!head)
return new_node;

for (tmp = head; tmp->next; tmp=tmp->next);

tmp->next = new_node;

return head;
}

void cmark_llist_free_full(cmark_llist *head, CMarkListFreeFunc free_func) {
cmark_llist *tmp, *prev;

for (tmp = head; tmp;) {
if (free_func)
free_func(tmp->data);

prev = tmp;
tmp = tmp->next;
free(prev);
}
}

void cmark_llist_free(cmark_llist *head) {
cmark_llist_free_full(head, NULL);
}

0 comments on commit bb2116a

Please sign in to comment.