libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. #ifndef cmData_h
  2. #define cmData_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /*
  7. TODO:
  8. 0) Figure out an error handling scheme that does not rely on
  9. a global errno. This is not useful in multi-thread environments.
  10. It might be ok to go with an 'all errors are fatal' model
  11. (except in the var-args functions).
  12. Consider the use of a context object for use with functions
  13. that can have runtime errors or need to allocate memory.
  14. 1) Implement the canConvert and willTruncate functions.
  15. 2) Make a set of cmDataAllocXXXPtr() functions which take
  16. a flag indicating whether or not to dynamically allocate
  17. the array space. This will allow dynamic allocattion to
  18. occur at runtime. Make var args functions for list and
  19. record objects which also take this flag.
  20. Whereever a function may be implemented using
  21. static/dynamic allocation this flag should be present.
  22. (e.g. string allocation for pair labels)
  23. This choice is common enough that it may be worth
  24. suffixing function names with a capital letter to
  25. be clear what the functions memory policy is.
  26. 3) Come up with a var-args format which allows a
  27. hierchy of records to be defined in one line.
  28. 4) Implement the serialization functions.
  29. 5) Implement an ascii string/parse format for writing/reading.
  30. 6) Implement fast lookup of record fields.
  31. 7) Allow for user defined types. For example a 'matrix'
  32. data type. This might be as simple as adding an extra 'user_tid'
  33. field to cmData_t.
  34. 8) Implement type specific cmDataGetRecordValueXXX() functions.
  35. 9) Implement cmDataIsEqual(), cmDataIsLtE(), ...
  36. */
  37. enum
  38. {
  39. kOkDtRC = cmOkRC,
  40. kAssertErrDtRC,
  41. kConstErrDtRC,
  42. kCvtErrDtRC,
  43. kInvalidContDtRC,
  44. kInvalidTypeDtRC,
  45. kMissingFieldDtRC,
  46. kLexFailDtRC,
  47. kParseStackFailDtRC,
  48. kSyntaxErrDtRC,
  49. kEolDtRC
  50. };
  51. typedef unsigned cmDtRC_t;
  52. typedef enum
  53. {
  54. kInvalidTypeDtId,// 0
  55. kNullDtId, // 1 the data object exists but it has no data
  56. kBoolDtId, // 2
  57. kUCharDtId, // 3
  58. kCharDtId, // 4
  59. kUShortDtId, // 5
  60. kShortDtId, // 6
  61. kUIntDtId, // 7
  62. kIntDtId, // 8
  63. kULongDtId, // 9
  64. kLongDtId, // 10
  65. kFloatDtId, // 11
  66. kDoubleDtId, // 12
  67. kStrDtId, // 13 zero terminated string
  68. kBlobDtId, // 14 application defined raw memory object
  69. kStructDtId, // 15 node is a pair,list, or recd
  70. } cmDataTypeId_t;
  71. typedef enum
  72. {
  73. kInvalidCntDtId = kInvalidTypeDtId,
  74. kScalarDtId = 0x00100000,
  75. kArrayDtId = 0x00200000,
  76. kPairDtId = 0x00400000,
  77. kListDtId = 0x00800000,
  78. kRecordDtId = 0x01000000,
  79. kContainerDtMask = 0x01f00000,
  80. } cmDataContainerId_t;
  81. enum
  82. {
  83. kNoFlagsDtFl = 0x00,
  84. kOptArgDtFl = 0x02000000,
  85. // Indicate that the memory used by the data object
  86. // was dynamically allocated and should be released
  87. // by cmDataFree().
  88. kFreeObjDtFl = 0x04000000,
  89. // Indicate that the memory used by strings, blobs
  90. // and arrays should be freed by cmDataFree().
  91. kFreeValueDtFl = 0x08000000,
  92. // Indicate that the value of the object cannot be changed.
  93. // (but the object could be reassigned as a new type).
  94. kConstValueDtFl = 0x10000000,
  95. // Indicate that the type of the object cannot be changed.
  96. // (but the value may be changed).
  97. kConstObjDtFl = 0x20000000,
  98. // Indicate that the array or string should not be
  99. // internally reallocated but rather the source pointer
  100. // should be taken as the new value of the object.
  101. kNoCopyDtFl = 0x40000000,
  102. kFlagsDtMask = 0x7e000000
  103. };
  104. // The kInvalidDtXXX constants are used to indicate an error when returned
  105. // from the cmDtXXX() functions below.
  106. #define kInvalidDtFloat FLT_MAX
  107. #define kInvalidDtDouble DBL_MAX
  108. enum
  109. {
  110. kInvalidDtChar = 0xff,
  111. kInvalidDtShort = 0xffff,
  112. kInvalidDtInt = 0xffffffff,
  113. kInvalidDtLong = kInvalidDtInt,
  114. kInvalidDtBool = kInvalidDtInt
  115. };
  116. typedef struct cmData_str
  117. {
  118. cmDataTypeId_t tid; // data format id
  119. cmDataContainerId_t cid; // container id
  120. unsigned flags; //
  121. struct cmData_str* parent; // this childs parent
  122. struct cmData_str* sibling; // this childs left sibling
  123. unsigned cnt; // byte cnt for strings/blobs and ele count for arrays
  124. union
  125. {
  126. bool b;
  127. char c;
  128. unsigned char uc;
  129. short s;
  130. unsigned short us;
  131. int i;
  132. unsigned int ui;
  133. long l;
  134. unsigned long ul;
  135. float f;
  136. double d;
  137. cmChar_t* z;
  138. void* vp;
  139. struct cmData_str* child; // first child (list,record,pair)
  140. } u;
  141. } cmData_t;
  142. extern cmData_t cmDataNull;
  143. const cmChar_t* cmDataTypeToLabel( cmDataTypeId_t tid );
  144. cmDataTypeId_t cmDataLabelToType( const cmChar_t* typeLabelStr );
  145. // Returns 1 for kStrDtId.
  146. // Returns cmInvalidCnt if tid is not recognized.
  147. unsigned dmDataByteWidth( cmDataTypeId_t tid );
  148. const cmChar_t* cmDataContainerIdToLabel( cmDataContainerId_t tid );
  149. cmDataContainerId_t cmDataLabelToContainerId( const cmChar_t* contLabelStr );
  150. bool cmDataIsConstObj( const cmData_t* d );
  151. void cmDataEnableConstObj( cmData_t* d, bool enaFl );
  152. bool cmDataIsConstValue( const cmData_t* d );
  153. void cmDataEnableConstValue( cmData_t* d, bool enaFl );
  154. bool cmDataIsFreeValue( const cmData_t* d );
  155. void cmDataEnableFreeValue( cmData_t* d, bool enaFl );
  156. // Returns true if this is a scalar or array node.
  157. bool cmDataIsLeaf( const cmData_t* d);
  158. // Return true if this is NOT a scalar or array node.
  159. bool cmDataIsStruct( const cmData_t* d );
  160. //----------------------------------------------------------------------------
  161. // Scalar related functions
  162. //
  163. // Dynamically allocate a scalar object and set it's value.
  164. // The 'flags' argument may include kConstValueDtFl and kConstObjDtFl.
  165. // The string and blob constructors may also use the
  166. // kNoCopyDtFl and the kFreeValueDtFl.
  167. // Generic:
  168. // 'byteCnt' is ignored for all types other than strings and blobs.
  169. cmDtRC_t cmDataNewScalar( cmData_t* parent, cmDataTypeId_t tid, unsigned flags, void* vp, unsigned byteCnt, cmData_t** ref );
  170. // Type specific
  171. cmDtRC_t cmDataNewNull( cmData_t* parent, unsigned flags, cmData_t** ref );
  172. cmDtRC_t cmDataNewBool( cmData_t* parent, unsigned flags, bool v, cmData_t** ref );
  173. cmDtRC_t cmDataNewChar( cmData_t* parent, unsigned flags, char v, cmData_t** ref );
  174. cmDtRC_t cmDataNewUChar( cmData_t* parent, unsigned flags, unsigned char v, cmData_t** ref );
  175. cmDtRC_t cmDataNewShort( cmData_t* parent, unsigned flags, short v, cmData_t** ref );
  176. cmDtRC_t cmDataNewUShort( cmData_t* parent, unsigned flags, unsigned short v, cmData_t** ref );
  177. cmDtRC_t cmDataNewInt( cmData_t* parent, unsigned flags, int v, cmData_t** ref );
  178. cmDtRC_t cmDataNewUInt( cmData_t* parent, unsigned flags, unsigned int v, cmData_t** ref );
  179. cmDtRC_t cmDataNewLong( cmData_t* parent, unsigned flags, long v, cmData_t** ref );
  180. cmDtRC_t cmDataNewULong( cmData_t* parent, unsigned flags, unsigned long v, cmData_t** ref );
  181. cmDtRC_t cmDataNewFloat( cmData_t* parent, unsigned flags, float v, cmData_t** ref );
  182. cmDtRC_t cmDataNewDouble( cmData_t* parent, unsigned flags, double v, cmData_t** ref );
  183. cmDtRC_t cmDataNewStr( cmData_t* parent, unsigned flags, cmChar_t* str, cmData_t** ref );
  184. cmDtRC_t cmDataNewConstStr( cmData_t* parent, unsigned flags, const cmChar_t* str, cmData_t** ref );
  185. cmDtRC_t cmDataNewStrN( cmData_t* parent, unsigned flags, cmChar_t* str, unsigned charCnt, cmData_t** ref );
  186. cmDtRC_t cmDataNewConstStrN(cmData_t* parent, unsigned flags, const cmChar_t* str, unsigned charCnt, cmData_t** ref );
  187. cmDtRC_t cmDataNewBlob( cmData_t* parent, unsigned flags, void* vp, unsigned byteCnt, cmData_t** ref );
  188. cmDtRC_t cmDataNewConstBlob(cmData_t* parent, unsigned flags, const void* vp, unsigned byteCnt, cmData_t** ref );
  189. // Set the value and type of an existing scalar object.
  190. // These functions begin by releasing any resources held by *p
  191. // prior to resetting the type and value of the object.
  192. // The 'flags' argument to cmDataSetStr() and cmDataSetConstStr()
  193. // may use the kNoCopyDtFl and the kFreeValueDtFl
  194. cmDtRC_t cmDataSetScalarValue( cmData_t* d, cmDataTypeId_t tid, void* vp, unsigned byteCnt, unsigned flags );
  195. cmDtRC_t cmDataSetNull( cmData_t* p );
  196. cmDtRC_t cmDataSetBool( cmData_t* p, bool v );
  197. cmDtRC_t cmDataSetChar( cmData_t* p, char v );
  198. cmDtRC_t cmDataSetUChar( cmData_t* p, unsigned char v );
  199. cmDtRC_t cmDataSetShort( cmData_t* p, short v );
  200. cmDtRC_t cmDataSetUShort( cmData_t* p, unsigned short v );
  201. cmDtRC_t cmDataSetInt( cmData_t* p, int v );
  202. cmDtRC_t cmDataSetUInt( cmData_t* p, unsigned int v );
  203. cmDtRC_t cmDataSetLong( cmData_t* p, long v );
  204. cmDtRC_t cmDataSetULong( cmData_t* p, unsigned long v );
  205. cmDtRC_t cmDataSetFloat( cmData_t* p, float v );
  206. cmDtRC_t cmDataSetDouble( cmData_t* p, double v );
  207. cmDtRC_t cmDataSetStr( cmData_t* p, unsigned flags, cmChar_t* s );
  208. cmDtRC_t cmDataSetConstStr( cmData_t* p, unsigned flags, const cmChar_t* s );
  209. cmDtRC_t cmDataSetStrN( cmData_t* p, unsigned flags, cmChar_t* s, unsigned charCnt );
  210. cmDtRC_t cmDataSetConstStrN( cmData_t* p, unsigned flags, const cmChar_t* s, unsigned charCnt );
  211. cmDtRC_t cmDataSetBlob( cmData_t* p, unsigned flags, void* v, unsigned byteCnt );
  212. cmDtRC_t cmDataSetConstBlob( cmData_t* p, unsigned flags, const void* v, unsigned byteCnt );
  213. // Get the value of an object. No conversion is applied the
  214. // type must match exactly or an error is generated.
  215. cmDtRC_t cmDataBool( const cmData_t* p, bool* v );
  216. cmDtRC_t cmDataChar( const cmData_t* p, char* v );
  217. cmDtRC_t cmDataUChar( const cmData_t* p, unsigned char* v );
  218. cmDtRC_t cmDataShort( const cmData_t* p, short* v );
  219. cmDtRC_t cmDataUShort( const cmData_t* p, unsigned short* v );
  220. cmDtRC_t cmDataInt( const cmData_t* p, int* v );
  221. cmDtRC_t cmDataUInt( const cmData_t* p, unsigned int* v );
  222. cmDtRC_t cmDataLong( const cmData_t* p, long* v );
  223. cmDtRC_t cmDataULong( const cmData_t* p, unsigned long* v );
  224. cmDtRC_t cmDataFloat( const cmData_t* p, float* v );
  225. cmDtRC_t cmDataDouble( const cmData_t* p, double* v );
  226. cmDtRC_t cmDataStr( const cmData_t* p, cmChar_t** v );
  227. cmDtRC_t cmDataConstStr( const cmData_t* p, const cmChar_t** v );
  228. cmDtRC_t cmDataBlob( const cmData_t* p, void** v, unsigned* byteCntRef );
  229. cmDtRC_t cmDataConstBlob( const cmData_t* p, const void** v, unsigned* byteCntRef );
  230. // Functions in this group which return pointers will return NULL
  231. // on error. The other function indicate an error by returning
  232. // kInvalidDtXXX depending on their type.
  233. // Note that there is no guarantee, except as determined by the
  234. // application, that one of the kInvalidDtXXX is not in fact a legal return value.
  235. // These function are simple wrappers around calls to cmDataXXX() and
  236. // therefore do NOT do any type conversion.
  237. bool cmDtBool( const cmData_t* p );
  238. char cmDtChar( const cmData_t* p );
  239. unsigned char cmDtUChar( const cmData_t* p );
  240. short cmDtShort( const cmData_t* p );
  241. unsigned short cmDtUShort( const cmData_t* p );
  242. int cmDtInt( const cmData_t* p );
  243. unsigned cmDtUInt( const cmData_t* p );
  244. long cmDtLong( const cmData_t* p );
  245. unsigned long cmDtULong( const cmData_t* p );
  246. float cmDtFloat( const cmData_t* p );
  247. double cmDtDouble( const cmData_t* p );
  248. char* cmDtStr( const cmData_t* p );
  249. const char* cmDtConstStr( const cmData_t* p );
  250. void* cmDtBlob( const cmData_t* p, unsigned* byteCntRef );
  251. const void* cmDtConstBlob( const cmData_t* p, unsigned* byteCntRef );
  252. // Get the value of an object with conversion.
  253. cmDtRC_t cmDataGetBool( const cmData_t* p, bool* v );
  254. cmDtRC_t cmDataGetChar( const cmData_t* p, char* v );
  255. cmDtRC_t cmDataGetUChar( const cmData_t* p, unsigned char* v );
  256. cmDtRC_t cmDataGetShort( const cmData_t* p, short* v );
  257. cmDtRC_t cmDataGetUShort( const cmData_t* p, unsigned short* v );
  258. cmDtRC_t cmDataGetInt( const cmData_t* p, int* v );
  259. cmDtRC_t cmDataGetUInt( const cmData_t* p, unsigned int* v );
  260. cmDtRC_t cmDataGetLong( const cmData_t* p, long* v );
  261. cmDtRC_t cmDataGetULong( const cmData_t* p, unsigned long* v );
  262. cmDtRC_t cmDataGetFloat( const cmData_t* p, float* v );
  263. cmDtRC_t cmDataGetDouble( const cmData_t* p, double* v );
  264. // Functions in this group which return pointers will return NULL
  265. // on error. The other function indicate an error by returning
  266. // kInvalidDtXXX depending on their type.
  267. // Note that there is no guarantee, except as determined by the
  268. // application that one of the kInvalidDtXXX is not in fact a legal return value.
  269. // These function are simple wrappers around calls to cmDataGetXXX() and
  270. // therefore do type conversion.
  271. bool cmDtGetBool( const cmData_t* p );
  272. char cmDtGetChar( const cmData_t* p );
  273. unsigned char cmDtGetUChar( const cmData_t* p );
  274. short cmDtGetShort( const cmData_t* p );
  275. unsigned short cmDtGetUShort( const cmData_t* p );
  276. int cmDtGetInt( const cmData_t* p );
  277. unsigned cmDtGetUInt( const cmData_t* p );
  278. long cmDtGetLong( const cmData_t* p );
  279. unsigned long cmDtGetULong( const cmData_t* p );
  280. float cmDtGetFloat( const cmData_t* p );
  281. double cmDtGetDouble( const cmData_t* p );
  282. //----------------------------------------------------------------------------
  283. // Array related functions
  284. //
  285. // Notes:
  286. // 1) string arrays are arrays of string pointers.
  287. // 2) blob arrays (array of void pointers) are not supported because
  288. // there is no direct way to determine the length of each blob
  289. // and therefore they cannot be internally duplicated - a special scheme
  290. // could be devised (length goes in first 4 bytes) to make this
  291. // work but we will defer that until the need arises.
  292. //
  293. // Dynamically allocate a new array data object.
  294. //
  295. // eleCnt referes to the number of elements in the array pointed
  296. // to by 'vp'. The number of bytes pointed to by 'vp' is then
  297. // cmDataByteWidth(tid)*eleCnt.
  298. //
  299. // If no flags are set then the array pointed to by 'vp' is reallocated
  300. // and kDataFreeDtFl is set.
  301. //
  302. // If kFreeValueDtFl is set then the object will take responsibility for
  303. // releasing the memory pointed to by 'vp' when the object is destroyed
  304. // or the array is reassigned.
  305. //
  306. // If kNoCopyDtFl is set then 'vp' becomes the internal array
  307. // value (vp[cnt]) is NOT reallocated). In this case the client is
  308. // responsibile for eventually releasing the associated memory - when
  309. // the data object is no longer valid.
  310. cmDtRC_t cmDataNewArray( cmData_t* parent, cmDataTypeId_t tid, void* vp, unsigned eleCnt, unsigned flags, cmData_t** ref );
  311. cmDtRC_t cmDataNewBoolArray( cmData_t* parent, bool* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  312. cmDtRC_t cmDataNewCharArray( cmData_t* parent, char* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  313. cmDtRC_t cmDataNewUCharArray( cmData_t* parent, unsigned char* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  314. cmDtRC_t cmDataNewShortArray( cmData_t* parent, short* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  315. cmDtRC_t cmDataNewUShortArray( cmData_t* parent, unsigned short* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  316. cmDtRC_t cmDataNewIntArray( cmData_t* parent, int* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  317. cmDtRC_t cmDataNewUIntArray( cmData_t* parent, unsigned int* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  318. cmDtRC_t cmDataNewLongArray( cmData_t* parent, long* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  319. cmDtRC_t cmDataNewULongArray( cmData_t* parent, unsigned long* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  320. cmDtRC_t cmDataNewFloatArray( cmData_t* parent, float* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  321. cmDtRC_t cmDataNewDoubleArray( cmData_t* parent, double* v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  322. cmDtRC_t cmDataNewStrArray( cmData_t* parent, cmChar_t** v, unsigned eleCnt, unsigned flags, cmData_t** ref );
  323. cmDtRC_t cmDataNewConstStrArray( cmData_t* parent, const cmChar_t** v,unsigned eleCnt, unsigned flags, cmData_t** ref );
  324. // Set the value and type of an existing scalar object.
  325. //
  326. // These functions begin by releasing any resources held by *p
  327. // prior to resetting the type and value of the object.
  328. // The 'flags' argument may include kConstValueDtFl, kConstObjDtFl,
  329. // kNoCopyDtFl and the kFreeValueDtFl.
  330. // Generic set array functions. 'vp' is assumed to point to an array
  331. // of the type defined by 'tid'.
  332. cmDtRC_t cmDataSetArrayValue( cmData_t* dt, cmDataTypeId_t tid, void* vp, unsigned eleCnt, unsigned flags );
  333. // Type sepctific set array functions.
  334. cmDtRC_t cmDataSetBoolArray( cmData_t* d, bool* v, unsigned eleCnt, unsigned flags );
  335. cmDtRC_t cmDataSetCharArray( cmData_t* d, char* v, unsigned eleCnt, unsigned flags );
  336. cmDtRC_t cmDataSetUCharArray( cmData_t* d, unsigned char* v, unsigned eleCnt, unsigned flags );
  337. cmDtRC_t cmDataSetShortArray( cmData_t* d, short* v, unsigned eleCnt, unsigned flags );
  338. cmDtRC_t cmDataSetUShortArray( cmData_t* d, unsigned short* v, unsigned eleCnt, unsigned flags );
  339. cmDtRC_t cmDataSetIntArray( cmData_t* d, int* v, unsigned eleCnt, unsigned flags );
  340. cmDtRC_t cmDataSetUIntArray( cmData_t* d, unsigned int* v, unsigned eleCnt, unsigned flags );
  341. cmDtRC_t cmDataSetLongArray( cmData_t* d, long* v, unsigned eleCnt, unsigned flags );
  342. cmDtRC_t cmDataSetULongArray( cmData_t* d, unsigned long* v, unsigned eleCnt, unsigned flags );
  343. cmDtRC_t cmDataSetFloatArray( cmData_t* d, float* v, unsigned eleCnt, unsigned flags );
  344. cmDtRC_t cmDataSetDoubleArray( cmData_t* d, double* v, unsigned eleCnt, unsigned flags );
  345. cmDtRC_t cmDataSetStrArray( cmData_t* d, cmChar_t** v, unsigned eleCnt, unsigned flags );
  346. cmDtRC_t cmDataSetConstStrArray(cmData_t* d,const cmChar_t** v,unsigned eleCnt, unsigned flags );
  347. // Return the count of elements in a n array.
  348. unsigned cmDataArrayEleCount( const cmData_t* d );
  349. // Get a pointer to the base of an array.
  350. // The type must match exactly or an error is generated.
  351. // Use cmDataEleCount() to determine the number of elements in the array.
  352. cmDtRC_t cmDataBoolArray( const cmData_t* d, bool** v );
  353. cmDtRC_t cmDataCharArray( const cmData_t* d, char** v );
  354. cmDtRC_t cmDataUCharArray( const cmData_t* d, unsigned char** v );
  355. cmDtRC_t cmDataShortArray( const cmData_t* d, short** v );
  356. cmDtRC_t cmDataUShortArray( const cmData_t* d, unsigned short** v );
  357. cmDtRC_t cmDataIntArray( const cmData_t* d, int** v );
  358. cmDtRC_t cmDataUIntArray( const cmData_t* d, unsigned int** v );
  359. cmDtRC_t cmDataLongArray( const cmData_t* d, long** v );
  360. cmDtRC_t cmDataULongArray( const cmData_t* d, unsigned long** v );
  361. cmDtRC_t cmDataFloatArray( const cmData_t* d, float** v );
  362. cmDtRC_t cmDataDoubleArray( const cmData_t* d, double** v );
  363. // This group of functions is a wrapper around calls to the same named
  364. // cmDataXXXArray() functions above. On error they return NULL.
  365. bool* cmDtBoolArray( const cmData_t* d );
  366. char* cmDtCharArray( const cmData_t* d );
  367. unsigned char* cmDtUCharArray( const cmData_t* d );
  368. short* cmDtShortArray( const cmData_t* d );
  369. unsigned short* cmDtUShortArray( const cmData_t* d );
  370. int* cmDtIntArray( const cmData_t* d );
  371. unsigned* cmDtUIntArray( const cmData_t* d );
  372. long* cmDtLongArray( const cmData_t* d );
  373. unsigned long* cmDtULongArray( const cmData_t* d );
  374. float* cmDtFloatArray( const cmData_t* d );
  375. double* cmDtDoubleArray( const cmData_t* d );
  376. //----------------------------------------------------------------------------
  377. // Structure related functions
  378. //
  379. // Release an object and any resources held by it.
  380. // Note the this function does not unlink the object
  381. // from it's parent. Use cmDataUnlinkAndFree()
  382. // to remove a object from it's parent list prior
  383. // to releasing it.
  384. void cmDataFree( cmData_t* p );
  385. // Unlink 'p' from its parents and siblings.
  386. // Asserts if parent is not a structure.
  387. // Returns 'p'.
  388. cmData_t* cmDataUnlink( cmData_t* p );
  389. // Wrapper function to cmDataUnlink() and cmDataFree().
  390. void cmDataUnlinkAndFree( cmData_t* p );
  391. // Replace the 'dst' node with the 'src' node and
  392. // return 'src'. This operation does not duplicate
  393. // 'src' it simply links in 'src' at the location of
  394. // 'dst' and then unlinks and free's 'dst'.
  395. cmData_t* cmDataReplace( cmData_t* dst, cmData_t* src );
  396. // Return the count of child nodes.
  397. // Scalars and arrays have no children.
  398. // Pairs have 2 children.
  399. // Lists have one child per element.
  400. // Records have one child per pair.
  401. unsigned cmDataChildCount( const cmData_t* p );
  402. // Returns the ith child of 'p'.
  403. // Returns NULL if p has no children or index is invalid.
  404. cmData_t* cmDataChild( cmData_t* p, unsigned index );
  405. // Prepend 'p' to 'parents' child list.
  406. // The source node 'p' is not duplicated it is simply linked in.
  407. // 'p' is automatically unlinked prior to being prepended.
  408. // Returns 'p'.
  409. cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* p );
  410. // Append 'p' to the end of 'parent' child list.
  411. // The source node 'p' is not duplicated it is simply linked in.
  412. // 'p' is automatically unlinked prior to being appended.
  413. // Returns 'p'.
  414. cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p );
  415. // Insert 'p' at index. Index must be in the range:
  416. // 0 to cmDataChildCount(parent).
  417. // The source node 'p' is not duplicated it is simply linked in.
  418. // 'p' is automatically unlinked prior to being inserted.
  419. // Returns 'p'.
  420. cmData_t* cmDataInsertChild( cmData_t* parent, unsigned index, cmData_t* p );
  421. //----------------------------------------------------------------------------
  422. // Pair related functions
  423. //
  424. // Get the key/value of a pair
  425. cmData_t* cmDataPairKey( cmData_t* p );
  426. unsigned cmDataPairKeyId( cmData_t* p );
  427. const cmChar_t* cmDataPairKeyLabel( cmData_t* p );
  428. cmData_t* cmDataPairValue( cmData_t* p );
  429. // Set the value of an existing pair node.
  430. // 'value' is not duplicated it is simply linked in place of the
  431. // previous pair value node. The previous pair value node is
  432. // unlinked and freed.
  433. // Returns 'p'.
  434. cmData_t* cmDataPairSetValue( cmData_t* p, cmData_t* value );
  435. // Set the key of an existing pair node.
  436. // The previous key is unlinked and freed.
  437. cmData_t* cmDataPairSetKey( cmData_t* p, cmData_t* key );
  438. cmData_t* cmDataPairSetKeyId( cmData_t* p, unsigned id );
  439. // The data space for the 'label' string is dynamically allocated.
  440. cmData_t* cmDataPairSetKeyLabel( cmData_t* p, const cmChar_t* label );
  441. // Create a pair value by assigning a key and value to 'p'.
  442. // 'p' is unlinked and freed prior to the key value assignment.
  443. // 'key' and 'value' are simply linked in they are not duplicated or reallocated.
  444. cmData_t* cmDataPairMake( cmData_t* parent, cmData_t* p, cmData_t* key, cmData_t* value );
  445. // Dynamically allocate a pair node. Both the key and value nodes are reallocated.
  446. cmRC_t cmDataPairAlloc( cmData_t* parent, const cmData_t* key, const cmData_t* value, cmData_t** ref );
  447. // Dynamically allocate the id but link (w/o realloc) the value.
  448. cmRC_t cmDataPairAllocId( cmData_t* parent, unsigned keyId, cmData_t* value, cmData_t** ref );
  449. // Dynamically allocate the label but link (w/o realloc) the value.
  450. cmRC_t cmDataPairAllocLabelN(cmData_t* parent, const cmChar_t* label, unsigned charCnt, cmData_t* value, cmData_t** ref);
  451. cmRC_t cmDataPairAllocLabel( cmData_t* parent, const cmChar_t* label, cmData_t* value, cmData_t** ref );
  452. //----------------------------------------------------------------------------
  453. // List related functions
  454. //
  455. // Return the count of ele's in the list.
  456. unsigned cmDataListCount( const cmData_t* p );
  457. // Return the ith element in the list.
  458. cmData_t* cmDataListEle( cmData_t* p, unsigned index );
  459. //
  460. cmData_t* cmDataListMake( cmData_t* parent, cmData_t* p );
  461. cmData_t* cmDataListAlloc( cmData_t* parent);
  462. // Dynamically allocate a new list object and fill it with values
  463. // using a variable argument list.
  464. //
  465. // 1) Var-args fmt:
  466. // <typeId> {<value>} {<cnt>}
  467. //
  468. // 2) <typeId> is formed by OR-ing one of the container type flags with a
  469. // data type id. (e.g. kArrayDtId | kIntDtId). Note that if no
  470. // container is given then the type is assumed to be scalar.
  471. //
  472. // scalar types: <value> is literal, <cnt> should not be included
  473. // null scalar object should not include <value> or <cnt>
  474. // array types: <value> is pointer,<cnt> is element count
  475. // struct types: <value> is cmData_t pointer, <cnt> should not be included.
  476. // The struct object is appended to the child list of parent.
  477. //
  478. // 3) Indicate the end of argument list by setting <typeId> to kInvalidDtId.
  479. // 4) Scalar and array objects will be dynamically allocated.
  480. // 5) Data attribute flags (kXXXDtFl) make also be OR-ed into the <typeId>.
  481. // 6) Data attribute flags will only be used with scalar and array object and
  482. // will be ignored for all other object types.
  483. cmRC_t cmDataListAllocV( cmData_t* parent, cmErr_t* err, cmRC_t errRC, cmData_t** ref, va_list vl );
  484. cmRC_t cmDataListAllocA( cmData_t* parent, cmErr_t* err, cmRC_t errRC, cmData_t** ref, ... );
  485. // Returns a ptr to 'ele'.
  486. cmData_t* cmDataListAppendEle( cmData_t* p, cmData_t* ele );
  487. cmDtRC_t cmDataListAppendV( cmData_t* p, cmErr_t* err, cmRC_t errRC, va_list vl );
  488. cmDtRC_t cmDataListAppend( cmData_t* p, cmErr_t* err, cmRC_t errRC, ... );
  489. // Return 'p'.
  490. cmData_t* cmDataListInsertEle( cmData_t* p, unsigned index, cmData_t* ele );
  491. cmData_t* cmDataListInsertEleN(cmData_t* p, unsigned index, cmData_t* ele[], unsigned n );
  492. //----------------------------------------------------------------------------
  493. // Record related functions
  494. //
  495. // Return count of pairs.
  496. unsigned cmDataRecdCount( const cmData_t* p );
  497. // Return the ith pair.
  498. cmData_t* cmDataRecdEle( cmData_t* p, unsigned index );
  499. // Return the ith value.
  500. cmData_t* cmDataRecdValueFromIndex( cmData_t* p, unsigned index );
  501. cmData_t* cmDataRecdValueFromId( cmData_t* p, unsigned id );
  502. cmData_t* cmDataRecdValueFromLabel( cmData_t* p, const cmChar_t* label );
  503. // Return the ith key or NULL if it does not exist.
  504. cmData_t* cmDataRecdKey( cmData_t* p, unsigned index );
  505. // Returns cmInvalidId if the ith key does not exist or is not an integer.
  506. unsigned cmDataRecdKeyId( cmData_t* p, unsigned index );
  507. // Return NULL if the ith key does not exist or is not a string.
  508. const cmChar_t* cmDataRecdKeyLabel( cmData_t* p, unsigned index );
  509. cmData_t* cmDataRecdMake( cmData_t* parent, cmData_t* p );
  510. cmData_t* cmDataRecdAlloc( cmData_t* parent );
  511. // Append a pair node by linking the pair node 'pair' to the record node 'p'.
  512. // 'pair' is simply linked to 'p' via cmDataAppendChild() no
  513. // reallocation or duplicattion takes place.
  514. cmData_t* cmDataRecdAppendPair( cmData_t* p, cmData_t* pair );
  515. // Dynamically allocate a new record object and fill it with values
  516. // using a variable argument list.
  517. //
  518. // 1) Var-args format:
  519. // <label|id> <value>
  520. //
  521. // The <label|id> arg. gives the record pair label and the <value>
  522. // argument list gives the pair value.
  523. //
  524. // 2) The <value> argument has the same syntax as the cmDataListAllocLabelV()
  525. // call.
  526. // 3) The cmDataRecdAllocLabelV() arg. list should be terminated with NULL.
  527. // The cmDataRecdAllocIdV() arg. list should be terminated with 'kInvalidDtId'.
  528. cmData_t* cmDataRecdAllocLabelV( cmData_t* parent, cmErr_t* err, cmRC_t errRC, va_list vl );
  529. cmData_t* cmDataRecdAllocLabelA( cmData_t* parent, cmErr_t* err, cmRC_t errRC, ... );
  530. cmData_t* cmDataRecdAllocIdV( cmData_t* parent, cmErr_t* err, cmRC_t errRC, va_list vl );
  531. cmData_t* cmDataRecdAllocIdA( cmData_t* parent, cmErr_t* err, cmRC_t errRC, ... );
  532. // Extract the data in a record to C variables.
  533. // Var-args format:
  534. // (label | id) <cid> <typeId> <ptr> {cnt_ptr}
  535. // The var-args list must be NULL terminated.
  536. // The <'id' | 'label'> identify a pair.
  537. // The <cid> indicates the type of the target container.
  538. // The <typeId> indicates the C type of 'pointer'.
  539. // If <cid> is kArrayDtId then the <cnt_ptr} must be include to receive the
  540. // count of elements in the array.
  541. // The actual field type must be convertable into this pointer type or the
  542. // function will fail.
  543. // 'err' is an application supplied error object to be used if a required
  544. // field is missing. 'errRC' is the client result code to be passed with
  545. // the error report. See cmErrMsg(). Both 'err' and 'errRC' are optional.
  546. // Set kOptArgDtFl on 'typeId' to indicate that a field is optional.
  547. // <label|id> (<typeId> | kOptArgDtFl) <pointer>
  548. cmDtRC_t cmDataRecdParseLabelV(cmData_t* p, cmErr_t* err, unsigned errRC, va_list vl );
  549. cmDtRC_t cmDataRecdParseLabel( cmData_t* p, cmErr_t* err, unsigned errRC, ... );
  550. cmDtRC_t cmDataRecdParseIdV( cmData_t* p, cmErr_t* err, unsigned errRC, va_list vl );
  551. cmDtRC_t cmDataRecdParseId( cmData_t* p, cmErr_t* err, unsigned errRC, ... );
  552. //----------------------------------------------------------------------------
  553. // Serialization related functions
  554. //
  555. unsigned cmDataSerializeByteCount( const cmData_t* p );
  556. cmDtRC_t cmDataSerialize( const cmData_t* p, void* buf, unsigned bufByteCnt );
  557. cmDtRC_t cmDataDeserialize( const void* buf, unsigned bufByteCnt, cmData_t** pp );
  558. //----------------------------------------------------------------------------
  559. // Text to Data related functions
  560. //
  561. typedef cmHandle_t cmDataParserH_t;
  562. extern cmDataParserH_t cmDataParserNullHandle;
  563. cmDtRC_t cmDataParserCreate( cmCtx_t* ctx, cmDataParserH_t* hp );
  564. cmDtRC_t cmDataParserDestroy( cmDataParserH_t* hp );
  565. bool cmDataParserIsValid( cmDataParserH_t h );
  566. // Parse a text representation into a 'record' type.
  567. // Note that the text is wrapped with implied curly braces
  568. // (e.g. "{ text }"). The contents of the text should therefore
  569. // fit the record syntax (e.g. the first token should be a
  570. // 'pair' label.
  571. cmDtRC_t cmDataParserExec( cmDataParserH_t h, const cmChar_t* text, cmData_t** pp );
  572. //-----------------------------------------------------------------------------
  573. void cmDataPrint( const cmData_t* p, cmRpt_t* rpt );
  574. void cmDataTest( cmCtx_t* ctx );
  575. #ifdef __cplusplus
  576. }
  577. #endif
  578. #endif