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 6.3KB

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