00001 /***************************************************************************** 00002 * Copyright 2005 Daniel Ferullo * 00003 * * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); * 00005 * you may not use this file except in compliance with the License. * 00006 * You may obtain a copy of the License at * 00007 * * 00008 * http://www.apache.org/licenses/LICENSE-2.0 * 00009 * * 00010 * Unless required by applicable law or agreed to in writing, software * 00011 * distributed under the License is distributed on an "AS IS" BASIS, * 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 00013 * See the License for the specific language governing permissions and * 00014 * limitations under the License. * 00015 * * 00016 *****************************************************************************/ 00017 00018 /** 00019 * @file list.h 00020 * @author Daniel Ferullo (ferullo@cmu.edu) 00021 * 00022 * @brief Functions to implement a list 00023 */ 00024 00025 #ifndef __LIST_H__ 00026 #define __LIST_H__ 00027 00028 /* custom error codes */ 00029 #include "errorcodes.h" 00030 #include <stdlib.h> 00031 00032 /** @brief a macro for the found return value in a match function */ 00033 #define LIST_FOUND 1 00034 /** @brief a macro for the not found return value in a match function */ 00035 #define LIST_NOT_FOUND 0 00036 /** @brief a macro for the fatal error return value in a match function */ 00037 #define LIST_FATAL -1 00038 00039 /** @brief structure that is a single list node */ 00040 struct list_node { 00041 /** @brief the next list node */ 00042 struct list_node *next; 00043 /** @brief the contents of this node */ 00044 void *item; 00045 } __attribute__ ((packed)); 00046 00047 /** @brief typedef for the list_node structure */ 00048 typedef struct list_node list_node_t; 00049 00050 /** @brief structure to contain list information */ 00051 struct list { 00052 /** @brief the head of the list */ 00053 list_node_t *head; 00054 /** @brief a pointer to the last node an item was returned from with 00055 * get(). This is used to speed up sequential gets() 00056 */ 00057 list_node_t *last_get; 00058 /** @brief the index of the last item gotten */ 00059 int last_get_num; 00060 /** @brief the number of items in the list */ 00061 int size; 00062 } __attribute__ ((packed)); 00063 00064 /** @brief typedef for the list structure */ 00065 typedef struct list list_t; 00066 00067 /** 00068 * @brief initializes the list 00069 * 00070 * Use of the list functions before this function is called is undefined 00071 * 00072 * @param list a pointer to the list the initalize 00073 * @return SUCCESS, errorcode on failure 00074 */ 00075 errorcode list_init(list_t *list); 00076 00077 /** 00078 * @brief deletes all entries from a list 00079 * 00080 * Use of the list functions after this function is called is undefined 00081 * 00082 * @param list a pointer to the list to destroy 00083 * @param func a function pointer to a user defined function that will clean up 00084 * each item if cleanup is necessary. If no cleanup is needed, NULL can 00085 * be supplied by the user. This function must be of the form 00086 * void func(void*,void*) where the first argument is the current 00087 * argument to cleanup, and the second is the user defined argument that 00088 * is passed in as the third argument to this function. 00089 * @param arg the one argument allowed to the user defined cleanup function 00090 * @return SUCCESS, errorcode on failure 00091 */ 00092 errorcode list_destroy(list_t *list, void (*func)(void*,void*), void *arg); 00093 00094 /** 00095 * @brief adds an item to the list 00096 * 00097 * @param list a pointer to the list to add the item to 00098 * @param item the item to add to the list 00099 * @return SUCCESS, errorcode on failure 00100 */ 00101 errorcode list_add(list_t *list, void * item); 00102 00103 /** 00104 * @brief finds an item and returns a pointer to it through an argument. 00105 * matches the item by a user specified function 00106 * 00107 * The first item that matches will be returned 00108 * 00109 * @param list a pointer to the list to find in 00110 * @param func a function pointer to the function to match with to find the 00111 * item to remove. Ths function must be of the form 00112 * int func(void*,void*)) where it returns LIST_FOUND when the item is 00113 * found, LIST_NOT_FOUND on not found and LIST_FATAL on "fatal error, 00114 * stop looking". The first parameter to the function is the item in 00115 * the list, and the second is an optional user defined argument (which 00116 * is the third argument to this find function). 00117 * @param arg the optional func argument 00118 * @param found_item a pointer to a location to fill in the found item in. If 00119 * the function returns error, this value is undefined. 00120 * @return SUCCESS (item found) errorcode on failure/not find 00121 */ 00122 errorcode list_find(list_t *list, int (*func)(void*,void*), void* arg, 00123 void**found_item); 00124 00125 /** 00126 * @brief gets the next item in the list after the index passed in 00127 * 00128 * an index is a integer which indexes into the list like it is an array. So, 00129 * an index of 0 is the first element. If the index is negative, or higher 00130 * than the number of elements, an error is returned. Make no assumptions 00131 * about what item should be located at a given location, only assume that if 00132 * this function is called in succession without other list functions called 00133 * in between then the entire list will be returned with no duplicates. 00134 * 00135 * @param list a pointer to the list to get an item from 00136 * @param index to an item to get 00137 * @param item a pointer to a pointer that will be filled in with the returned 00138 * item upon success 00139 * @return SUCCESS, errorcode on failure 00140 */ 00141 errorcode list_get(list_t *list, int index, void **item); 00142 00143 /** 00144 * @brief removes an item from the list, matching the item by the user 00145 * specified function 00146 * 00147 * Only the first item that matches will be removed 00148 * 00149 * @param list a pointer to the list to remove from 00150 * @param func a function pointer to the function to match with to find the 00151 * item to remove. This function must be of the form int 00152 * func(void*,void*) where it return LIST_FOUND when the item is found, 00153 * LIST_NOT_FOUND on not found, and LIST_FATAL on "fatal error, stop 00154 * looking". The first parameter to the function is the item from the 00155 * list, and the second is an optional user defined argument (this is 00156 * the third argument to this remove function). 00157 * @param arg the optional func argument 00158 * @return SUCCESS (removal or item not found), errorcode on failure 00159 */ 00160 errorcode list_remove(list_t *list, int (*func)(void*,void*), void *arg); 00161 00162 /** 00163 * @brief gets the number of items in the list 00164 * 00165 * @param list a pointer to the list to get the item count from 00166 * @return the number of items, neg on failure 00167 */ 00168 00169 int list_count(list_t *list); 00170 00171 #endif /* __LIST_H__ */ 00172