libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmData.c 77KB

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