1 |
/* RCSid $Id$ */ |
2 |
#ifndef __G3NAMEDLIST_H |
3 |
#define __G3NAMEDLIST_H |
4 |
|
5 |
#ifdef __cplusplus |
6 |
extern "C" { |
7 |
#endif |
8 |
|
9 |
#include <string.h> |
10 |
#include "gbasic.h" |
11 |
#include "g3list.h" |
12 |
|
13 |
#define G3NL_NAME_LEN 200 |
14 |
#define G3NL_STARTCAP 2 |
15 |
|
16 |
|
17 |
|
18 |
typedef char g3nl_names[G3NL_NAME_LEN]; |
19 |
|
20 |
typedef struct g3nList { |
21 |
create_f ocreate; |
22 |
free_f ofree; |
23 |
copy_f ocopy; |
24 |
int cap; |
25 |
int size; |
26 |
unsigned char** items; |
27 |
g3nl_names* names; |
28 |
} g3nList; |
29 |
|
30 |
|
31 |
|
32 |
g3nList* g3nl_create(create_f cf,free_f ff,copy_f cpf); |
33 |
//void g3nl_free(g3nList* nl); |
34 |
|
35 |
//unsigned char* g3nl_append_new(g3nList* nl,const char* name); |
36 |
int g3nl_append(g3nList* nl,const char* name,unsigned char* el); |
37 |
unsigned char* g3nl_get(g3nList* nl,int id); |
38 |
char* g3nl_get_name(g3nList* nl,int id); |
39 |
unsigned char* g3nl_get_by_name(g3nList* nl,const char* name); |
40 |
//unsigned char* g3nl_get_copy(g3nList* nl,int id); |
41 |
//unsigned char* g3nl_get_copy_by_name(g3nList* nl,const char* name); |
42 |
|
43 |
int g3nl_remove(g3nList* nl,int id); |
44 |
int g3nl_remove_by_name(g3nList* nl,const char* name); |
45 |
unsigned char* g3nl_remove_last(g3nList* nl); |
46 |
int g3nl_get_size(g3nList* nl); |
47 |
|
48 |
|
49 |
|
50 |
#define G3N_MKHEADER(pref,type) \ |
51 |
g3nList* pref ## _nlist_create(); \ |
52 |
void pref ## _nlist_free(g3nList* nl); \ |
53 |
void pref ## _nlist_free_content(g3nList* nl); \ |
54 |
type* pref ## _nlist_append_new(g3nList* nl,const char* name); \ |
55 |
int pref ## _nlist_append(g3nList* nl,const char* name,type* it); \ |
56 |
type* pref ## _nlist_get(g3nList* nl,int id); \ |
57 |
char* pref ## _nlist_get_name(g3nList* nl,int id); \ |
58 |
type* pref ## _nlist_get_by_name(g3nList* nl,const char* name); \ |
59 |
type* pref ## _nlist_get_copy(g3nList* nl,int id); \ |
60 |
type* pref ## _nlist_get_copy_by_name(g3nList* nl,const char* name); \ |
61 |
int pref ## _nlist_remove(g3nList* nl,int id); \ |
62 |
int pref ## _nlist_remove_by_name(g3nList* nl,const char* name); \ |
63 |
type* pref ## _nlist_remove_last(g3nList* nl); \ |
64 |
int pref ## _nlist_get_size(g3nList* nl); \ |
65 |
void pref ## _nlist_clear(g3nList* nl); |
66 |
|
67 |
|
68 |
|
69 |
#define G3N_MKLIST(pref,type,crf,ff,cpf) \ |
70 |
g3nList* pref ## _nlist_create() { \ |
71 |
return g3nl_create((create_f) crf,(free_f) ff,(copy_f) cpf); \ |
72 |
}; \ |
73 |
void pref ## _nlist_free(g3nList* nl) { \ |
74 |
free(nl->items); \ |
75 |
free(nl); \ |
76 |
nl = NULL; \ |
77 |
} \ |
78 |
void pref ## _nlist_free_content(g3nList* nl) { \ |
79 |
int i; \ |
80 |
for(i=0;i<nl->cap;i++)\ |
81 |
nl->ofree((unsigned char*) nl->items[i]);\ |
82 |
free(nl->items); \ |
83 |
free(nl); \ |
84 |
nl = NULL; \ |
85 |
} \ |
86 |
type* pref ## _nlist_append_new(g3nList* nl,const char* name) { \ |
87 |
type* res = (type*) nl->ocreate(); \ |
88 |
if (!g3nl_append(nl,name,((unsigned char*) res))) \ |
89 |
return NULL; \ |
90 |
return res; \ |
91 |
} \ |
92 |
int pref ## _nlist_append(g3nList* nl,const char* name,type* it) { \ |
93 |
return g3nl_append(nl,name,((unsigned char*) it)); \ |
94 |
} \ |
95 |
type* pref ## _nlist_get(g3nList* nl,int id) { \ |
96 |
return ((type*) g3nl_get(nl,id)); \ |
97 |
} \ |
98 |
char* pref ## _nlist_get_name(g3nList* nl,int id) { \ |
99 |
return g3nl_get_name(nl,id); \ |
100 |
} \ |
101 |
type* pref ## _nlist_get_by_name(g3nList* nl,const char* name) { \ |
102 |
return ((type*) g3nl_get_by_name(nl,name)); \ |
103 |
} \ |
104 |
type* pref ## _nlist_get_copy(g3nList* nl,int id) { \ |
105 |
unsigned char* it; \ |
106 |
unsigned char* res; \ |
107 |
if (!(nl->ocopy)) \ |
108 |
return NULL; \ |
109 |
res = nl->ocreate(); \ |
110 |
if (!(it = (unsigned char*) pref ## _nlist_get(nl,id))) \ |
111 |
return NULL; \ |
112 |
if (!(nl->ocopy(res,it))) { \ |
113 |
fprintf(stderr,"nlist: copying failed for type %s\n","type"); \ |
114 |
return NULL; \ |
115 |
} \ |
116 |
return ((type*) res); \ |
117 |
} \ |
118 |
type* pref ## _nlist_get_copy_by_name(g3nList* nl,const char* name) { \ |
119 |
int i; \ |
120 |
unsigned char* it; \ |
121 |
unsigned char* res; \ |
122 |
if (!(nl->ocopy)) \ |
123 |
return NULL; \ |
124 |
res = nl->ocreate(); \ |
125 |
for(i=0;i<nl->size;i++)\ |
126 |
if (strncmp(nl->names[i],name,G3NL_NAME_LEN-1)) \ |
127 |
break; \ |
128 |
if (i == nl->size) \ |
129 |
return NULL; \ |
130 |
if (!(it = (unsigned char*) pref ## _nlist_get_copy(nl,i))) \ |
131 |
return NULL; \ |
132 |
if (!(nl->ocopy(res,it))) { \ |
133 |
fprintf(stderr,"nlist: copying failed for type %s\n","type"); \ |
134 |
return NULL; \ |
135 |
} \ |
136 |
return ((type*) res); \ |
137 |
} \ |
138 |
int pref ## _nlist_remove(g3nList* nl,int id) { \ |
139 |
return g3nl_remove(nl,id); \ |
140 |
}\ |
141 |
int pref ## _nlist_remove_by_name(g3nList* nl,const char* name) {\ |
142 |
return g3nl_remove_by_name(nl,name); \ |
143 |
} \ |
144 |
type* pref ## _nlist_remove_last(g3nList* nl) { \ |
145 |
return ((type*) g3nl_remove_last(nl)); \ |
146 |
} \ |
147 |
int pref ## _nlist_get_size(g3nList* nl) { \ |
148 |
return g3nl_get_size(nl); \ |
149 |
} \ |
150 |
void pref ## _nlist_clear(g3nList* nl) { \ |
151 |
nl->size = 0; \ |
152 |
} |
153 |
|
154 |
|
155 |
|
156 |
#ifdef __cplusplus |
157 |
} |
158 |
#endif |
159 |
#endif |