libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmData.c 77KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971
  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 "cmLinkedHeap.h"
  8. #include "cmData.h"
  9. #include "cmLex.h"
  10. #include "cmText.h"
  11. #include "cmStack.h"
  12. cmDtRC_t _cmDataErrNo = kOkDtRC;
  13. cmData_t cmDataNull = { kInvalidDtId,0,NULL,NULL,0 };
  14. cmDtRC_t _cmDataSetError( unsigned err )
  15. {
  16. _cmDataErrNo = err;
  17. return 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 = 0;
  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. cmDtRC_t cmDataGetUChar( const cmData_t* p, unsigned char* vp )
  118. {
  119. switch( p->tid )
  120. {
  121. case kUCharDtId: *vp = p->u.uc; break;
  122. case kCharDtId: *vp = (unsigned char)p->u.c; break;
  123. case kUShortDtId: *vp = (unsigned char)p->u.us; break;
  124. case kShortDtId: *vp = (unsigned char)p->u.s; break;
  125. case kUIntDtId: *vp = (unsigned char)p->u.ui; break;
  126. case kIntDtId: *vp = (unsigned char)p->u.i; break;
  127. case kULongDtId: *vp = (unsigned char)p->u.ul; break;
  128. case kLongDtId: *vp = (unsigned char)p->u.l; break;
  129. case kFloatDtId: *vp = (unsigned char)p->u.f; break;
  130. case kDoubleDtId: *vp = (unsigned char)p->u.d; break;
  131. default:
  132. return _cmDataSetError(kCvtErrDtRC);
  133. }
  134. return kOkDtRC;
  135. }
  136. cmDtRC_t cmDataGetChar( const cmData_t* p, char* vp )
  137. {
  138. switch( p->tid )
  139. {
  140. case kUCharDtId: *vp = (char)p->u.uc; break;
  141. case kCharDtId: *vp = p->u.c; break;
  142. case kUShortDtId: *vp = (char)p->u.us; break;
  143. case kShortDtId: *vp = (char)p->u.s; break;
  144. case kUIntDtId: *vp = (char)p->u.ui; break;
  145. case kIntDtId: *vp = (char)p->u.i; break;
  146. case kULongDtId: *vp = (char)p->u.ul; break;
  147. case kLongDtId: *vp = (char)p->u.l; break;
  148. case kFloatDtId: *vp = (char)p->u.f; break;
  149. case kDoubleDtId: *vp = (char)p->u.d; break;
  150. default:
  151. return _cmDataSetError(kCvtErrDtRC);
  152. }
  153. return kOkDtRC;
  154. }
  155. cmDtRC_t cmDataGetShort( const cmData_t* p, short* vp )
  156. {
  157. switch( p->tid )
  158. {
  159. case kUCharDtId: *vp = (short)p->u.uc; break;
  160. case kCharDtId: *vp = (short)p->u.c; break;
  161. case kUShortDtId: *vp = (short)p->u.us; break;
  162. case kShortDtId: *vp = p->u.s; break;
  163. case kUIntDtId: *vp = (short)p->u.ui; break;
  164. case kIntDtId: *vp = (short)p->u.i; break;
  165. case kULongDtId: *vp = (short)p->u.ul; break;
  166. case kLongDtId: *vp = (short)p->u.l; break;
  167. case kFloatDtId: *vp = (short)p->u.f; break;
  168. case kDoubleDtId: *vp = (short)p->u.d; break;
  169. default:
  170. return _cmDataSetError(kCvtErrDtRC);
  171. }
  172. return kOkDtRC;
  173. }
  174. cmDtRC_t cmDataGetUShort( const cmData_t* p, unsigned short* vp )
  175. {
  176. switch( p->tid )
  177. {
  178. case kUCharDtId: *vp = (unsigned short)p->u.uc; break;
  179. case kCharDtId: *vp = (unsigned short)p->u.c; break;
  180. case kUShortDtId: *vp = p->u.us; break;
  181. case kShortDtId: *vp = (unsigned short)p->u.s; break;
  182. case kUIntDtId: *vp = (unsigned short)p->u.ui; break;
  183. case kIntDtId: *vp = (unsigned short)p->u.i; break;
  184. case kULongDtId: *vp = (unsigned short)p->u.ul; break;
  185. case kLongDtId: *vp = (unsigned short)p->u.l; break;
  186. case kFloatDtId: *vp = (unsigned short)p->u.f; break;
  187. case kDoubleDtId: *vp = (unsigned short)p->u.d; break;
  188. default:
  189. return _cmDataSetError(kCvtErrDtRC);
  190. }
  191. return kOkDtRC;
  192. }
  193. cmDtRC_t cmDataGetInt( const cmData_t* p, int* vp )
  194. {
  195. switch( p->tid )
  196. {
  197. case kUCharDtId: *vp = (int)p->u.uc; break;
  198. case kCharDtId: *vp = (int)p->u.c; break;
  199. case kUShortDtId: *vp = (int)p->u.us; break;
  200. case kShortDtId: *vp = (int)p->u.s; break;
  201. case kUIntDtId: *vp = (int)p->u.ui; break;
  202. case kIntDtId: *vp = p->u.i; break;
  203. case kULongDtId: *vp = (int)p->u.ul; break;
  204. case kLongDtId: *vp = (int)p->u.l; break;
  205. case kFloatDtId: *vp = (int)p->u.f; break;
  206. case kDoubleDtId: *vp = (int)p->u.d; break;
  207. default:
  208. return _cmDataSetError(kCvtErrDtRC);
  209. }
  210. return kOkDtRC;
  211. }
  212. cmDtRC_t cmDataGetUInt( const cmData_t* p, unsigned int* vp )
  213. {
  214. switch( p->tid )
  215. {
  216. case kUCharDtId: *vp = (unsigned int)p->u.uc; break;
  217. case kCharDtId: *vp = (unsigned int)p->u.c; break;
  218. case kUShortDtId: *vp = (unsigned int)p->u.us; break;
  219. case kShortDtId: *vp = (unsigned int)p->u.s; break;
  220. case kUIntDtId: *vp = p->u.ui; break;
  221. case kIntDtId: *vp = (unsigned int)p->u.i; break;
  222. case kULongDtId: *vp = (unsigned int)p->u.ul; break;
  223. case kLongDtId: *vp = (unsigned int)p->u.l; break;
  224. case kFloatDtId: *vp = (unsigned int)p->u.f; break;
  225. case kDoubleDtId: *vp = (unsigned int)p->u.d; break;
  226. default:
  227. return _cmDataSetError(kCvtErrDtRC);
  228. }
  229. return kOkDtRC;
  230. }
  231. cmDtRC_t cmDataGetLong( const cmData_t* p, long* vp )
  232. {
  233. switch( p->tid )
  234. {
  235. case kUCharDtId: *vp = (long)p->u.uc; break;
  236. case kCharDtId: *vp = (long)p->u.c; break;
  237. case kUShortDtId: *vp = (long)p->u.us; break;
  238. case kShortDtId: *vp = (long)p->u.s; break;
  239. case kUIntDtId: *vp = (long)p->u.ui; break;
  240. case kIntDtId: *vp = (long)p->u.i; break;
  241. case kULongDtId: *vp = (long)p->u.ul; break;
  242. case kLongDtId: *vp = p->u.l; break;
  243. case kFloatDtId: *vp = (long)p->u.f; break;
  244. case kDoubleDtId: *vp = (long)p->u.d; break;
  245. default:
  246. return _cmDataSetError(kCvtErrDtRC);
  247. }
  248. return kOkDtRC;
  249. }
  250. cmDtRC_t cmDataGetULong( const cmData_t* p, unsigned long* vp )
  251. {
  252. switch( p->tid )
  253. {
  254. case kUCharDtId: *vp = (unsigned long)p->u.uc; break;
  255. case kCharDtId: *vp = (unsigned long)p->u.c; break;
  256. case kUShortDtId: *vp = (unsigned long)p->u.us; break;
  257. case kShortDtId: *vp = (unsigned long)p->u.s; break;
  258. case kUIntDtId: *vp = (unsigned long)p->u.ui; break;
  259. case kIntDtId: *vp = (unsigned long)p->u.i; break;
  260. case kULongDtId: *vp = p->u.ul; break;
  261. case kLongDtId: *vp = (unsigned long)p->u.l; break;
  262. case kFloatDtId: *vp = (unsigned long)p->u.f; break;
  263. case kDoubleDtId: *vp = (unsigned long)p->u.d; break;
  264. default:
  265. return _cmDataSetError(kCvtErrDtRC);
  266. }
  267. return kOkDtRC;
  268. }
  269. cmDtRC_t cmDataGetFloat( const cmData_t* p, float* vp )
  270. {
  271. switch( p->tid )
  272. {
  273. case kUCharDtId: *vp = (float)p->u.uc; break;
  274. case kCharDtId: *vp = (float)p->u.c; break;
  275. case kUShortDtId: *vp = (float)p->u.us; break;
  276. case kShortDtId: *vp = (float)p->u.s; break;
  277. case kUIntDtId: *vp = (float)p->u.ui; break;
  278. case kIntDtId: *vp = (float)p->u.i; break;
  279. case kULongDtId: *vp = (float)p->u.ul; break;
  280. case kLongDtId: *vp = (float)p->u.l; break;
  281. case kFloatDtId: *vp = p->u.f; break;
  282. case kDoubleDtId: *vp = (float)p->u.d; break;
  283. default:
  284. return _cmDataSetError(kCvtErrDtRC);
  285. }
  286. return kOkDtRC;
  287. }
  288. cmDtRC_t cmDataGetDouble( const cmData_t* p, double* vp )
  289. {
  290. switch( p->tid )
  291. {
  292. case kUCharDtId: *vp = (double)p->u.uc; break;
  293. case kCharDtId: *vp = (double)p->u.c; break;
  294. case kUShortDtId: *vp = (double)p->u.us; break;
  295. case kShortDtId: *vp = (double)p->u.s; break;
  296. case kUIntDtId: *vp = (double)p->u.ui; break;
  297. case kIntDtId: *vp = (double)p->u.i; break;
  298. case kULongDtId: *vp = (double)p->u.ul; break;
  299. case kLongDtId: *vp = (double)p->u.l; break;
  300. case kFloatDtId: *vp = (double)p->u.f; break;
  301. case kDoubleDtId: *vp = p->u.d; break;
  302. default:
  303. return _cmDataSetError(kCvtErrDtRC);
  304. }
  305. return kOkDtRC;
  306. }
  307. cmDtRC_t cmDataGetStr( const cmData_t* p, cmChar_t** vp )
  308. {
  309. if( p->tid == kStrDtId || p->tid == kConstStrDtId)
  310. {
  311. *vp = (p->tid == kStrDtId || p->tid == kConstStrDtId) ? p->u.z : NULL;
  312. return kOkDtRC;
  313. }
  314. return _cmDataSetError(kCvtErrDtRC);
  315. }
  316. cmDtRC_t cmDataGetConstStr( const cmData_t* p, const cmChar_t** vp )
  317. {
  318. if( p->tid == kStrDtId || p->tid == kConstStrDtId)
  319. {
  320. *vp = (p->tid == kStrDtId || p->tid == kConstStrDtId) ? p->u.cz : NULL;
  321. return kOkDtRC;
  322. }
  323. return _cmDataSetError(kCvtErrDtRC);
  324. }
  325. cmDtRC_t cmDataGetVoidPtr( const cmData_t* p, void** vp )
  326. {
  327. if( kMinPtrDtId <= p->tid && p->tid <= kMaxPtrDtId )
  328. {
  329. *vp = ( kMinPtrDtId <= p->tid && p->tid <= kMaxPtrDtId ) ? p->u.vp : NULL;
  330. return kOkDtRC;
  331. }
  332. return _cmDataSetError(kCvtErrDtRC);
  333. }
  334. cmDtRC_t cmDataGetCharPtr( const cmData_t* p, char** vp )
  335. {
  336. if( p->tid == kCharPtrDtId || p->tid == kUCharPtrDtId )
  337. {
  338. *vp = (p->tid == kCharPtrDtId || p->tid == kUCharPtrDtId) ? p->u.cp : NULL;
  339. return kOkDtRC;
  340. }
  341. return _cmDataSetError(kCvtErrDtRC);
  342. }
  343. cmDtRC_t cmDataGetUCharPtr( const cmData_t* p, unsigned char** vp )
  344. {
  345. if( p->tid == kCharPtrDtId || p->tid == kUCharPtrDtId )
  346. {
  347. *vp = (p->tid == kCharPtrDtId || p->tid == kUCharPtrDtId) ? p->u.ucp : NULL;
  348. return kOkDtRC;
  349. }
  350. return _cmDataSetError(kCvtErrDtRC);
  351. }
  352. cmDtRC_t cmDataGetShortPtr( const cmData_t* p, short** vp )
  353. {
  354. if( p->tid == kShortPtrDtId || p->tid == kUShortPtrDtId )
  355. {
  356. *vp = (p->tid == kShortPtrDtId || p->tid == kUShortPtrDtId ) ? p->u.sp : NULL;
  357. return kOkDtRC;
  358. }
  359. return _cmDataSetError(kCvtErrDtRC);
  360. }
  361. cmDtRC_t cmDataGetUShortPtr( const cmData_t* p, unsigned short** vp )
  362. {
  363. if( p->tid == kShortPtrDtId || p->tid == kUShortPtrDtId )
  364. {
  365. *vp = (p->tid == kShortPtrDtId || p->tid == kUShortPtrDtId ) ? p->u.usp : NULL;
  366. return kOkDtRC;
  367. }
  368. return _cmDataSetError(kCvtErrDtRC);
  369. }
  370. cmDtRC_t cmDataGetIntPtr( const cmData_t* p, int** vp )
  371. {
  372. if( p->tid == kIntPtrDtId || p->tid == kUIntPtrDtId )
  373. {
  374. *vp = (p->tid == kIntPtrDtId || p->tid == kUIntPtrDtId ) ? p->u.ip : NULL;
  375. return kOkDtRC;
  376. }
  377. return _cmDataSetError(kCvtErrDtRC);
  378. }
  379. cmDtRC_t cmDataGetUIntPtr( const cmData_t* p, unsigned int** vp )
  380. {
  381. if( p->tid == kIntPtrDtId || p->tid == kUIntPtrDtId )
  382. {
  383. *vp = (p->tid == kIntPtrDtId || p->tid == kUIntPtrDtId ) ? p->u.uip : NULL;
  384. return kOkDtRC;
  385. }
  386. return _cmDataSetError(kCvtErrDtRC);
  387. }
  388. cmDtRC_t cmDataGetLongPtr( const cmData_t* p, long** vp )
  389. {
  390. if( p->tid == kLongPtrDtId || p->tid == kULongPtrDtId )
  391. {
  392. *vp = (p->tid == kLongPtrDtId || p->tid == kULongPtrDtId ) ? p->u.lp : NULL;
  393. return kOkDtRC;
  394. }
  395. return _cmDataSetError(kCvtErrDtRC);
  396. }
  397. cmDtRC_t cmDataGetULongPtr( const cmData_t* p, unsigned long** vp )
  398. {
  399. if( p->tid == kLongPtrDtId || p->tid == kULongPtrDtId )
  400. {
  401. *vp = (p->tid == kLongPtrDtId || p->tid == kULongPtrDtId ) ? p->u.ulp : NULL;
  402. return kOkDtRC;
  403. }
  404. return _cmDataSetError(kCvtErrDtRC);
  405. }
  406. cmDtRC_t cmDataGetFloatPtr( const cmData_t* p, float** vp )
  407. {
  408. if( p->tid == kFloatPtrDtId )
  409. {
  410. *vp = p->u.fp;
  411. return kOkDtRC;
  412. }
  413. return _cmDataSetError(kCvtErrDtRC);
  414. }
  415. cmDtRC_t cmDataGetDoublePtr( const cmData_t* p, double** vp )
  416. {
  417. if( p->tid == kDoublePtrDtId )
  418. {
  419. *vp = p->u.dp;
  420. return kOkDtRC;
  421. }
  422. return _cmDataSetError(kCvtErrDtRC);
  423. }
  424. // Set the value of an existing data object.
  425. cmData_t* cmDataSetNull( cmData_t* p )
  426. {
  427. _cmDataFreeArray(p);
  428. p->tid = kNullDtId;
  429. return p;
  430. }
  431. cmData_t* cmDataSetChar( cmData_t* p, char v )
  432. {
  433. _cmDataFreeArray(p);
  434. p->tid = kCharDtId;
  435. p->u.c = v;
  436. return p;
  437. }
  438. cmData_t* cmDataSetUChar( cmData_t* p, unsigned char v )
  439. {
  440. _cmDataFreeArray(p);
  441. p->tid = kUCharDtId;
  442. p->u.uc = v;
  443. return p;
  444. }
  445. cmData_t* cmDataSetShort( cmData_t* p, short v )
  446. {
  447. _cmDataFreeArray(p);
  448. p->tid = kShortDtId;
  449. p->u.s = v;
  450. return p;
  451. }
  452. cmData_t* cmDataSetUShort( cmData_t* p, unsigned short v )
  453. {
  454. _cmDataFreeArray(p);
  455. p->tid = kUShortDtId;
  456. p->u.us = v;
  457. return p;
  458. }
  459. cmData_t* cmDataSetInt( cmData_t* p, int v )
  460. {
  461. _cmDataFreeArray(p);
  462. p->tid = kCharDtId;
  463. p->u.c = v;
  464. return p;
  465. }
  466. cmData_t* cmDataSetUInt( cmData_t* p, unsigned int v )
  467. {
  468. _cmDataFreeArray(p);
  469. p->tid = kUIntDtId;
  470. p->u.ui = v;
  471. return p;
  472. }
  473. cmData_t* cmDataSetLong( cmData_t* p, long v )
  474. {
  475. _cmDataFreeArray(p);
  476. p->tid = kLongDtId;
  477. p->u.l = v;
  478. return p;
  479. }
  480. cmData_t* cmDataSetULong( cmData_t* p, unsigned long v )
  481. {
  482. _cmDataFreeArray(p);
  483. p->tid = kULongDtId;
  484. p->u.ul = v;
  485. return p;
  486. }
  487. cmData_t* cmDataSetFloat( cmData_t* p, float v )
  488. {
  489. _cmDataFreeArray(p);
  490. p->tid = kFloatDtId;
  491. p->u.f = v;
  492. return p;
  493. }
  494. cmData_t* cmDataSetDouble( cmData_t* p, double v )
  495. {
  496. _cmDataFreeArray(p);
  497. p->tid = kDoubleDtId;
  498. p->u.d = v;
  499. return p;
  500. }
  501. cmData_t* cmDataSetStr( cmData_t* p, cmChar_t* s )
  502. {
  503. _cmDataFreeArray(p);
  504. p->tid = kStrDtId;
  505. p->u.z = s;
  506. return p;
  507. }
  508. cmData_t* cmDataSetConstStr( cmData_t* p, const cmChar_t* s )
  509. {
  510. _cmDataFreeArray(p);
  511. p->tid = kConstStrDtId;
  512. p->u.cz = s;
  513. return p;
  514. }
  515. // Set the value of an existing data object to an external array.
  516. // The array is not copied.
  517. cmData_t* cmDataSetVoidPtr( cmData_t* p, void* vp, unsigned cnt )
  518. {
  519. cmDataSetCharPtr(p,(char*)vp,cnt);
  520. p->tid = kVoidPtrDtId;
  521. return p;
  522. }
  523. cmData_t* cmDataSetCharPtr( cmData_t* p, char* vp, unsigned cnt )
  524. {
  525. _cmDataFreeArray(p);
  526. p->tid = kCharPtrDtId;
  527. p->u.cp = vp;
  528. p->cnt = cnt;
  529. return p;
  530. }
  531. cmData_t* cmDataSetUCharPtr( cmData_t* p, unsigned char* vp, unsigned cnt )
  532. {
  533. _cmDataFreeArray(p);
  534. p->tid = kUCharPtrDtId;
  535. p->u.ucp = vp;
  536. p->cnt = cnt;
  537. return p;
  538. }
  539. cmData_t* cmDataSetShortPtr( cmData_t* p, short* vp, unsigned cnt )
  540. {
  541. _cmDataFreeArray(p);
  542. p->tid = kShortPtrDtId;
  543. p->u.sp = vp;
  544. p->cnt = cnt;
  545. return p;
  546. }
  547. cmData_t* cmDataSetUShortPtr( cmData_t* p, unsigned short* vp, unsigned cnt )
  548. {
  549. _cmDataFreeArray(p);
  550. p->tid = kUShortPtrDtId;
  551. p->u.usp = vp;
  552. p->cnt = cnt;
  553. return p;
  554. }
  555. cmData_t* cmDataSetIntPtr( cmData_t* p, int* vp, unsigned cnt )
  556. {
  557. _cmDataFreeArray(p);
  558. p->tid = kCharPtrDtId;
  559. p->u.ip = vp;
  560. p->cnt = cnt;
  561. return p;
  562. }
  563. cmData_t* cmDataSetUIntPtr( cmData_t* p, unsigned int* vp, unsigned cnt )
  564. {
  565. _cmDataFreeArray(p);
  566. p->tid = kUIntPtrDtId;
  567. p->u.uip = vp;
  568. p->cnt = cnt;
  569. return p;
  570. }
  571. cmData_t* cmDataSetLongPtr( cmData_t* p, long* vp, unsigned cnt )
  572. {
  573. _cmDataFreeArray(p);
  574. p->tid = kLongPtrDtId;
  575. p->u.lp = vp;
  576. p->cnt = cnt;
  577. return p;
  578. }
  579. cmData_t* cmDataSetULongPtr( cmData_t* p, unsigned long* vp, unsigned cnt )
  580. {
  581. _cmDataFreeArray(p);
  582. p->tid = kULongPtrDtId;
  583. p->u.ulp = vp;
  584. p->cnt = cnt;
  585. return p;
  586. }
  587. cmData_t* cmDataSetFloatPtr( cmData_t* p, float* vp, unsigned cnt )
  588. {
  589. _cmDataFreeArray(p);
  590. p->tid = kFloatPtrDtId;
  591. p->u.fp = vp;
  592. p->cnt = cnt;
  593. return p;
  594. }
  595. cmData_t* cmDataSetDoublePtr( cmData_t* p, double* vp, unsigned cnt )
  596. {
  597. _cmDataFreeArray(p);
  598. p->tid = kDoublePtrDtId;
  599. p->u.dp = vp;
  600. p->cnt = cnt;
  601. return p;
  602. }
  603. // Set the value of an existing array based data object.
  604. // Allocate the internal array and copy the array into it.
  605. cmData_t* cmDataSetStrAllocN( cmData_t* p, const cmChar_t* s, unsigned charCnt )
  606. {
  607. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  608. cmMemResizeStrN(p->u.z,s,charCnt);
  609. else
  610. {
  611. _cmDataFreeArray(p);
  612. p->u.z = cmMemAllocStrN(s,charCnt);
  613. }
  614. p->tid = kStrDtId;
  615. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  616. return p;
  617. }
  618. cmData_t* cmDataSetStrAlloc( cmData_t* p, const cmChar_t* s )
  619. { return cmDataSetStrAllocN(p,s,strlen(s)); }
  620. cmData_t* cmDataSetConstStrAllocN( cmData_t* p, const cmChar_t* s, unsigned charCnt )
  621. { return cmDataSetStrAllocN(p,s,charCnt); }
  622. cmData_t* cmDataSetConstStrAlloc( cmData_t* p, const cmChar_t* s )
  623. { return cmDataSetStrAlloc(p,s); }
  624. cmData_t* cmDataSetVoidAllocPtr( cmData_t* p, const void* vp, unsigned cnt )
  625. { return cmDataSetCharAllocPtr(p,(char*)vp,cnt); }
  626. cmData_t* cmDataSetCharAllocPtr( cmData_t* p, const char* vp, unsigned cnt )
  627. {
  628. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  629. p->u.cp = cmMemResize(char, p->u.cp, cnt );
  630. else
  631. {
  632. _cmDataFreeArray(p);
  633. p->u.cp = cmMemAlloc(char, cnt );
  634. }
  635. memcpy(p->u.cp,vp,sizeof(char)*cnt);
  636. p->tid = kCharPtrDtId;
  637. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  638. p->cnt = cnt;
  639. return p;
  640. }
  641. cmData_t* cmDataSetUCharAllocPtr( cmData_t* p, const unsigned char* vp, unsigned cnt )
  642. {
  643. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  644. p->u.ucp = cmMemResize(unsigned char, p->u.ucp, cnt );
  645. else
  646. {
  647. _cmDataFreeArray(p);
  648. p->u.ucp = cmMemAlloc(unsigned char, cnt );
  649. }
  650. memcpy(p->u.ucp,vp,sizeof(unsigned char)*cnt);
  651. p->tid = kUCharPtrDtId;
  652. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  653. p->cnt = cnt;
  654. return p;
  655. }
  656. cmData_t* cmDataSetShortAllocPtr( cmData_t* p, const short* vp, unsigned cnt )
  657. {
  658. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  659. p->u.sp = cmMemResize(short, p->u.sp, cnt );
  660. else
  661. {
  662. _cmDataFreeArray(p);
  663. p->u.sp = cmMemAlloc(short, cnt );
  664. }
  665. memcpy(p->u.sp,vp,sizeof(short)*cnt);
  666. p->tid = kShortPtrDtId;
  667. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  668. p->cnt = cnt;
  669. return p;
  670. }
  671. cmData_t* cmDataSetUShortAllocPtr( cmData_t* p, const unsigned short* vp, unsigned cnt )
  672. {
  673. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  674. p->u.usp = cmMemResize(unsigned short, p->u.usp, cnt );
  675. else
  676. {
  677. _cmDataFreeArray(p);
  678. p->u.usp = cmMemAlloc(unsigned short, cnt );
  679. }
  680. memcpy(p->u.usp,vp,sizeof(unsigned short)*cnt);
  681. p->tid = kUShortPtrDtId;
  682. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  683. p->cnt = cnt;
  684. return p;
  685. }
  686. cmData_t* cmDataSetIntAllocPtr( cmData_t* p, const int* vp, unsigned cnt )
  687. {
  688. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  689. p->u.ip = cmMemResize(int, p->u.ip, cnt );
  690. else
  691. {
  692. _cmDataFreeArray(p);
  693. p->u.ip = cmMemAlloc(int, cnt );
  694. }
  695. memcpy(p->u.ip,vp,sizeof(int)*cnt);
  696. p->tid = kIntPtrDtId;
  697. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  698. p->cnt = cnt;
  699. return p;
  700. }
  701. cmData_t* cmDataSetUIntAllocPtr( cmData_t* p, const unsigned int* vp, unsigned cnt )
  702. {
  703. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  704. p->u.uip = cmMemResize(unsigned int, p->u.uip, cnt );
  705. else
  706. {
  707. _cmDataFreeArray(p);
  708. p->u.uip = cmMemAlloc(unsigned int, cnt );
  709. }
  710. memcpy(p->u.uip,vp,sizeof(unsigned int)*cnt);
  711. p->tid = kUIntPtrDtId;
  712. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  713. p->cnt = cnt;
  714. return p;
  715. }
  716. cmData_t* cmDataSetLongAllocPtr( cmData_t* p, const long* vp, unsigned cnt )
  717. {
  718. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  719. p->u.lp = cmMemResize(long, p->u.lp, cnt );
  720. else
  721. {
  722. _cmDataFreeArray(p);
  723. p->u.lp = cmMemAlloc(long, cnt );
  724. }
  725. memcpy(p->u.lp,vp,sizeof(long)*cnt);
  726. p->tid = kLongPtrDtId;
  727. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  728. p->cnt = cnt;
  729. return p;
  730. }
  731. cmData_t* cmDataSetULongAllocPtr( cmData_t* p, const unsigned long* vp, unsigned cnt )
  732. {
  733. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  734. p->u.ulp = cmMemResize(unsigned long, p->u.ulp, cnt );
  735. else
  736. {
  737. _cmDataFreeArray(p);
  738. p->u.ulp = cmMemAlloc(unsigned long, cnt );
  739. }
  740. memcpy(p->u.ulp,vp,sizeof(unsigned long)*cnt);
  741. p->tid = kULongPtrDtId;
  742. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  743. p->cnt = cnt;
  744. return p;
  745. }
  746. cmData_t* cmDataSetFloatAllocPtr( cmData_t* p, const float* vp, unsigned cnt )
  747. {
  748. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  749. p->u.fp = cmMemResize(float, p->u.fp, cnt );
  750. else
  751. {
  752. _cmDataFreeArray(p);
  753. p->u.fp = cmMemAlloc(float, cnt );
  754. }
  755. memcpy(p->u.fp,vp,sizeof(float)*cnt);
  756. p->tid = kFloatPtrDtId;
  757. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  758. p->cnt = cnt;
  759. return p;
  760. }
  761. cmData_t* cmDataSetDoubleAllocPtr( cmData_t* p, const double* vp, unsigned cnt )
  762. {
  763. if( cmIsFlag(p->flags,kDynPtrDtFl) )
  764. p->u.dp = cmMemResize(double, p->u.dp, cnt );
  765. else
  766. {
  767. _cmDataFreeArray(p);
  768. p->u.dp = cmMemAlloc(double, cnt );
  769. }
  770. memcpy(p->u.dp,vp,sizeof(double)*cnt);
  771. p->tid = kDoublePtrDtId;
  772. p->flags = cmSetFlag(p->flags,kDynPtrDtFl);
  773. p->cnt = cnt;
  774. return p;
  775. }
  776. // Dynamically allocate a data object and set it's value.
  777. cmData_t* cmDataAllocNull( cmData_t* parent )
  778. { return _cmDataAllocNode(parent,kNullDtId); }
  779. cmData_t* cmDataAllocChar( cmData_t* parent, char v )
  780. {
  781. cmData_t* p = _cmDataAllocNode(parent,kCharDtId);
  782. cmDataSetChar(p,v);
  783. return p;
  784. }
  785. cmData_t* cmDataAllocUChar( cmData_t* parent, unsigned char v )
  786. {
  787. cmData_t* p = _cmDataAllocNode(parent,kUCharDtId);
  788. cmDataSetUChar(p,v);
  789. return p;
  790. }
  791. cmData_t* cmDataAllocShort( cmData_t* parent, short v )
  792. {
  793. cmData_t* p = _cmDataAllocNode(parent,kShortDtId);
  794. cmDataSetShort(p,v);
  795. return p;
  796. }
  797. cmData_t* cmDataAllocUShort( cmData_t* parent, unsigned short v )
  798. {
  799. cmData_t* p = _cmDataAllocNode(parent,kUShortDtId);
  800. cmDataSetUShort(p,v);
  801. return p;
  802. }
  803. cmData_t* cmDataAllocInt( cmData_t* parent, int v )
  804. {
  805. cmData_t* p = _cmDataAllocNode(parent,kIntDtId);
  806. cmDataSetInt(p,v);
  807. return p;
  808. }
  809. cmData_t* cmDataAllocUInt( cmData_t* parent, unsigned int v )
  810. {
  811. cmData_t* p = _cmDataAllocNode(parent,kUIntDtId);
  812. cmDataSetUInt(p,v);
  813. return p;
  814. }
  815. cmData_t* cmDataAllocLong( cmData_t* parent, long v )
  816. {
  817. cmData_t* p = _cmDataAllocNode(parent,kLongDtId);
  818. cmDataSetLong(p,v);
  819. return p;
  820. }
  821. cmData_t* cmDataAllocULong( cmData_t* parent, unsigned long v )
  822. {
  823. cmData_t* p = _cmDataAllocNode(parent,kULongDtId);
  824. cmDataSetULong(p,v);
  825. return p;
  826. }
  827. cmData_t* cmDataAllocFloat( cmData_t* parent, float v )
  828. {
  829. cmData_t* p = _cmDataAllocNode(parent,kFloatDtId);
  830. cmDataSetFloat(p,v);
  831. return p;
  832. }
  833. cmData_t* cmDataAllocDouble( cmData_t* parent, double v )
  834. {
  835. cmData_t* p = _cmDataAllocNode(parent,kDoubleDtId);
  836. cmDataSetDouble(p,v);
  837. return p;
  838. }
  839. // Dynamically allocate a data object and set its array value to an external
  840. // array. The data is not copied.
  841. cmData_t* cmDataAllocStr( cmData_t* parent, cmChar_t* str )
  842. {
  843. cmData_t* p = _cmDataAllocNode(parent,kStrDtId);
  844. cmDataSetStr(p,str);
  845. return p;
  846. }
  847. cmData_t* cmDataAllocConstStr( cmData_t* parent, const cmChar_t* str )
  848. {
  849. cmData_t* p = _cmDataAllocNode(parent,kConstStrDtId);
  850. cmDataSetConstStr(p,str);
  851. return p;
  852. }
  853. cmData_t* cmDataAllocCharPtr( cmData_t* parent, char* v, unsigned cnt )
  854. {
  855. cmData_t* p = _cmDataAllocNode(parent,kCharPtrDtId);
  856. cmDataSetCharPtr(p,(char*)v,cnt);
  857. return p;
  858. }
  859. cmData_t* cmDataAllocUCharPtr( cmData_t* parent, unsigned char* v, unsigned cnt )
  860. {
  861. cmData_t* p = _cmDataAllocNode(parent,kUCharPtrDtId);
  862. cmDataSetUCharPtr(p,(unsigned char*)v,cnt);
  863. return p;
  864. }
  865. cmData_t* cmDataAllocShortPtr( cmData_t* parent, short* v, unsigned cnt )
  866. {
  867. cmData_t* p = _cmDataAllocNode(parent,kShortPtrDtId);
  868. cmDataSetShortPtr(p,(short*)v,cnt);
  869. return p;
  870. }
  871. cmData_t* cmDataAllocUShortPtr( cmData_t* parent, unsigned short* v, unsigned cnt )
  872. {
  873. cmData_t* p = _cmDataAllocNode(parent,kUShortPtrDtId);
  874. cmDataSetUShortPtr(p,(unsigned short*)v,cnt);
  875. return p;
  876. }
  877. cmData_t* cmDataAllocIntPtr( cmData_t* parent, int* v, unsigned cnt )
  878. {
  879. cmData_t* p = _cmDataAllocNode(parent,kIntPtrDtId);
  880. cmDataSetIntPtr(p,(int*)v,cnt);
  881. return p;
  882. }
  883. cmData_t* cmDataAllocUIntPtr( cmData_t* parent, unsigned int* v, unsigned cnt )
  884. {
  885. cmData_t* p = _cmDataAllocNode(parent,kUIntPtrDtId);
  886. cmDataSetUIntPtr(p,(unsigned int*)v,cnt);
  887. return p;
  888. }
  889. cmData_t* cmDataAllocLongPtr( cmData_t* parent, long* v, unsigned cnt )
  890. {
  891. cmData_t* p = _cmDataAllocNode(parent,kLongPtrDtId);
  892. cmDataSetLongPtr(p,(long*)v,cnt);
  893. return p;
  894. }
  895. cmData_t* cmDataAllocULongPtr( cmData_t* parent, unsigned long* v, unsigned cnt )
  896. {
  897. cmData_t* p = _cmDataAllocNode(parent,kULongPtrDtId);
  898. cmDataSetULongPtr(p,(unsigned long*)v,cnt);
  899. return p;
  900. }
  901. cmData_t* cmDataAllocFloatPtr( cmData_t* parent, float* v, unsigned cnt )
  902. {
  903. cmData_t* p = _cmDataAllocNode(parent,kFloatPtrDtId);
  904. cmDataSetFloatPtr(p,(float*)v,cnt);
  905. return p;
  906. }
  907. cmData_t* cmDataAllocDoublePtr( cmData_t* parent, double* v, unsigned cnt )
  908. {
  909. cmData_t* p = _cmDataAllocNode(parent,kDoublePtrDtId);
  910. cmDataSetDoublePtr(p,(double*)v,cnt);
  911. return p;
  912. }
  913. cmData_t* cmDataAllocVoidPtr( cmData_t* parent, void* v, unsigned cnt )
  914. {
  915. cmData_t* p = _cmDataAllocNode(parent,kVoidPtrDtId);
  916. cmDataSetCharPtr(p,(char*)v,cnt);
  917. p->tid = kVoidPtrDtId;
  918. return p;
  919. }
  920. // Dynamically allocate a data object and its array value.
  921. // v[cnt] is copied into the allocated array.
  922. cmData_t* cmDataStrAlloc( cmData_t* parent, cmChar_t* str )
  923. {
  924. cmData_t* p = _cmDataAllocNode(parent,kStrDtId);
  925. cmDataSetStrAlloc(p,str);
  926. return p;
  927. }
  928. cmData_t* cmDataConstStrAllocN( cmData_t* parent, const cmChar_t* str, unsigned charCnt )
  929. {
  930. cmData_t* p = _cmDataAllocNode(parent,kConstStrDtId);
  931. cmDataSetConstStrAllocN(p,str,charCnt);
  932. return p;
  933. }
  934. cmData_t* cmDataConstStrAlloc( cmData_t* parent, const cmChar_t* str )
  935. {
  936. cmData_t* p = _cmDataAllocNode(parent,kConstStrDtId);
  937. cmDataSetConstStrAlloc(p,str);
  938. return p;
  939. }
  940. cmData_t* cmDataCharAllocPtr( cmData_t* parent, const char* v, unsigned cnt )
  941. {
  942. cmData_t* p = _cmDataAllocNode(parent,kCharPtrDtId);
  943. cmDataSetCharAllocPtr(p, v, cnt );
  944. return p;
  945. }
  946. cmData_t* cmDataUCharAllocPtr( cmData_t* parent, const unsigned char* v, unsigned cnt )
  947. {
  948. cmData_t* p = _cmDataAllocNode(parent,kUCharPtrDtId);
  949. cmDataSetUCharAllocPtr(p, v, cnt );
  950. return p;
  951. }
  952. cmData_t* cmDataShortAllocPtr( cmData_t* parent, const short* v, unsigned cnt )
  953. {
  954. cmData_t* p = _cmDataAllocNode(parent,kShortPtrDtId);
  955. cmDataSetShortAllocPtr(p, v, cnt );
  956. return p;
  957. }
  958. cmData_t* cmDataUShortAllocPtr( cmData_t* parent, const unsigned short* v, unsigned cnt )
  959. {
  960. cmData_t* p = _cmDataAllocNode(parent,kUShortPtrDtId);
  961. cmDataSetUShortAllocPtr(p, v, cnt );
  962. return p;
  963. }
  964. cmData_t* cmDataIntAllocPtr( cmData_t* parent, const int* v, unsigned cnt )
  965. {
  966. cmData_t* p = _cmDataAllocNode(parent,kIntPtrDtId);
  967. cmDataSetIntAllocPtr(p, v, cnt );
  968. return p;
  969. }
  970. cmData_t* cmDataUIntAllocPtr( cmData_t* parent, const unsigned int* v, unsigned cnt )
  971. {
  972. cmData_t* p = _cmDataAllocNode(parent,kUIntPtrDtId);
  973. cmDataSetUIntAllocPtr(p, v, cnt );
  974. return p;
  975. }
  976. cmData_t* cmDataLongAllocPtr( cmData_t* parent, const long* v, unsigned cnt )
  977. {
  978. cmData_t* p = _cmDataAllocNode(parent,kLongPtrDtId);
  979. cmDataSetLongAllocPtr(p, v, cnt );
  980. return p;
  981. }
  982. cmData_t* cmDataULongAllocPtr( cmData_t* parent, const unsigned long* v, unsigned cnt )
  983. {
  984. cmData_t* p = _cmDataAllocNode(parent,kULongPtrDtId);
  985. cmDataSetULongAllocPtr(p, v, cnt );
  986. return p;
  987. }
  988. cmData_t* cmDataFloatAllocPtr( cmData_t* parent, const float* v, unsigned cnt )
  989. {
  990. cmData_t* p = _cmDataAllocNode(parent,kFloatPtrDtId);
  991. cmDataSetFloatAllocPtr(p, v, cnt );
  992. return p;
  993. }
  994. cmData_t* cmDataDoubleAllocPtr( cmData_t* parent, const double* v, unsigned cnt )
  995. {
  996. cmData_t* p = _cmDataAllocNode(parent,kDoublePtrDtId);
  997. cmDataSetDoubleAllocPtr(p, v, cnt );
  998. return p;
  999. }
  1000. cmData_t* cmDataVoidAllocPtr( cmData_t* parent, const void* v, unsigned cnt )
  1001. {
  1002. cmData_t* p = _cmDataAllocNode(parent,kVoidPtrDtId);
  1003. cmDataSetCharAllocPtr(p, (const char*)v, cnt );
  1004. p->tid = kVoidPtrDtId;
  1005. return p;
  1006. }
  1007. void cmDataFree( cmData_t* p )
  1008. {
  1009. _cmDataFree(p);
  1010. }
  1011. cmData_t* cmDataUnlink( cmData_t* p )
  1012. {
  1013. if( p->parent == NULL )
  1014. return p;
  1015. assert( cmDataIsStruct(p->parent) );
  1016. cmData_t* cp = p->u.child;
  1017. cmData_t* pp = NULL;
  1018. for(; cp!=NULL; cp=cp->sibling)
  1019. if( cp == p )
  1020. {
  1021. if( pp == NULL )
  1022. p->parent->u.child = p->sibling;
  1023. else
  1024. pp->sibling = cp->sibling;
  1025. }
  1026. return p;
  1027. }
  1028. void cmDataUnlinkAndFree( cmData_t* p )
  1029. {
  1030. cmDataUnlink(p);
  1031. cmDataFree(p);
  1032. }
  1033. cmData_t* _cmDataDupl( const cmData_t* p, cmData_t* parent )
  1034. {
  1035. cmData_t* rp = NULL;
  1036. switch( p->tid )
  1037. {
  1038. case kNullDtId: rp = cmDataAllocNull(parent); break;
  1039. case kUCharDtId: rp = cmDataAllocUChar(parent,p->u.uc); break;
  1040. case kCharDtId: rp = cmDataAllocChar(parent,p->u.c); break;
  1041. case kUShortDtId: rp = cmDataAllocShort(parent,p->u.us); break;
  1042. case kShortDtId: rp = cmDataAllocUShort(parent,p->u.s); break;
  1043. case kUIntDtId: rp = cmDataAllocUInt(parent,p->u.i); break;
  1044. case kIntDtId: rp = cmDataAllocInt(parent,p->u.ui); break;
  1045. case kULongDtId: rp = cmDataAllocULong(parent,p->u.ul); break;
  1046. case kLongDtId: rp = cmDataAllocLong(parent,p->u.l); break;
  1047. case kFloatDtId: rp = cmDataAllocFloat(parent,p->u.f); break;
  1048. case kDoubleDtId: rp = cmDataAllocDouble(parent,p->u.d); break;
  1049. case kStrDtId: rp = cmDataStrAlloc(parent,p->u.z); break;
  1050. case kConstStrDtId: rp = cmDataConstStrAlloc(parent,p->u.cz); break;
  1051. case kUCharPtrDtId: rp = cmDataUCharAllocPtr(parent,p->u.ucp,p->cnt); break;
  1052. case kCharPtrDtId: rp = cmDataCharAllocPtr(parent,p->u.cp,p->cnt); break;
  1053. case kUShortPtrDtId: rp = cmDataUShortAllocPtr(parent,p->u.usp,p->cnt); break;
  1054. case kShortPtrDtId: rp = cmDataShortAllocPtr(parent,p->u.sp,p->cnt); break;
  1055. case kUIntPtrDtId: rp = cmDataUIntAllocPtr(parent,p->u.uip,p->cnt); break;
  1056. case kIntPtrDtId: rp = cmDataIntAllocPtr(parent,p->u.ip,p->cnt); break;
  1057. case kULongPtrDtId: rp = cmDataULongAllocPtr(parent,p->u.ulp,p->cnt); break;
  1058. case kLongPtrDtId: rp = cmDataLongAllocPtr(parent,p->u.lp,p->cnt); break;
  1059. case kFloatPtrDtId: rp = cmDataFloatAllocPtr(parent,p->u.fp,p->cnt); break;
  1060. case kDoublePtrDtId: rp = cmDataDoubleAllocPtr(parent,p->u.dp,p->cnt); break;
  1061. case kVoidPtrDtId: rp = cmDataVoidAllocPtr(parent,p->u.vp,p->cnt); break;
  1062. case kListDtId:
  1063. case kPairDtId:
  1064. case kRecordDtId:
  1065. {
  1066. rp = _cmDataAllocNode(parent,p->tid);
  1067. cmData_t* cp = p->u.child;
  1068. for(; cp!=NULL; cp=cp->sibling)
  1069. cmDataAppendChild(rp,_cmDataDupl(cp,rp));
  1070. }
  1071. break;
  1072. default:
  1073. assert(0);
  1074. }
  1075. return rp;
  1076. }
  1077. cmData_t* cmDataDupl( const cmData_t* p )
  1078. { return _cmDataDupl(p,NULL); }
  1079. cmData_t* cmDataReplace( cmData_t* dst, cmData_t* src )
  1080. {
  1081. if( dst->parent == NULL )
  1082. {
  1083. cmDataUnlinkAndFree(dst);
  1084. src->parent = NULL;
  1085. return src;
  1086. }
  1087. cmData_t* parent = dst->parent;
  1088. cmData_t* cp = parent->u.child;
  1089. cmData_t* pp = NULL;
  1090. unsigned i = 0;
  1091. unsigned n = cmDataChildCount(parent);
  1092. // locate dst's right sibling
  1093. for(i=0; i<n; ++i,cp=cp->sibling)
  1094. {
  1095. if( cp == dst )
  1096. {
  1097. // link in 'src' in place of 'dst'
  1098. src->sibling = dst->sibling;
  1099. // free dst
  1100. cmDataUnlinkAndFree(dst);
  1101. // update the sibling link to
  1102. if( pp == NULL )
  1103. parent->u.child = src;
  1104. else
  1105. pp->sibling = src;
  1106. src->parent = parent;
  1107. break;
  1108. }
  1109. pp = cp;
  1110. }
  1111. return src;
  1112. }
  1113. unsigned cmDataChildCount( const cmData_t* p )
  1114. {
  1115. if( !cmDataIsStruct(p) )
  1116. return 0;
  1117. unsigned n = 0;
  1118. const cmData_t* cp = p->u.child;
  1119. for(; cp!=NULL; cp=cp->sibling)
  1120. ++n;
  1121. return n;
  1122. }
  1123. cmData_t* cmDataChild( cmData_t* p, unsigned index )
  1124. {
  1125. if( !cmDataIsStruct(p) )
  1126. return NULL;
  1127. unsigned n = 0;
  1128. cmData_t* cp = p->u.child;
  1129. for(; cp!=NULL; cp=cp->sibling)
  1130. {
  1131. if( n == index )
  1132. break;
  1133. ++n;
  1134. }
  1135. return cp;
  1136. }
  1137. cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* p )
  1138. {
  1139. assert( cmDataIsStruct(p) );
  1140. cmDataUnlink(p);
  1141. p->u.child = parent->u.child;
  1142. parent->u.child = p;
  1143. p->parent = parent;
  1144. return p;
  1145. }
  1146. cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p )
  1147. {
  1148. assert( cmDataIsStruct(parent) );
  1149. assert( parent->tid != kRecordDtId || (parent->tid == kRecordDtId && p->tid==kPairDtId));
  1150. cmDataUnlink(p);
  1151. cmData_t* cp = parent->u.child;
  1152. if( cp == NULL )
  1153. parent->u.child = p;
  1154. else
  1155. {
  1156. for(; cp!=NULL; cp=cp->sibling)
  1157. if( cp->sibling == NULL )
  1158. {
  1159. cp->sibling = p;
  1160. break;
  1161. }
  1162. }
  1163. p->parent = parent;
  1164. p->sibling = NULL;
  1165. return p;
  1166. }
  1167. cmData_t* cmDataInsertChild( cmData_t* parent, unsigned index, cmData_t* p )
  1168. {
  1169. if( !cmDataIsStruct(parent) )
  1170. return NULL;
  1171. cmDataUnlink(p);
  1172. unsigned n = 0;
  1173. cmData_t* cp = parent->u.child;
  1174. cmData_t* pp = NULL;
  1175. for(; cp!=NULL; cp=cp->sibling)
  1176. {
  1177. if( n == index )
  1178. {
  1179. if( pp == NULL )
  1180. {
  1181. parent->u.child = p;
  1182. p->sibling = NULL;
  1183. }
  1184. else
  1185. {
  1186. p->sibling = pp->sibling;
  1187. pp->sibling = p;
  1188. }
  1189. break;
  1190. }
  1191. ++n;
  1192. }
  1193. p->parent = parent;
  1194. return p;
  1195. }
  1196. //----------------------------------------------------------------------------
  1197. bool _cmDataPairIsValid( const cmData_t* p )
  1198. {
  1199. assert( p->tid == kPairDtId );
  1200. const cmData_t* cp = p->u.child;
  1201. bool fl = cp->u.child == NULL || cp->u.child->sibling == NULL || cp->u.child->sibling->sibling!=NULL;
  1202. return !fl;
  1203. }
  1204. // Get the key/value of a pair
  1205. cmData_t* cmDataPairKey( cmData_t* p )
  1206. {
  1207. assert( _cmDataPairIsValid(p) );
  1208. return p->u.child;
  1209. }
  1210. unsigned cmDataPairKeyId( cmData_t* p )
  1211. {
  1212. assert( _cmDataPairIsValid(p) );
  1213. unsigned id = cmInvalidId;
  1214. cmDataGetUInt(p->u.child,&id);
  1215. return id;
  1216. }
  1217. const cmChar_t* cmDataPairKeyLabel( cmData_t* p )
  1218. {
  1219. assert( _cmDataPairIsValid(p) );
  1220. const cmChar_t* label = NULL;
  1221. cmDataGetConstStr(p->u.child,&label);
  1222. return label;
  1223. }
  1224. cmData_t* cmDataPairValue( cmData_t* p )
  1225. {
  1226. assert( _cmDataPairIsValid(p) );
  1227. return p->u.child->sibling;
  1228. }
  1229. // Set the key or value of an existing pair node.
  1230. cmData_t* cmDataPairSetValue( cmData_t* p, cmData_t* value )
  1231. {
  1232. assert( _cmDataPairIsValid(p) );
  1233. cmDataReplace( cmDataPairValue(p), value );
  1234. return p;
  1235. }
  1236. cmData_t* cmDataPairSetKey( cmData_t* p, cmData_t* key )
  1237. {
  1238. assert( _cmDataPairIsValid(p) );
  1239. cmDataReplace( cmDataPairValue(p), key );
  1240. return p;
  1241. }
  1242. cmData_t* cmDataPairSetKeyId( cmData_t* p, unsigned id )
  1243. {
  1244. assert( _cmDataPairIsValid(p) );
  1245. cmDataSetUInt(p->u.child,id);
  1246. return p;
  1247. }
  1248. cmData_t* cmDataPairSetKeyLabel( cmData_t* p, const cmChar_t* label )
  1249. {
  1250. assert( _cmDataPairIsValid(p) );
  1251. cmDataSetConstStrAlloc(p->u.child,label);
  1252. return p;
  1253. }
  1254. cmData_t* cmDataMakePair( cmData_t* parent, cmData_t* p, cmData_t* key, cmData_t* value )
  1255. {
  1256. _cmDataFree(p);
  1257. p->tid = kPairDtId;
  1258. p->parent = parent;
  1259. p->flags = 0;
  1260. p->u.child = NULL;
  1261. cmDataAppendChild(p,key);
  1262. cmDataAppendChild(p,value);
  1263. return p;
  1264. }
  1265. // Dynamically allocate a pair node
  1266. cmData_t* cmDataAllocPair( cmData_t* parent, const cmData_t* key, const cmData_t* value )
  1267. {
  1268. cmData_t* p = _cmDataAllocNode(parent,kPairDtId);
  1269. cmData_t* kp = cmDataDupl(key);
  1270. cmData_t* vp = cmDataDupl(value);
  1271. cmDataPrependChild(p,vp);
  1272. cmDataPrependChild(p,kp);
  1273. return p;
  1274. }
  1275. cmData_t* cmDataAllocPairId(cmData_t* parent, unsigned keyId, cmData_t* value )
  1276. {
  1277. cmData_t* p = _cmDataAllocNode(parent,kPairDtId);
  1278. cmDataAllocUInt(p,keyId);
  1279. cmDataAppendChild(p,value);
  1280. return p;
  1281. }
  1282. cmData_t* cmDataAllocPairLabel( cmData_t* parent, const cmChar_t *label, cmData_t* value )
  1283. {
  1284. cmData_t* p = _cmDataAllocNode(parent,kPairDtId);
  1285. cmDataConstStrAlloc(p,label);
  1286. cmDataAppendChild(p,value);
  1287. return p;
  1288. }
  1289. cmData_t* cmDataAllocPairLabelN(cmData_t* parent, const cmChar_t* label, unsigned charCnt, cmData_t* value)
  1290. {
  1291. cmData_t* p = _cmDataAllocNode(parent,kPairDtId);
  1292. cmDataConstStrAllocN(p,label,charCnt);
  1293. cmDataAppendChild(p,value);
  1294. return p;
  1295. }
  1296. //----------------------------------------------------------------------------
  1297. unsigned cmDataListCount(const cmData_t* p )
  1298. { return cmDataChildCount(p); }
  1299. cmData_t* cmDataListEle( cmData_t* p, unsigned index )
  1300. { return cmDataChild(p,index); }
  1301. cmData_t* cmDataListMake( cmData_t* parent, cmData_t* p )
  1302. {
  1303. _cmDataFree(p);
  1304. p->parent = parent;
  1305. p->tid = kListDtId;
  1306. p->flags = 0;
  1307. p->u.child = NULL;
  1308. return p;
  1309. }
  1310. cmData_t* cmDataListAlloc( cmData_t* parent)
  1311. { return _cmDataAllocNode(parent,kListDtId); }
  1312. cmDtRC_t _cmDataParseArgV( cmData_t* parent, va_list vl, cmData_t** vpp )
  1313. {
  1314. cmDtRC_t rc = kOkDtRC;
  1315. cmData_t* vp = NULL;
  1316. unsigned tid = va_arg(vl,unsigned);
  1317. switch(tid)
  1318. {
  1319. case kInvalidDtId: rc = kEolDtRC; break;
  1320. case kNullDtId: vp = cmDataAllocNull(parent); break;
  1321. case kUCharDtId: vp = cmDataAllocUChar( parent,va_arg(vl,int)); break;
  1322. case kCharDtId: vp = cmDataAllocChar( parent,va_arg(vl,int)); break;
  1323. case kUShortDtId: vp = cmDataAllocUShort( parent,va_arg(vl,int)); break;
  1324. case kShortDtId: vp = cmDataAllocShort( parent,va_arg(vl,int)); break;
  1325. case kUIntDtId: vp = cmDataAllocUInt( parent,va_arg(vl,unsigned int)); break;
  1326. case kIntDtId: vp = cmDataAllocInt( parent,va_arg(vl,int)); break;
  1327. case kULongDtId: vp = cmDataAllocULong( parent,va_arg(vl,unsigned long)); break;
  1328. case kLongDtId: vp = cmDataAllocLong( parent,va_arg(vl,long)); break;
  1329. case kFloatDtId: vp = cmDataAllocFloat( parent,va_arg(vl,double)); break;
  1330. case kDoubleDtId: vp = cmDataAllocDouble( parent,va_arg(vl,double)); break;
  1331. case kStrDtId: vp = cmDataStrAlloc( parent,va_arg(vl,cmChar_t*)); break;
  1332. case kConstStrDtId: vp = cmDataConstStrAlloc( parent,va_arg(vl,const cmChar_t*)); break;
  1333. case kUCharPtrDtId:
  1334. {
  1335. unsigned char* p = va_arg(vl,unsigned char*);
  1336. vp = cmDataUCharAllocPtr(parent, p, va_arg(vl,unsigned));
  1337. }
  1338. break;
  1339. case kCharPtrDtId:
  1340. {
  1341. char* p = va_arg(vl,char*);
  1342. vp = cmDataCharAllocPtr(parent, p, va_arg(vl,unsigned));
  1343. }
  1344. break;
  1345. case kUShortPtrDtId:
  1346. {
  1347. unsigned short* p = va_arg(vl,unsigned short*);
  1348. vp = cmDataUShortAllocPtr(parent, p, va_arg(vl,unsigned));
  1349. }
  1350. break;
  1351. case kShortPtrDtId:
  1352. {
  1353. short* p = va_arg(vl,short*);
  1354. vp = cmDataShortAllocPtr(parent, p, va_arg(vl,unsigned));
  1355. }
  1356. break;
  1357. case kUIntPtrDtId:
  1358. {
  1359. unsigned int* p = va_arg(vl,unsigned int*);
  1360. vp = cmDataUIntAllocPtr(parent, p, va_arg(vl,unsigned));
  1361. }
  1362. break;
  1363. case kIntPtrDtId:
  1364. {
  1365. int * p = va_arg(vl,int*);
  1366. vp = cmDataIntAllocPtr(parent, p, va_arg(vl,unsigned));
  1367. }
  1368. break;
  1369. case kULongPtrDtId:
  1370. {
  1371. unsigned long* p = va_arg(vl,unsigned long*);
  1372. vp = cmDataULongAllocPtr(parent, p, va_arg(vl,unsigned));
  1373. }
  1374. break;
  1375. case kLongPtrDtId:
  1376. {
  1377. long* p = va_arg(vl,long*);
  1378. vp = cmDataLongAllocPtr(parent, p, va_arg(vl,unsigned));
  1379. }
  1380. break;
  1381. case kFloatPtrDtId:
  1382. {
  1383. float* p = va_arg(vl,float*);
  1384. vp = cmDataFloatAllocPtr(parent, p, va_arg(vl,unsigned));
  1385. }
  1386. break;
  1387. case kDoublePtrDtId:
  1388. {
  1389. double* p = va_arg(vl,double*);
  1390. vp = cmDataDoubleAllocPtr(parent,p, va_arg(vl,unsigned));
  1391. }
  1392. break;
  1393. case kVoidPtrDtId:
  1394. {
  1395. void* p = va_arg(vl,void*);
  1396. vp = cmDataVoidAllocPtr(parent, p, va_arg(vl,unsigned));
  1397. }
  1398. break;
  1399. case kListDtId:
  1400. case kPairDtId:
  1401. case kRecordDtId:
  1402. vp = _cmDataAllocNode(parent,tid);
  1403. break;
  1404. default:
  1405. _cmDataSetError(kVarArgErrDtRC);
  1406. break;
  1407. }
  1408. *vpp = vp;
  1409. return rc;
  1410. }
  1411. cmData_t* _cmDataListParseV(cmData_t* parent, va_list vl )
  1412. {
  1413. cmData_t* p = NULL;
  1414. bool contFl = true;
  1415. while( contFl )
  1416. {
  1417. cmData_t* vp;
  1418. cmDtRC_t rc = _cmDataParseArgV(parent, vl, &vp);
  1419. if(rc != kOkDtRC || cmDataAppendChild(parent,vp)==NULL )
  1420. contFl = false;
  1421. }
  1422. return p;
  1423. }
  1424. cmData_t* cmDataListAllocV(cmData_t* parent, va_list vl )
  1425. {
  1426. cmData_t* p = cmDataListAlloc(parent);
  1427. _cmDataListParseV(p, vl );
  1428. return p;
  1429. }
  1430. cmData_t* cmDataListAllocA(cmData_t* parent, ... )
  1431. {
  1432. va_list vl;
  1433. va_start(vl,parent);
  1434. cmData_t* p = cmDataListAllocV(parent,vl);
  1435. va_end(vl);
  1436. return p;
  1437. }
  1438. cmData_t* cmDataListAppendEle( cmData_t* p, cmData_t* ele )
  1439. {
  1440. assert(p->tid == kListDtId);
  1441. return cmDataAppendChild(p,ele);
  1442. }
  1443. cmData_t* cmDataListAppendEleN(cmData_t* p, cmData_t* ele[], unsigned n )
  1444. {
  1445. assert(p->tid == kListDtId);
  1446. cmData_t* rp = NULL;
  1447. unsigned i;
  1448. for(i=0; i<n; ++i)
  1449. {
  1450. cmData_t* ep = cmDataAppendChild(p,ele[i]);
  1451. if( rp == NULL )
  1452. rp = ep;
  1453. }
  1454. return rp;
  1455. }
  1456. cmDtRC_t cmDataListAppendV( cmData_t* p, va_list vl )
  1457. {
  1458. if( _cmDataListParseV(p, vl ) == NULL )
  1459. return _cmDataErrNo;
  1460. return kOkDtRC;
  1461. }
  1462. cmDtRC_t cmDataListAppend( cmData_t* p, ... )
  1463. {
  1464. va_list vl;
  1465. va_start(vl,p);
  1466. cmDtRC_t rc = cmDataListAppendV(p,vl);
  1467. va_end(vl);
  1468. return rc;
  1469. }
  1470. cmData_t* cmDataListInsertEle( cmData_t* p, unsigned index, cmData_t* ele )
  1471. { return cmDataInsertChild(p,index,ele); }
  1472. cmData_t* cmDataListInsertEleN(cmData_t* p, unsigned index, cmData_t* ele[], unsigned n )
  1473. {
  1474. unsigned i;
  1475. for(i=0; i<n; ++i)
  1476. cmDataListInsertEle(p,index+i,ele[i]);
  1477. return p;
  1478. }
  1479. //----------------------------------------------------------------------------
  1480. unsigned cmDataRecdCount( const cmData_t* p )
  1481. {
  1482. assert( p->tid == kRecordDtId );
  1483. return cmDataChildCount(p);
  1484. }
  1485. cmData_t* cmDataRecdEle( cmData_t* p, unsigned index )
  1486. {
  1487. assert( p->tid == kRecordDtId );
  1488. cmData_t* cp = cmDataChild(p,index);
  1489. assert( p->tid == kPairDtId );
  1490. return cp;
  1491. }
  1492. cmData_t* cmDataRecdValueFromIndex( cmData_t* p, unsigned index )
  1493. {
  1494. assert( p->tid == kRecordDtId );
  1495. cmData_t* cp = cmDataChild(p,index);
  1496. assert( p->tid == kPairDtId );
  1497. return cmDataPairValue(cp);
  1498. }
  1499. cmData_t* cmDataRecdValueFromId( cmData_t* p, unsigned id )
  1500. {
  1501. assert( p->tid == kRecordDtId );
  1502. cmData_t* cp = p->u.child;
  1503. for(; cp!=NULL; cp=cp->sibling)
  1504. if( cmDataPairKeyId(cp) == id )
  1505. break;
  1506. assert( cp!=NULL && cp->tid==kPairDtId );
  1507. return cmDataPairValue(cp);
  1508. }
  1509. cmData_t* cmDataRecdValueFromLabel( cmData_t* p, const cmChar_t* label )
  1510. {
  1511. assert( p->tid == kRecordDtId );
  1512. cmData_t* cp = p->u.child;
  1513. for(; cp!=NULL; cp=cp->sibling)
  1514. {
  1515. const cmChar_t* lp = cmDataPairKeyLabel(cp);
  1516. if( lp!=NULL && strcmp(lp,label)==0 )
  1517. break;
  1518. }
  1519. assert( cp!=NULL && cp->tid==kPairDtId );
  1520. return cmDataPairValue(cp);
  1521. }
  1522. cmData_t* cmDataRecdKey( cmData_t* p, unsigned index )
  1523. {
  1524. assert( p->tid == kRecordDtId );
  1525. cmData_t* cp = cmDataChild(p,index);
  1526. assert( p->tid == kPairDtId );
  1527. return cmDataPairKey(cp);
  1528. }
  1529. unsigned cmDataRecdKeyId( cmData_t* p, unsigned index )
  1530. {
  1531. cmData_t* kp = cmDataRecdKey(p,index);
  1532. unsigned id = cmInvalidId;
  1533. cmDataGetUInt(kp,&id);
  1534. return id;
  1535. }
  1536. const cmChar_t* cmDataRecdKeyLabel( cmData_t* p, unsigned index )
  1537. {
  1538. cmData_t* kp = cmDataRecdKey(p,index);
  1539. const cmChar_t* label = NULL;
  1540. cmDataGetConstStr(kp,&label);
  1541. return label;
  1542. }
  1543. cmData_t* cmDataRecdMake( cmData_t* parent, cmData_t* p )
  1544. {
  1545. _cmDataFree(p);
  1546. p->parent = parent;
  1547. p->tid = kRecordDtId;
  1548. p->flags = 0;
  1549. p->u.child = NULL;
  1550. return p;
  1551. }
  1552. cmData_t* cmDataRecdAlloc(cmData_t* parent)
  1553. { return _cmDataAllocNode(parent,kRecordDtId); }
  1554. cmData_t* cmDataRecdAppendPair( cmData_t* p, cmData_t* pair )
  1555. {
  1556. assert( p!=NULL && p->tid==kRecordDtId);
  1557. cmDataAppendChild(p,pair);
  1558. return p;
  1559. }
  1560. cmDtRC_t _cmDataRecdParseInputV(cmData_t* parent, unsigned idFl, va_list vl )
  1561. {
  1562. assert( parent != NULL && parent->tid == kRecordDtId );
  1563. bool contFl = true;
  1564. cmDtRC_t rc = kOkDtRC;
  1565. // for each record field
  1566. while( contFl )
  1567. {
  1568. cmData_t* vp = NULL;
  1569. unsigned id = cmInvalidId;
  1570. const cmChar_t* label = NULL;
  1571. // parse the field idenfier
  1572. if( idFl )
  1573. id = va_arg(vl,unsigned); // numeric field identifier
  1574. else
  1575. label = va_arg(vl,const char*); // text field label identifier
  1576. // validate the field identifier
  1577. if( (idFl && id==kInvalidDtId) || (!idFl && label==NULL) )
  1578. break;
  1579. // parse the field data
  1580. if((rc =_cmDataParseArgV( NULL, vl, &vp )) != kOkDtRC )
  1581. {
  1582. contFl = false;
  1583. }
  1584. else
  1585. {
  1586. // create the field pair
  1587. if( idFl )
  1588. cmDataAllocPairId(parent,id,vp);
  1589. else
  1590. cmDataAllocPairLabel(parent,label,vp);
  1591. }
  1592. }
  1593. return rc;
  1594. }
  1595. cmData_t* cmDataRecdAllocLabelV( cmData_t* parent, va_list vl )
  1596. {
  1597. cmData_t* p = cmDataRecdAlloc(parent);
  1598. cmDtRC_t rc = _cmDataRecdParseInputV(p, false, vl );
  1599. if( rc != kOkDtRC )
  1600. {
  1601. cmDataFree(p);
  1602. p = NULL;
  1603. }
  1604. return p;
  1605. }
  1606. cmData_t* cmDataRecdAllocLabelA( cmData_t* parent, ... )
  1607. {
  1608. va_list vl;
  1609. va_start(vl,parent);
  1610. cmData_t* p = cmDataRecdAllocLabelV(parent,vl);
  1611. va_end(vl);
  1612. return p;
  1613. }
  1614. cmData_t* cmDataRecdAllocIdV( cmData_t* parent, va_list vl )
  1615. {
  1616. cmData_t* p = cmDataRecdAlloc(parent);
  1617. cmDtRC_t rc = _cmDataRecdParseInputV(p, true, vl );
  1618. if( rc != kOkDtRC )
  1619. {
  1620. cmDataFree(p);
  1621. p = NULL;
  1622. }
  1623. return p;
  1624. }
  1625. cmData_t* cmDataRecdAllocIdA( cmData_t* parent, ... )
  1626. {
  1627. va_list vl;
  1628. va_start(vl,parent);
  1629. cmData_t* p = cmDataRecdAllocIdV(parent,vl);
  1630. va_end(vl);
  1631. return p;
  1632. }
  1633. cmDtRC_t _cmDataRecdParseV(cmData_t* p, bool idFl, cmErr_t* err, unsigned errRC, va_list vl )
  1634. {
  1635. bool contFl = true;
  1636. cmDtRC_t rc = kOkDtRC;
  1637. while( contFl )
  1638. {
  1639. unsigned id;
  1640. const char* label;
  1641. // parse the field idenfier
  1642. if( idFl )
  1643. id = va_arg(vl,unsigned); // numeric field identifier
  1644. else
  1645. label = va_arg(vl,const char*); // text field label identifier
  1646. // validate the field identifier
  1647. if( (idFl && id==kInvalidDtId) || (!idFl && label==NULL) )
  1648. break;
  1649. cmDataFmtId_t typeId = va_arg(vl,unsigned);
  1650. void* v = va_arg(vl,void*);
  1651. cmData_t* np = NULL;
  1652. bool optFl = cmIsFlag(typeId,kOptArgDtFl);
  1653. cmDtRC_t rc0 = kOkDtRC;
  1654. typeId = cmClrFlag(typeId,kOptArgDtFl);
  1655. if( idFl )
  1656. np = cmDataRecdValueFromLabel( p, label );
  1657. else
  1658. np = cmDataRecdValueFromId( p, id );
  1659. // if the field was not found
  1660. if( np == NULL )
  1661. {
  1662. if(optFl)
  1663. continue;
  1664. if( err != NULL )
  1665. {
  1666. if( idFl )
  1667. cmErrMsg(err,errRC,"The required field (id=%i) was not found.",id);
  1668. else
  1669. cmErrMsg(err,errRC,"The required field '%s' was not found.",cmStringNullGuard(label));
  1670. }
  1671. rc = kMissingFieldDtRC;
  1672. }
  1673. switch(typeId)
  1674. {
  1675. case kNullDtId:
  1676. break;
  1677. case kUCharDtId:
  1678. rc0 = cmDataGetUChar(np,(unsigned char*)v);
  1679. break;
  1680. case kCharDtId:
  1681. rc0 = cmDataGetChar(np,(char*)v);
  1682. break;
  1683. case kUShortDtId:
  1684. rc0 = cmDataGetUShort(np,(unsigned short*)v);
  1685. break;
  1686. case kShortDtId:
  1687. rc0 = cmDataGetShort(np,(short*)v);
  1688. break;
  1689. case kUIntDtId:
  1690. rc0 = cmDataGetUInt(np,(unsigned int*)v);
  1691. break;
  1692. case kIntDtId:
  1693. rc0 = cmDataGetInt(np,(int*)v);
  1694. break;
  1695. case kULongDtId:
  1696. rc0 = cmDataGetULong(np,(unsigned long*)v);
  1697. break;
  1698. case kLongDtId:
  1699. rc0 = cmDataGetLong(np,(long*)v);
  1700. break;
  1701. case kFloatDtId:
  1702. rc0 = cmDataGetFloat(np,(float*)v);
  1703. break;
  1704. case kDoubleDtId:
  1705. rc0 = cmDataGetDouble(np,(double*)v);
  1706. break;
  1707. case kStrDtId:
  1708. rc0 = cmDataGetStr(np,(char**)v);
  1709. break;
  1710. case kConstStrDtId:
  1711. rc0 = cmDataGetConstStr(np,(const char**)v);
  1712. break;
  1713. case kUCharPtrDtId:
  1714. rc0 = cmDataGetUCharPtr(np,(unsigned char**)v);
  1715. break;
  1716. case kCharPtrDtId:
  1717. rc0 = cmDataGetCharPtr(np,(char**)v);
  1718. break;
  1719. case kUShortPtrDtId:
  1720. rc0 = cmDataGetUShortPtr(np,(unsigned short**)v);
  1721. break;
  1722. case kShortPtrDtId:
  1723. rc0 = cmDataGetShortPtr(np,(short**)v);
  1724. break;
  1725. case kUIntPtrDtId:
  1726. rc0 = cmDataGetUIntPtr(np,(unsigned int**)v);
  1727. break;
  1728. case kIntPtrDtId:
  1729. rc0 = cmDataGetIntPtr(np,(int**)v);
  1730. break;
  1731. case kULongPtrDtId:
  1732. rc0 = cmDataGetULongPtr(np,(unsigned long**)v);
  1733. break;
  1734. case kLongPtrDtId:
  1735. rc0 = cmDataGetLongPtr(np,(long**)v);
  1736. break;
  1737. case kFloatPtrDtId:
  1738. rc0 = cmDataGetFloatPtr(np,(float**)v);
  1739. break;
  1740. case kDoublePtrDtId:
  1741. rc0 = cmDataGetDoublePtr(np,(double**)v);
  1742. break;
  1743. case kVoidPtrDtId:
  1744. rc0 = cmDataGetVoidPtr(np,(void**)v);
  1745. break;
  1746. case kListDtId:
  1747. case kPairDtId:
  1748. case kRecordDtId:
  1749. if( np->tid != typeId )
  1750. rc0 = _cmDataSetError(kCvtErrDtRC);
  1751. else
  1752. *(cmData_t**)v = np;
  1753. break;
  1754. default:
  1755. rc0 = _cmDataSetError(kVarArgErrDtRC);
  1756. assert(0);
  1757. }
  1758. if( rc0 != kOkDtRC && err!=NULL)
  1759. {
  1760. if( idFl )
  1761. cmErrMsg(err,errRC,"Unable to convert field (id=%i) to requested type.",id);
  1762. else
  1763. cmErrMsg(err,errRC,"Unable to convert field '%s' to requisted type.",cmStringNullGuard(label));
  1764. rc = rc0;
  1765. }
  1766. }
  1767. return rc;
  1768. }
  1769. cmDtRC_t cmDataRecdParseLabelV(cmData_t* p, cmErr_t* err, unsigned errRC, va_list vl )
  1770. { return _cmDataRecdParseV(p,false,err,errRC,vl); }
  1771. cmDtRC_t cmDataRecdParseLabel(cmData_t* p, cmErr_t* err, unsigned errRC, ... )
  1772. {
  1773. va_list vl;
  1774. va_start(vl,errRC);
  1775. cmDtRC_t rc = cmDataRecdParseLabelV(p,err,errRC,vl);
  1776. va_end(vl);
  1777. return rc;
  1778. }
  1779. cmDtRC_t cmDataRecdParseIdV(cmData_t* p, cmErr_t* err, unsigned errRC, va_list vl )
  1780. { return _cmDataRecdParseV(p,true,err,errRC,vl); }
  1781. cmDtRC_t cmDataRecdParseId(cmData_t* p, cmErr_t* err, unsigned errRC, ... )
  1782. {
  1783. va_list vl;
  1784. va_start(vl,errRC);
  1785. cmDtRC_t rc = cmDataRecdParseIdV(p,err,errRC,vl);
  1786. va_end(vl);
  1787. return rc;
  1788. }
  1789. //============================================================================
  1790. //============================================================================
  1791. //============================================================================
  1792. unsigned _cmDataSerializeNodeByteCount( const cmData_t* p )
  1793. {
  1794. unsigned n = 0;
  1795. // all serialized data ele's begin with a cmDataFmtId_t
  1796. n += sizeof(cmDataFmtId_t);
  1797. // arrays then have a count of bytes and structures have a child count
  1798. if( cmDataIsPtr(p) || cmDataIsStruct(p) )
  1799. n += sizeof(unsigned);
  1800. // then the data itself takes a variable number of bytes
  1801. n += _cmDataByteCount(p);
  1802. return n;
  1803. }
  1804. unsigned cmDataSerializeByteCount( const cmData_t* p )
  1805. {
  1806. unsigned bn = 0;
  1807. // if this data type has a child then calculate it's size
  1808. if( kMinStructDtId <= p->tid && p->tid <= kMaxStructDtId && p->u.child != NULL )
  1809. bn = cmDataSerializeByteCount(p->u.child);
  1810. // if this data type has siblings get their type
  1811. cmData_t* dp = p->u.child;
  1812. for(; dp != NULL; dp=dp->sibling )
  1813. bn += cmDataSerializeByteCount(dp->sibling);
  1814. //
  1815. return bn + _cmDataSerializeNodeByteCount(p);
  1816. }
  1817. char* _cmDataSerializeWriteArray( cmData_t* np, char* dp, const char* ep )
  1818. {
  1819. unsigned byteCnt = _cmDataByteCount(np);
  1820. *((unsigned*)dp) = byteCnt;
  1821. dp += sizeof(unsigned);
  1822. memcpy(dp,np->u.vp,byteCnt);
  1823. dp += byteCnt;
  1824. return dp;
  1825. }
  1826. char* _cmDataSerializeWriteStruct( cmData_t* np, char* dp, const char* ep )
  1827. {
  1828. *((unsigned*)dp) = cmDataChildCount(np);
  1829. dp += sizeof(unsigned);
  1830. return dp;
  1831. }
  1832. char* _cmDataSerializeWrite( cmData_t* np, char* dp, const char* ep )
  1833. {
  1834. assert( dp + _cmDataSerializeNodeByteCount(np) <= ep );
  1835. *((cmDataFmtId_t*)dp) = np->tid;
  1836. dp += sizeof(cmDataFmtId_t);
  1837. switch( np->tid )
  1838. {
  1839. case kNullDtId: break;
  1840. case kUCharDtId: *((unsigned char*)dp) = cmDataUChar(np); dp+=sizeof(unsigned char); break;
  1841. case kCharDtId: *((char*)dp) = cmDataChar(np); dp+=sizeof(char); break;
  1842. case kUShortDtId: *((unsigned short*)dp) = cmDataUShort(np); dp+=sizeof(unsigned short); break;
  1843. case kShortDtId: *((short*)dp) = cmDataShort(np); dp+=sizeof(short); break;
  1844. case kUIntDtId: *((unsigned int*)dp) = cmDataUInt(np); dp+=sizeof(unsigned int); break;
  1845. case kIntDtId: *((int*)dp) = cmDataInt(np); dp+=sizeof(int); break;
  1846. case kULongDtId: *((unsigned long*)dp) = cmDataULong(np); dp+=sizeof(unsigned long); break;
  1847. case kLongDtId: *((long*)dp) = cmDataLong(np); dp+=sizeof(long); break;
  1848. case kFloatDtId: *((float*)dp) = cmDataFloat(np); dp+=sizeof(float); break;
  1849. case kDoubleDtId: *((double*)dp) = cmDataDouble(np); dp+=sizeof(double); break;
  1850. case kStrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1851. case kConstStrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1852. case kUCharPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1853. case kCharPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1854. case kUShortPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1855. case kShortPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1856. case kUIntPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1857. case kIntPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1858. case kULongPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1859. case kLongPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1860. case kFloatPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1861. case kDoublePtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1862. case kVoidPtrDtId: dp = _cmDataSerializeWriteArray(np,dp,ep); break;
  1863. case kListDtId: dp = _cmDataSerializeWriteStruct(np,dp,ep); break;
  1864. case kPairDtId: dp = _cmDataSerializeWriteStruct(np,dp,ep); break;
  1865. case kRecordDtId: dp = _cmDataSerializeWriteStruct(np,dp,ep); break;
  1866. default:
  1867. { assert(0); }
  1868. }
  1869. return dp;
  1870. }
  1871. char* _cmDataSerialize( const cmData_t* p, char* buf, const char* ep )
  1872. {
  1873. buf = _cmDataSerializeWrite(p,buf,ep);
  1874. // if this data type has a child then write the child
  1875. if( kMinStructDtId <= p->tid && p->tid <= kMaxStructDtId && p->u.child != NULL )
  1876. buf = _cmDataSerialize(p->u.child,buf,ep);
  1877. // if this data type has siblings then write sibings
  1878. cmData_t* dp = p->u.child;
  1879. for(; dp != NULL; dp=dp->sibling )
  1880. buf = cmDataSerialize(dp->sibling,buf,ep);
  1881. return buf;
  1882. }
  1883. cmDtRC_t cmDataSerialize( const cmData_t* p, void* buf, unsigned bufByteCnt )
  1884. {
  1885. const char* ep = (char*)p + bufByteCnt;
  1886. buf = _cmDataSerialize(p,buf,bufByteCnt);
  1887. assert( buf <= ep );
  1888. return kOkDtRC;
  1889. }
  1890. cmDtRC_t cmDataDeserialize( const void* buf, unsigned bufByteCnt, cmData_t** pp )
  1891. {
  1892. return kOkDtRC;
  1893. }
  1894. //============================================================================
  1895. //============================================================================
  1896. //============================================================================
  1897. enum
  1898. {
  1899. kLCurlyLexTId = kUserLexTId + 1,
  1900. kRCurlyLexTId,
  1901. kLParenLexTId,
  1902. kRParenLexTId,
  1903. kLBrackLexTId,
  1904. kRBrackLexTId,
  1905. kColonLexTId,
  1906. kCommaLexTId,
  1907. };
  1908. typedef struct
  1909. {
  1910. unsigned id;
  1911. const cmChar_t* label;
  1912. } cmDtToken_t;
  1913. cmDtToken_t _cmDtTokenArray[] =
  1914. {
  1915. { kLCurlyLexTId, "{" },
  1916. { kRCurlyLexTId, "}" },
  1917. { kLBrackLexTId, "[" },
  1918. { kRBrackLexTId, "]" },
  1919. { kLParenLexTId, "(" },
  1920. { kRParenLexTId, ")" },
  1921. { kColonLexTId, ":" },
  1922. { kCommaLexTId, "," },
  1923. { kErrorLexTId,""}
  1924. };
  1925. typedef struct
  1926. {
  1927. cmErr_t err;
  1928. cmLexH lexH;
  1929. cmStackH_t stH;
  1930. } cmDataParser_t;
  1931. cmDataParserH_t cmDataParserNullHandle = cmSTATIC_NULL_HANDLE;
  1932. cmDataParser_t* _cmDataParserHandleToPtr( cmDataParserH_t h )
  1933. {
  1934. cmDataParser_t* p = (cmDataParser_t*)h.h;
  1935. assert( p!= NULL );
  1936. return p;
  1937. }
  1938. cmDtRC_t _cmDataParserDestroy( cmDataParser_t* p )
  1939. {
  1940. if( cmLexFinal(&p->lexH) != kOkLexRC )
  1941. cmErrMsg(&p->err,kLexFailDtRC,"Lexer release failed.");
  1942. if( cmStackFree(&p->stH) != kOkStRC )
  1943. cmErrMsg(&p->err,kParseStackFailDtRC,"The data object parser stack release failed.");
  1944. return kOkDtRC;
  1945. }
  1946. cmDtRC_t cmDataParserCreate( cmCtx_t* ctx, cmDataParserH_t* hp )
  1947. {
  1948. cmDtRC_t rc;
  1949. unsigned i;
  1950. if((rc = cmDataParserDestroy(hp)) != kOkDtRC )
  1951. return rc;
  1952. cmDataParser_t* p = cmMemAllocZ(cmDataParser_t,1);
  1953. cmErrSetup(&p->err,&ctx->rpt,"Data Parser");
  1954. if(cmLexIsValid(p->lexH = cmLexInit(NULL,0,0,&ctx->rpt))==false)
  1955. {
  1956. rc = cmErrMsg(&p->err, kLexFailDtRC, "The data object parser lexer create failed.");
  1957. goto errLabel;
  1958. }
  1959. for(i=0; _cmDtTokenArray[i].id != kErrorLexTId; ++i)
  1960. if( cmLexRegisterToken(p->lexH, _cmDtTokenArray[i].id, _cmDtTokenArray[i].label) != kOkLexRC )
  1961. {
  1962. rc = cmErrMsg(&p->err,kLexFailDtRC,"The data object parser lexer could not register the '%s' token.",_cmDtTokenArray[i].label);
  1963. goto errLabel;
  1964. }
  1965. if( cmStackAlloc(ctx, &p->stH, 1024, 1024, sizeof(cmData_t*)) != kOkStRC )
  1966. {
  1967. rc = cmErrMsg(&p->err,kParseStackFailDtRC,"The data object parser stack create failed.");
  1968. goto errLabel;
  1969. }
  1970. hp->h = p;
  1971. errLabel:
  1972. if( rc != kOkDtRC )
  1973. _cmDataParserDestroy(p);
  1974. return kOkDtRC;
  1975. }
  1976. cmDtRC_t cmDataParserDestroy( cmDataParserH_t* hp )
  1977. {
  1978. cmDtRC_t rc=kOkDtRC;
  1979. if( hp==NULL || cmDataParserIsValid(*hp)==false )
  1980. return rc;
  1981. cmDataParser_t* p = _cmDataParserHandleToPtr(*hp);
  1982. if((rc = _cmDataParserDestroy(p)) != kOkDtRC )
  1983. return rc;
  1984. hp->h = NULL;
  1985. return kOkDtRC;
  1986. }
  1987. bool cmDataParserIsValid( cmDataParserH_t h )
  1988. { return h.h != NULL; }
  1989. // {
  1990. // id0 : scalar_value
  1991. // id1 : ( heterogenous, array, value )
  1992. // id2 : [ homogeneous array values ]
  1993. // id3 :
  1994. // }
  1995. // flags describing the expected next token
  1996. enum
  1997. {
  1998. kValueExpFl = 0x01,
  1999. kIdExpFl = 0x02,
  2000. kColonExpFl = 0x04,
  2001. kCommaExpFl = 0x08
  2002. };
  2003. typedef struct
  2004. {
  2005. cmData_t* dp;
  2006. } cmDataStEle_t;
  2007. typedef struct
  2008. {
  2009. cmDataParser_t* p;
  2010. cmData_t* cnp;
  2011. unsigned flags;
  2012. cmChar_t* tmpStr;
  2013. unsigned arrayCnt;
  2014. void* arrayMem;
  2015. } cmDataParserCtx_t;
  2016. cmDtRC_t _cmDpSyntaxErrV( cmDataParserCtx_t* c, const cmChar_t* fmt, va_list vl )
  2017. {
  2018. cmChar_t* s0 = NULL;
  2019. s0 = cmTsVPrintfP(s0,fmt,vl);
  2020. cmDtRC_t rc = cmErrMsg(&c->p->err,kSyntaxErrDtRC,"Syntax error on line %i. %s",cmLexCurrentLineNumber(c->p->lexH),cmStringNullGuard(s0));
  2021. cmMemFree(s0);
  2022. return rc;
  2023. }
  2024. cmDtRC_t _cmDpSyntaxErr( cmDataParserCtx_t* c, const cmChar_t* fmt, ... )
  2025. {
  2026. va_list vl;
  2027. va_start(vl,fmt);
  2028. cmDtRC_t rc = _cmDpSyntaxErrV(c,fmt,vl);
  2029. va_end(vl);
  2030. return rc;
  2031. }
  2032. cmDtRC_t _cmDpPopStack( cmDataParserCtx_t* c, cmData_t** pp )
  2033. {
  2034. const void* vp;
  2035. if((vp = cmStackTop(c->p->stH)) == NULL )
  2036. return _cmDpSyntaxErr(c,"Stack underflow.");
  2037. *pp = *(cmData_t**)vp;
  2038. return kOkDtRC;
  2039. }
  2040. cmDtRC_t _cmDpStoreArrayEle( cmDataParserCtx_t* c, void* dp, unsigned byteCnt )
  2041. {
  2042. char* vp = cmMemResize(char, c->cnp->u.vp, c->cnp->cnt+byteCnt);
  2043. memcpy(vp + c->cnp->cnt,dp,byteCnt);
  2044. c->cnp->u.vp = vp;
  2045. c->cnp->cnt += byteCnt;
  2046. c->flags = kValueExpFl | kCommaExpFl;
  2047. return kOkDtRC;
  2048. }
  2049. cmDtRC_t _cmDataParserOpenPair( cmDataParserCtx_t* c )
  2050. {
  2051. assert( c->cnp->tid == kRecordDtId );
  2052. cmData_t* nnp = cmDataAllocNull(NULL);
  2053. cmData_t* pnp = cmDataAllocPairLabelN( c->cnp, cmLexTokenText(c->p->lexH), cmLexTokenCharCount(c->p->lexH), nnp );
  2054. // store the current node
  2055. if( cmStackPush(c->p->stH, &c->cnp, 1 ) != kOkStRC )
  2056. return _cmDpSyntaxErr(c,"Parser stack push failed.");
  2057. // make the new pair the current node
  2058. c->cnp = pnp;
  2059. // pair openings must be followed by a colon.
  2060. c->flags = kColonExpFl;
  2061. return kOkDtRC;
  2062. }
  2063. cmDtRC_t _cmDataParserClosePair( cmDataParserCtx_t* c )
  2064. {
  2065. cmDtRC_t rc;
  2066. // make the pair's parent record the current node
  2067. if((rc = _cmDpPopStack(c, &c->cnp )) != kOkDtRC )
  2068. return rc;
  2069. // pairs only occur in records
  2070. if( c->cnp->tid != kRecordDtId )
  2071. return _cmDpSyntaxErr(c,"A 'pair' end was found outside of a 'record'.");
  2072. // pairs must be followed by id's or comma's
  2073. c->flags = kIdExpFl | kCommaExpFl;
  2074. return rc;
  2075. }
  2076. cmDtRC_t _cmDpStoreValue( cmDataParserCtx_t* c, cmData_t* np, const cmChar_t* typeLabel )
  2077. {
  2078. assert( np != NULL );
  2079. cmDtRC_t rc = kOkDtRC;
  2080. switch( c->cnp->tid )
  2081. {
  2082. case kPairDtId:
  2083. // assign the new node as the value of the pair
  2084. cmDataPairSetValue(c->cnp,np);
  2085. // close the values parent pair
  2086. rc = _cmDataParserClosePair(c);
  2087. break;
  2088. case kListDtId:
  2089. cmDataAppendChild(c->cnp,np);
  2090. c->flags = kValueExpFl;
  2091. break;
  2092. default:
  2093. rc = _cmDpSyntaxErr(c,"A '%s' value was found outside of a valid container.",typeLabel);
  2094. }
  2095. c->flags |= kCommaExpFl;
  2096. return rc;
  2097. }
  2098. cmDtRC_t _cmDataParserReal( cmDataParserCtx_t* c )
  2099. {
  2100. cmDtRC_t rc = kOkDtRC;
  2101. double val = cmLexTokenDouble(c->p->lexH);
  2102. if( c->cnp->tid == kVoidPtrDtId )
  2103. rc = _cmDpStoreArrayEle(c,&val,sizeof(val));
  2104. else
  2105. rc = _cmDpStoreValue(c,cmDataAllocDouble(NULL,val),"real");
  2106. return rc;
  2107. }
  2108. cmDtRC_t _cmDataParserInt( cmDataParserCtx_t* c )
  2109. {
  2110. cmDtRC_t rc = kOkDtRC;
  2111. int val = cmLexTokenInt(c->p->lexH);
  2112. if( c->cnp->tid == kVoidPtrDtId )
  2113. rc = _cmDpStoreArrayEle(c,&val,sizeof(val));
  2114. else
  2115. rc = _cmDpStoreValue(c,cmDataAllocInt(NULL,val),"int");
  2116. return rc;
  2117. }
  2118. cmDtRC_t _cmDataParserString( cmDataParserCtx_t* c )
  2119. {
  2120. // if we are expecting a pair label
  2121. if( cmIsFlag(c->flags,kIdExpFl) )
  2122. return _cmDataParserOpenPair(c);
  2123. // otherwise a 'value' must be expected
  2124. if( cmIsNotFlag(c->flags,kValueExpFl) )
  2125. return _cmDpSyntaxErr(c,"Unexpected string.");
  2126. cmData_t* np = cmDataConstStrAllocN(NULL,cmLexTokenText(c->p->lexH), cmLexTokenCharCount(c->p->lexH));
  2127. return _cmDpStoreValue(c,np,"string");
  2128. }
  2129. cmDtRC_t _cmDataParserOpenRecd( cmDataParserCtx_t* c )
  2130. {
  2131. // records are values - so we must be expecting a value
  2132. if( cmIsFlag(c->flags,kValueExpFl) == false )
  2133. return _cmDpSyntaxErr(c,"Unexpected '{'.");
  2134. // store the current node
  2135. if( cmStackPush(c->p->stH, &c->cnp, 1 ) != kOkStRC )
  2136. return _cmDpSyntaxErr(c,"Parser stack push failed.");
  2137. // alloc a new record and make it the current node
  2138. if( (c->cnp = cmDataRecdAlloc(c->cnp)) == NULL )
  2139. return _cmDpSyntaxErr(c,"'recd' allocate failed.");
  2140. // new records must be followed by an id token.
  2141. c->flags = kIdExpFl;
  2142. return kOkDtRC;
  2143. }
  2144. cmDtRC_t _cmDataParserCloseRecd( cmDataParserCtx_t* c )
  2145. {
  2146. cmDtRC_t rc;
  2147. // make the parent node the new curren node
  2148. if((rc = _cmDpPopStack(c,&c->cnp)) != kOkDtRC )
  2149. return rc;
  2150. switch( c->cnp->tid )
  2151. {
  2152. case kPairDtId:
  2153. // if the parent node is a pair then close it
  2154. rc = _cmDataParserClosePair(c);
  2155. break;
  2156. case kListDtId:
  2157. // parent node is a list - so expect another value
  2158. c->flags = kValueExpFl;
  2159. break;
  2160. default:
  2161. return _cmDpSyntaxErr(c,"'records' may only be contained in other records or heterogenous arrays.");
  2162. }
  2163. c->flags |= kCommaExpFl;
  2164. return rc;
  2165. }
  2166. cmDtRC_t _cmDataParserOpenList( cmDataParserCtx_t* c )
  2167. {
  2168. // lists are values - so we must be expecting a value
  2169. if( cmIsFlag(c->flags,kValueExpFl) == false )
  2170. return _cmDpSyntaxErr(c,"Unexpected '('.");
  2171. // store the current node
  2172. if( cmStackPush(c->p->stH, &c->cnp, 1 ) != kOkStRC )
  2173. return _cmDpSyntaxErr(c,"Parser stack push failed.");
  2174. // create a new list
  2175. if( (c->cnp = cmDataListAlloc(c->cnp)) == NULL )
  2176. return _cmDpSyntaxErr(c,"'list' allocate failed.");
  2177. // new lists must be followed by a value
  2178. c->flags = kValueExpFl;
  2179. return kOkDtRC;
  2180. }
  2181. cmDtRC_t _cmDataParserCloseList( cmDataParserCtx_t* c )
  2182. {
  2183. cmDtRC_t rc;
  2184. // make the list's parent the current node
  2185. if((rc = _cmDpPopStack(c,&c->cnp)) != kOkDtRC )
  2186. return rc;
  2187. switch( c->cnp->tid )
  2188. {
  2189. case kPairDtId:
  2190. // if the list's parent is a pair then close it
  2191. rc = _cmDataParserClosePair(c);
  2192. break;
  2193. case kListDtId:
  2194. // the list's parent is another list so expect a value
  2195. c->flags = kValueExpFl;
  2196. break;
  2197. default:
  2198. return _cmDpSyntaxErr(c,"'lists' may only be contained in other records or lists.");
  2199. }
  2200. c->flags |= kCommaExpFl;
  2201. return rc;
  2202. }
  2203. cmDtRC_t _cmDataParserOpenArray( cmDataParserCtx_t* c )
  2204. {
  2205. // arrays are values - so we must be expecting a value
  2206. if( cmIsFlag(c->flags,kValueExpFl) == false )
  2207. return _cmDpSyntaxErr(c,"Unexpected '('.");
  2208. // store the current node
  2209. if( cmStackPush(c->p->stH, &c->cnp, 1 ) != kOkStRC )
  2210. return _cmDpSyntaxErr(c,"Parser stack push failed.");
  2211. // create a new array
  2212. if( (c->cnp = cmDataSetVoidAllocPtr(c->cnp, NULL, 0 )) == NULL )
  2213. return _cmDpSyntaxErr(c,"'array' allocate failed.");
  2214. // new arrays must be followed by a value
  2215. c->flags = kValueExpFl;
  2216. return kOkDtRC;
  2217. }
  2218. cmDtRC_t _cmDataParserCloseArray( cmDataParserCtx_t* c )
  2219. {
  2220. cmDtRC_t rc;
  2221. // make the arrays parent the current node
  2222. if((rc = _cmDpPopStack(c,&c->cnp)) != kOkDtRC )
  2223. return rc;
  2224. switch( c->cnp->tid )
  2225. {
  2226. case kPairDtId:
  2227. // the arrays parent is a pair - so close it
  2228. rc = _cmDataParserClosePair(c);
  2229. break;
  2230. case kListDtId:
  2231. // the arrays parent is a list - so expect a value
  2232. c->flags = kValueExpFl;
  2233. break;
  2234. default:
  2235. return _cmDpSyntaxErr(c,"'arrays' may only be contained in other records or lists.");
  2236. }
  2237. c->flags |= kCommaExpFl;
  2238. return rc;
  2239. }
  2240. cmDtRC_t _cmDataParserOnColon( cmDataParserCtx_t* c )
  2241. {
  2242. // colons only follow field identifiers and are always followed by values.
  2243. if( cmIsFlag(c->flags,kColonExpFl) == false )
  2244. return _cmDpSyntaxErr(c,"Unexpected colon.");
  2245. c->flags = kValueExpFl;
  2246. return kOkDtRC;
  2247. }
  2248. cmDtRC_t _cmDataParserOnComma( cmDataParserCtx_t* c )
  2249. {
  2250. // comma's may be found in three places:
  2251. // 1) following field values
  2252. // 2) between list values
  2253. // 3) between array values
  2254. // comma's are always followed by values
  2255. if( cmIsFlag(c->flags,kCommaExpFl) == false )
  2256. return _cmDpSyntaxErr(c, "Unexpected comma.");
  2257. c->flags = kValueExpFl;
  2258. return kOkDtRC;
  2259. }
  2260. cmDtRC_t cmDataParserExec( cmDataParserH_t h, const cmChar_t* text, cmData_t** pp )
  2261. {
  2262. cmDtRC_t rc = kOkDtRC;
  2263. unsigned tokenId;
  2264. cmDataParserCtx_t ctx;
  2265. cmDataParser_t* p = _cmDataParserHandleToPtr(h);
  2266. ctx.cnp = NULL; // current node ptr
  2267. ctx.p = p;
  2268. ctx.flags = kValueExpFl;
  2269. if( cmLexSetTextBuffer(p->lexH,text,strlen(text)) != kOkLexRC )
  2270. return cmErrMsg(&p->err,kLexFailDtRC,"The data object lexer failed during reset.");
  2271. cmStackClear(p->stH,false);
  2272. while((tokenId = cmLexGetNextToken(p->lexH)) != kEofLexTId )
  2273. {
  2274. switch(tokenId)
  2275. {
  2276. case kRealLexTId: // real number (contains a decimal point or is in scientific notation)
  2277. rc = _cmDataParserReal(&ctx);
  2278. break;
  2279. case kIntLexTId: // decimal integer
  2280. rc = _cmDataParserInt(&ctx);
  2281. break;
  2282. case kHexLexTId: // hexidecimal integer
  2283. rc = _cmDataParserInt(&ctx);
  2284. break;
  2285. case kIdentLexTId: // identifiers are treated as strings
  2286. case kQStrLexTId: // quoted string
  2287. rc = _cmDataParserString(&ctx);
  2288. break;
  2289. case kLCurlyLexTId: // a new record is starting
  2290. rc = _cmDataParserOpenRecd(&ctx);
  2291. break;
  2292. case kRCurlyLexTId: // the current record is finished
  2293. rc = _cmDataParserCloseRecd(&ctx);
  2294. break;
  2295. case kLParenLexTId: // a list is starting
  2296. rc = _cmDataParserOpenList(&ctx);
  2297. break;
  2298. case kRParenLexTId: // a list is finished
  2299. rc = _cmDataParserCloseList(&ctx);
  2300. break;
  2301. case kLBrackLexTId: // an array is starting
  2302. rc = _cmDataParserOpenArray(&ctx);
  2303. break;
  2304. case kRBrackLexTId: // an array is ending
  2305. rc = _cmDataParserCloseArray(&ctx);
  2306. break;
  2307. case kColonLexTId: // the previous id was a field id
  2308. rc = _cmDataParserOnColon(&ctx);
  2309. break;
  2310. case kCommaLexTId: // comma sep. for array or fields
  2311. rc = _cmDataParserOnComma(&ctx);
  2312. break;
  2313. case kBlockCmtLexTId: // block comment
  2314. case kLineCmtLexTId: // line comment
  2315. case kErrorLexTId: // the lexer was unable to identify the current token
  2316. case kUnknownLexTId: // the token is of an unknown type (only used when kReturnUnknownLexFl is set)
  2317. case kEofLexTId: // the lexer reached the end of input
  2318. case kSpaceLexTId: // white space
  2319. {
  2320. rc = cmErrMsg(&p->err,kLexFailDtRC,"The data object lexer failed with an unexpected token '%s' on line '%i'.",cmLexIdToLabel(p->lexH,tokenId),cmLexCurrentLineNumber(p->lexH));
  2321. goto errLabel;
  2322. }
  2323. }
  2324. }
  2325. errLabel:
  2326. return rc;
  2327. }
  2328. //============================================================================
  2329. //============================================================================
  2330. //============================================================================
  2331. #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)
  2332. void _cmDataPrintIndent( cmRpt_t* rpt, unsigned indent )
  2333. {
  2334. unsigned j=0;
  2335. for(; j<indent; ++j)
  2336. cmRptPrintf(rpt," ");
  2337. }
  2338. void _cmDataPrint( const cmData_t* p, cmRpt_t* rpt, unsigned indent )
  2339. {
  2340. cmData_t* cp;
  2341. //_cmDataPrintIndent(rpt,indent);
  2342. switch(p->tid)
  2343. {
  2344. case kNullDtId: cmRptPrintf(rpt,"<null>"); break;
  2345. case kUCharDtId: cmRptPrintf(rpt,"%c ",cmDataUChar(p)); break;
  2346. case kCharDtId: cmRptPrintf(rpt,"%c ",cmDataChar(p)); break;
  2347. case kUShortDtId: cmRptPrintf(rpt,"%i ",cmDataUShort(p)); break;
  2348. case kShortDtId: cmRptPrintf(rpt,"%i ",cmDataShort(p)); break;
  2349. case kUIntDtId: cmRptPrintf(rpt,"%i ",cmDataUInt(p)); break;
  2350. case kIntDtId: cmRptPrintf(rpt,"%i ",cmDataInt(p)); break;
  2351. case kULongDtId: cmRptPrintf(rpt,"%i ",cmDataULong(p)); break;
  2352. case kLongDtId: cmRptPrintf(rpt,"%i ",cmDataLong(p)); break;
  2353. case kFloatDtId: cmRptPrintf(rpt,"%f ",cmDataFloat(p)); break;
  2354. case kDoubleDtId: cmRptPrintf(rpt,"%f ",cmDataDouble(p)); break;
  2355. case kStrDtId: cmRptPrintf(rpt,"%s ",cmDataStr(p)); break;
  2356. case kConstStrDtId: cmRptPrintf(rpt,"%s ",cmDataConstStr(p)); break;
  2357. case kUCharPtrDtId: parr(rpt,"%c ",cmDataUCharPtr(p), p->cnt); break;
  2358. case kCharPtrDtId: parr(rpt,"%c ",cmDataCharPtr(p), p->cnt); break;
  2359. case kUShortPtrDtId: parr(rpt,"%i ",cmDataUShortPtr(p),p->cnt); break;
  2360. case kShortPtrDtId: parr(rpt,"%i ",cmDataShortPtr(p), p->cnt); break;
  2361. case kUIntPtrDtId: parr(rpt,"%i ",cmDataUIntPtr(p), p->cnt); break;
  2362. case kIntPtrDtId: parr(rpt,"%i ",cmDataIntPtr(p), p->cnt); break;
  2363. case kULongPtrDtId: parr(rpt,"%i ",cmDataULongPtr(p), p->cnt); break;
  2364. case kLongPtrDtId: parr(rpt,"%i ",cmDataLongPtr(p), p->cnt); break;
  2365. case kFloatPtrDtId: parr(rpt,"%f ",cmDataFloatPtr(p), p->cnt); break;
  2366. case kDoublePtrDtId: parr(rpt,"%f ",cmDataDoublePtr(p),p->cnt); break;
  2367. case kVoidPtrDtId: cmRptPrintf(rpt,"<void:%i>",p->cnt); break;
  2368. case kPairDtId:
  2369. _cmDataPrint(p->u.child,rpt,indent);
  2370. cmRptPrintf(rpt," : ");
  2371. _cmDataPrint(p->u.child->sibling,rpt,indent);
  2372. cmRptPrintf(rpt,"\n");
  2373. break;
  2374. case kListDtId:
  2375. cmRptPrintf(rpt,"(\n");
  2376. indent += 2;
  2377. cp = p->u.child;
  2378. for(; cp!=NULL; cp=cp->sibling)
  2379. {
  2380. _cmDataPrintIndent(rpt,indent);
  2381. _cmDataPrint(cp,rpt,indent);
  2382. cmRptPrintf(rpt,"\n");
  2383. }
  2384. indent -= 2;
  2385. _cmDataPrintIndent(rpt,indent);
  2386. cmRptPrintf(rpt,")\n");
  2387. break;
  2388. case kRecordDtId:
  2389. cmRptPrintf(rpt,"{\n");
  2390. indent += 2;
  2391. cp = p->u.child;
  2392. for(; cp!=NULL; cp=cp->sibling)
  2393. {
  2394. _cmDataPrintIndent(rpt,indent);
  2395. _cmDataPrint(cp,rpt, indent);
  2396. }
  2397. indent -= 2;
  2398. _cmDataPrintIndent(rpt,indent);
  2399. cmRptPrintf(rpt,"}\n");
  2400. break;
  2401. default:
  2402. break;
  2403. }
  2404. }
  2405. void cmDataPrint( const cmData_t* p, cmRpt_t* rpt )
  2406. { _cmDataPrint(p,rpt,0); }
  2407. cmDtRC_t cmDataParserTest( cmCtx_t* ctx )
  2408. {
  2409. cmDtRC_t rc = kOkDtRC;
  2410. cmDataParserH_t h = cmDataParserNullHandle;
  2411. cmErr_t err;
  2412. cmData_t* dp = NULL;
  2413. const cmChar_t text[] =
  2414. {
  2415. "{ f0:1.23 f1:\"hey\" "
  2416. };
  2417. cmErrSetup(&err,&ctx->rpt,"Data Parser Tester");
  2418. if((rc = cmDataParserCreate(ctx, &h )) != kOkDtRC )
  2419. {
  2420. rc = cmErrMsg(&err,rc,"Data parser create failed.");
  2421. goto errLabel;
  2422. }
  2423. if( cmDataParserExec(h,text,&dp) != kOkDtRC )
  2424. rc = cmErrMsg(&err,rc,"Data parser exec failed.");
  2425. else
  2426. {
  2427. cmDataPrint(dp,&ctx->rpt);
  2428. }
  2429. errLabel:
  2430. if( cmDataParserDestroy( &h ) != kOkDtRC )
  2431. {
  2432. rc = cmErrMsg(&err,rc,"Data parser destroy failed.");
  2433. goto errLabel;
  2434. }
  2435. cmDataFree(dp);
  2436. return rc;
  2437. }
  2438. void cmDataTest( cmCtx_t* ctx )
  2439. {
  2440. float farr[] = { 1.23, 45.6, 7.89 };
  2441. cmDataParserTest(ctx);
  2442. return;
  2443. cmData_t* d0 = cmDataRecdAllocLabelA(NULL,
  2444. "name",kConstStrDtId,"This is a string.",
  2445. "id", kUIntDtId, 21,
  2446. "real",kFloatDtId, 1.23,
  2447. "arr", kFloatPtrDtId, farr, 3,
  2448. NULL);
  2449. cmDataPrint(d0,&ctx->rpt);
  2450. cmDataFree(d0);
  2451. cmData_t* d1 = cmDataListAllocA(NULL,
  2452. kUIntDtId, 53,
  2453. kStrDtId, "Blah blah",
  2454. kFloatPtrDtId, farr, 3,
  2455. kInvalidDtId );
  2456. cmDataPrint(d1,&ctx->rpt);
  2457. cmDataFree(d1);
  2458. cmRptPrintf(&ctx->rpt,"Done!.\n");
  2459. }