DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / structures / string_list.c
1 /********************************************************************
3 string_list.c - function for handling string lists
5 Niksa Orlic, 2004-04-28
7 ********************************************************************/
9 #include "../util/strings.h"
10 #include "../util/error.h"
11 //#include "../util/message.h"
12 #include "string_list.h"
15 #include <string.h>
16 #include <stdlib.h>
18 /*
19 Create a new empty list
20 */
21 string_list *string_list_create()
22 {
23 string_list *new_list = (string_list*) mem_alloc(sizeof(string_list));
25 if (new_list == NULL)
26 die(1);
28 new_list->data = NULL;
29 new_list->next = NULL;
31 return new_list;
32 }
35 /*
36 Delete the list and the data in the list
37 */
38 void string_list_destroy(string_list* item)
39 {
40 string_list *it, *next;
42 it = item;
43 while (it != NULL)
44 {
45 next = it->next;
47 if (it->data != NULL)
48 string_destroy(it->data);
50 mem_free (it);
52 it = next;
53 }
54 }
56 /*
57 Creates a copy of the list, the data
58 values are also copied
59 */
60 string_list *string_list_duplicate(string_list *item)
61 {
62 string_list *new_list;
64 new_list = string_list_create();
66 if (item->data == NULL)
67 return new_list;
69 do
70 {
71 if(item->data == NULL)
72 break;
74 string_list_append(new_list, string_duplicate(item->data));
75 item = item->next;
76 } while (item != NULL);
78 return new_list;
79 }
82 /*
83 Add an element into the list, the data is
84 copied.
85 */
86 void string_list_append(string_list *item, string *data)
87 {
88 string_list *new_element;
90 if (item->data == NULL)
91 item->data = string_duplicate(data);
92 else
93 {
94 new_element = (string_list*) mem_alloc(sizeof(string_list));
96 if (new_element == NULL)
97 die(1);
99 new_element->data = string_duplicate(data);
100 new_element->next = NULL;
102 /* move to the end of the list */
103 while (item->next != NULL)
104 item = item->next;
106 item->next = new_element;
111 /*
112 Returns the number of elements in the list
113 */
114 int string_list_length(string_list *item)
116 int counter = 0;
118 if (item->data == NULL)
119 return counter;
121 do
123 item = item->next;
124 counter ++;
125 } while (item != NULL);
127 return counter;