[64] | 1 | // This file is a part of the Framsticks GDK library. |
---|
| 2 | // Copyright (C) 2002-2011 Szymon Ulatowski. See LICENSE.txt for details. |
---|
| 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
[5] | 4 | |
---|
| 5 | #include "paramobj.h" |
---|
| 6 | #include "extvalue.h" |
---|
[81] | 7 | #include "nonstd_stl.h" |
---|
[5] | 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=gcount; |
---|
| 33 | t->flags=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=stripgroups?0:pi->group(i); |
---|
| 52 | t->flags=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++; |
---|
| 60 | n++; |
---|
| 61 | if (n>count) break; |
---|
| 62 | } |
---|
| 63 | offs++; |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | t->id=0; t->group=0; t->flags=dupentries?MUTPARAM_ALLOCENTRY:0; |
---|
| 67 | return tab; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | void ParamObject::freeParamTab(ParamEntry *pe) |
---|
| 71 | { |
---|
| 72 | if (pe[pe->flags+pe->group].flags & MUTPARAM_ALLOCENTRY) |
---|
| 73 | { |
---|
| 74 | int i; |
---|
| 75 | ParamEntry *e; |
---|
| 76 | maybefree((void*)pe->name); |
---|
| 77 | maybefree((void*)pe->type); |
---|
| 78 | for (i=0,e=pe;i<pe->group;i++,e++) |
---|
| 79 | maybefree((void*)e->id); |
---|
| 80 | for (i=pe->group,e=pe+i;i<pe->group+pe->flags;i++,e++) |
---|
| 81 | { |
---|
| 82 | maybefree((void*)e->id); |
---|
| 83 | maybefree((void*)e->name); |
---|
| 84 | maybefree((void*)e->type); |
---|
| 85 | maybefree((void*)e->help); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | free(pe); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void* ParamObject::makeObject(ParamEntry *tab) |
---|
| 92 | { |
---|
| 93 | if (!tab) return 0; |
---|
| 94 | int n=tab->flags; |
---|
| 95 | if (!n) return 0; |
---|
[91] | 96 | ExtValue *ret=new ExtValue[n+1]; |
---|
[5] | 97 | ret->setInt(n); |
---|
[91] | 98 | ExtValue *v=ret+1; |
---|
[5] | 99 | tab+=tab->group; |
---|
| 100 | for (;n>0;n--,v++,tab++) |
---|
| 101 | switch(*tab->type) |
---|
| 102 | { |
---|
| 103 | case 'd': v->setInt(0); break; |
---|
| 104 | case 'f': v->setDouble(0); break; |
---|
[81] | 105 | case 's': v->setString(SString::empty()); break; |
---|
[5] | 106 | case 'o': v->setObject(ExtObject()); break; |
---|
| 107 | case 'x': break; |
---|
| 108 | } |
---|
| 109 | return ret; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void ParamObject::copyObject(void* dst,void* src) |
---|
| 113 | { |
---|
| 114 | if ((!dst)||(!src)) return; |
---|
| 115 | ExtValue *s=(ExtValue *)src; |
---|
| 116 | ExtValue *d=(ExtValue *)dst; |
---|
| 117 | int n=min(s->getInt(),d->getInt()); |
---|
| 118 | s++; d++; |
---|
| 119 | for (int i=0;i<n;i++,d++,s++) |
---|
| 120 | *d=*s; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | void* ParamObject::dupObject(void* src) |
---|
| 124 | { |
---|
[91] | 125 | if (!src) return NULL; |
---|
[5] | 126 | ExtValue *s=(ExtValue *)src; |
---|
| 127 | int n=s->getInt(); |
---|
| 128 | |
---|
| 129 | ExtValue *ret=new ExtValue[n+1]; |
---|
| 130 | ExtValue *d=ret; |
---|
| 131 | d->setInt(n); |
---|
| 132 | d++; s++; |
---|
| 133 | |
---|
| 134 | for (int i=0;i<n;i++,d++,s++) |
---|
| 135 | *d=*s; |
---|
| 136 | |
---|
| 137 | return ret; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void ParamObject::freeObject(void* obj) |
---|
| 141 | { |
---|
| 142 | if (!obj) return; |
---|
[91] | 143 | delete[] (ExtValue *)obj; |
---|
[5] | 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | |
---|