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

cmData.c 83KB

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