ekg2  GIT master
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 #include <glib.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /*
32  * typedef list_t
33  *
34  * list_t jest prostym typem listy używanej w praktycznie wszystkich
35  * dynamicznych strukturach ekg. obecnie jest to lista jednokierunkowa
36  * (pole `prev' jest równe NULL), ale zostawiono możliwość rozbudowy
37  * do dwukierunkowej bez zmiany ABI. dane są przechowywane w polu `data',
38  * kolejny element w `next'. przykładowy kod iteracji:
39  *
40  * list_t l;
41  *
42  * for (l = lista; l; l = l->next) {
43  * struct cokolwiek *c = l->data;
44  * printf("%s\n", c->cokolwiek);
45  * }
46  *
47  * większość list występujących w ekg można iterować bez obawy o zmiany
48  * ABI. pierwsze pole, będące jednoznacznym identyfikatorem elementu listy
49  * jest dostępne bezpośrednio, reszta przez odpowiednie funkcje.
50  */
51 
52 /*
53  * New *3() lists
54  *
55  * Instead of allocing additional list of dual-pointer structs, we add
56  * 'next' field to the beginning of real struct. C allows us to point
57  * to that first field independently of which struct is it, so we can
58  * use some type-indepdendent functions for that. The main target is
59  * to totally remove old functions, but leave 'list_t'.
60  *
61  * Then, for example, session_t would point to first entry of userlist
62  * (as userlist_t*), and that entry would point to second one, second
63  * one to third, etc. But if we want to group few userlist entries
64  * independently of their original structure, we could just catch them
65  * in standard list_t and use its' 'next' field.
66  */
67 
68 struct list {
69  /* it is important that we keep 'next' first field,
70  * because C allows us to call first field of any structure
71  * without knowing its' type
72  *
73  * so *3() would work peacefully w/ both list_t and not-list_t lists */
74  struct list *next;
75 
76  void *data;
77 };
78 
79 typedef struct list *list_t;
80 
81 #ifndef EKG2_WIN32_NOFUNCTION
82 #define LIST_ADD_COMPARE(x, type) int x(const type data1, const type data2)
83 #define LIST_ADD_SORTED(list, data, comp) list_add_sorted(list, data, (void *) comp)
84 #define LIST_ADD_SORTED2(list, data, comp) list_add_sorted3((list_t *) (void *) list, (list_t) data, (void *) comp)
85 #define LIST_ADD_BEGINNING2(list, data) list_add_beginning3((list_t *) (void *) list, (list_t) data)
86 #define LIST_ADD2(list, data) list_add3((list_t *) (void *) list, (list_t) data)
87 
88 #define LIST_COUNT2(list) list_count((list_t) list)
89 #define LIST_GET_NTH2(list, id) list_get_nth3((list_t) list, id)
90 #define LIST_RESORT(list, comp) list_resort(list, (void *) comp)
91 #define LIST_RESORT2(list, comp) list_resort3((list_t *) (void *) list, (void *) comp)
92 
93 #define LIST_REMOVE(list, data, func) list_remove2(list, data, (void *) func)
94 #define LIST_REMOVE2(list, elem, func) list_remove3((list_t *) (void *) list, (list_t) elem, (void *) func)
95 #define LIST_UNLINK2(list, elem) list_unlink3((list_t *) (void *) list, (list_t) elem)
96 #define LIST_FREE_ITEM(x, type) void x(type data)
97 
98 #define LIST_DESTROY(list, func) list_destroy2(list, (void *) func)
99 #define LIST_DESTROY2(list, func) list_destroy3((list_t) list, (void *) func)
100 
101 void *list_add(list_t *list, void *data);
102 void *list_add_beginning(list_t *list, void *data);
103 void *list_add_sorted(list_t *list, void *data, int (*comparision)(void *, void *));
104 
105 void *list_add3(list_t *list, list_t new_);
106 void *list_add_beginning3(list_t *list, list_t new_);
107 void *list_add_sorted3(list_t *list, list_t new_, int (*comparision)(void *, void *));
108 
109 
110 int list_count(list_t list);
111 void *list_get_nth(list_t list, int id);
112 void *list_get_nth3(list_t list, int id);
113 void list_resort(list_t *list, int (*comparision)(void *, void *));
114 void list_resort3(list_t *list, int (*comparision)(void *, void *));
115 
116 int list_remove(list_t *list, void *data, int free_data);
117 int list_remove2(list_t *list, void *data, void (*func)(void *));
118 void *list_remove3(list_t *list, list_t elem, void (*func)(list_t));
119 void *list_remove3i(list_t *list, list_t elem, void (*func)(list_t data));
120 void *list_unlink3(list_t *list, list_t elem);
121 
122 int list_destroy(list_t list, int free_data);
123 int list_destroy2(list_t list, void (*func)(void *));
124 int list_destroy3(list_t list, void (*func)(void *));
125 
126 void list_cleanup(list_t *list);
127 int list_remove_safe(list_t *list, void *data, int free_data);
128 #endif
129 
130 /*
131  * typedef string_t
132  *
133  * prosty typ tekstowy pozwalający tworzyć ciągi tekstowe o dowolnej
134  * długości bez obawy o rozmiar bufora. ciąg tekstowy jest dostępny
135  * przez pole `str'. nie należy go zmieniać bezpośrednio. przykładowy
136  * kod:
137  *
138  * string_t s;
139  *
140  * s = string_init("ala");
141  * string_append_c(s, ' ');
142  * string_append(s, "ma kota");
143  * printf("%s\n", s->str);
144  * string_free(s, 1);
145  */
146 
147 typedef GString *string_t;
148 
149 string_t string_init(const char *str);
150 int string_append(string_t s, const char *str);
151 int string_append_n(string_t s, const char *str, int count);
152 int string_append_c(string_t s, char ch);
153 int string_append_raw(string_t s, const char *str, int count);
154 int string_append_format(string_t s, const char *format, ...);
155 void string_insert(string_t s, int index, const char *str);
156 void string_insert_n(string_t s, int index, const char *str, int count);
157 void string_remove(string_t s, int count);
158 void string_clear(string_t s);
159 char *string_free(string_t s, int free_string);
160 
161 /* tablice stringow */
162 char **array_make(const char *string, const char *sep, int max, int trim, int quotes);
163 char *array_join_count(char **array, const char *sep, int count);
164 
165 int array_add(char ***array, char *string);
166 int array_add_check(char ***array, char *string, int casesensitive);
167 int array_contains(char **array, const char *string, int casesensitive);
168 int array_item_contains(char **array, const char *string, int casesensitive);
169 char *array_shift(char ***array);
170 void array_free_count(char **array, int count);
171 
172 /* rozszerzenia libców */
173 
174 const char *ekg_itoa(long int i);
175 const char *cssfind(const char *haystack, const char *needle, const char sep, int caseinsensitive);
176 
177 char *escape(const char *src);
178 char *unescape(const char *src);
179 
180 /*
181  * handle private data
182  */
183 typedef struct private_data_s {
185 
186  char *name;
187  char *value;
189 
190 int private_item_get_safe(private_data_t **data, const char *item_name, char **result);
191 const char *private_item_get(private_data_t **data, const char *item_name);
192 
193 int private_item_get_int_safe(private_data_t **data, const char *item_name, int *result);
194 int private_item_get_int(private_data_t **data, const char *item_name);
195 
196 void private_item_set(private_data_t **data, const char *item_name, const char *value);
197 void private_item_set_int(private_data_t **data, const char *item_name, int value);
198 
200 
201 #if !GLIB_CHECK_VERSION(2, 28, 0)
202 void g_slist_free_full(GSList *list, GDestroyNotify free_func);
203 #endif
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif /* __EKG_DYNSTUFF_H */
210 
211 /*
212  * Local Variables:
213  * mode: c
214  * c-file-style: "k&r"
215  * c-basic-offset: 8
216  * indent-tabs-mode: t
217  * End:
218  */
int private_item_get_int(private_data_t **data, const char *item_name)
Definition: dynstuff.c:1283
void * list_remove3(list_t *list, list_t elem, void(*func)(list_t))
int list_count(list_t list)
Definition: dynstuff.c:471
void string_remove(string_t s, int count)
Definition: dynstuff.c:723
void list_resort3(list_t *list, int(*comparision)(void *, void *))
Definition: dynstuff.c:448
void list_resort(list_t *list, int(*comparision)(void *, void *))
Definition: dynstuff.c:431
GString * string_t
Definition: dynstuff.h:147
void * list_add(list_t *list, void *data)
Definition: dynstuff.c:129
const char * private_item_get(private_data_t **data, const char *item_name)
Definition: dynstuff.c:1267
void list_cleanup(list_t *list)
Definition: dynstuff.c:236
int array_item_contains(char **array, const char *string, int casesensitive)
Definition: dynstuff.c:997
int string_append_n(string_t s, const char *str, int count)
Definition: dynstuff.c:575
void * data
Definition: ekg_hash_benchmark.c:15
void string_insert(string_t s, int index, const char *str)
Definition: dynstuff.c:682
char * string_free(string_t s, int free_string)
Definition: dynstuff.c:758
Definition: ekg_hash_benchmark.c:47
string_t string_init(const char *str)
Definition: dynstuff.c:697
struct list * list_t
Definition: dynstuff.h:79
void * list_add_sorted3(list_t *list, list_t new_, int(*comparision)(void *, void *))
Definition: dynstuff.c:134
int string_append_format(string_t s, const char *format,...)
Definition: dynstuff.c:608
void g_slist_free_full(GSList *list, GDestroyNotify free_func)
Definition: dynstuff.c:1313
char * name
Definition: dynstuff.h:186
void * list_remove3i(list_t *list, list_t elem, void(*func)(list_t data))
Definition: testcase_for_remove_iter.c:55
void string_clear(string_t s)
Definition: dynstuff.c:739
void * list_get_nth3(list_t list, int id)
Definition: dynstuff.c:419
int list_destroy2(list_t list, void(*func)(void *))
Definition: dynstuff.c:481
char * escape(const char *src)
Definition: dynstuff.c:1120
char * array_shift(char ***array)
Definition: dynstuff.c:1014
int string_append_c(string_t s, char ch)
Definition: dynstuff.c:551
void * list_add_beginning(list_t *list, void *data)
Definition: ekg_hash_benchmark.c:31
int list_destroy(list_t list, int free_data)
Definition: dynstuff.c:535
int list_remove(list_t *list, void *data, int free_data)
Definition: dynstuff.c:390
char ch
Definition: completion.c:638
int i
Definition: ekg_hash_benchmark.c:110
char * value
Definition: dynstuff.h:187
int private_item_get_safe(private_data_t **data, const char *item_name, char **result)
Definition: dynstuff.c:1256
char * unescape(const char *src)
Definition: dynstuff.c:1163
void * list_get_nth(list_t list, int id)
Definition: dynstuff.c:405
int list_destroy3(list_t list, void(*func)(void *))
Definition: dynstuff.c:498
struct list * next
Definition: ekg_hash_benchmark.c:17
void * list_add_beginning3(list_t *list, list_t new_)
Definition: testcase_for_remove_iter.c:43
struct private_data_s * next
Definition: dynstuff.h:184
char * array_join_count(char **array, const char *sep, int count)
Definition: dynstuff.c:940
char ** array_make(const char *string, const char *sep, int max, int trim, int quotes)
Definition: dynstuff.c:806
int list_remove2(list_t *list, void *data, void(*func)(void *))
int array_add_check(char ***array, char *string, int casesensitive)
Definition: dynstuff.c:931
int string_append_raw(string_t s, const char *str, int count)
Definition: dynstuff.c:627
int private_item_get_int_safe(private_data_t **data, const char *item_name, int *result)
Definition: dynstuff.c:1274
void string_insert_n(string_t s, int index, const char *str, int count)
Definition: dynstuff.c:657
Definition: ekg_hash_benchmark.c:14
void array_free_count(char **array, int count)
Definition: dynstuff.c:1033
void private_item_set(private_data_t **data, const char *item_name, const char *value)
Definition: dynstuff.c:1289
void * list_unlink3(list_t *list, list_t elem)
Definition: dynstuff.c:350
Definition: dynstuff.h:183
void private_items_destroy(private_data_t **data)
Definition: dynstuff.c:1237
void private_item_set_int(private_data_t **data, const char *item_name, int value)
Definition: dynstuff.c:1308
const char * ekg_itoa(long int i)
Definition: dynstuff.c:775
int list_remove_safe(list_t *list, void *data, int free_data)
Definition: dynstuff.c:208
struct private_data_s private_data_t
#define s
const char * cssfind(const char *haystack, const char *needle, const char sep, int caseinsensitive)
Definition: dynstuff.c:1058
int string_append(string_t s, const char *str)
Definition: dynstuff.c:642
int array_contains(char **array, const char *string, int casesensitive)
Definition: dynstuff.c:969
void * list_add_sorted(list_t *list, void *data, int(*comparision)(void *, void *))
Definition: dynstuff.c:43
void * list_add3(list_t *list, list_t new_)
Definition: dynstuff.c:188
int array_add(char ***array, char *string)
Definition: dynstuff.c:907