Skip to content

Commit

Permalink
Moved singly linked-list functions to their own source file.
Browse files Browse the repository at this point in the history
  • Loading branch information
allinurl committed May 5, 2016
1 parent c83a1cb commit 8aec04a
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 148 deletions.
6 changes: 4 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ goaccess_SOURCES = \
src/gmenu.h \
src/goaccess.c \
src/goaccess.h \
src/gslist.c \
src/gslist.h \
src/gstorage.c \
src/gstorage.h \
src/json.c \
Expand All @@ -76,10 +78,10 @@ goaccess_SOURCES = \
src/output.h \
src/parser.c \
src/parser.h \
src/sort.c \
src/sort.h \
src/settings.c \
src/settings.h \
src/sort.c \
src/sort.h \
src/ui.c \
src/ui.h \
src/util.c \
Expand Down
1 change: 1 addition & 0 deletions src/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "color.h"

#include "error.h"
#include "gslist.h"
#include "util.h"
#include "xmalloc.h"

Expand Down
127 changes: 0 additions & 127 deletions src/commons.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,133 +144,6 @@ get_module_enum (const char *str)
return str2enum (enum_modules, ARRAY_SIZE (enum_modules), str);
}

/* Instantiate a new Singly linked-list node.
*
* On error, aborts if node can't be malloc'd.
* On success, the GSLList node. */
GSLList *
list_create (void *data)
{
GSLList *node = xmalloc (sizeof (GSLList));
node->data = data;
node->next = NULL;

return node;
}

/* Create and insert a node after a given node.
*
* On error, aborts if node can't be malloc'd.
* On success, the newly created node. */
GSLList *
list_insert_append (GSLList * node, void *data)
{
GSLList *newnode;
newnode = list_create (data);
newnode->next = node->next;
node->next = newnode;

return newnode;
}

/* Create and insert a node in front of the list.
*
* On error, aborts if node can't be malloc'd.
* On success, the newly created node. */
GSLList *
list_insert_prepend (GSLList * list, void *data)
{
GSLList *newnode;
newnode = list_create (data);
newnode->next = list;

return newnode;
}

/* Find a node given a pointer to a function that compares them.
*
* If comparison fails, NULL is returned.
* On success, the existing node is returned. */
GSLList *
list_find (GSLList * node, int (*func) (void *, void *), void *data)
{
while (node) {
if (func (node->data, data) > 0)
return node;
node = node->next;
}

return NULL;
}

/* Remove all nodes from the list.
*
* On success, 0 is returned. */
int
list_remove_nodes (GSLList * list)
{
GSLList *tmp;
while (list != NULL) {
tmp = list->next;
if (list->data)
free (list->data);
free (list);
list = tmp;
}

return 0;
}

/* Remove the given node from the list.
*
* On error, 1 is returned.
* On success, 0 is returned. */
int
list_remove_node (GSLList * list, GSLList * node)
{
while (list->next && list->next != node)
list = list->next;

if (list->next) {
list->next = node->next;
if (node->data)
free (node->data);
free (node);
return 0;
}
return 1;
}

/* Iterate over the singly linked-list and call function pointer.
*
* If function pointer does not return 0, -1 is returned.
* On success, 0 is returned. */
int
list_foreach (GSLList * node, int (*func) (void *, void *), void *user_data)
{
while (node) {
if (func (node->data, user_data) != 0)
return -1;
node = node->next;
}

return 0;
}

/* Count the number of elements on the linked-list.
*
* On success, the number of elements is returned. */
int
list_count (GSLList * node)
{
int count = 0;
while (node != 0) {
count++;
node = node->next;
}
return count;
}

/* Instantiate a new GAgents structure.
*
* On success, the newly malloc'd structure is returned. */
Expand Down
17 changes: 0 additions & 17 deletions src/commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,6 @@ typedef struct GAgents_
struct GAgentItem_ *items;
} GAgents;

/* Generic Singly linked-list */
typedef struct GSLList_
{
void *data;
struct GSLList_ *next;
} GSLList;

#define FOREACH_MODULE(item, array) \
for (item = item; (item < ARRAY_SIZE(array)) && array[item] != -1; ++item)

Expand Down Expand Up @@ -261,16 +254,6 @@ uint32_t get_num_modules(void);
void display_default_config_file (void);
void display_storage (void);
void display_version (void);

/* singly linked-list */
GSLList *list_create (void *data);
GSLList *list_find (GSLList * node, int (*func) (void *, void *), void *data);
GSLList *list_insert_append (GSLList * node, void *data);
GSLList *list_insert_prepend (GSLList * list, void *data);
int list_count (GSLList * list);
int list_foreach (GSLList * node, int (*func) (void *, void *), void *user_data);
int list_remove_node (GSLList * list, GSLList * node);
int list_remove_nodes (GSLList * list);
/* *INDENT-ON* */

#endif
4 changes: 3 additions & 1 deletion src/gkhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
#define GKHASH_H_INCLUDED

#include <stdint.h>
#include "parser.h"

#include "gslist.h"
#include "gstorage.h"
#include "khash.h"
#include "parser.h"

/* int keys, int payload */
KHASH_MAP_INIT_INT (ii32, int);
Expand Down
167 changes: 167 additions & 0 deletions src/gslist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* gslist.c -- A Singly link list implementation
* _______ _______ __ __
* / ____/ | / / ___/____ _____/ /_____ / /_
* / / __ | | /| / /\__ \/ __ \/ ___/ //_/ _ \/ __/
* / /_/ / | |/ |/ /___/ / /_/ / /__/ ,< / __/ /_
* \____/ |__/|__//____/\____/\___/_/|_|\___/\__/
*
*
* The MIT License (MIT)
* Copyright (c) 2009-2016 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "gslist.h"
#include "xmalloc.h"

/* Instantiate a new Single linked-list node.
*
* On error, aborts if node can't be malloc'd.
* On success, the GSLList node. */
GSLList *
list_create (void *data)
{
GSLList *node = xmalloc (sizeof (GSLList));
node->data = data;
node->next = NULL;

return node;
}

/* Create and insert a node after a given node.
*
* On error, aborts if node can't be malloc'd.
* On success, the newly created node. */
GSLList *
list_insert_append (GSLList * node, void *data)
{
GSLList *newnode;
newnode = list_create (data);
newnode->next = node->next;
node->next = newnode;

return newnode;
}

/* Create and insert a node in front of the list.
*
* On error, aborts if node can't be malloc'd.
* On success, the newly created node. */
GSLList *
list_insert_prepend (GSLList * list, void *data)
{
GSLList *newnode;
newnode = list_create (data);
newnode->next = list;

return newnode;
}

/* Find a node given a pointer to a function that compares them.
*
* If comparison fails, NULL is returned.
* On success, the existing node is returned. */
GSLList *
list_find (GSLList * node, int (*func) (void *, void *), void *data)
{
while (node) {
if (func (node->data, data) > 0)
return node;
node = node->next;
}

return NULL;
}

/* Remove all nodes from the list.
*
* On success, 0 is returned. */
int
list_remove_nodes (GSLList * list)
{
GSLList *tmp;
while (list != NULL) {
tmp = list->next;
if (list->data)
free (list->data);
free (list);
list = tmp;
}

return 0;
}

/* Remove the given node from the list.
*
* On error, 1 is returned.
* On success, 0 is returned. */
int
list_remove_node (GSLList ** list, GSLList * node)
{
GSLList **current = list, *next = NULL;
for (; *current; current = &(*current)->next) {
if ((*current) != node)
continue;

next = (*current)->next;
if ((*current)->data)
free ((*current)->data);
free (*current);
*current = next;
return 0;
}
return 1;
}

/* Iterate over the single linked-list and call function pointer.
*
* If function pointer does not return 0, -1 is returned.
* On success, 0 is returned. */
int
list_foreach (GSLList * node, int (*func) (void *, void *), void *user_data)
{
while (node) {
if (func (node->data, user_data) != 0)
return -1;
node = node->next;
}

return 0;
}

/* Count the number of elements on the linked-list.
*
* On success, the number of elements is returned. */
int
list_count (GSLList * node)
{
int count = 0;
while (node != 0) {
count++;
node = node->next;
}
return count;
}
Loading

0 comments on commit 8aec04a

Please sign in to comment.