libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmData.c 51KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. #include "cmGlobal.h"
  2. #include "cmRpt.h"
  3. #include "cmErr.h"
  4. #include "cmCtx.h"
  5. #include "cmMem.h"
  6. #include "cmMallocDebug.h"
  7. #include "cmData.h"
  8. cmDtRC_t _cmDataErrNo = kOkDtRC;
  9. cmData_t cmDataNull = { kInvalidDtId,0,NULL,NULL,0 };
  10. typedef struct
  11. {
  12. cmDataFmtId_t tid;
  13. unsigned cnt;
  14. } cmDataSerialHdr_t;
  15. void _cmDataSetError( unsigned err )
  16. {
  17. _cmDataErrNo = err;
  18. }
  19. void _cmDataFreeArray( cmData_t* p )
  20. {
  21. if(cmIsFlag(p->flags,kDynPtrDtFl))
  22. {
  23. cmMemFree(p->u.vp);
  24. p->u.vp = NULL;
  25. p->flags = cmClrFlag(p->flags,kDynPtrDtFl);
  26. }
  27. p->tid = kInvalidDtId;
  28. p->cnt = 0;
  29. }
  30. void _cmDataFree( cmData_t* p )
  31. {
  32. if( cmDataIsStruct(p) )
  33. {
  34. cmData_t* cp = p->u.child;
  35. for(; cp!=NULL; cp=cp->sibling)
  36. _cmDataFree(cp);
  37. }
  38. _cmDataFreeArray(p);
  39. if( cmIsFlag(p->flags,kDynObjDtFl) )
  40. cmMemFree(p);
  41. }
  42. cmData_t* _cmDataAllocNode( cmData_t* parent, cmDataFmtId_t tid )
  43. {
  44. cmData_t* p = cmMemAllocZ(cmData_t,1);
  45. p->tid = tid;
  46. p->flags = kDynObjDtFl;
  47. p->parent = parent;
  48. if( parent != NULL )
  49. return cmDataAppendChild(parent,p);
  50. return p;
  51. }
  52. unsigned _cmDataByteCount( const cmData_t* p )
  53. {
  54. unsigned n = sizeof(cmDataSerialHdr_t);
  55. switch( p->tid )
  56. {
  57. case kInvalidDtId: return 0;
  58. case kNullDtId: return n;
  59. case kUCharDtId: return n + sizeof(unsigned char);
  60. case kCharDtId: return n + sizeof(char);
  61. case kUShortDtId: return n + sizeof(unsigned short);
  62. case kShortDtId: return n + sizeof(short);
  63. case kUIntDtId: return n + sizeof(unsigned int);
  64. case kIntDtId: return n + sizeof(int);
  65. case kULongDtId: return n + sizeof(unsigned long);
  66. case kLongDtId: return n + sizeof(long);
  67. case kFloatDtId: return n + sizeof(float);
  68. case kDoubleDtId: return n + sizeof(double);
  69. case kStrDtId: return n + (p->u.z ==NULL ? 0 : strlen(p->u.z) + 1);
  70. case kConstStrDtId: return n + (p->u.cz==NULL ? 0 : strlen(p->u.cz) + 1);
  71. case kUCharPtrDtId: return n + p->cnt * sizeof(unsigned char);
  72. case kCharPtrDtId: return n + p->cnt * sizeof(char);
  73. case kUShortPtrDtId: return n + p->cnt * sizeof(unsigned short);
  74. case kShortPtrDtId: return n + p->cnt * sizeof(short);
  75. case kUIntPtrDtId: return n + p->cnt * sizeof(unsigned int);
  76. case kIntPtrDtId: return n + p->cnt * sizeof(int);
  77. case kULongPtrDtId: return n + p->cnt * sizeof(unsigned long);
  78. case kLongPtrDtId: return n + p->cnt * sizeof(long);
  79. case kFloatPtrDtId: return n + p->cnt * sizeof(float);
  80. case kDoublePtrDtId: return n + p->cnt * sizeof(double);
  81. case kVoidPtrDtId: return n + p->cnt * sizeof(char);
  82. default:
  83. return n;
  84. }
  85. assert(0);
  86. return 0;
  87. }
  88. bool cmDataIsValue( const cmData_t* p )
  89. { return kMinValDtId <= p->tid && p->tid <= kMaxValDtId; }
  90. bool cmDataIsPtr( const cmData_t* p )
  91. { return kMinPtrDtId <= p->tid && p->tid <= kMaxPtrDtId; }
  92. bool cmDataIsStruct( const cmData_t* p )
  93. { return kMinStructDtId <= p->tid && p->tid <= kMaxStructDtId; }
  94. char cmDataChar( const cmData_t* p ) { assert(p->tid==kCharDtId); return p->u.c; }
  95. unsigned char cmDataUChar( const cmData_t* p ) { assert(p->tid==kUCharDtId); return p->u.uc; }
  96. short cmDataShort( const cmData_t* p ) { assert(p->tid==kShortDtId); return p->u.s; }
  97. unsigned short cmDataUShort( const cmData_t* p ) { assert(p->tid==kUShortDtId); return p->u.us; }
  98. int cmDataInt( const cmData_t* p ) { assert(p->tid==kIntDtId); return p->u.i; }
  99. unsigned int cmDataUInt( const cmData_t* p ) { assert(p->tid==kUIntDtId); return p->u.ui; }
  100. long cmDataLong( const cmData_t* p ) { assert(p->tid==kLongDtId); return p->u.l; }
  101. unsigned long cmDataULong( const cmData_t* p ) { assert(p->tid==kULongDtId); return p->u.ul; }
  102. float cmDataFloat( const cmData_t* p ) { assert(p->tid==kFloatDtId); return p->u.f; }
  103. double cmDataDouble( const cmData_t* p ) { assert(p->tid==kDoubleDtId); return p->u.d; }
  104. cmChar_t* cmDataStr( const cmData_t* p ) { assert(p->tid==kStrDtId); return p->u.z; }
  105. const cmChar_t* cmDataConstStr( const cmData_t* p ) { assert(p->tid==kConstStrDtId); return p->u.cz; }
  106. void* cmDataVoidPtr( const cmData_t* p ) { assert(p->tid==kVoidPtrDtId); return p->u.vp; }
  107. char* cmDataCharPtr( const cmData_t* p ) { assert(p->tid==kCharPtrDtId); return p->u.cp; }
  108. unsigned char* cmDataUCharPtr( const cmData_t* p ) { assert(p->tid==kUCharPtrDtId); return p->u.ucp; }
  109. short* cmDataShortPtr( const cmData_t* p ) { assert(p->tid==kShortPtrDtId); return p->u.sp; }
  110. unsigned short* cmDataUShortPtr( const cmData_t* p ) { assert(p->tid==kUShortPtrDtId); return p->u.usp; }
  111. int* cmDataIntPtr( const cmData_t* p ) { assert(p->tid==kIntPtrDtId); return p->u.ip; }
  112. unsigned int* cmDataUIntPtr( const cmData_t* p ) { assert(p->tid==kUIntPtrDtId); return p->u.uip; }
  113. long* cmDataLongPtr( const cmData_t* p ) { assert(p->tid==kLongPtrDtId); return p->u.lp; }
  114. unsigned long* cmDataULongPtr( const cmData_t* p ) { assert(p->tid==kULongPtrDtId); return p->u.ulp; }
  115. float* cmDataFloatPtr( const cmData_t* p ) { assert(p->tid==kFloatPtrDtId); return p->u.fp; }
  116. double* cmDataDoublePtr( const cmData_t* p ) { assert(p->tid==kDoublePtrDtId); return p->u.dp; }
  117. unsigned char cmDataGetUChar( const cmData_t* p )
  118. {
  119. unsigned char v = kInvalidDtUChar;
  120. switch( p->tid )
  121. {
  122. case kUCharDtId: v = p->u.uc; break;
  123. case kCharDtId: v = (unsigned char)p->u.c; break;
  124. case kUShortDtId: v = (unsigned char)p->u.us; break;
  125. case kShortDtId: v = (unsigned char)p->u.s; break;
  126. case kUIntDtId: v = (unsigned char)p->u.ui; break;
  127. case kIntDtId: v = (unsigned char)p->u.i; break;
  128. case kULongDtId: v = (unsigned char)p->u.ul; break;
  129. case kLongDtId: v = (unsigned char)p->u.l; break;
  130. case kFloatDtId: v = (unsigned char)p->u.f; break;
  131. case kDoubleDtId: v = (unsigned char)p->u.d; break;
  132. default:
  133. _cmDataSetError(kCvtErrDtRC);
  134. }
  135. return v;
  136. }
  137. char cmDataGetChar( const cmData_t* p )
  138. {
  139. char v = kInvalidDtChar;
  140. switch( p->tid )
  141. {
  142. case kUCharDtId: v = (char)p->u.uc; break;
  143. case kCharDtId: v = p->u.c; break;
  144. case kUShortDtId: v = (char)p->u.us; break;
  145. case kShortDtId: v = (char)p->u.s; break;
  146. case kUIntDtId: v = (char)p->u.ui; break;
  147. case kIntDtId: v = (char)p->u.i; break;
  148. case kULongDtId: v = (char)p->u.ul; break;
  149. case kLongDtId: v = (char)p->u.l; break;
  150. case kFloatDtId: v = (char)p->u.f; break;
  151. case kDoubleDtId: v = (char)p->u.d; break;
  152. default:
  153. _cmDataSetError(kCvtErrDtRC);
  154. }
  155. return v;
  156. }
  157. short cmDataGetShort( const cmData_t* p )
  158. {
  159. short v = kInvalidDtShort;
  160. switch( p->tid )
  161. {
  162. case kUCharDtId: v = (short)p->u.uc; break;
  163. case kCharDtId: v = (short)p->u.c; break;
  164. case kUShortDtId: v = (short)p->u.us; break;
  165. case kShortDtId: v = p->u.s; break;
  166. case kUIntDtId: v = (short)p->u.ui; break;
  167. case kIntDtId: v = (short)p->u.i; break;
  168. case kULongDtId: v = (short)p->u.ul; break;
  169. case kLongDtId: v = (short)p->u.l; break;
  170. case kFloatDtId: v = (short)p->u.f; break;
  171. case kDoubleDtId: v = (short)p->u.d; break;
  172. default:
  173. _cmDataSetError(kCvtErrDtRC);
  174. }
  175. return v;
  176. }
  177. unsigned short cmDataGetUShort( const cmData_t* p )
  178. {
  179. unsigned short v = kInvalidDtUShort;
  180. switch( p->tid )
  181. {
  182. case kUCharDtId: v = (unsigned short)p->u.uc; break;
  183. case kCharDtId: v = (unsigned short)p->u.c; break;
  184. case kUShortDtId: v = p->u.us; break;
  185. case kShortDtId: v = (unsigned short)p->u.s; break;
  186. case kUIntDtId: v = (unsigned short)p->u.ui; break;
  187. case kIntDtId: v = (unsigned short)p->u.i; break;
  188. case kULongDtId: v = (unsigned short)p->u.ul; break;
  189. case kLongDtId: v = (unsigned short)p->u.l; break;
  190. case kFloatDtId: v = (unsigned short)p->u.f; break;
  191. case kDoubleDtId: v = (unsigned short)p->u.d; break;
  192. default:
  193. _cmDataSetError(kCvtErrDtRC);
  194. }
  195. return v;
  196. }
  197. int cmDataGetInt( const cmData_t* p )
  198. {
  199. int v = kInvalidDtInt;
  200. switch( p->tid )
  201. {
  202. case kUCharDtId: v = (int)p->u.uc; break;
  203. case kCharDtId: v = (int)p->u.c; break;
  204. case kUShortDtId: v = (int)p->u.us; break;
  205. case kShortDtId: v = (int)p->u.s; break;
  206. case kUIntDtId: v = (int)p->u.ui; break;
  207. case kIntDtId: v = p->u.i; break;
  208. case kULongDtId: v = (int)p->u.ul; break;
  209. case kLongDtId: v = (int)p->u.l; break;
  210. case kFloatDtId: v = (int)p->u.f; break;
  211. case kDoubleDtId: v = (int)p->u.d; break;
  212. default:
  213. _cmDataSetError(kCvtErrDtRC);
  214. }
  215. return v;
  216. }
  217. unsigned int cmDataGetUInt( const cmData_t* p )
  218. {
  219. unsigned int v = kInvalidDtUInt;
  220. switch( p->tid )
  221. {
  222. case kUCharDtId: v = (unsigned int)p->u.uc; break;
  223. case kCharDtId: v = (unsigned int)p->u.c; break;
  224. case kUShortDtId: v = (unsigned int)p->u.us; break;
  225. case kShortDtId: v = (unsigned int)p->u.s; break;
  226. case kUIntDtId: v = p->u.ui; break;
  227. case kIntDtId: v = (unsigned int)p->u.i; break;
  228. case kULongDtId: v = (unsigned int)p->u.ul; break;
  229. case kLongDtId: v = (unsigned int)p->u.l; break;
  230. case kFloatDtId: v = (unsigned int)p->u.f; break;
  231. case kDoubleDtId: v = (unsigned int)p->u.d; break;
  232. default:
  233. _cmDataSetError(kCvtErrDtRC);
  234. }
  235. return v;
  236. }
  237. long cmDataGetLong( const cmData_t* p )
  238. {
  239. long v = kInvalidDtLong;
  240. switch( p->tid )
  241. {
  242. case kUCharDtId: v = (long)p->u.uc; break;
  243. case kCharDtId: v = (long)p->u.c; break;
  244. case kUShortDtId: v = (long)p->u.us; break;
  245. case kShortDtId: v = (long)p->u.s; break;
  246. case kUIntDtId: v = (long)p->u.ui; break;
  247. case kIntDtId: v = (long)p->u.i; break;
  248. case kULongDtId: v = (long)p->u.ul; break;
  249. case kLongDtId: v = p->u.l; break;
  250. case kFloatDtId: v = (long)p->u.f; break;
  251. case kDoubleDtId: v = (long)p->u.d; break;
  252. default:
  253. _cmDataSetError(kCvtErrDtRC);
  254. }
  255. return v;
  256. }
  257. unsigned long cmDataGetULong( const cmData_t* p )
  258. {
  259. unsigned long v = kInvalidDtULong;
  260. switch( p->tid )
  261. {
  262. case kUCharDtId: v = (unsigned long)p->u.uc; break;
  263. case kCharDtId: v = (unsigned long)p->u.c; break;
  264. case kUShortDtId: v = (unsigned long)p->u.us; break;
  265. case kShortDtId: v = (unsigned long)p->u.s; break;
  266. case kUIntDtId: v = (unsigned long)p->u.ui; break;
  267. case kIntDtId: v = (unsigned long)p->u.i; break;
  268. case kULongDtId: v = p->u.ul; break;
  269. case kLongDtId: v = (unsigned long)p->u.l; break;
  270. case kFloatDtId: v = (unsigned long)p->u.f; break;
  271. case kDoubleDtId: v = (unsigned long)p->u.d; break;
  272. default:
  273. _cmDataSetError(kCvtErrDtRC);
  274. }
  275. return v;
  276. }
  277. float cmDataGetFloat( const cmData_t* p )
  278. {
  279. float v = FLT_MAX;
  280. switch( p->tid )
  281. {
  282. case kUCharDtId: v = (float)p->u.uc; break;
  283. case kCharDtId: v = (float)p->u.c; break;
  284. case kUShortDtId: v = (float)p->u.us; break;
  285. case kShortDtId: v = (float)p->u.s; break;
  286. case kUIntDtId: v = (float)p->u.ui; break;
  287. case kIntDtId: v = (float)p->u.i; break;
  288. case kULongDtId: v = (float)p->u.ul; break;
  289. case kLongDtId: v = (float)p->u.l; break;
  290. case kFloatDtId: v = p->u.f; break;
  291. case kDoubleDtId: v = (float)p->u.d; break;
  292. default:
  293. _cmDataSetError(kCvtErrDtRC);
  294. }
  295. return v;
  296. }
  297. double cmDataGetDouble( const cmData_t* p )
  298. {
  299. double v = DBL_MAX;
  300. switch( p->tid )
  301. {
  302. case kUCharDtId: v = (double)p->u.uc; break;
  303. case kCharDtId: v = (double)p->u.c; break;
  304. case kUShortDtId: v = (double)p->u.us; break;
  305. case kShortDtId: v = (double)p->u.s; break;
  306. case kUIntDtId: v = (double)p->u.ui; break;
  307. case kIntDtId: v = (double)p->u.i; break;
  308. case kULongDtId: v = (double)p->u.ul; break;
  309. case kLongDtId: v = (double)p->u.l; break;
  310. case kFloatDtId: v = (double)p->u.f; break;
  311. case kDoubleDtId: v = p->u.d; break;
  312. default:
  313. _cmDataSetError(kCvtErrDtRC);
  314. }
  315. return v;
  316. }
  317. cmChar_t* cmDataGetStr( const cmData_t* p )
  318. {
  319. assert( p->tid == kStrDtId || p->tid == kConstStrDtId);
  320. return (p->tid == kStrDtId || p->tid == kConstStrDtId) ? p->u.z : NULL;
  321. }
  322. const cmChar_t* cmDataGetConstStr( const cmData_t* p )
  323. {
  324. assert( p->tid == kStrDtId || p->tid == kConstStrDtId);
  325. return (p->tid == kStrDtId || p->tid == kConstStrDtId) ? p->u.cz : NULL;
  326. }
  327. void* cmDataGetVoidPtr( const cmData_t* p )
  328. {
  329. assert( kMinPtrDtId <= p->tid && p->tid <= kMaxPtrDtId );
  330. return ( kMinPtrDtId <= p->tid && p->tid <= kMaxPtrDtId ) ? p->u.vp : NULL;
  331. }
  332. char* cmDataGetCharPtr( const cmData_t* p )
  333. {
  334. assert( p->tid == kCharPtrDtId || p->tid == kUCharPtrDtId );
  335. return (p->tid == kCharPtrDtId || p->tid == kUCharPtrDtId) ? p->u.cp : NULL;
  336. }
  337. unsigned char* cmDataGetUCharPtr( const cmData_t* p )
  338. {
  339. assert( p->tid == kCharPtrDtId || p->tid == kUCharPtrDtId );
  340. return (p->tid == kCharPtrDtId || p->tid == kUCharPtrDtId) ? p->u.ucp : NULL;
  341. }
  342. short* cmDataGetShortPtr( const cmData_t* p )
  343. {
  344. assert( p->tid == kShortPtrDtId || p->tid == kUShortPtrDtId );
  345. return (p->tid == kShortPtrDtId || p->tid == kUShortPtrDtId ) ? p->u.sp : NULL;
  346. }
  347. unsigned short* cmDataGetUShortPtr( const cmData_t* p )
  348. {
  349. assert( p->tid == kShortPtrDtId || p->tid == kUShortPtrDtId );
  350. return (p->tid == kShortPtrDtId || p->tid == kUShortPtrDtId ) ? p->u.usp : NULL;
  351. }
  352. int* cmDataGetIntPtr( const cmData_t* p )
  353. {
  354. assert( p->tid == kIntPtrDtId || p->tid == kUIntPtrDtId );
  355. return (p->tid == kIntPtrDtId || p->tid == kUIntPtrDtId ) ? p->u.ip : NULL;
  356. }
  357. unsigned int* cmDataGetUIntPtr( const cmData_t* p )
  358. {
  359. assert( p->tid == kIntPtrDtId || p->tid == kUIntPtrDtId );
  360. return (p->tid == kIntPtrDtId || p->tid == kUIntPtrDtId ) ? p->u.uip : NULL;
  361. }
  362. long* cmDataGetLongPtr( const cmData_t* p )
  363. {
  364. assert( p->tid == kLongPtrDtId || p->tid == kULongPtrDtId );
  365. return (p->tid == kLongPtrDtId || p->tid == kULongPtrDtId ) ? p->u.lp : NULL;
  366. }
  367. unsigned long* cmDataGetULongPtr( const cmData_t* p )
  368. {
  369. assert( p->tid == kLongPtrDtId || p->tid == kULongPtrDtId );
  370. return (p->tid == kLongPtrDtId || p->tid == kULongPtrDtId ) ? p->u.ulp : NULL;
  371. }
  372. float* cmDataGetFloatPtr( const cmData_t* p )
  373. { return p->tid == kFloatPtrDtId ? p->u.fp : NULL; }
  374. double* cmDataGetDoublePtr( const cmData_t* p )
  375. { return p->tid == kDoublePtrDtId ? p->u.dp : NULL; }
  376. // Set the value of an existing data object.
  377. cmData_t* cmDataSetNull( cmData_t* p )
  378. {
  379. _cmDataFreeArray(p);
  380. p->tid = kNullDtId;
  381. return p;
  382. }
  383. cmData_t* cmDataSetChar( cmData_t* p, char v )
  384. {
  385. _cmDataFreeArray(p);
  386. p->tid = kCharDtId;
  387. p->u.c = v;
  388. return p;
  389. }
  390. cmData_t* cmDataSetUChar( cmData_t* p, unsigned char v )
  391. {
  392. _cmDataFreeArray(p);
  393. p->tid = kUCharDtId;
  394. p->u.uc = v;
  395. return p;
  396. }
  397. cmData_t* cmDataSetShort( cmData_t* p, short v )
  398. {
  399. _cmDataFreeArray(p);
  400. p->tid = kShortDtId;
  401. p->u.s = v;
  402. return p;
  403. }
  404. cmData_t* cmDataSetUShort( cmData_t* p, unsigned short v )
  405. {
  406. _cmDataFreeArray(p);
  407. p->tid = kUShortDtId;
  408. p->u.us = v;
  409. return p;
  410. }
  411. cmData_t* cmDataSetInt( cmData_t* p, int v )
  412. {
  413. _cmDataFreeArray(p);
  414. p->tid = kCharDtId;
  415. p->u.c = v;
  416. return p;
  417. }
  418. cmData_t* cmDataSetUInt( cmData_t* p, unsigned int v )
  419. {
  420. _cmDataFreeArray(p);
  421. p->tid = kUIntDtId;
  422. p->u.ui = v;
  423. return p;
  424. }
  425. cmData_t* cmDataSetLong( cmData_t* p, long v )
  426. {
  427. _cmDataFreeArray(p);
  428. p->tid = kLongDtId;
  429. p->u.l = v;
  430. return p;
  431. }
  432. cmData_t* cmDataSetULong( cmData_t* p, unsigned long v )
  433. {
  434. _cmDataFreeArray(p);
  435. p->tid = kULongDtId;
  436. p->u.ul = v;
  437. return p;
  438. }
  439. cmData_t* cmDataSetFloat( cmData_t* p, float v )
  440. {
  441. _cmDataFreeArray(p);
  442. p->tid = kFloatDtId;
  443. p->u.f = v;
  444. return p;
  445. }
  446. cmData_t* cmDataSetDouble( cmData_t* p, double v )
  447. {
  448. _cmDataFreeArray(p);
  449. p->tid = kDoubleDtId;
  450. p->u.d = v;
  451. return p;
  452. }
  453. cmData_t* cmDataSetStr( cmData_t* p, cmChar_t* s )
  454. {
  455. _cmDataFreeArray(p);
  456. p->tid = kStrDtId;
  457. p->u.z = s;
  458. return p;
  459. }
  460. cmData_t* cmDataSetConstStr( cmData_t* p, const cmChar_t* s )
  461. {
  462. _cmDataFreeArray(p);
  463. p->tid = kConstStrDtId;
  464. p->u.cz = s;
  465. return p;
  466. }
  467. // Set the value of an existing data object to an external array.
  468. // The array is not copied.
  469. cmData_t* cmDataSetVoidPtr( cmData_t* p, void* vp, unsigned cnt )
  470. {
  471. cmDataSetCharPtr(p,(char*)vp,cnt);
  472. p->tid = kVoidPtrDtId;
  473. return p;
  474. }
  475. cmData_t* cmDataSetCharPtr( cmData_t* p, char* vp, unsigned cnt )
  476. {
  477. _cmDataFreeArray(p);
  478. p->tid = kCharPtrDtId;
  479. p->u.cp = vp;
  480. p->cnt = cnt;
  481. return p;
  482. }
  483. cmData_t* cmDataSetUCharPtr( cmData_t* p, unsigned char* vp, unsigned cnt )
  484. {
  485. _cmDataFreeArray(p);
  486. p->tid = kUCharPtrDtId;
  487. p->u.ucp = vp;
  488. p->cnt = cnt;
  489. return p;
  490. }
  491. cmData_t* cmDataSetShortPtr( cmData_t* p, short* vp, unsigned cnt )
  492. {
  493. _cmDataFreeArray(p);
  494. p->tid = kShortPtrDtId;
  495. p->u.sp = vp;
  496. p->cnt = cnt;
  497. return p;
  498. }
  499. cmData_t* cmDataSetUShortPtr( cmData_t* p, unsigned short* vp, unsigned cnt )
  500. {
  501. _cmDataFreeArray(p);
  502. p->tid = kUShortPtrDtId;
  503. p->u.usp = vp;
  504. p->cnt = cnt;
  505. return p;
  506. }
  507. cmData_t* cmDataSetIntPtr( cmData_t* p, int* vp, unsigned cnt )
  508. {
  509. _cmDataFreeArray(p);
  510. p->tid = kCharPtrDtId;
  511. p->u.ip = vp;
  512. p->cnt = cnt;
  513. return p;
  514. }
  515. cmData_t* cmDataSetUIntPtr( cmData_t* p, unsigned int* vp, unsigned cnt )
  516. {
  517. _cmDataFreeArray(p);
  518. p->tid = kUIntPtrDtId;
  519. p->u.uip = vp;
  520. p->cnt = cnt;
  521. return p;
  522. }
  523. cmData_t* cmDataSetLongPtr( cmData_t* p, long* vp, unsigned cnt )
  524. {
  525. _cmDataFreeArray(p);
  526. p->tid = kLongPtrDtId;
  527. p->u.lp = vp;
  528. p->cnt = cnt;
  529. return p;
  530. }
  531. cmData_t* cmDataSetULongPtr( cmData_t* p, unsigned long* vp, unsigned cnt )
  532. {
  533. _cmDataFreeArray(p);
  534. p->tid = kULongPtrDtId;
  535. p->u.ulp = vp;
  536. p->cnt = cnt;
  537. return p;
  538. }
  539. cmData_t* cmDataSetFloatPtr( cmData_t* p, float* vp, unsigned cnt )
  540. {
  541. _cmDataFreeArray(p);
  542. p->tid = kFloatPtrDtId;
  543. p->u.fp = vp;
  544. p->cnt = cnt;
  545. return p;
  546. }
  547. cmData_t* cmDataSetDoublePtr( cmData_t* p, double* vp, unsigned cnt )
  548. {
  549. _cmDataFreeArray(p);
  550. p->tid = kDoublePtrDtId;
  551. p->u.dp = vp;
  552. p->cnt = cnt;
  553. return p;
  554. }
  555. // Set the value of an existing array based data object.
  556. // Allocate the internal array and copy the array into it.
  557. cmData_t* cmDataSetStrAlloc( cmData_t* p, const cmChar_t* s )
  558. {
  559. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  560. cmMemResizeStr(p->u.z,s);
  561. else
  562. {
  563. _cmDataFreeArray(p);
  564. p->u.z = cmMemAllocStr(s);
  565. }
  566. p->tid = kStrDtId;
  567. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  568. return p;
  569. }
  570. cmData_t* cmDataSetConstStrAlloc( cmData_t* p, const cmChar_t* s )
  571. { return cmDataSetStrAlloc(p,s); }
  572. cmData_t* cmDataSetVoidAllocPtr( cmData_t* p, const void* vp, unsigned cnt )
  573. { return cmDataSetCharAllocPtr(p,(char*)vp,cnt); }
  574. cmData_t* cmDataSetCharAllocPtr( cmData_t* p, const char* vp, unsigned cnt )
  575. {
  576. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  577. p->u.cp = cmMemResize(char, p->u.cp, cnt );
  578. else
  579. {
  580. _cmDataFreeArray(p);
  581. p->u.cp = cmMemAlloc(char, cnt );
  582. }
  583. memcpy(p->u.cp,vp,sizeof(char)*cnt);
  584. p->tid = kCharPtrDtId;
  585. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  586. p->cnt = cnt;
  587. return p;
  588. }
  589. cmData_t* cmDataSetUCharAllocPtr( cmData_t* p, const unsigned char* vp, unsigned cnt )
  590. {
  591. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  592. p->u.ucp = cmMemResize(unsigned char, p->u.ucp, cnt );
  593. else
  594. {
  595. _cmDataFreeArray(p);
  596. p->u.ucp = cmMemAlloc(unsigned char, cnt );
  597. }
  598. memcpy(p->u.ucp,vp,sizeof(unsigned char)*cnt);
  599. p->tid = kUCharPtrDtId;
  600. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  601. p->cnt = cnt;
  602. return p;
  603. }
  604. cmData_t* cmDataSetShortAllocPtr( cmData_t* p, const short* vp, unsigned cnt )
  605. {
  606. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  607. p->u.sp = cmMemResize(short, p->u.sp, cnt );
  608. else
  609. {
  610. _cmDataFreeArray(p);
  611. p->u.sp = cmMemAlloc(short, cnt );
  612. }
  613. memcpy(p->u.sp,vp,sizeof(short)*cnt);
  614. p->tid = kShortPtrDtId;
  615. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  616. p->cnt = cnt;
  617. return p;
  618. }
  619. cmData_t* cmDataSetUShortAllocPtr( cmData_t* p, const unsigned short* vp, unsigned cnt )
  620. {
  621. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  622. p->u.usp = cmMemResize(unsigned short, p->u.usp, cnt );
  623. else
  624. {
  625. _cmDataFreeArray(p);
  626. p->u.usp = cmMemAlloc(unsigned short, cnt );
  627. }
  628. memcpy(p->u.usp,vp,sizeof(unsigned short)*cnt);
  629. p->tid = kUShortPtrDtId;
  630. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  631. p->cnt = cnt;
  632. return p;
  633. }
  634. cmData_t* cmDataSetIntAllocPtr( cmData_t* p, const int* vp, unsigned cnt )
  635. {
  636. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  637. p->u.ip = cmMemResize(int, p->u.ip, cnt );
  638. else
  639. {
  640. _cmDataFreeArray(p);
  641. p->u.ip = cmMemAlloc(int, cnt );
  642. }
  643. memcpy(p->u.ip,vp,sizeof(int)*cnt);
  644. p->tid = kIntPtrDtId;
  645. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  646. p->cnt = cnt;
  647. return p;
  648. }
  649. cmData_t* cmDataSetUIntAllocPtr( cmData_t* p, const unsigned int* vp, unsigned cnt )
  650. {
  651. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  652. p->u.uip = cmMemResize(unsigned int, p->u.uip, cnt );
  653. else
  654. {
  655. _cmDataFreeArray(p);
  656. p->u.uip = cmMemAlloc(unsigned int, cnt );
  657. }
  658. memcpy(p->u.uip,vp,sizeof(unsigned int)*cnt);
  659. p->tid = kUIntPtrDtId;
  660. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  661. p->cnt = cnt;
  662. return p;
  663. }
  664. cmData_t* cmDataSetLongAllocPtr( cmData_t* p, const long* vp, unsigned cnt )
  665. {
  666. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  667. p->u.lp = cmMemResize(long, p->u.lp, cnt );
  668. else
  669. {
  670. _cmDataFreeArray(p);
  671. p->u.lp = cmMemAlloc(long, cnt );
  672. }
  673. memcpy(p->u.lp,vp,sizeof(long)*cnt);
  674. p->tid = kLongPtrDtId;
  675. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  676. p->cnt = cnt;
  677. return p;
  678. }
  679. cmData_t* cmDataSetULongAllocPtr( cmData_t* p, const unsigned long* vp, unsigned cnt )
  680. {
  681. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  682. p->u.ulp = cmMemResize(unsigned long, p->u.ulp, cnt );
  683. else
  684. {
  685. _cmDataFreeArray(p);
  686. p->u.ulp = cmMemAlloc(unsigned long, cnt );
  687. }
  688. memcpy(p->u.ulp,vp,sizeof(unsigned long)*cnt);
  689. p->tid = kULongPtrDtId;
  690. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  691. p->cnt = cnt;
  692. return p;
  693. }
  694. cmData_t* cmDataSetFloatAllocPtr( cmData_t* p, const float* vp, unsigned cnt )
  695. {
  696. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  697. p->u.fp = cmMemResize(float, p->u.fp, cnt );
  698. else
  699. {
  700. _cmDataFreeArray(p);
  701. p->u.fp = cmMemAlloc(float, cnt );
  702. }
  703. memcpy(p->u.fp,vp,sizeof(float)*cnt);
  704. p->tid = kFloatPtrDtId;
  705. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  706. p->cnt = cnt;
  707. return p;
  708. }
  709. cmData_t* cmDataSetDoubleAllocPtr( cmData_t* p, const double* vp, unsigned cnt )
  710. {
  711. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  712. p->u.dp = cmMemResize(double, p->u.dp, cnt );
  713. else
  714. {
  715. _cmDataFreeArray(p);
  716. p->u.dp = cmMemAlloc(double, cnt );
  717. }
  718. memcpy(p->u.dp,vp,sizeof(double)*cnt);
  719. p->tid = kDoublePtrDtId;
  720. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  721. p->cnt = cnt;
  722. return p;
  723. }
  724. // Dynamically allocate a data object and set it's value.
  725. cmData_t* cmDataAllocNull( cmData_t* parent )
  726. { return _cmDataAllocNode(parent,kNullDtId); }
  727. cmData_t* cmDataAllocChar( cmData_t* parent, char v )
  728. {
  729. cmData_t* p = _cmDataAllocNode(parent,kCharDtId);
  730. cmDataSetChar(p,v);
  731. return p;
  732. }
  733. cmData_t* cmDataAllocUChar( cmData_t* parent, unsigned char v )
  734. {
  735. cmData_t* p = _cmDataAllocNode(parent,kUCharDtId);
  736. cmDataSetUChar(p,v);
  737. return p;
  738. }
  739. cmData_t* cmDataAllocShort( cmData_t* parent, short v )
  740. {
  741. cmData_t* p = _cmDataAllocNode(parent,kShortDtId);
  742. cmDataSetShort(p,v);
  743. return p;
  744. }
  745. cmData_t* cmDataAllocUShort( cmData_t* parent, unsigned short v )
  746. {
  747. cmData_t* p = _cmDataAllocNode(parent,kUShortDtId);
  748. cmDataSetUShort(p,v);
  749. return p;
  750. }
  751. cmData_t* cmDataAllocInt( cmData_t* parent, int v )
  752. {
  753. cmData_t* p = _cmDataAllocNode(parent,kIntDtId);
  754. cmDataSetInt(p,v);
  755. return p;
  756. }
  757. cmData_t* cmDataAllocUInt( cmData_t* parent, unsigned int v )
  758. {
  759. cmData_t* p = _cmDataAllocNode(parent,kUIntDtId);
  760. cmDataSetUInt(p,v);
  761. return p;
  762. }
  763. cmData_t* cmDataAllocLong( cmData_t* parent, long v )
  764. {
  765. cmData_t* p = _cmDataAllocNode(parent,kLongDtId);
  766. cmDataSetLong(p,v);
  767. return p;
  768. }
  769. cmData_t* cmDataAllocULong( cmData_t* parent, unsigned long v )
  770. {
  771. cmData_t* p = _cmDataAllocNode(parent,kULongDtId);
  772. cmDataSetULong(p,v);
  773. return p;
  774. }
  775. cmData_t* cmDataAllocFloat( cmData_t* parent, float v )
  776. {
  777. cmData_t* p = _cmDataAllocNode(parent,kFloatDtId);
  778. cmDataSetFloat(p,v);
  779. return p;
  780. }
  781. cmData_t* cmDataAllocDouble( cmData_t* parent, double v )
  782. {
  783. cmData_t* p = _cmDataAllocNode(parent,kDoubleDtId);
  784. cmDataSetDouble(p,v);
  785. return p;
  786. }
  787. // Dynamically allocate a data object and set its array value to an external
  788. // array. The data is not copied.
  789. cmData_t* cmDataAllocStr( cmData_t* parent, cmChar_t* str )
  790. {
  791. cmData_t* p = _cmDataAllocNode(parent,kStrDtId);
  792. cmDataSetStr(p,str);
  793. return p;
  794. }
  795. cmData_t* cmDataAllocConstStr( cmData_t* parent, const cmChar_t* str )
  796. {
  797. cmData_t* p = _cmDataAllocNode(parent,kConstStrDtId);
  798. cmDataSetConstStr(p,str);
  799. return p;
  800. }
  801. cmData_t* cmDataAllocCharPtr( cmData_t* parent, char* v, unsigned cnt )
  802. {
  803. cmData_t* p = _cmDataAllocNode(parent,kCharPtrDtId);
  804. cmDataSetCharPtr(p,(char*)v,cnt);
  805. return p;
  806. }
  807. cmData_t* cmDataAllocUCharPtr( cmData_t* parent, unsigned char* v, unsigned cnt )
  808. {
  809. cmData_t* p = _cmDataAllocNode(parent,kUCharPtrDtId);
  810. cmDataSetUCharPtr(p,(unsigned char*)v,cnt);
  811. return p;
  812. }
  813. cmData_t* cmDataAllocShortPtr( cmData_t* parent, short* v, unsigned cnt )
  814. {
  815. cmData_t* p = _cmDataAllocNode(parent,kShortPtrDtId);
  816. cmDataSetShortPtr(p,(short*)v,cnt);
  817. return p;
  818. }
  819. cmData_t* cmDataAllocUShortPtr( cmData_t* parent, unsigned short* v, unsigned cnt )
  820. {
  821. cmData_t* p = _cmDataAllocNode(parent,kUShortPtrDtId);
  822. cmDataSetUShortPtr(p,(unsigned short*)v,cnt);
  823. return p;
  824. }
  825. cmData_t* cmDataAllocIntPtr( cmData_t* parent, int* v, unsigned cnt )
  826. {
  827. cmData_t* p = _cmDataAllocNode(parent,kIntPtrDtId);
  828. cmDataSetIntPtr(p,(int*)v,cnt);
  829. return p;
  830. }
  831. cmData_t* cmDataAllocUIntPtr( cmData_t* parent, unsigned int* v, unsigned cnt )
  832. {
  833. cmData_t* p = _cmDataAllocNode(parent,kUIntPtrDtId);
  834. cmDataSetUIntPtr(p,(unsigned int*)v,cnt);
  835. return p;
  836. }
  837. cmData_t* cmDataAllocLongPtr( cmData_t* parent, long* v, unsigned cnt )
  838. {
  839. cmData_t* p = _cmDataAllocNode(parent,kLongPtrDtId);
  840. cmDataSetLongPtr(p,(long*)v,cnt);
  841. return p;
  842. }
  843. cmData_t* cmDataAllocULongPtr( cmData_t* parent, unsigned long* v, unsigned cnt )
  844. {
  845. cmData_t* p = _cmDataAllocNode(parent,kULongPtrDtId);
  846. cmDataSetULongPtr(p,(unsigned long*)v,cnt);
  847. return p;
  848. }
  849. cmData_t* cmDataAllocFloatPtr( cmData_t* parent, float* v, unsigned cnt )
  850. {
  851. cmData_t* p = _cmDataAllocNode(parent,kFloatPtrDtId);
  852. cmDataSetFloatPtr(p,(float*)v,cnt);
  853. return p;
  854. }
  855. cmData_t* cmDataAllocDoublePtr( cmData_t* parent, double* v, unsigned cnt )
  856. {
  857. cmData_t* p = _cmDataAllocNode(parent,kDoublePtrDtId);
  858. cmDataSetDoublePtr(p,(double*)v,cnt);
  859. return p;
  860. }
  861. cmData_t* cmDataAllocVoidPtr( cmData_t* parent, void* v, unsigned cnt )
  862. {
  863. cmData_t* p = _cmDataAllocNode(parent,kVoidPtrDtId);
  864. cmDataSetCharPtr(p,(char*)v,cnt);
  865. p->tid = kVoidPtrDtId;
  866. return p;
  867. }
  868. // Dynamically allocate a data object and its array value.
  869. // v[cnt] is copied into the allocated array.
  870. cmData_t* cmDataStrAlloc( cmData_t* parent, cmChar_t* str )
  871. {
  872. cmData_t* p = _cmDataAllocNode(parent,kStrDtId);
  873. cmDataSetStrAlloc(p,str);
  874. return p;
  875. }
  876. cmData_t* cmDataConstStrAlloc( cmData_t* parent, const cmChar_t* str )
  877. {
  878. cmData_t* p = _cmDataAllocNode(parent,kConstStrDtId);
  879. cmDataSetConstStrAlloc(p,str);
  880. return p;
  881. }
  882. cmData_t* cmDataCharAllocPtr( cmData_t* parent, const char* v, unsigned cnt )
  883. {
  884. cmData_t* p = _cmDataAllocNode(parent,kCharPtrDtId);
  885. cmDataSetCharAllocPtr(p, v, cnt );
  886. return p;
  887. }
  888. cmData_t* cmDataUCharAllocPtr( cmData_t* parent, const unsigned char* v, unsigned cnt )
  889. {
  890. cmData_t* p = _cmDataAllocNode(parent,kUCharPtrDtId);
  891. cmDataSetUCharAllocPtr(p, v, cnt );
  892. return p;
  893. }
  894. cmData_t* cmDataShortAllocPtr( cmData_t* parent, const short* v, unsigned cnt )
  895. {
  896. cmData_t* p = _cmDataAllocNode(parent,kShortPtrDtId);
  897. cmDataSetShortAllocPtr(p, v, cnt );
  898. return p;
  899. }
  900. cmData_t* cmDataUShortAllocPtr( cmData_t* parent, const unsigned short* v, unsigned cnt )
  901. {
  902. cmData_t* p = _cmDataAllocNode(parent,kUShortPtrDtId);
  903. cmDataSetUShortAllocPtr(p, v, cnt );
  904. return p;
  905. }
  906. cmData_t* cmDataIntAllocPtr( cmData_t* parent, const int* v, unsigned cnt )
  907. {
  908. cmData_t* p = _cmDataAllocNode(parent,kIntPtrDtId);
  909. cmDataSetIntAllocPtr(p, v, cnt );
  910. return p;
  911. }
  912. cmData_t* cmDataUIntAllocPtr( cmData_t* parent, const unsigned int* v, unsigned cnt )
  913. {
  914. cmData_t* p = _cmDataAllocNode(parent,kUIntPtrDtId);
  915. cmDataSetUIntAllocPtr(p, v, cnt );
  916. return p;
  917. }
  918. cmData_t* cmDataLongAllocPtr( cmData_t* parent, const long* v, unsigned cnt )
  919. {
  920. cmData_t* p = _cmDataAllocNode(parent,kLongPtrDtId);
  921. cmDataSetLongAllocPtr(p, v, cnt );
  922. return p;
  923. }
  924. cmData_t* cmDataULongAllocPtr( cmData_t* parent, const unsigned long* v, unsigned cnt )
  925. {
  926. cmData_t* p = _cmDataAllocNode(parent,kULongPtrDtId);
  927. cmDataSetULongAllocPtr(p, v, cnt );
  928. return p;
  929. }
  930. cmData_t* cmDataFloatAllocPtr( cmData_t* parent, const float* v, unsigned cnt )
  931. {
  932. cmData_t* p = _cmDataAllocNode(parent,kFloatPtrDtId);
  933. cmDataSetFloatAllocPtr(p, v, cnt );
  934. return p;
  935. }
  936. cmData_t* cmDataDoubleAllocPtr( cmData_t* parent, const double* v, unsigned cnt )
  937. {
  938. cmData_t* p = _cmDataAllocNode(parent,kDoublePtrDtId);
  939. cmDataSetDoubleAllocPtr(p, v, cnt );
  940. return p;
  941. }
  942. cmData_t* cmDataVoidAllocPtr( cmData_t* parent, const void* v, unsigned cnt )
  943. {
  944. cmData_t* p = _cmDataAllocNode(parent,kVoidPtrDtId);
  945. cmDataSetCharAllocPtr(p, (const char*)v, cnt );
  946. p->tid = kVoidPtrDtId;
  947. return p;
  948. }
  949. void cmDataFree( cmData_t* p )
  950. {
  951. _cmDataFree(p);
  952. }
  953. cmData_t* cmDataUnlink( cmData_t* p )
  954. {
  955. if( p->parent == NULL )
  956. return p;
  957. assert( cmDataIsStruct(p->parent) );
  958. cmData_t* cp = p->u.child;
  959. cmData_t* pp = NULL;
  960. for(; cp!=NULL; cp=cp->sibling)
  961. if( cp == p )
  962. {
  963. if( pp == NULL )
  964. p->parent->u.child = p->sibling;
  965. else
  966. pp->sibling = cp->sibling;
  967. }
  968. return p;
  969. }
  970. void cmDataUnlinkAndFree( cmData_t* p )
  971. {
  972. cmDataUnlink(p);
  973. cmDataFree(p);
  974. }
  975. cmData_t* _cmDataDupl( const cmData_t* p, cmData_t* parent )
  976. {
  977. cmData_t* rp = NULL;
  978. switch( p->tid )
  979. {
  980. case kNullDtId: rp = cmDataAllocNull(parent); break;
  981. case kUCharDtId: rp = cmDataAllocUChar(parent,p->u.uc); break;
  982. case kCharDtId: rp = cmDataAllocChar(parent,p->u.c); break;
  983. case kUShortDtId: rp = cmDataAllocShort(parent,p->u.us); break;
  984. case kShortDtId: rp = cmDataAllocUShort(parent,p->u.s); break;
  985. case kUIntDtId: rp = cmDataAllocUInt(parent,p->u.i); break;
  986. case kIntDtId: rp = cmDataAllocInt(parent,p->u.ui); break;
  987. case kULongDtId: rp = cmDataAllocULong(parent,p->u.ul); break;
  988. case kLongDtId: rp = cmDataAllocLong(parent,p->u.l); break;
  989. case kFloatDtId: rp = cmDataAllocFloat(parent,p->u.f); break;
  990. case kDoubleDtId: rp = cmDataAllocDouble(parent,p->u.d); break;
  991. case kStrDtId: rp = cmDataStrAlloc(parent,p->u.z); break;
  992. case kConstStrDtId: rp = cmDataConstStrAlloc(parent,p->u.cz); break;
  993. case kUCharPtrDtId: rp = cmDataUCharAllocPtr(parent,p->u.ucp,p->cnt); break;
  994. case kCharPtrDtId: rp = cmDataCharAllocPtr(parent,p->u.cp,p->cnt); break;
  995. case kUShortPtrDtId: rp = cmDataUShortAllocPtr(parent,p->u.usp,p->cnt); break;
  996. case kShortPtrDtId: rp = cmDataShortAllocPtr(parent,p->u.sp,p->cnt); break;
  997. case kUIntPtrDtId: rp = cmDataUIntAllocPtr(parent,p->u.uip,p->cnt); break;
  998. case kIntPtrDtId: rp = cmDataIntAllocPtr(parent,p->u.ip,p->cnt); break;
  999. case kULongPtrDtId: rp = cmDataULongAllocPtr(parent,p->u.ulp,p->cnt); break;
  1000. case kLongPtrDtId: rp = cmDataLongAllocPtr(parent,p->u.lp,p->cnt); break;
  1001. case kFloatPtrDtId: rp = cmDataFloatAllocPtr(parent,p->u.fp,p->cnt); break;
  1002. case kDoublePtrDtId: rp = cmDataDoubleAllocPtr(parent,p->u.dp,p->cnt); break;
  1003. case kVoidPtrDtId: rp = cmDataVoidAllocPtr(parent,p->u.vp,p->cnt); break;
  1004. case kListDtId:
  1005. case kPairDtId:
  1006. case kRecordDtId:
  1007. {
  1008. rp = _cmDataAllocNode(parent,p->tid);
  1009. cmData_t* cp = p->u.child;
  1010. for(; cp!=NULL; cp=cp->sibling)
  1011. cmDataAppendChild(rp,_cmDataDupl(cp,rp));
  1012. }
  1013. break;
  1014. default:
  1015. assert(0);
  1016. }
  1017. return rp;
  1018. }
  1019. cmData_t* cmDataDupl( const cmData_t* p )
  1020. { return _cmDataDupl(p,NULL); }
  1021. cmData_t* cmDataReplace( cmData_t* dst, cmData_t* src )
  1022. {
  1023. if( dst->parent == NULL )
  1024. {
  1025. cmDataUnlinkAndFree(dst);
  1026. src->parent = NULL;
  1027. return src;
  1028. }
  1029. cmData_t* parent = dst->parent;
  1030. cmData_t* cp = parent->u.child;
  1031. cmData_t* pp = NULL;
  1032. unsigned i = 0;
  1033. unsigned n = cmDataChildCount(parent);
  1034. // locate dst's right sibling
  1035. for(i=0; i<n; ++i,cp=cp->sibling)
  1036. {
  1037. if( cp == dst )
  1038. {
  1039. // link in 'src' in place of 'dst'
  1040. src->sibling = dst->sibling;
  1041. // free dst
  1042. cmDataUnlinkAndFree(dst);
  1043. // update the sibling link to
  1044. if( pp == NULL )
  1045. parent->u.child = src;
  1046. else
  1047. pp->sibling = src;
  1048. src->parent = parent;
  1049. break;
  1050. }
  1051. pp = cp;
  1052. }
  1053. return src;
  1054. }
  1055. unsigned cmDataChildCount( const cmData_t* p )
  1056. {
  1057. if( !cmDataIsStruct(p) )
  1058. return 0;
  1059. unsigned n = 0;
  1060. const cmData_t* cp = p->u.child;
  1061. for(; cp!=NULL; cp=cp->sibling)
  1062. ++n;
  1063. return n;
  1064. }
  1065. cmData_t* cmDataChild( cmData_t* p, unsigned index )
  1066. {
  1067. if( !cmDataIsStruct(p) )
  1068. return NULL;
  1069. unsigned n = 0;
  1070. cmData_t* cp = p->u.child;
  1071. for(; cp!=NULL; cp=cp->sibling)
  1072. {
  1073. if( n == index )
  1074. break;
  1075. ++n;
  1076. }
  1077. return cp;
  1078. }
  1079. cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* p )
  1080. {
  1081. assert( cmDataIsStruct(p) );
  1082. p->u.child = parent->u.child;
  1083. parent->u.child = p;
  1084. return p;
  1085. }
  1086. cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p )
  1087. {
  1088. assert( cmDataIsStruct(parent) );
  1089. assert( parent->tid != kRecordDtId || (parent->tid == kRecordDtId && p->tid==kPairDtId));
  1090. cmData_t* cp = parent->u.child;
  1091. if( cp == NULL )
  1092. parent->u.child = p;
  1093. else
  1094. {
  1095. for(; cp!=NULL; cp=cp->sibling)
  1096. if( cp->sibling == NULL )
  1097. {
  1098. cp->sibling = p;
  1099. break;
  1100. }
  1101. }
  1102. p->sibling = NULL;
  1103. return p;
  1104. }
  1105. cmData_t* cmDataInsertChild( cmData_t* parent, unsigned index, cmData_t* p )
  1106. {
  1107. if( !cmDataIsStruct(parent) )
  1108. return NULL;
  1109. unsigned n = 0;
  1110. cmData_t* cp = parent->u.child;
  1111. cmData_t* pp = NULL;
  1112. for(; cp!=NULL; cp=cp->sibling)
  1113. {
  1114. if( n == index )
  1115. {
  1116. if( pp == NULL )
  1117. {
  1118. parent->u.child = p;
  1119. p->sibling = NULL;
  1120. }
  1121. else
  1122. {
  1123. p->sibling = pp->sibling;
  1124. pp->sibling = p;
  1125. }
  1126. break;
  1127. }
  1128. ++n;
  1129. }
  1130. return p;
  1131. }
  1132. //----------------------------------------------------------------------------
  1133. bool _cmDataPairIsValid( const cmData_t* p )
  1134. {
  1135. assert( p->tid == kPairDtId );
  1136. const cmData_t* cp = p->u.child;
  1137. bool fl = cp->u.child == NULL || cp->u.child->sibling == NULL || cp->u.child->sibling->sibling!=NULL;
  1138. return !fl;
  1139. }
  1140. // Get the key/value of a pair
  1141. cmData_t* cmDataPairKey( cmData_t* p )
  1142. {
  1143. assert( _cmDataPairIsValid(p) );
  1144. return p->u.child;
  1145. }
  1146. unsigned cmDataPairKeyId( cmData_t* p )
  1147. {
  1148. assert( _cmDataPairIsValid(p) );
  1149. return cmDataGetUInt(p->u.child);
  1150. }
  1151. const cmChar_t* cmDataPairKeyLabel( cmData_t* p )
  1152. {
  1153. assert( _cmDataPairIsValid(p) );
  1154. return cmDataGetConstStr(p->u.child);
  1155. }
  1156. cmData_t* cmDataPairValue( cmData_t* p )
  1157. {
  1158. assert( _cmDataPairIsValid(p) );
  1159. return p->u.child->sibling;
  1160. }
  1161. // Set the key or value of an existing pair node.
  1162. cmData_t* cmDataPairSetValue( cmData_t* p, cmData_t* value )
  1163. {
  1164. assert( _cmDataPairIsValid(p) );
  1165. cmDataReplace( cmDataPairValue(p), value );
  1166. return p;
  1167. }
  1168. cmData_t* cmDataPairSetKey( cmData_t* p, cmData_t* key )
  1169. {
  1170. assert( _cmDataPairIsValid(p) );
  1171. cmDataReplace( cmDataPairValue(p), key );
  1172. return p;
  1173. }
  1174. cmData_t* cmDataPairSetKeyId( cmData_t* p, unsigned id )
  1175. {
  1176. assert( _cmDataPairIsValid(p) );
  1177. cmDataSetUInt(p->u.child,id);
  1178. return p;
  1179. }
  1180. cmData_t* cmDataPairSetKeyLabel( cmData_t* p, const cmChar_t* label )
  1181. {
  1182. assert( _cmDataPairIsValid(p) );
  1183. cmDataSetConstStrAlloc(p->u.child,label);
  1184. return p;
  1185. }
  1186. cmData_t* cmDataMakePair( cmData_t* parent, cmData_t* p, cmData_t* key, cmData_t* value )
  1187. {
  1188. _cmDataFree(p);
  1189. p->tid = kPairDtId;
  1190. p->parent = parent;
  1191. p->flags = 0;
  1192. p->u.child = NULL;
  1193. return p;
  1194. }
  1195. // Dynamically allocate a pair node
  1196. cmData_t* cmDataAllocPair( cmData_t* parent, const cmData_t* key, const cmData_t* value )
  1197. {
  1198. cmData_t* p = _cmDataAllocNode(parent,kPairDtId);
  1199. cmData_t* kp = cmDataDupl(key);
  1200. cmData_t* vp = cmDataDupl(value);
  1201. cmDataPrependChild(p,vp);
  1202. cmDataPrependChild(p,kp);
  1203. return p;
  1204. }
  1205. cmData_t* cmDataAllocPairId(cmData_t* parent, unsigned keyId, cmData_t* value )
  1206. {
  1207. cmData_t* p = _cmDataAllocNode(parent,kPairDtId);
  1208. cmDataAllocUInt(p,keyId);
  1209. cmDataAppendChild(p,value);
  1210. return p;
  1211. }
  1212. cmData_t* cmDataAllocPairLabel( cmData_t* parent, const cmChar_t *label, cmData_t* value )
  1213. {
  1214. cmData_t* p = _cmDataAllocNode(parent,kPairDtId);
  1215. cmDataConstStrAlloc(p,label);
  1216. cmDataAppendChild(p,value);
  1217. return p;
  1218. }
  1219. //----------------------------------------------------------------------------
  1220. unsigned cmDataListCount(const cmData_t* p )
  1221. { return cmDataChildCount(p); }
  1222. cmData_t* cmDataListEle( cmData_t* p, unsigned index )
  1223. { return cmDataChild(p,index); }
  1224. cmData_t* cmDataListMake( cmData_t* parent, cmData_t* p )
  1225. {
  1226. _cmDataFree(p);
  1227. p->parent = parent;
  1228. p->tid = kListDtId;
  1229. p->flags = 0;
  1230. p->u.child = NULL;
  1231. return p;
  1232. }
  1233. cmData_t* cmDataListAlloc( cmData_t* parent)
  1234. { return _cmDataAllocNode(parent,kListDtId); }
  1235. cmDtRC_t _cmDataParseArgV( cmData_t* parent, va_list vl, cmData_t** vpp )
  1236. {
  1237. cmDtRC_t rc = kOkDtRC;
  1238. cmData_t* vp = NULL;
  1239. unsigned tid = va_arg(vl,unsigned);
  1240. switch(tid)
  1241. {
  1242. case kInvalidDtId: rc = kEolDtRC; break;
  1243. case kNullDtId: vp = cmDataAllocNull(parent); break;
  1244. case kUCharDtId: vp = cmDataAllocUChar( parent,va_arg(vl,int)); break;
  1245. case kCharDtId: vp = cmDataAllocChar( parent,va_arg(vl,int)); break;
  1246. case kUShortDtId: vp = cmDataAllocUShort( parent,va_arg(vl,int)); break;
  1247. case kShortDtId: vp = cmDataAllocShort( parent,va_arg(vl,int)); break;
  1248. case kUIntDtId: vp = cmDataAllocUInt( parent,va_arg(vl,unsigned int)); break;
  1249. case kIntDtId: vp = cmDataAllocInt( parent,va_arg(vl,int)); break;
  1250. case kULongDtId: vp = cmDataAllocULong( parent,va_arg(vl,unsigned long)); break;
  1251. case kLongDtId: vp = cmDataAllocLong( parent,va_arg(vl,long)); break;
  1252. case kFloatDtId: vp = cmDataAllocFloat( parent,va_arg(vl,double)); break;
  1253. case kDoubleDtId: vp = cmDataAllocDouble( parent,va_arg(vl,double)); break;
  1254. case kStrDtId: vp = cmDataStrAlloc( parent,va_arg(vl,cmChar_t*)); break;
  1255. case kConstStrDtId: vp = cmDataConstStrAlloc( parent,va_arg(vl,const cmChar_t*)); break;
  1256. case kUCharPtrDtId:
  1257. {
  1258. unsigned char* p = va_arg(vl,unsigned char*);
  1259. vp = cmDataUCharAllocPtr(parent, p, va_arg(vl,unsigned));
  1260. }
  1261. break;
  1262. case kCharPtrDtId:
  1263. {
  1264. char* p = va_arg(vl,char*);
  1265. vp = cmDataCharAllocPtr(parent, p, va_arg(vl,unsigned));
  1266. }
  1267. break;
  1268. case kUShortPtrDtId:
  1269. {
  1270. unsigned short* p = va_arg(vl,unsigned short*);
  1271. vp = cmDataUShortAllocPtr(parent, p, va_arg(vl,unsigned));
  1272. }
  1273. break;
  1274. case kShortPtrDtId:
  1275. {
  1276. short* p = va_arg(vl,short*);
  1277. vp = cmDataShortAllocPtr(parent, p, va_arg(vl,unsigned));
  1278. }
  1279. break;
  1280. case kUIntPtrDtId:
  1281. {
  1282. unsigned int* p = va_arg(vl,unsigned int*);
  1283. vp = cmDataUIntAllocPtr(parent, p, va_arg(vl,unsigned));
  1284. }
  1285. break;
  1286. case kIntPtrDtId:
  1287. {
  1288. int * p = va_arg(vl,int*);
  1289. vp = cmDataIntAllocPtr(parent, p, va_arg(vl,unsigned));
  1290. }
  1291. break;
  1292. case kULongPtrDtId:
  1293. {
  1294. unsigned long* p = va_arg(vl,unsigned long*);
  1295. vp = cmDataULongAllocPtr(parent, p, va_arg(vl,unsigned));
  1296. }
  1297. break;
  1298. case kLongPtrDtId:
  1299. {
  1300. long* p = va_arg(vl,long*);
  1301. vp = cmDataLongAllocPtr(parent, p, va_arg(vl,unsigned));
  1302. }
  1303. break;
  1304. case kFloatPtrDtId:
  1305. {
  1306. float* p = va_arg(vl,float*);
  1307. vp = cmDataFloatAllocPtr(parent, p, va_arg(vl,unsigned));
  1308. }
  1309. break;
  1310. case kDoublePtrDtId:
  1311. {
  1312. double* p = va_arg(vl,double*);
  1313. vp = cmDataDoubleAllocPtr(parent,p, va_arg(vl,unsigned));
  1314. }
  1315. break;
  1316. case kVoidPtrDtId:
  1317. {
  1318. void* p = va_arg(vl,void*);
  1319. vp = cmDataVoidAllocPtr(parent, p, va_arg(vl,unsigned));
  1320. }
  1321. break;
  1322. case kListDtId:
  1323. case kPairDtId:
  1324. case kRecordDtId:
  1325. vp = _cmDataAllocNode(parent,tid);
  1326. break;
  1327. default:
  1328. _cmDataSetError(kVarArgErrDtRC);
  1329. break;
  1330. }
  1331. *vpp = vp;
  1332. return rc;
  1333. }
  1334. cmData_t* _cmDataListParseV(cmData_t* parent, va_list vl )
  1335. {
  1336. cmData_t* p = NULL;
  1337. bool contFl = true;
  1338. while( contFl )
  1339. {
  1340. cmData_t* vp;
  1341. cmDtRC_t rc = _cmDataParseArgV(parent, vl, &vp);
  1342. if(rc != kOkDtRC || cmDataAppendChild(parent,vp)==NULL )
  1343. contFl = false;
  1344. }
  1345. return p;
  1346. }
  1347. cmData_t* cmDataListAllocV(cmData_t* parent, va_list vl )
  1348. {
  1349. cmData_t* p = cmDataListAlloc(parent);
  1350. _cmDataListParseV(p, vl );
  1351. return p;
  1352. }
  1353. cmData_t* cmDataListAllocA(cmData_t* parent, ... )
  1354. {
  1355. va_list vl;
  1356. va_start(vl,parent);
  1357. cmData_t* p = cmDataListAllocV(parent,vl);
  1358. va_end(vl);
  1359. return p;
  1360. }
  1361. cmData_t* cmDataListAppendEle( cmData_t* p, cmData_t* ele )
  1362. {
  1363. assert(p->tid == kListDtId);
  1364. return cmDataAppendChild(p,ele);
  1365. }
  1366. cmData_t* cmDataListAppendEleN(cmData_t* p, cmData_t* ele[], unsigned n )
  1367. {
  1368. assert(p->tid == kListDtId);
  1369. cmData_t* rp = NULL;
  1370. unsigned i;
  1371. for(i=0; i<n; ++i)
  1372. {
  1373. cmData_t* ep = cmDataAppendChild(p,ele[i]);
  1374. if( rp == NULL )
  1375. rp = ep;
  1376. }
  1377. return rp;
  1378. }
  1379. cmDtRC_t cmDataListAppendV( cmData_t* p, va_list vl )
  1380. {
  1381. if( _cmDataListParseV(p, vl ) == NULL )
  1382. return _cmDataErrNo;
  1383. return kOkDtRC;
  1384. }
  1385. cmDtRC_t cmDataListAppend( cmData_t* p, ... )
  1386. {
  1387. va_list vl;
  1388. va_start(vl,p);
  1389. cmDtRC_t rc = cmDataListAppendV(p,vl);
  1390. va_end(vl);
  1391. return rc;
  1392. }
  1393. cmData_t* cmDataListInsertEle( cmData_t* p, unsigned index, cmData_t* ele )
  1394. { return cmDataInsertChild(p,index,ele); }
  1395. cmData_t* cmDataListInsertEleN(cmData_t* p, unsigned index, cmData_t* ele[], unsigned n )
  1396. {
  1397. unsigned i;
  1398. for(i=0; i<n; ++i)
  1399. cmDataListInsertEle(p,index+i,ele[i]);
  1400. return p;
  1401. }
  1402. //----------------------------------------------------------------------------
  1403. unsigned cmDataRecdCount( const cmData_t* p )
  1404. {
  1405. assert( p->tid == kRecordDtId );
  1406. return cmDataChildCount(p);
  1407. }
  1408. cmData_t* cmDataRecdEle( cmData_t* p, unsigned index )
  1409. {
  1410. assert( p->tid == kRecordDtId );
  1411. cmData_t* cp = cmDataChild(p,index);
  1412. assert( p->tid == kPairDtId );
  1413. return cp;
  1414. }
  1415. cmData_t* cmDataRecdValueFromIndex( cmData_t* p, unsigned index )
  1416. {
  1417. assert( p->tid == kRecordDtId );
  1418. cmData_t* cp = cmDataChild(p,index);
  1419. assert( p->tid == kPairDtId );
  1420. return cmDataPairValue(cp);
  1421. }
  1422. cmData_t* cmDataRecdValueFromId( cmData_t* p, unsigned id )
  1423. {
  1424. assert( p->tid == kRecordDtId );
  1425. cmData_t* cp = p->u.child;
  1426. for(; cp!=NULL; cp=cp->sibling)
  1427. if( cmDataPairKeyId(cp) == id )
  1428. break;
  1429. assert( cp!=NULL && cp->tid==kPairDtId );
  1430. return cmDataPairValue(cp);
  1431. }
  1432. cmData_t* cmDataRecdValueFromLabel( cmData_t* p, const cmChar_t* label )
  1433. {
  1434. assert( p->tid == kRecordDtId );
  1435. cmData_t* cp = p->u.child;
  1436. for(; cp!=NULL; cp=cp->sibling)
  1437. {
  1438. const cmChar_t* lp = cmDataPairKeyLabel(cp);
  1439. if( lp!=NULL && strcmp(lp,label)==0 )
  1440. break;
  1441. }
  1442. assert( cp!=NULL && cp->tid==kPairDtId );
  1443. return cmDataPairValue(cp);
  1444. }
  1445. cmData_t* cmDataRecdKey( cmData_t* p, unsigned index )
  1446. {
  1447. assert( p->tid == kRecordDtId );
  1448. cmData_t* cp = cmDataChild(p,index);
  1449. assert( p->tid == kPairDtId );
  1450. return cmDataPairKey(cp);
  1451. }
  1452. unsigned cmDataRecdKeyId( cmData_t* p, unsigned index )
  1453. {
  1454. cmData_t* kp = cmDataRecdKey(p,index);
  1455. return cmDataGetUInt(kp);
  1456. }
  1457. const cmChar_t* cmDataRecdKeyLabel( cmData_t* p, unsigned index )
  1458. {
  1459. cmData_t* kp = cmDataRecdKey(p,index);
  1460. return cmDataGetConstStr(kp);
  1461. }
  1462. cmData_t* cmRecdMake( cmData_t* parent, cmData_t* p )
  1463. {
  1464. _cmDataFree(p);
  1465. p->parent = parent;
  1466. p->tid = kRecordDtId;
  1467. p->flags = 0;
  1468. p->u.child = NULL;
  1469. return p;
  1470. }
  1471. cmData_t* cmRecdAlloc(cmData_t* parent)
  1472. { return _cmDataAllocNode(parent,kRecordDtId); }
  1473. cmData_t* cmRecdAppendPair( cmData_t* p, cmData_t* pair )
  1474. {
  1475. assert( p!=NULL && p->tid==kRecordDtId);
  1476. cmDataAppendChild(p,pair);
  1477. return p;
  1478. }
  1479. cmDtRC_t _cmDataRecdParseV(cmData_t* parent, unsigned idFl, va_list vl )
  1480. {
  1481. assert( parent != NULL && parent->tid == kRecordDtId );
  1482. bool contFl = true;
  1483. cmDtRC_t rc = kOkDtRC;
  1484. // for each record field
  1485. while( contFl )
  1486. {
  1487. cmData_t* vp = NULL;
  1488. unsigned id = cmInvalidId;
  1489. const cmChar_t* label = NULL;
  1490. // parse the field idenfier
  1491. if( idFl )
  1492. id = va_arg(vl,unsigned); // numeric field identifier
  1493. else
  1494. label = va_arg(vl,const char*); // text field label identifier
  1495. // validate the field identifier
  1496. if( id==kInvalidDtId || label==NULL )
  1497. break;
  1498. // parse the field data
  1499. if((rc =_cmDataParseArgV( NULL, vl, &vp )) != kOkDtRC )
  1500. {
  1501. contFl = false;
  1502. }
  1503. else
  1504. {
  1505. // create the field pair
  1506. if( idFl )
  1507. cmDataAllocPairId(parent,id,vp);
  1508. else
  1509. cmDataAllocPairLabel(parent,label,vp);
  1510. }
  1511. }
  1512. return rc;
  1513. }
  1514. cmData_t* cmDataRecdAllocLabelV( cmData_t* parent, va_list vl )
  1515. {
  1516. cmData_t* p = cmRecdAlloc(parent);
  1517. cmDtRC_t rc = _cmDataRecdParseV(p, false, vl );
  1518. if( rc != kOkDtRC )
  1519. {
  1520. cmDataFree(p);
  1521. p = NULL;
  1522. }
  1523. return p;
  1524. }
  1525. cmData_t* cmDataRecdAllocLabelA( cmData_t* parent, ... )
  1526. {
  1527. va_list vl;
  1528. va_start(vl,parent);
  1529. cmData_t* p = cmDataRecdAllocLabelV(parent,vl);
  1530. va_end(vl);
  1531. return p;
  1532. }
  1533. cmData_t* cmDataRecdAllocIdV( cmData_t* parent, va_list vl )
  1534. {
  1535. cmData_t* p = cmRecdAlloc(parent);
  1536. cmDtRC_t rc = _cmDataRecdParseV(p, true, vl );
  1537. if( rc != kOkDtRC )
  1538. {
  1539. cmDataFree(p);
  1540. p = NULL;
  1541. }
  1542. return p;
  1543. }
  1544. cmData_t* cmDataRecdAllocIdA( cmData_t* parent, ... )
  1545. {
  1546. va_list vl;
  1547. va_start(vl,parent);
  1548. cmData_t* p = cmDataRecdAllocIdV(parent,vl);
  1549. va_end(vl);
  1550. return p;
  1551. }
  1552. //----------------------------------------------------------------------------
  1553. unsigned cmDataSerializeByteCount( const cmData_t* p )
  1554. {
  1555. unsigned bn = 0;
  1556. // if this data type has a child then calculate it's size
  1557. if( kMinStructDtId <= p->tid && p->tid <= kMaxStructDtId && p->u.child != NULL )
  1558. bn = cmDataSerializeByteCount(p->u.child);
  1559. // if this data type has siblings get their type
  1560. cmData_t* dp = p->u.child;
  1561. for(; dp != NULL; dp=dp->sibling )
  1562. bn += cmDataSerializeByteCount(dp->sibling);
  1563. //
  1564. return _cmDataByteCount(p) + bn;
  1565. }
  1566. cmDtRC_t cmDataSerialize( const cmData_t* p, void* buf, unsigned bufByteCnt )
  1567. {
  1568. return kOkDtRC;
  1569. }
  1570. cmDtRC_t cmDataDeserialize( const void* buf, unsigned bufByteCnt, cmData_t** pp )
  1571. {
  1572. return kOkDtRC;
  1573. }
  1574. #define parr(rpt,fmt,arr,n) do{int i=0; cmRptPrintf(rpt,"[ "); for(;i<n;++i) cmRptPrintf(rpt,fmt,arr[i]); cmRptPrintf(rpt," ]"); }while(0)
  1575. void _cmDataPrintIndent( cmRpt_t* rpt, unsigned indent )
  1576. {
  1577. unsigned j=0;
  1578. for(; j<indent; ++j)
  1579. cmRptPrintf(rpt," ");
  1580. }
  1581. void _cmDataPrint( const cmData_t* p, cmRpt_t* rpt, unsigned indent )
  1582. {
  1583. cmData_t* cp;
  1584. //_cmDataPrintIndent(rpt,indent);
  1585. switch(p->tid)
  1586. {
  1587. case kNullDtId: cmRptPrintf(rpt,"<null>"); break;
  1588. case kUCharDtId: cmRptPrintf(rpt,"%c ",cmDataGetUChar(p)); break;
  1589. case kCharDtId: cmRptPrintf(rpt,"%c ",cmDataGetChar(p)); break;
  1590. case kUShortDtId: cmRptPrintf(rpt,"%i ",cmDataGetUShort(p)); break;
  1591. case kShortDtId: cmRptPrintf(rpt,"%i ",cmDataGetShort(p)); break;
  1592. case kUIntDtId: cmRptPrintf(rpt,"%i ",cmDataGetUInt(p)); break;
  1593. case kIntDtId: cmRptPrintf(rpt,"%i ",cmDataGetInt(p)); break;
  1594. case kULongDtId: cmRptPrintf(rpt,"%i ",cmDataGetULong(p)); break;
  1595. case kLongDtId: cmRptPrintf(rpt,"%i ",cmDataGetLong(p)); break;
  1596. case kFloatDtId: cmRptPrintf(rpt,"%f ",cmDataGetFloat(p)); break;
  1597. case kDoubleDtId: cmRptPrintf(rpt,"%f ",cmDataGetDouble(p)); break;
  1598. case kStrDtId: cmRptPrintf(rpt,"%s ",cmDataGetStr(p)); break;
  1599. case kConstStrDtId: cmRptPrintf(rpt,"%s ",cmDataGetConstStr(p)); break;
  1600. case kUCharPtrDtId: parr(rpt,"%c ",cmDataGetUCharPtr(p), p->cnt); break;
  1601. case kCharPtrDtId: parr(rpt,"%c ",cmDataGetCharPtr(p), p->cnt); break;
  1602. case kUShortPtrDtId: parr(rpt,"%i ",cmDataGetUShortPtr(p),p->cnt); break;
  1603. case kShortPtrDtId: parr(rpt,"%i ",cmDataGetShortPtr(p), p->cnt); break;
  1604. case kUIntPtrDtId: parr(rpt,"%i ",cmDataGetUIntPtr(p), p->cnt); break;
  1605. case kIntPtrDtId: parr(rpt,"%i ",cmDataGetIntPtr(p), p->cnt); break;
  1606. case kULongPtrDtId: parr(rpt,"%i ",cmDataGetULongPtr(p), p->cnt); break;
  1607. case kLongPtrDtId: parr(rpt,"%i ",cmDataGetLongPtr(p), p->cnt); break;
  1608. case kFloatPtrDtId: parr(rpt,"%f ",cmDataGetFloatPtr(p), p->cnt); break;
  1609. case kDoublePtrDtId: parr(rpt,"%f ",cmDataGetDoublePtr(p),p->cnt); break;
  1610. case kVoidPtrDtId: cmRptPrintf(rpt,"<void:%i>",p->cnt); break;
  1611. case kPairDtId:
  1612. _cmDataPrint(p->u.child,rpt,indent);
  1613. cmRptPrintf(rpt," : ");
  1614. _cmDataPrint(p->u.child->sibling,rpt,indent);
  1615. cmRptPrintf(rpt,"\n");
  1616. break;
  1617. case kListDtId:
  1618. cmRptPrintf(rpt,"(\n");
  1619. indent += 2;
  1620. cp = p->u.child;
  1621. for(; cp!=NULL; cp=cp->sibling)
  1622. {
  1623. _cmDataPrintIndent(rpt,indent);
  1624. _cmDataPrint(cp,rpt,indent);
  1625. cmRptPrintf(rpt,"\n");
  1626. }
  1627. indent -= 2;
  1628. _cmDataPrintIndent(rpt,indent);
  1629. cmRptPrintf(rpt,")\n");
  1630. break;
  1631. case kRecordDtId:
  1632. cmRptPrintf(rpt,"{\n");
  1633. indent += 2;
  1634. cp = p->u.child;
  1635. for(; cp!=NULL; cp=cp->sibling)
  1636. {
  1637. _cmDataPrintIndent(rpt,indent);
  1638. _cmDataPrint(cp,rpt, indent);
  1639. }
  1640. indent -= 2;
  1641. _cmDataPrintIndent(rpt,indent);
  1642. cmRptPrintf(rpt,"}\n");
  1643. break;
  1644. default:
  1645. break;
  1646. }
  1647. }
  1648. void cmDataPrint( const cmData_t* p, cmRpt_t* rpt )
  1649. { _cmDataPrint(p,rpt,0); }
  1650. void cmDataTest( cmCtx_t* ctx )
  1651. {
  1652. float farr[] = { 1.23, 45.6, 7.89 };
  1653. cmData_t* d0 = cmDataRecdAllocLabelA(NULL,
  1654. "name",kConstStrDtId,"This is a string.",
  1655. "id", kUIntDtId, 21,
  1656. "real",kFloatDtId, 1.23,
  1657. "arr", kFloatPtrDtId, farr, 3,
  1658. NULL);
  1659. cmDataPrint(d0,&ctx->rpt);
  1660. cmDataFree(d0);
  1661. cmData_t* d1 = cmDataListAllocA(NULL,
  1662. kUIntDtId, 53,
  1663. kStrDtId, "Blah blah",
  1664. kFloatPtrDtId, farr, 3,
  1665. kInvalidDtId );
  1666. cmDataPrint(d1,&ctx->rpt);
  1667. cmDataFree(d1);
  1668. cmRptPrintf(&ctx->rpt,"Done!.\n");
  1669. }