ekg2
 All Struktury Danych Pliki Funkcje Zmienne Definicje typów Wyliczenia Wartości wyliczeń Definicje Grupay Strony
dynstuff.h
Idź do dokumentacji tego pliku.
1 /* $Id$ */
2 
3 /*
4  * (C) Copyright 2001-2002 Wojtek Kaniewski <wojtekka@irc.pl>
5  * Dawid Jarosz <dawjar@poczta.onet.pl>
6  * Adam Wysocki <gophi@ekg.chmurka.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License Version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #ifndef __EKG_DYNSTUFF_H
23 #define __EKG_DYNSTUFF_H
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /*
30  * typedef list_t
31  *
32  * list_t jest prostym typem listy używanej w praktycznie wszystkich
33  * dynamicznych strukturach ekg. obecnie jest to lista jednokierunkowa
34  * (pole `prev' jest równe NULL), ale zostawiono możliwość rozbudowy
35  * do dwukierunkowej bez zmiany ABI. dane są przechowywane w polu `data',
36  * kolejny element w `next'. przykładowy kod iteracji:
37  *
38  * list_t l;
39  *
40  * for (l = lista; l; l = l->next) {
41  * struct cokolwiek *c = l->data;
42  * printf("%s\n", c->cokolwiek);
43  * }
44  *
45  * większość list występujących w ekg można iterować bez obawy o zmiany
46  * ABI. pierwsze pole, będące jednoznacznym identyfikatorem elementu listy
47  * jest dostępne bezpośrednio, reszta przez odpowiednie funkcje.
48  */
49 
50 /*
51  * New *3() lists
52  *
53  * Instead of allocing additional list of dual-pointer structs, we add
54  * 'next' field to the beginning of real struct. C allows us to point
55  * to that first field independently of which struct is it, so we can
56  * use some type-indepdendent functions for that. The main target is
57  * to totally remove old functions, but leave 'list_t'.
58  *
59  * Then, for example, session_t would point to first entry of userlist
60  * (as userlist_t*), and that entry would point to second one, second
61  * one to third, etc. But if we want to group few userlist entries
62  * independently of their original structure, we could just catch them
63  * in standard list_t and use its' 'next' field.
64  */
65 
66 struct list {
67  /* it is important that we keep 'next' first field,
68  * because C allows us to call first field of any structure
69  * without knowing its' type
70  *
71  * so *3() would work peacefully w/ both list_t and not-list_t lists */
72  struct list *next;
73 
74  void *data;
75 };
76 
77 typedef struct list *list_t;
78 
79 #ifndef EKG2_WIN32_NOFUNCTION
80 #define LIST_ADD_COMPARE(x, type) int x(const type data1, const type data2)
81 #define LIST_ADD_SORTED(list, data, comp) list_add_sorted(list, data, (void *) comp)
82 #define LIST_ADD_SORTED2(list, data, comp) list_add_sorted3((list_t *) (void *) list, (list_t) data, (void *) comp)
83 #define LIST_ADD_BEGINNING2(list, data) list_add_beginning3((list_t *) (void *) list, (list_t) data)
84 #define LIST_ADD2(list, data) list_add3((list_t *) (void *) list, (list_t) data)
85 
86 #define LIST_COUNT2(list) list_count((list_t) list)
87 #define LIST_GET_NTH2(list, id) list_get_nth3((list_t) list, id)
88 #define LIST_RESORT(list, comp) list_resort(list, (void *) comp)
89 #define LIST_RESORT2(list, comp) list_resort3((list_t *) (void *) list, (void *) comp)
90 
91 #define LIST_REMOVE(list, data, func) list_remove2(list, data, (void *) func)
92 #define LIST_REMOVE2(list, elem, func) list_remove3((list_t *) (void *) list, (list_t) elem, (void *) func)
93 #define LIST_UNLINK2(list, elem) list_unlink3((list_t *) (void *) list, (list_t) elem)
94 #define LIST_FREE_ITEM(x, type) void x(type data)
95 
96 #define LIST_DESTROY(list, func) list_destroy2(list, (void *) func)
97 #define LIST_DESTROY2(list, func) list_destroy3((list_t) list, (void *) func)
98 
99 void *list_add(list_t *list, void *data);
100 void *list_add_beginning(list_t *list, void *data);
101 void *list_add_sorted(list_t *list, void *data, int (*comparision)(void *, void *));
102 
103 void *list_add3(list_t *list, list_t new_);
104 void *list_add_beginning3(list_t *list, list_t new_);
105 void *list_add_sorted3(list_t *list, list_t new_, int (*comparision)(void *, void *));
106 
107 
108 int list_count(list_t list);
109 void *list_get_nth(list_t list, int id);
110 void *list_get_nth3(list_t list, int id);
111 void list_resort(list_t *list, int (*comparision)(void *, void *));
112 void list_resort3(list_t *list, int (*comparision)(void *, void *));
113 
114 int list_remove(list_t *list, void *data, int free_data);
115 int list_remove2(list_t *list, void *data, void (*func)(void *));
116 void *list_remove3(list_t *list, list_t elem, void (*func)(list_t));
117 void *list_remove3i(list_t *list, list_t elem, void (*func)(list_t data));
118 void *list_unlink3(list_t *list, list_t elem);
119 
120 int list_destroy(list_t list, int free_data);
121 int list_destroy2(list_t list, void (*func)(void *));
122 int list_destroy3(list_t list, void (*func)(void *));
123 
124 void list_cleanup(list_t *list);
125 int list_remove_safe(list_t *list, void *data, int free_data);
126 #endif
127 
128 /*
129  * typedef string_t
130  *
131  * prosty typ tekstowy pozwalający tworzyć ciągi tekstowe o dowolnej
132  * długości bez obawy o rozmiar bufora. ciąg tekstowy jest dostępny
133  * przez pole `str'. nie należy go zmieniać bezpośrednio. przykładowy
134  * kod:
135  *
136  * string_t s;
137  *
138  * s = string_init("ala");
139  * string_append_c(s, ' ');
140  * string_append(s, "ma kota");
141  * printf("%s\n", s->str);
142  * string_free(s, 1);
143  */
144 
145 struct string {
146  char *str;
147  int len, size;
148 };
149 
150 typedef struct string *string_t;
151 
152 #ifndef EKG2_WIN32_NOFUNCTION
153 
154 string_t string_init(const char *str);
155 int string_append(string_t s, const char *str);
156 int string_append_n(string_t s, const char *str, int count);
157 int string_append_c(string_t s, char ch);
158 int string_append_raw(string_t s, const char *str, int count);
159 int string_append_format(string_t s, const char *format, ...);
160 void string_insert(string_t s, int index, const char *str);
161 void string_insert_n(string_t s, int index, const char *str, int count);
162 void string_remove(string_t s, int count);
163 void string_clear(string_t s);
164 char *string_free(string_t s, int free_string);
165 
166 /* tablice stringow */
167 char **array_make(const char *string, const char *sep, int max, int trim, int quotes);
168 char *array_join(char **array, const char *sep);
169 char *array_join_count(char **array, const char *sep, int count);
170 
171 int array_add(char ***array, char *string);
172 int array_add_check(char ***array, char *string, int casesensitive);
173 int array_count(char **array);
174 int array_contains(char **array, const char *string, int casesensitive);
175 int array_item_contains(char **array, const char *string, int casesensitive);
176 char *array_shift(char ***array);
177 void array_free(char **array);
178 void array_free_count(char **array, int count);
179 
180 /* rozszerzenia libców */
181 
182 const char *itoa(long int i);
183 const char *cssfind(const char *haystack, const char *needle, const char sep, int caseinsensitive);
184 
185 #endif
186 
187 char *escape(const char *src);
188 char *unescape(const char *src);
189 
190 /*
191  * handle private data
192  */
193 typedef struct private_data_s {
195 
196  char *name;
197  char *value;
199 
200 int private_item_get_safe(private_data_t **data, const char *item_name, char **result);
201 const char *private_item_get(private_data_t **data, const char *item_name);
202 
203 int private_item_get_int_safe(private_data_t **data, const char *item_name, int *result);
204 int private_item_get_int(private_data_t **data, const char *item_name);
205 
206 void private_item_set(private_data_t **data, const char *item_name, const char *value);
207 void private_item_set_int(private_data_t **data, const char *item_name, int value);
208 
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif /* __EKG_DYNSTUFF_H */
216 
217 /*
218  * Local Variables:
219  * mode: c
220  * c-file-style: "k&r"
221  * c-basic-offset: 8
222  * indent-tabs-mode: t
223  * End:
224  */