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.2KB

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