Update to 6762
[qball-mpd.git] / src / list.h
blob35451ec242e8d85b10f0fe87760bbb8cf8f17abb
1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef LIST_H
20 #define LIST_H
22 #include "../config.h"
24 #include <stdlib.h>
26 /* used to make a list where free() will be used to free data in list */
27 #define DEFAULT_FREE_DATA_FUNC free
29 /* typedef for function to free data stored in the list nodes */
30 typedef void ListFreeDataFunc(void *);
32 typedef struct _ListNode {
33 /* used to identify node (ie. when using findInList) */
34 char *key;
35 /* data store in node */
36 void *data;
37 /* next node in list */
38 struct _ListNode *nextNode;
39 /* previous node in list */
40 struct _ListNode *prevNode;
41 } ListNode;
43 typedef struct _List {
44 /* first node in list */
45 ListNode *firstNode;
46 /* last node in list */
47 ListNode *lastNode;
48 /* function used to free data stored in nodes of the list */
49 ListFreeDataFunc *freeDataFunc;
50 /* number of nodes */
51 long numberOfNodes;
52 /* array for searching when list is sorted */
53 ListNode **nodesArray;
54 /* sorted */
55 int sorted;
56 /* whether to strdup() key's on insertion */
57 int strdupKeys;
58 } List;
60 /* allocates memory for a new list and initializes it
61 * _freeDataFunc_ -> pointer to function used to free data, use
62 * DEFAULT_FREE_DATAFUNC to use free()
63 * returns pointer to new list if successful, NULL otherwise
65 List *makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys);
67 /* inserts a node into _list_ with _key_ and _data_
68 * _list_ -> list the data will be inserted in
69 * _key_ -> identifier for node/data to be inserted into list
70 * _data_ -> data to be inserted in list
71 * returns 1 if successful, 0 otherwise
73 ListNode *insertInList(List * list, char *key, void *data);
75 ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode,
76 int pos, char *key, void *data);
78 int insertInListWithoutKey(List * list, void *data);
80 /* deletes the first node in the list with the key _key_
81 * _list_ -> list the node will be deleted from
82 * _key_ -> key used to identify node to delete
83 * returns 1 if node is found and deleted, 0 otherwise
85 int deleteFromList(List * list, char *key);
87 void deleteNodeFromList(List * list, ListNode * node);
89 /* finds data in a list based on key
90 * _list_ -> list to search for _key_ in
91 * _key_ -> which node is being searched for
92 * _data_ -> a pointer to where data will be placed,
93 * _data_ memory should not by allocated or freed
94 * _data_ can be NULL
95 * returns 1 if successful, 0 otherwise
97 int findInList(List * list, char *key, void **data);
99 /* if _key_ is not found, *_node_ is assigned to the node before which
100 the info would be found */
101 int findNodeInList(List * list, char *key, ListNode ** node, int *pos);
104 * returns ListNode at position _pos_ from first node. If no ListNode exists
105 * at position _pos_ returns NULL
107 ListNode *getNodeByPosition(List *list, int pos);
109 /* frees memory malloc'd for list and its nodes
110 * _list_ -> List to be free'd
112 void freeList(void *list);
114 void sortList(List * list);
116 #endif