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