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