libcm is a C development framework with an emphasis on audio signal processing applications.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLex.h"
  9. #include "cmLinkedHeap.h"
  10. #include "cmSymTbl.h"
  11. #include "cmCsv.h"
  12. #include "cmText.h"
  13. enum
  14. {
  15. kCommaLexTId = kUserLexTId+1,
  16. kMaxLexTId // always last in the lex id list
  17. };
  18. typedef struct
  19. {
  20. const char* text;
  21. unsigned id;
  22. } cmCsvToken_t;
  23. cmCsvToken_t _cmCsvTokenArray[] =
  24. {
  25. { ",", kCommaLexTId },
  26. { "", kErrorLexTId }
  27. };
  28. // The binder linked list contains a list of records which tracks the first
  29. // (left-most) non-blank column in each row.
  30. typedef struct cmCsvBind_str
  31. {
  32. struct cmCsvBind_str* linkPtr;
  33. cmCsvCell_t* rowPtr;
  34. } cmCsvBind_t;
  35. typedef struct cmCsvUdef_str
  36. {
  37. struct cmCsvUdef_str* linkPtr;
  38. unsigned id;
  39. } cmCsvUdef_t;
  40. typedef struct
  41. {
  42. cmErr_t err;
  43. void* rptDataPtr; //
  44. cmLexH lexH; // parsing lexer
  45. cmSymTblH_t symTblH; // all XML identifiers and data is stored as a symbol in this table
  46. cmLHeapH_t heapH;
  47. cmCsvBind_t* bindPtr; // base of the binder linked list
  48. cmCsvCell_t* curRowPtr; // used by the parser to track the row being filled
  49. cmCsvUdef_t* udefList;
  50. } cmCsv_t;
  51. cmCsvH_t cmCsvNullHandle = { NULL };
  52. cmCsv_t* _cmCsvHandleToPtr( cmCsvH_t h )
  53. {
  54. cmCsv_t* p = (cmCsv_t*)h.h;
  55. assert( p != NULL );
  56. cmErrClearRC(&p->err);
  57. return p;
  58. }
  59. cmCsvRC_t _cmCsvVError( cmCsv_t* p, cmCsvRC_t rc, const char* fmt, va_list vl )
  60. { return cmErrVMsg(&p->err,rc,fmt,vl); }
  61. cmCsvRC_t _cmCsvError( cmCsv_t* p, cmCsvRC_t rc, const char* fmt, ... )
  62. {
  63. va_list vl;
  64. va_start(vl,fmt);
  65. rc = _cmCsvVError(p,rc,fmt,vl);
  66. va_end(vl);
  67. return rc;
  68. }
  69. cmCsvRC_t cmCsvInitialize( cmCsvH_t *hp, cmCtx_t* ctx )
  70. {
  71. cmCsvRC_t rc;
  72. cmCsv_t* p = NULL;
  73. unsigned i;
  74. if((rc = cmCsvFinalize(hp)) != kOkCsvRC )
  75. return rc;
  76. // create the base object
  77. p = cmMemAllocZ( cmCsv_t, 1 );
  78. assert(p != NULL);
  79. cmErrSetup(&p->err,&ctx->rpt,"CSV");
  80. // create the symbol table
  81. if( cmSymTblIsValid(p->symTblH = cmSymTblCreate(cmSymTblNullHandle,0,ctx)) == false )
  82. {
  83. rc = _cmCsvError(p,kSymTblErrCsvRC,"Symbol table creation failed.");
  84. goto errLabel;
  85. }
  86. // allocate the linked heap mgr
  87. if( cmLHeapIsValid(p->heapH = cmLHeapCreate(1024,ctx)) == false )
  88. {
  89. rc = _cmCsvError(p,kMemAllocErrCsvRC,"Linked heap object allocation failed.");
  90. goto errLabel;
  91. }
  92. // allocate the lexer
  93. if(cmLexIsValid(p->lexH = cmLexInit(NULL,0,0,&ctx->rpt)) == false )
  94. {
  95. rc = _cmCsvError(p,kLexErrCsvRC,"Lex allocation failed.");
  96. goto errLabel;
  97. }
  98. // register CSV specific tokens with the lexer
  99. for(i=0; _cmCsvTokenArray[i].id != kErrorLexTId; ++i)
  100. {
  101. cmRC_t lexRC;
  102. if( (lexRC = cmLexRegisterToken(p->lexH, _cmCsvTokenArray[i].id, _cmCsvTokenArray[i].text )) != kOkLexRC )
  103. {
  104. rc = _cmCsvError(p,kLexErrCsvRC,"Lex token registration failed for:'%s'.\nLexer Error:%s",_cmCsvTokenArray[i].text, cmLexRcToMsg(lexRC) );
  105. goto errLabel;
  106. }
  107. }
  108. hp->h = p;
  109. return kOkCsvRC;
  110. errLabel:
  111. cmMemPtrFree(&p);
  112. if( cmLHeapIsValid(p->heapH) )
  113. cmLHeapDestroy(&p->heapH);
  114. if( cmLexIsValid(p->lexH) )
  115. cmLexFinal(&p->lexH);
  116. return rc;
  117. }
  118. cmCsvRC_t cmCsvFinalize( cmCsvH_t *hp )
  119. {
  120. cmRC_t lexRC;
  121. if( hp == NULL || hp->h == NULL )
  122. return kOkCsvRC;
  123. cmCsv_t* p = _cmCsvHandleToPtr(*hp);
  124. // free the internal heap object
  125. cmLHeapDestroy( &p->heapH );
  126. // free the lexer
  127. if((lexRC = cmLexFinal(&p->lexH)) != kOkLexRC )
  128. return _cmCsvError(p,kLexErrCsvRC,"Lexer finalization failed.\nLexer Error:%s",cmLexRcToMsg(lexRC));
  129. // free the symbol table
  130. cmSymTblDestroy(&p->symTblH);
  131. // free the handle
  132. cmMemPtrFree(&hp->h);
  133. return kOkCsvRC;
  134. }
  135. cmCsvRC_t cmCsvInitializeFromFile( cmCsvH_t *hp, const char* fn, unsigned maxRowCnt, cmCtx_t* ctx )
  136. {
  137. cmCsvRC_t rc;
  138. if((rc = cmCsvInitialize(hp,ctx)) != kOkCsvRC )
  139. return rc;
  140. return cmCsvParseFile( *hp, fn, maxRowCnt);
  141. }
  142. bool cmCsvIsValid( cmCsvH_t h)
  143. { return h.h != NULL; }
  144. cmCsvRC_t cmCsvLastRC( cmCsvH_t h )
  145. {
  146. cmCsv_t* p = _cmCsvHandleToPtr(h);
  147. return cmErrLastRC(&p->err);
  148. }
  149. void cmCsvClearRC( cmCsvH_t h )
  150. {
  151. cmCsv_t* p = _cmCsvHandleToPtr(h);
  152. cmErrClearRC(&p->err);
  153. }
  154. cmCsvRC_t cmCsvParseFile( cmCsvH_t h, const char* fn, unsigned maxRowCnt )
  155. {
  156. cmCsvRC_t rc = kOkCsvRC;
  157. FILE* fp = NULL;
  158. cmCsv_t* p = _cmCsvHandleToPtr(h);
  159. unsigned n = 0;
  160. char* textBuf = NULL;
  161. assert( fn != NULL && p != NULL );
  162. // open the file
  163. if((fp = fopen(fn,"rb")) == NULL )
  164. return _cmCsvError(p,kFileOpenErrCsvRC,"Unable to open the file:'%s'.",fn);
  165. // seek to the end
  166. if( fseek(fp,0,SEEK_END) != 0 )
  167. {
  168. rc= _cmCsvError(p,kFileSeekErrCsvRC,"Unable to seek to the end of '%s'.",fn);
  169. goto errLabel;
  170. }
  171. // get the length of the file
  172. if( (n=ftell(fp)) == 0 )
  173. {
  174. rc = _cmCsvError(p,kFileOpenErrCsvRC,"The file '%s' appears to be empty.",fn);
  175. goto errLabel;
  176. }
  177. // rewind the file
  178. if( fseek(fp,0,SEEK_SET) != 0 )
  179. {
  180. rc = _cmCsvError(p,kFileSeekErrCsvRC,"Unable to seek to the beginning of '%s'.",fn);
  181. goto errLabel;
  182. }
  183. // allocate the text buffer
  184. if((textBuf = cmMemAllocZ( char, n+1)) == NULL )
  185. {
  186. rc = _cmCsvError(p,kMemAllocErrCsvRC,"Unable to allocate the text file buffer for:'%s'.",fn);
  187. goto errLabel;
  188. }
  189. // read the file into the text buffer
  190. if( fread(textBuf,n,1,fp) != 1 )
  191. {
  192. rc = _cmCsvError(p,kFileReadErrCsvRC,"File read failed on:'%s'.",fn);
  193. goto errLabel;
  194. }
  195. rc = cmCsvParse(h,textBuf,n,maxRowCnt);
  196. errLabel:
  197. // close the file
  198. if( fclose(fp) != 0 )
  199. {
  200. rc = _cmCsvError(p,kFileCloseErrCsvRC,"File close failed on:'%s'.",fn);
  201. goto errLabel;
  202. }
  203. // free the buffer
  204. if( textBuf != NULL )
  205. cmMemFree(textBuf);
  206. return rc;
  207. }
  208. cmCsvRC_t _cmCsvAppendNewBindRecd( cmCsv_t* p, cmCsvBind_t** newBindPtrPtr, unsigned* newRowIdxPtr )
  209. {
  210. cmCsvBind_t* nbp;
  211. cmCsvBind_t* bp = p->bindPtr;
  212. unsigned newRowIdx = 0;
  213. if( newBindPtrPtr != NULL )
  214. *newBindPtrPtr = NULL;
  215. if( newRowIdxPtr != NULL )
  216. *newRowIdxPtr = cmInvalidIdx;
  217. // allocate the binder record
  218. if((nbp = cmLHeapAllocZ( p->heapH, sizeof(cmCsvBind_t))) == NULL )
  219. return _cmCsvError(p,kMemAllocErrCsvRC,"Binding record allocation failed.");
  220. // if this is the first binder record
  221. if( p->bindPtr == NULL )
  222. {
  223. p->bindPtr = nbp;
  224. bp = nbp;
  225. }
  226. else
  227. {
  228. newRowIdx = 1;
  229. // iterate to the bottom of the binding
  230. while( bp->linkPtr != NULL )
  231. {
  232. bp = bp->linkPtr;
  233. ++newRowIdx;
  234. }
  235. bp->linkPtr = nbp;
  236. }
  237. if( newBindPtrPtr != NULL )
  238. *newBindPtrPtr = nbp;
  239. if( newRowIdxPtr != NULL )
  240. *newRowIdxPtr = newRowIdx;
  241. return kOkCsvRC;
  242. }
  243. cmCsvRC_t _cmCsvCreateBind( cmCsv_t* p, cmCsvCell_t* cp, const char* tokenText, unsigned lexRow, unsigned lexCol )
  244. {
  245. cmCsvRC_t rc;
  246. cmCsvBind_t* nbp;
  247. if((rc = _cmCsvAppendNewBindRecd(p,&nbp,NULL)) != kOkCsvRC )
  248. return _cmCsvError(p,kMemAllocErrCsvRC,"Binding record allocation failed for '%s' on line %i column %i.",tokenText,lexRow,lexCol);
  249. nbp->rowPtr = cp;
  250. return rc;
  251. }
  252. cmCsvRC_t _cmCsvAllocCell( cmCsv_t* p, unsigned symId, unsigned flags, unsigned cellRow, unsigned cellCol, cmCsvCell_t** cpp, unsigned lexTId )
  253. {
  254. cmCsvCell_t* cp;
  255. // allocate cell memory
  256. if(( cp = cmLHeapAllocZ(p->heapH, sizeof(cmCsvCell_t) )) == NULL )
  257. return _cmCsvError(p,kMemAllocErrCsvRC,"Cell allocation failed. for row: %i column %i.",cellRow+1,cellCol+1);
  258. cp->row = cellRow;
  259. cp->col = cellCol;
  260. cp->symId = symId;
  261. cp->flags = flags;
  262. cp->lexTId= lexTId;
  263. *cpp = cp;
  264. return kOkCsvRC;
  265. }
  266. cmCsvRC_t _cmCsvCreateCell( cmCsv_t* p, const char* tokenText, unsigned flags, unsigned cellRow, unsigned cellCol, unsigned lexTId )
  267. {
  268. unsigned symId = cmInvalidId;
  269. cmCsvCell_t* cp = NULL;
  270. unsigned lexRow = cmLexCurrentLineNumber(p->lexH);
  271. unsigned lexCol = cmLexCurrentColumnNumber(p->lexH);
  272. cmCsvRC_t rc = kOkCsvRC;
  273. // register the token text as a symbol
  274. if((symId = cmSymTblRegisterSymbol(p->symTblH,tokenText)) == cmInvalidId )
  275. return _cmCsvError(p,kSymTblErrCsvRC,"Symbol registration failed. for '%s' on line %i column %i.",tokenText,lexRow,lexCol);
  276. // allocate a cell
  277. if((rc = _cmCsvAllocCell(p,symId,flags,cellRow,cellCol,&cp,lexTId)) != kOkCsvRC )
  278. return rc;
  279. // if this is the first cell in the row
  280. if( p->curRowPtr == NULL )
  281. {
  282. // link in a new binding record
  283. if((rc = _cmCsvCreateBind( p, cp,tokenText,lexRow,lexCol)) != kOkCsvRC )
  284. return rc; // this return will result in a leak from the lheap but it is a fatal error so ignore it
  285. // update the current row ptr
  286. p->curRowPtr = cp;
  287. }
  288. else
  289. {
  290. // iterate to the end of the row
  291. cmCsvCell_t* rp = p->curRowPtr;
  292. while( rp->rowPtr != NULL )
  293. rp = rp->rowPtr;
  294. rp->rowPtr = cp;
  295. }
  296. return rc;
  297. }
  298. const cmCsvUdef_t* _cmCsvFindUdef( cmCsv_t* p, unsigned id )
  299. {
  300. const cmCsvUdef_t* u = p->udefList;
  301. while( u != NULL )
  302. {
  303. if( u->id == id )
  304. return u;
  305. u = u->linkPtr;
  306. }
  307. return NULL;
  308. }
  309. cmCsvRC_t _cmCsvRegUdef( cmCsv_t* p, unsigned id )
  310. {
  311. if( _cmCsvFindUdef(p,id) != NULL )
  312. return _cmCsvError(p,kDuplicateLexCsvId,"%i has already been used as a user defined id.",id);
  313. cmCsvUdef_t* u = cmLhAllocZ(p->heapH,cmCsvUdef_t,1);
  314. u->id = id;
  315. u->linkPtr = p->udefList;
  316. p->udefList = u;
  317. return kOkCsvRC;
  318. }
  319. cmCsvRC_t cmCsvLexRegisterToken( cmCsvH_t h, unsigned id, const cmChar_t* token )
  320. {
  321. cmCsv_t* p = _cmCsvHandleToPtr(h);
  322. if(cmLexRegisterToken(p->lexH,id,token) != kOkLexRC )
  323. return _cmCsvError(p, kLexErrCsvRC,"Error registering user defined token '%s'.",cmStringNullGuard(token));
  324. return _cmCsvRegUdef(p,id);
  325. }
  326. cmCsvRC_t cmCsvLexRegisterMatcher( cmCsvH_t h, unsigned id, cmLexUserMatcherPtr_t matchFunc )
  327. {
  328. cmCsv_t* p = _cmCsvHandleToPtr(h);
  329. if( cmLexRegisterMatcher(p->lexH,id,matchFunc) != kOkLexRC )
  330. return _cmCsvError(p, kLexErrCsvRC,"Error registering user defined matching function.");
  331. return _cmCsvRegUdef(p,id);
  332. }
  333. unsigned cmCsvLexNextAvailId( cmCsvH_t h )
  334. { return kMaxLexTId; }
  335. cmCsvRC_t cmCsvParse( cmCsvH_t h, const char* buf, unsigned bufCharCnt, unsigned maxRowCnt )
  336. {
  337. cmCsvRC_t rc = kOkCsvRC;
  338. unsigned prvLexRow = 1;
  339. unsigned csvRowIdx = 0;
  340. unsigned csvColIdx = 0;
  341. unsigned lexTId = kErrorLexTId; // cur lex token type id
  342. cmCsv_t* p = _cmCsvHandleToPtr(h);
  343. // assign the text buffer and reset the lexer
  344. if((rc = cmLexSetTextBuffer( p->lexH, buf, bufCharCnt )) != kOkLexRC )
  345. return _cmCsvError( p, kLexErrCsvRC, "Error setting lexer buffer.\nLexer Error:%s",cmLexRcToMsg(rc));
  346. // get the next token
  347. while( (lexTId = cmLexGetNextToken( p->lexH )) != kErrorLexTId && (lexTId != kEofLexTId ) && (rc==kOkCsvRC) && (maxRowCnt==0 || csvRowIdx<maxRowCnt))
  348. {
  349. unsigned flags = 0;
  350. unsigned curLexRow = cmLexCurrentLineNumber(p->lexH);
  351. // if we are starting a new row
  352. if( curLexRow != prvLexRow )
  353. {
  354. prvLexRow = curLexRow;
  355. ++csvRowIdx;
  356. csvColIdx = 0;
  357. p->curRowPtr = NULL; // force a new binding record
  358. }
  359. // copy the token text in a buffer
  360. unsigned n = cmLexTokenCharCount(p->lexH);
  361. char buf[n+1];
  362. strncpy(buf, cmLexTokenText(p->lexH), n );
  363. buf[n]=0;
  364. // do cell data type specific processing
  365. switch( lexTId )
  366. {
  367. case kCommaLexTId:
  368. ++csvColIdx;
  369. break;
  370. case kRealLexTId: flags = kRealCsvTFl; break;
  371. case kIntLexTId: flags = kIntCsvTFl; break;
  372. case kHexLexTId: flags = kHexCsvTFl; break;
  373. case kIdentLexTId:flags = kIdentCsvTFl; break;
  374. case kQStrLexTId: flags = kStrCsvTFl; break;
  375. default:
  376. {
  377. const cmCsvUdef_t* u;
  378. if((u = _cmCsvFindUdef(p,lexTId)) == NULL )
  379. rc = _cmCsvError(p, kSyntaxErrCsvRC, "Unrecognized token type: '%s' for token: '%s' row:%i col:%i.",cmLexIdToLabel(p->lexH,lexTId),buf,csvRowIdx+1,csvColIdx+1);
  380. else
  381. flags = kStrCsvTFl | kUdefCsvTFl;
  382. }
  383. }
  384. // if no error occurred and the token is not a comma and the cell is not empty
  385. if( rc == kOkCsvRC && lexTId != kCommaLexTId && strlen(buf) > 0 )
  386. if((rc = _cmCsvCreateCell( p, buf,flags, csvRowIdx, csvColIdx, lexTId )) != kOkCsvRC )
  387. rc = _cmCsvError(p,rc,"CSV cell create fail on row:%i col:%i\n",csvRowIdx+1,csvColIdx+1);
  388. }
  389. if( lexTId == kErrorLexTId )
  390. rc = _cmCsvError(p, kSyntaxErrCsvRC,"The lexer encountered an unrecognized token row:%i col:%i.",csvRowIdx+1,csvColIdx+1);
  391. return rc;
  392. }
  393. unsigned cmCsvRowCount( cmCsvH_t h )
  394. {
  395. cmCsv_t* p = _cmCsvHandleToPtr(h);
  396. unsigned rowCnt = 0;
  397. cmCsvBind_t* bp = p->bindPtr;
  398. for(; bp != NULL; ++rowCnt )
  399. bp = bp->linkPtr;
  400. return rowCnt;
  401. }
  402. cmCsvCell_t* cmCsvRowPtr( cmCsvH_t h, unsigned row )
  403. {
  404. cmCsv_t* p = _cmCsvHandleToPtr(h);
  405. cmCsvBind_t* bp = p->bindPtr;
  406. while( bp != NULL )
  407. {
  408. // note: bp->rowPtr of blank rows will be NULL
  409. if( bp->rowPtr!=NULL && bp->rowPtr->row == row )
  410. return bp->rowPtr;
  411. bp = bp->linkPtr;
  412. }
  413. return NULL;
  414. }
  415. cmCsvCell_t* cmCsvCellPtr( cmCsvH_t h, unsigned row, unsigned col )
  416. {
  417. cmCsvCell_t* cp;
  418. if((cp = cmCsvRowPtr(h,row)) == NULL )
  419. return NULL;
  420. while( cp != NULL )
  421. {
  422. if( cp->col == col )
  423. return cp;
  424. cp = cp->rowPtr;
  425. }
  426. return NULL;
  427. }
  428. cmCsvCell_t* _cmCsvCellPtr( cmCsvH_t h, unsigned row, unsigned col )
  429. {
  430. cmCsvCell_t* cp;
  431. if((cp = cmCsvCellPtr(h,row,col)) == NULL )
  432. _cmCsvError(_cmCsvHandleToPtr(h),kCellNotFoundCsvRC,"Cell at row:%i col:%i not found.",row,col);
  433. return cp;
  434. }
  435. const char* cmCsvCellSymText( cmCsvH_t h, unsigned symId )
  436. {
  437. cmCsv_t* p = _cmCsvHandleToPtr(h);
  438. const char* cp;
  439. if((cp = cmSymTblLabel(p->symTblH,symId)) == NULL )
  440. _cmCsvError(p,kSymTblErrCsvRC,"The text associated with the symbol '%i' was not found.",symId);
  441. return cp;
  442. }
  443. cmCsvRC_t cmCsvCellSymInt( cmCsvH_t h, unsigned symId, int* vp )
  444. {
  445. const char* cp;
  446. cmCsv_t* p = _cmCsvHandleToPtr(h);
  447. if((cp = cmCsvCellSymText(h,symId)) == NULL )
  448. return kSymTblErrCsvRC;
  449. if( cmTextToInt(cp,vp,&p->err) != kOkTxRC )
  450. return _cmCsvError(p,kDataCvtErrCsvRC,"CSV text to int value failed.");
  451. return kOkCsvRC;
  452. }
  453. cmCsvRC_t cmCsvCellSymUInt( cmCsvH_t h, unsigned symId, unsigned* vp )
  454. {
  455. const char* cp;
  456. cmCsv_t* p = _cmCsvHandleToPtr(h);
  457. if((cp = cmCsvCellSymText(h,symId)) == NULL )
  458. return kSymTblErrCsvRC;
  459. if( cmTextToUInt(cp,vp,&p->err) != kOkTxRC )
  460. return _cmCsvError(p,kDataCvtErrCsvRC,"CSV text to uint value failed.");
  461. return kOkCsvRC;
  462. }
  463. cmCsvRC_t cmCsvCellSymFloat( cmCsvH_t h, unsigned symId, float* vp )
  464. {
  465. const char* cp;
  466. cmCsv_t* p = _cmCsvHandleToPtr(h);
  467. if((cp = cmCsvCellSymText(h,symId)) == NULL )
  468. return kSymTblErrCsvRC;
  469. if( cmTextToFloat(cp,vp,&p->err) != kOkTxRC )
  470. return _cmCsvError(p,kDataCvtErrCsvRC,"CSV text to float value failed.");
  471. return kOkCsvRC;
  472. }
  473. cmCsvRC_t cmCsvCellSymDouble( cmCsvH_t h, unsigned symId, double* vp )
  474. {
  475. const char* cp;
  476. cmCsv_t* p = _cmCsvHandleToPtr(h);
  477. if((cp = cmCsvCellSymText(h,symId)) == NULL )
  478. return kSymTblErrCsvRC;
  479. if( cmTextToDouble(cp,vp,&p->err) != kOkTxRC )
  480. return _cmCsvError(p,kDataCvtErrCsvRC,"CSV text to double value failed.");
  481. return kOkCsvRC;
  482. }
  483. const char* cmCsvCellText( cmCsvH_t h, unsigned row, unsigned col )
  484. {
  485. cmCsvCell_t* cp;
  486. if((cp = cmCsvCellPtr(h,row,col)) == NULL )
  487. return NULL;
  488. return cmCsvCellSymText(h,cp->symId);
  489. }
  490. int cmCsvCellInt( cmCsvH_t h, unsigned row, unsigned col )
  491. {
  492. cmCsvCell_t* cp;
  493. int v;
  494. if((cp = cmCsvCellPtr(h,row,col)) == NULL )
  495. return INT_MAX;
  496. if(cmCsvCellSymInt(h,cp->symId,&v) != kOkCsvRC )
  497. return INT_MAX;
  498. return v;
  499. }
  500. unsigned cmCsvCellUInt( cmCsvH_t h, unsigned row, unsigned col )
  501. {
  502. cmCsvCell_t* cp;
  503. unsigned v;
  504. if((cp = cmCsvCellPtr(h,row,col)) == NULL )
  505. return UINT_MAX;
  506. if(cmCsvCellSymUInt(h,cp->symId,&v) != kOkCsvRC )
  507. return UINT_MAX;
  508. return v;
  509. }
  510. float cmCsvCellFloat( cmCsvH_t h, unsigned row, unsigned col )
  511. {
  512. cmCsvCell_t* cp;
  513. float v;
  514. if((cp = cmCsvCellPtr(h,row,col)) == NULL )
  515. return FLT_MAX;
  516. if(cmCsvCellSymFloat(h,cp->symId,&v) != kOkCsvRC )
  517. return FLT_MAX;
  518. return v;
  519. }
  520. double cmCsvCellDouble(cmCsvH_t h, unsigned row, unsigned col )
  521. {
  522. cmCsvCell_t* cp;
  523. double v;
  524. if((cp = cmCsvCellPtr(h,row,col)) == NULL )
  525. return DBL_MAX;
  526. if(cmCsvCellSymDouble(h,cp->symId,&v) != kOkCsvRC )
  527. return DBL_MAX;
  528. return v;
  529. }
  530. unsigned cmCsvInsertSymText( cmCsvH_t h, const char* text )
  531. {
  532. cmCsv_t* p = _cmCsvHandleToPtr(h);
  533. unsigned symId;
  534. if((symId = cmSymTblRegisterSymbol(p->symTblH,text)) == cmInvalidId )
  535. _cmCsvError(p,kSymTblErrCsvRC,"'%s' could not be inserted into the symbol table.",text);
  536. return symId;
  537. }
  538. unsigned cmCsvInsertSymInt( cmCsvH_t h, int v )
  539. {
  540. const char* fmt = "%i";
  541. unsigned n = snprintf(NULL,0,fmt,v)+1;
  542. char buf[n];
  543. buf[0]= 0;
  544. if( snprintf(buf,n,fmt,v) == n-1 )
  545. return cmCsvInsertSymText(h,buf);
  546. _cmCsvError(_cmCsvHandleToPtr(h),kDataCvtErrCsvRC,"The integer %i could not be converted to text.",v);
  547. return cmInvalidId;
  548. }
  549. unsigned cmCsvInsertSymUInt( cmCsvH_t h, unsigned v )
  550. {
  551. const char* fmt = "%i";
  552. unsigned n = snprintf(NULL,0,fmt,v)+1;
  553. char buf[n];
  554. buf[0]= 0;
  555. if( snprintf(buf,n,fmt,v) == n-1 )
  556. return cmCsvInsertSymText(h,buf);
  557. _cmCsvError(_cmCsvHandleToPtr(h),kDataCvtErrCsvRC,"The unsigned int %i could not be converted to text.",v);
  558. return cmInvalidId;
  559. }
  560. unsigned cmCsvInsertSymHex( cmCsvH_t h, unsigned v )
  561. {
  562. const char* fmt = "0x%x";
  563. unsigned n = snprintf(NULL,0,fmt,v)+1;
  564. char buf[n];
  565. buf[0]= 0;
  566. if( snprintf(buf,n,fmt,v) == n-1 )
  567. return cmCsvInsertSymText(h,buf);
  568. _cmCsvError(_cmCsvHandleToPtr(h),kDataCvtErrCsvRC,"The unsigned int 0x%x could not be converted to text.",v);
  569. return cmInvalidId;
  570. }
  571. unsigned cmCsvInsertSymFloat( cmCsvH_t h, float v )
  572. {
  573. const char* fmt = "%f";
  574. unsigned n = snprintf(NULL,0,fmt,v)+1;
  575. char buf[n];
  576. buf[0] = 0;
  577. if( snprintf(buf,n,fmt,v) == n-1 )
  578. return cmCsvInsertSymText(h,buf);
  579. _cmCsvError(_cmCsvHandleToPtr(h),kDataCvtErrCsvRC,"The float %f could not be converted to text.",v);
  580. return cmInvalidId;
  581. }
  582. unsigned cmCsvInsertSymDouble( cmCsvH_t h, double v )
  583. {
  584. const char* fmt = "%f";
  585. unsigned n = snprintf(NULL,0,fmt,v)+1;
  586. char buf[n];
  587. buf[0]= 0;
  588. if( snprintf(buf,n,fmt,v) == n-1 )
  589. return cmCsvInsertSymText(h,buf);
  590. _cmCsvError(_cmCsvHandleToPtr(h),kDataCvtErrCsvRC,"The double %f could not be converted to text.",v);
  591. return cmInvalidId;
  592. }
  593. cmCsvRC_t cmCsvSetCellText( cmCsvH_t h, unsigned row, unsigned col, const char* text )
  594. {
  595. cmCsvCell_t* cp;
  596. unsigned symId;
  597. if((cp = _cmCsvCellPtr(h,row,col)) == NULL )
  598. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  599. if((symId = cmCsvInsertSymText(h,text)) == cmInvalidId )
  600. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  601. cp->symId = symId;
  602. cp->flags &= !kTypeTMask;
  603. cp->flags |= kStrCsvTFl;
  604. return kOkCsvRC;
  605. }
  606. cmCsvRC_t cmCsvSetCellInt( cmCsvH_t h, unsigned row, unsigned col, int v )
  607. {
  608. cmCsvCell_t* cp;
  609. unsigned symId;
  610. if((cp = _cmCsvCellPtr(h,row,col)) == NULL )
  611. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  612. if((symId = cmCsvInsertSymInt(h,v)) == cmInvalidId )
  613. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  614. cp->symId = symId;
  615. cp->flags &= !kTypeTMask;
  616. cp->flags |= kIntCsvTFl;
  617. return kOkCsvRC;
  618. }
  619. cmCsvRC_t cmCsvSetCellUInt( cmCsvH_t h, unsigned row, unsigned col, unsigned v )
  620. {
  621. cmCsvCell_t* cp;
  622. unsigned symId;
  623. if((cp = _cmCsvCellPtr(h,row,col)) == NULL )
  624. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  625. if((symId = cmCsvInsertSymUInt(h,v)) == cmInvalidId )
  626. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  627. cp->symId = symId;
  628. cp->flags &= !kTypeTMask;
  629. cp->flags |= kIntCsvTFl;
  630. return kOkCsvRC;
  631. }
  632. cmCsvRC_t cmCsvSetCellHex( cmCsvH_t h, unsigned row, unsigned col, unsigned v )
  633. {
  634. cmCsvCell_t* cp;
  635. unsigned symId;
  636. if((cp = _cmCsvCellPtr(h,row,col)) == NULL )
  637. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  638. if((symId = cmCsvInsertSymHex(h,v)) == cmInvalidId )
  639. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  640. cp->symId = symId;
  641. cp->flags &= !kTypeTMask;
  642. cp->flags |= kIntCsvTFl;
  643. return kOkCsvRC;
  644. }
  645. cmCsvRC_t cmCsvSetCellFloat( cmCsvH_t h, unsigned row, unsigned col, float v )
  646. {
  647. cmCsvCell_t* cp;
  648. unsigned symId;
  649. if((cp = _cmCsvCellPtr(h,row,col)) == NULL )
  650. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  651. if((symId = cmCsvInsertSymFloat(h,v)) == cmInvalidId )
  652. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  653. cp->symId = symId;
  654. cp->flags &= !kTypeTMask;
  655. cp->flags |= kRealCsvTFl;
  656. return kOkCsvRC;
  657. }
  658. cmCsvRC_t cmCsvSetCellDouble( cmCsvH_t h, unsigned row, unsigned col, double v )
  659. {
  660. cmCsvCell_t* cp;
  661. unsigned symId;
  662. if((cp = _cmCsvCellPtr(h,row,col)) == NULL )
  663. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  664. if((symId = cmCsvInsertSymDouble(h,v)) == cmInvalidId )
  665. return cmErrLastRC(&_cmCsvHandleToPtr(h)->err);
  666. cp->symId = symId;
  667. cp->flags &= !kTypeTMask;
  668. cp->flags |= kRealCsvTFl;
  669. return kOkCsvRC;
  670. }
  671. cmCsvRC_t cmCsvInsertRowBefore( cmCsvH_t h, unsigned row, cmCsvCell_t** cellPtrPtr, unsigned symId, unsigned flags, unsigned lexTId )
  672. {
  673. cmCsvRC_t rc = kOkCsvRC;
  674. cmCsv_t* p = _cmCsvHandleToPtr(h);
  675. cmCsvBind_t* bp = p->bindPtr;
  676. cmCsvBind_t* nbp = NULL;
  677. unsigned i = 0;
  678. if( cellPtrPtr != NULL )
  679. *cellPtrPtr = NULL;
  680. // allocate the binder record
  681. if((nbp = cmLHeapAllocZ( p->heapH, sizeof(cmCsvBind_t))) == NULL )
  682. return _cmCsvError(p,kMemAllocErrCsvRC,"Binding record allocation failed row %i column %i.",row,0);
  683. // if a new first row is being inserted
  684. if( row == 0 )
  685. {
  686. bp = p->bindPtr;
  687. nbp->linkPtr = p->bindPtr;
  688. p->bindPtr = nbp;
  689. }
  690. else
  691. {
  692. bp = p->bindPtr;
  693. // iterate to the row before the new row
  694. for(i=0; bp != NULL; ++i )
  695. {
  696. if( i == (row-1) || bp->linkPtr == NULL )
  697. break;
  698. bp = bp->linkPtr;
  699. }
  700. assert( bp != NULL );
  701. nbp->linkPtr = bp->linkPtr;
  702. bp->linkPtr = nbp;
  703. bp = bp->linkPtr;
  704. }
  705. // update the row numbers in all cells below the new row
  706. while( bp != NULL )
  707. {
  708. cmCsvCell_t* cp = bp->rowPtr;
  709. while( cp != NULL )
  710. {
  711. cp->row += 1;
  712. cp = cp->rowPtr;
  713. }
  714. bp = bp->linkPtr;
  715. }
  716. // allocate the first cell in the new row
  717. if(cellPtrPtr != NULL && symId != cmInvalidId )
  718. {
  719. if((rc = _cmCsvAllocCell(p, symId, flags, row, 0, cellPtrPtr, lexTId )) != kOkCsvRC )
  720. return rc;
  721. nbp->rowPtr = *cellPtrPtr;
  722. }
  723. return rc;
  724. }
  725. cmCsvRC_t cmCsvAppendRow( cmCsvH_t h, cmCsvCell_t** cellPtrPtr, unsigned symId, unsigned flags, unsigned lexTId )
  726. {
  727. cmCsvRC_t rc = kOkCsvRC;
  728. cmCsv_t* p = _cmCsvHandleToPtr(h);
  729. cmCsvBind_t* nbp = NULL;
  730. unsigned newRowIdx = cmInvalidIdx;
  731. if( cellPtrPtr != NULL )
  732. *cellPtrPtr = NULL;
  733. if((rc = _cmCsvAppendNewBindRecd(p,&nbp,&newRowIdx)) != kOkCsvRC )
  734. return rc;
  735. // allocate the first cell in the new row
  736. if(cellPtrPtr != NULL && symId != cmInvalidId )
  737. {
  738. if((rc = _cmCsvAllocCell(p, symId, flags, newRowIdx, 0, cellPtrPtr, lexTId )) != kOkCsvRC )
  739. return rc;
  740. nbp->rowPtr = *cellPtrPtr;
  741. }
  742. return rc;
  743. }
  744. cmCsvRC_t cmCsvInsertColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, unsigned symId, unsigned flags, unsigned lexTId )
  745. {
  746. cmCsvRC_t rc = kOkCsvRC;
  747. cmCsvCell_t* ncp = NULL;
  748. cmCsvCell_t* cp = NULL;
  749. cmCsv_t* p = _cmCsvHandleToPtr(h);
  750. unsigned col;
  751. if(cellPtrPtr != NULL )
  752. *cellPtrPtr = NULL;
  753. // allocate the new cell
  754. if((rc = _cmCsvAllocCell(p, symId, flags, leftCellPtr->row, leftCellPtr->col+1, &ncp, lexTId )) != kOkCsvRC )
  755. return rc;
  756. // update the col values of cells to right of new cell
  757. cp = leftCellPtr->rowPtr;
  758. col = leftCellPtr->col + 1;
  759. for(; cp != NULL; ++col )
  760. {
  761. // don't update any col numbers after a blank (missing) column
  762. if( cp->col == col )
  763. cp->col += 1;
  764. cp = cp->rowPtr;
  765. }
  766. // link in the new cell
  767. ncp->rowPtr = leftCellPtr->rowPtr;
  768. leftCellPtr->rowPtr = ncp;
  769. if(cellPtrPtr != NULL )
  770. *cellPtrPtr = ncp;
  771. return rc;
  772. }
  773. cmCsvRC_t cmCsvInsertTextColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, const char* text, unsigned lexTId )
  774. {
  775. cmCsvRC_t rc;
  776. cmCsvCell_t* ncp;
  777. if( cellPtrPtr != NULL )
  778. *cellPtrPtr = NULL;
  779. if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC )
  780. if((rc = cmCsvSetCellText(h, ncp->row, ncp->col, text )) == kOkCsvRC )
  781. if( cellPtrPtr != NULL )
  782. *cellPtrPtr = ncp;
  783. return rc;
  784. }
  785. cmCsvRC_t cmCsvInsertIntColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, int val, unsigned lexTId )
  786. {
  787. cmCsvRC_t rc;
  788. cmCsvCell_t* ncp;
  789. if( cellPtrPtr != NULL )
  790. *cellPtrPtr = NULL;
  791. if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC )
  792. if((rc = cmCsvSetCellInt(h, ncp->row, ncp->col, val )) == kOkCsvRC )
  793. if( cellPtrPtr != NULL )
  794. *cellPtrPtr = ncp;
  795. return rc;
  796. }
  797. cmCsvRC_t cmCsvInsertUIntColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, unsigned val, unsigned lexTId )
  798. {
  799. cmCsvRC_t rc;
  800. cmCsvCell_t* ncp;
  801. if( cellPtrPtr != NULL )
  802. *cellPtrPtr = NULL;
  803. if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC )
  804. if((rc = cmCsvSetCellUInt(h, ncp->row, ncp->col, val )) == kOkCsvRC )
  805. if( cellPtrPtr != NULL )
  806. *cellPtrPtr = ncp;
  807. return rc;
  808. }
  809. cmCsvRC_t cmCsvInsertHexColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, unsigned val, unsigned lexTId )
  810. {
  811. cmCsvRC_t rc;
  812. cmCsvCell_t* ncp;
  813. if( cellPtrPtr != NULL )
  814. *cellPtrPtr = NULL;
  815. if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC )
  816. if((rc = cmCsvSetCellHex(h, ncp->row, ncp->col, val )) == kOkCsvRC )
  817. if( cellPtrPtr != NULL )
  818. *cellPtrPtr = ncp;
  819. return rc;
  820. }
  821. cmCsvRC_t cmCsvInsertFloatColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, float val, unsigned lexTId )
  822. {
  823. cmCsvRC_t rc;
  824. cmCsvCell_t* ncp;
  825. if( cellPtrPtr != NULL )
  826. *cellPtrPtr = NULL;
  827. if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC )
  828. if((rc = cmCsvSetCellFloat(h, ncp->row, ncp->col, val )) == kOkCsvRC )
  829. if( cellPtrPtr != NULL )
  830. *cellPtrPtr = ncp;
  831. return rc;
  832. }
  833. cmCsvRC_t cmCsvInsertDoubleColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, double val, unsigned lexTId )
  834. {
  835. cmCsvRC_t rc;
  836. cmCsvCell_t* ncp;
  837. if( cellPtrPtr != NULL )
  838. *cellPtrPtr = NULL;
  839. if((rc = cmCsvInsertColAfter(h, leftCellPtr, &ncp, cmInvalidId, 0, lexTId )) == kOkCsvRC )
  840. if((rc = cmCsvSetCellDouble(h, ncp->row, ncp->col, val )) == kOkCsvRC )
  841. if( cellPtrPtr != NULL )
  842. *cellPtrPtr = ncp;
  843. return rc;
  844. }
  845. cmCsvRC_t cmCsvWrite( cmCsvH_t h, const char* fn )
  846. {
  847. FILE* fp = NULL;
  848. cmCsvRC_t rc = kOkCsvRC;
  849. cmCsv_t* p = _cmCsvHandleToPtr(h);
  850. cmCsvBind_t* bp = p->bindPtr;
  851. if((fp = fopen(fn,"wt")) == NULL )
  852. return _cmCsvError(p,kFileCreateErrCsvRC,"Unable to create the output CSV file:'%s'.",fn);
  853. bp = p->bindPtr;
  854. // for each row
  855. while( bp != NULL )
  856. {
  857. cmCsvCell_t* cp = bp->rowPtr;
  858. unsigned col = 0;
  859. // for each column
  860. for(; cp != NULL; ++col )
  861. {
  862. // skip blank columns
  863. if( cp->col == col )
  864. {
  865. const char* tp;
  866. if((tp = cmSymTblLabel(p->symTblH,cp->symId)) == NULL )
  867. return _cmCsvError(p,kSymTblErrCsvRC,"Unable to locate the symbol text for cell at row:%i col:%i.",cp->row,cp->col);
  868. if( cmIsFlag(cp->flags,kTextTMask) )
  869. fprintf(fp,"\"");
  870. fputs(tp,fp);
  871. if( cmIsFlag(cp->flags,kTextTMask) )
  872. fprintf(fp,"\"");
  873. cp = cp->rowPtr;
  874. }
  875. if( cp == NULL )
  876. fprintf(fp,"\n"); // end of row
  877. else
  878. fprintf(fp,","); // between columns
  879. }
  880. bp = bp->linkPtr;
  881. }
  882. fclose(fp);
  883. return rc;
  884. }
  885. cmCsvRC_t cmCsvPrint( cmCsvH_t h, unsigned rowCnt )
  886. {
  887. cmCsv_t* p = _cmCsvHandleToPtr(h);
  888. cmCsvBind_t* bp = p->bindPtr;
  889. unsigned i;
  890. for(i=0; bp!=NULL && i<rowCnt; ++i,bp=bp->linkPtr)
  891. {
  892. cmCsvCell_t* cp = bp->rowPtr;
  893. unsigned j;
  894. for(j=0; cp!=NULL; ++j)
  895. {
  896. if( cp->col == j )
  897. {
  898. const char* tp;
  899. if((tp = cmSymTblLabel(p->symTblH,cp->symId)) == NULL )
  900. _cmCsvError(p,kSymTblErrCsvRC,"The text associated with the symbol '%i' was not found.",cp->symId);
  901. fputs(tp,stdin);
  902. }
  903. cp=cp->rowPtr;
  904. if( cp == NULL )
  905. printf("\n");
  906. else
  907. printf(",");
  908. }
  909. }
  910. return kOkCsvRC;
  911. }