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.

cmCsv.h 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef cmCsv_h
  2. #define cmCsv_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //( { file_desc:"Comma seperated value file reader and writer." kw[file] }
  7. enum
  8. {
  9. kOkCsvRC = 0,
  10. kMemAllocErrCsvRC,
  11. kLexErrCsvRC,
  12. kHashTblErrCsvRC,
  13. kSyntaxErrCsvRC,
  14. kFileOpenErrCsvRC,
  15. kFileCreateErrCsvRC,
  16. kFileReadErrCsvRC,
  17. kFileSeekErrCsvRC,
  18. kFileCloseErrCsvRC,
  19. kDataCvtErrCsvRC,
  20. kCellNotFoundCsvRC,
  21. kDuplicateLexCsvId
  22. };
  23. typedef unsigned cmCsvRC_t;
  24. typedef cmHandle_t cmCsvH_t;
  25. enum
  26. {
  27. kIntCsvTFl = 0x01,
  28. kHexCsvTFl = 0x02,
  29. kRealCsvTFl = 0x04,
  30. kIdentCsvTFl = 0x08,
  31. kStrCsvTFl = 0x10,
  32. kUdefCsvTFl = 0x20,
  33. kNumberTMask = kIntCsvTFl | kHexCsvTFl | kRealCsvTFl,
  34. kTextTMask = kIdentCsvTFl | kStrCsvTFl,
  35. kTypeTMask = kNumberTMask | kTextTMask
  36. };
  37. // Each non-blank CSV cell is represented by a cmCsvCell_t record.
  38. // All the non-blank cells in a given row are organized as a linked
  39. // list throught 'rowPtr'.
  40. typedef struct cmCsvCell_str
  41. {
  42. unsigned row; // CSV row number
  43. unsigned col; // CSV column number
  44. struct cmCsvCell_str* rowPtr; // links together cells in this row
  45. unsigned symId; // symbol id for this cell
  46. unsigned flags; // cell flags (see kXXXCsvTFl)
  47. unsigned lexTId;
  48. } cmCsvCell_t;
  49. extern cmCsvH_t cmCsvNullHandle;
  50. cmCsvRC_t cmCsvInitialize( cmCsvH_t *hp, cmCtx_t* ctx );
  51. cmCsvRC_t cmCsvFinalize( cmCsvH_t *hp );
  52. cmCsvRC_t cmCsvInitializeFromFile( cmCsvH_t *hp, const char* fn, unsigned maxRowCnt, cmCtx_t* ctx );
  53. bool cmCsvIsValid( cmCsvH_t h);
  54. cmCsvRC_t cmCsvLastRC( cmCsvH_t h);
  55. void cmCsvClearRC( cmCsvH_t h);
  56. // Register token matchers. See cmLexRegisterToken and cmLexRegisterMatcher
  57. // for details.
  58. cmCsvRC_t cmCsvLexRegisterToken( cmCsvH_t h, unsigned id, const cmChar_t* token );
  59. cmCsvRC_t cmCsvLexRegisterMatcher( cmCsvH_t h, unsigned id, cmLexUserMatcherPtr_t funcPtr );
  60. // Return the next available lexer token id above the token id's used internally
  61. // by the object. This value is fixed after cmCsvInitialize()
  62. // and does not change for the life of the CSV object. The application is
  63. // therefore free to choose any lexer id values equal to or above the
  64. // returned value.
  65. unsigned cmCsvLexNextAvailId( cmCsvH_t h );
  66. // Set 'maxRowCnt' to 0 if there is no row limit on the file.
  67. cmCsvRC_t cmCsvParse( cmCsvH_t h, const char* buf, unsigned bufCharCnt, unsigned maxRowCnt );
  68. cmCsvRC_t cmCsvParseFile( cmCsvH_t h, const char* fn, unsigned maxRowCnt );
  69. unsigned cmCsvRowCount( cmCsvH_t h );
  70. // Return a pointer to a given cell.
  71. cmCsvCell_t* cmCsvCellPtr( cmCsvH_t h, unsigned row, unsigned col );
  72. // Return a pointer to the first cell in a given row
  73. cmCsvCell_t* cmCsvRowPtr( cmCsvH_t h, unsigned row );
  74. // Convert a cell symbold id to a value.
  75. const char* cmCsvCellSymText( cmCsvH_t h, unsigned symId );
  76. cmCsvRC_t cmCsvCellSymInt( cmCsvH_t h, unsigned symId, int* vp );
  77. cmCsvRC_t cmCsvCellSymUInt( cmCsvH_t h, unsigned symId, unsigned* vp );
  78. cmCsvRC_t cmCsvCellSymFloat( cmCsvH_t h, unsigned symId, float* vp );
  79. cmCsvRC_t cmCsvCellSymDouble( cmCsvH_t h, unsigned symId, double* vp );
  80. // Return the value associated with a cell.
  81. const char* cmCsvCellText( cmCsvH_t h, unsigned row, unsigned col ); // Returns NULL on error.
  82. int cmCsvCellInt( cmCsvH_t h, unsigned row, unsigned col ); // Returns INT_MAX on error.
  83. unsigned cmCsvCellUInt( cmCsvH_t h, unsigned row, unsigned col ); // Returns UINT_MAX on error.
  84. float cmCsvCellFloat( cmCsvH_t h, unsigned row, unsigned col ); // Returns FLT_MAX on error.
  85. double cmCsvCellDouble(cmCsvH_t h, unsigned row, unsigned col ); // Returns DBL_MAX on error.
  86. // Insert a value into the internal symbol table.
  87. unsigned cmCsvInsertSymText( cmCsvH_t h, const char* text );
  88. unsigned cmCsvInsertSymInt( cmCsvH_t h, int v );
  89. unsigned cmCsvInsertSymUInt( cmCsvH_t h, unsigned v );
  90. unsigned cmCsvInsertSymHex( cmCsvH_t h, unsigned v );
  91. unsigned cmCsvInsertSymFloat( cmCsvH_t h, float v );
  92. unsigned cmCsvInsertSymDouble( cmCsvH_t h, double v );
  93. // Set the value associated with a cell.
  94. cmCsvRC_t cmCsvSetCellIdent( cmCsvH_t h, unsigned row, unsigned col, const char* text );
  95. cmCsvRC_t cmCsvSetCellQText( cmCsvH_t h, unsigned row, unsigned col, const char* text );
  96. cmCsvRC_t cmCsvSetCellInt( cmCsvH_t h, unsigned row, unsigned col, int v );
  97. cmCsvRC_t cmCsvSetCellUInt( cmCsvH_t h, unsigned row, unsigned col, unsigned v );
  98. cmCsvRC_t cmCsvSetCellHex( cmCsvH_t h, unsigned row, unsigned col, unsigned v );
  99. cmCsvRC_t cmCsvSetCellFloat( cmCsvH_t h, unsigned row, unsigned col, float v );
  100. cmCsvRC_t cmCsvSetCellDouble( cmCsvH_t h, unsigned row, unsigned col, double v );
  101. // Insert a new row and column 0 cell just above the row assigned to 'row'.
  102. // lexTId is an arbitrary id used by the application to set the value of
  103. // cmCsvCell_t.lexTId in the new cell. There are no constraints on its value.
  104. //cmCsvRC_t cmCsvInsertRowBefore( cmCsvH_t h, unsigned row, cmCsvCell_t** cellPtrPtr, unsigned symId, unsigned flags, unsigned lexTId );
  105. // Column 0 will be added if 'cellPtrPtr'!= NULL and 'symId'!=cmInvalidId.
  106. // If cellPtrPtr and symId are not valid then 'flags' and 'lexTId' are ignored.
  107. cmCsvRC_t cmCsvAppendRow( cmCsvH_t h, cmCsvCell_t** cellPtrPtr, unsigned symId, unsigned flags, unsigned lexTId );
  108. // Insert a new cell to the right of leftCellPtr.
  109. // lexTId is an arbitrary id used by the application to set the value of
  110. // cmCsvCell_t.lexTId in the new cell. There are no constraints on its value.
  111. cmCsvRC_t cmCsvInsertColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, unsigned symId, unsigned flags, unsigned lexTId );
  112. cmCsvRC_t cmCsvInsertIdentColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, const char* val, unsigned lexTId );
  113. cmCsvRC_t cmCsvInsertQTextColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, const char* val, unsigned lexTId );
  114. cmCsvRC_t cmCsvInsertIntColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, int val, unsigned lexTId );
  115. cmCsvRC_t cmCsvInsertUIntColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, unsigned val, unsigned lexTId );
  116. cmCsvRC_t cmCsvInsertHexColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, unsigned val, unsigned lexTId );
  117. cmCsvRC_t cmCsvInsertFloatColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, float val, unsigned lexTId );
  118. cmCsvRC_t cmCsvInsertDoubleColAfter( cmCsvH_t h, cmCsvCell_t* leftCellPtr, cmCsvCell_t** cellPtrPtr, double val, unsigned lexTId );
  119. // Write the CSV object out to a file.
  120. cmCsvRC_t cmCsvWrite( cmCsvH_t h, const char* fn );
  121. cmCsvRC_t cmCsvPrint( cmCsvH_t h, unsigned rowCnt );
  122. //)
  123. #ifdef __cplusplus
  124. }
  125. #endif
  126. #endif