1 | // This file is a part of Framsticks GDK library. |
---|
2 | // Copyright (C) 2002-2006 Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.frams.alife.pl/ for further information. |
---|
4 | |
---|
5 | #include "paramobj.h" |
---|
6 | #include "extvalue.h" |
---|
7 | |
---|
8 | static const char* maybedup(bool dup,const char* src) |
---|
9 | { return dup ? (src?strdup(src):0) : src; } |
---|
10 | static void maybefree(void* mem) |
---|
11 | { if (mem) free(mem); } |
---|
12 | |
---|
13 | ParamEntry* ParamObject::makeParamTab(ParamInterface *pi,bool stripgroups,bool stripproc, |
---|
14 | int firstprop, int maxprops, bool dupentries, int flagsexclude) |
---|
15 | { |
---|
16 | ParamEntry *tab,*t; |
---|
17 | int i,n,offs; |
---|
18 | static ExtValue ex; |
---|
19 | int count=0,gcount=1; |
---|
20 | if (!stripgroups) gcount=pi->getGroupCount(); |
---|
21 | if (stripproc||flagsexclude) |
---|
22 | for (int i=firstprop;i < pi->getPropCount();i++) |
---|
23 | { |
---|
24 | const char*t=pi->type(i); |
---|
25 | if ((!stripproc)||(strchr("dfsox",*t))) |
---|
26 | if ((!flagsexclude)||(!(pi->flags(i)&flagsexclude))) |
---|
27 | if (++count >= maxprops) break; |
---|
28 | } |
---|
29 | else count=pi->getPropCount()-firstprop; |
---|
30 | t=tab=(ParamEntry*)malloc(sizeof(ParamEntry)*(count+gcount+1)); |
---|
31 | t->group=gcount; |
---|
32 | t->flags=count; |
---|
33 | t->name=maybedup(dupentries,pi->getName()); |
---|
34 | t->type=maybedup(dupentries,pi->getDescription()); |
---|
35 | for(i=0;i<gcount;i++) |
---|
36 | { |
---|
37 | t->id=maybedup(dupentries,pi->grname(i)); |
---|
38 | t->offset=0; |
---|
39 | t++; |
---|
40 | } |
---|
41 | n=1; offs=1; |
---|
42 | for (i=firstprop;i < pi->getPropCount();i++) |
---|
43 | { |
---|
44 | if ((!stripproc)||(strchr("dfsox",*pi->type(i)))) |
---|
45 | { |
---|
46 | if ((!flagsexclude)||(!(pi->flags(i)&flagsexclude))) |
---|
47 | { |
---|
48 | t->offset=offs*sizeof(ExtValue); |
---|
49 | if (*pi->type(i)!='x') t->offset+=(((char*)&ex.data[0])-((char*)&ex)); |
---|
50 | t->group=stripgroups?0:pi->group(i); |
---|
51 | t->flags=pi->flags(i); |
---|
52 | t->fun1=0; |
---|
53 | t->fun2=0; |
---|
54 | t->id=maybedup(dupentries,pi->id(i)); |
---|
55 | t->name=maybedup(dupentries,pi->name(i)); |
---|
56 | t->type=maybedup(dupentries,pi->type(i)); |
---|
57 | t->help=maybedup(dupentries,pi->help(i)); |
---|
58 | t++; |
---|
59 | n++; |
---|
60 | if (n>count) break; |
---|
61 | } |
---|
62 | offs++; |
---|
63 | } |
---|
64 | } |
---|
65 | t->id=0; t->group=0; t->flags=dupentries?MUTPARAM_ALLOCENTRY:0; |
---|
66 | return tab; |
---|
67 | } |
---|
68 | |
---|
69 | void ParamObject::freeParamTab(ParamEntry *pe) |
---|
70 | { |
---|
71 | if (pe[pe->flags+pe->group].flags & MUTPARAM_ALLOCENTRY) |
---|
72 | { |
---|
73 | int i; |
---|
74 | ParamEntry *e; |
---|
75 | maybefree((void*)pe->name); |
---|
76 | maybefree((void*)pe->type); |
---|
77 | for (i=0,e=pe;i<pe->group;i++,e++) |
---|
78 | maybefree((void*)e->id); |
---|
79 | for (i=pe->group,e=pe+i;i<pe->group+pe->flags;i++,e++) |
---|
80 | { |
---|
81 | maybefree((void*)e->id); |
---|
82 | maybefree((void*)e->name); |
---|
83 | maybefree((void*)e->type); |
---|
84 | maybefree((void*)e->help); |
---|
85 | } |
---|
86 | } |
---|
87 | free(pe); |
---|
88 | } |
---|
89 | |
---|
90 | void* ParamObject::makeObject(ParamEntry *tab) |
---|
91 | { |
---|
92 | if (!tab) return 0; |
---|
93 | int n=tab->flags; |
---|
94 | if (!n) return 0; |
---|
95 | ExtValue *ret,*v; |
---|
96 | ret=v=(ExtValue*)malloc( sizeof(ExtValue)*(n+1) ); |
---|
97 | for (int i=0;i<=n;i++,v++) |
---|
98 | new(v) ExtValue; |
---|
99 | ret->setInt(n); |
---|
100 | v=ret+1; |
---|
101 | tab+=tab->group; |
---|
102 | for (;n>0;n--,v++,tab++) |
---|
103 | switch(*tab->type) |
---|
104 | { |
---|
105 | case 'd': v->setInt(0); break; |
---|
106 | case 'f': v->setDouble(0); break; |
---|
107 | case 's': v->setString(SString::empty); break; |
---|
108 | case 'o': v->setObject(ExtObject()); break; |
---|
109 | case 'x': break; |
---|
110 | } |
---|
111 | return ret; |
---|
112 | } |
---|
113 | |
---|
114 | void ParamObject::copyObject(void* dst,void* src) |
---|
115 | { |
---|
116 | if ((!dst)||(!src)) return; |
---|
117 | ExtValue *s=(ExtValue *)src; |
---|
118 | ExtValue *d=(ExtValue *)dst; |
---|
119 | int n=min(s->getInt(),d->getInt()); |
---|
120 | s++; d++; |
---|
121 | for (int i=0;i<n;i++,d++,s++) |
---|
122 | *d=*s; |
---|
123 | } |
---|
124 | |
---|
125 | void* ParamObject::dupObject(void* src) |
---|
126 | { |
---|
127 | if (!src) return 0; |
---|
128 | ExtValue *s=(ExtValue *)src; |
---|
129 | int n=s->getInt(); |
---|
130 | |
---|
131 | ExtValue *ret=new ExtValue[n+1]; |
---|
132 | ExtValue *d=ret; |
---|
133 | d->setInt(n); |
---|
134 | d++; s++; |
---|
135 | |
---|
136 | for (int i=0;i<n;i++,d++,s++) |
---|
137 | *d=*s; |
---|
138 | |
---|
139 | return ret; |
---|
140 | } |
---|
141 | |
---|
142 | void ParamObject::freeObject(void* obj) |
---|
143 | { |
---|
144 | if (!obj) return; |
---|
145 | ExtValue *o=(ExtValue *)obj,*t=o+1; |
---|
146 | int n=o->getInt(); |
---|
147 | for (int i=0;i<n;i++,t++) |
---|
148 | t -> ~ExtValue(); |
---|
149 | free(obj); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|