A tool to construct HDR-images from a series of exposures.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef CSVREADER_H
  2. #define CSVREADER_H
  3. #include <string>
  4. #include <vector>
  5. #include <istream>
  6. // A Field in a CSVRow
  7. class CSVField {
  8. public:
  9. CSVField(int start, int length, bool hasQuotes);
  10. // byte offset
  11. int start;
  12. // length in bytes
  13. int length;
  14. // contains quotes (use CSVRow::unquote)
  15. bool hasQuotes;
  16. };
  17. // A Row in a CSV Document
  18. class CSVRow {
  19. public:
  20. CSVRow(std::string *row,
  21. const std::string * const filePath=0,
  22. long lineNumber=-1,
  23. char delimiter=',',
  24. char quote='"');
  25. // Return a human readable representation of a CSVRow.
  26. std::string toString() const;
  27. // Get the value of a field.
  28. // fieldIndex starts at 0.
  29. // Returns false if fieldIndex is out of bounds or
  30. // field could not be converted to the specified type.
  31. bool getFieldAsString(unsigned int fieldIndex, std::string *out) const;
  32. bool getFieldAsLong(unsigned int fieldIndex, long *out) const;
  33. bool getFieldAsInt(unsigned int fieldIndex, int *out) const;
  34. bool getFieldAsDouble(unsigned int fieldIndex, double *out) const;
  35. std::string getFilePath() const;
  36. // Get the line number
  37. // starting at 1 for the first line.
  38. // Return a value < 0 if there is no line number specified.
  39. long getLineNumber() const;
  40. unsigned int getNumberOfFields() const;
  41. // Get the original string that was quoted using some of the rules from RFC 4180
  42. // Examples: unquote("'abc'", "'") == "abc"
  43. // unquote("'ab''c'", "'") == "ab'c"
  44. // Fields containing line breaks are not supported.
  45. static std::string unquote(const std::string &fieldStr, char quoteChar);
  46. // Quotes a string using some of the rules from RFC 4180
  47. // Example: quote("ab'c", "'") == "ab''c"
  48. // Fields containing line breaks are not supported.
  49. static std::string quote(const std::string &fieldStr, char quoteChar);
  50. private:
  51. // fill m_fields
  52. void parse(char delimiter, char quote);
  53. // raw bytes from CSV document
  54. std::string *m_row;
  55. // each field
  56. std::vector<CSVField> m_fields;
  57. // file path of the CSV document or NULL
  58. const std::string * const m_filePath;
  59. long m_lineNumber;
  60. // quote character
  61. char m_quote;
  62. };
  63. // static functions to parse CSV data
  64. class CSVReader {
  65. public:
  66. // callback function signature
  67. typedef bool (*RowCallback) (const CSVRow &row, void *userData);
  68. // Parse a CSV document given by filePath.
  69. // Invokes callback function for each row with the supplied userData.
  70. // Stop parsing if callback returns false.
  71. // filePath: pointer to a file path or NULL if there is no file specified
  72. // Returns false if file cannot be opened.
  73. static bool readFromFile(const std::string filePath,
  74. RowCallback callback,
  75. void *userData,
  76. char delimiter=',',
  77. char quote='"');
  78. // Parse CSV data from stream.
  79. // Could be a std::stringstream or a std::fstream, etc.
  80. // Invokes callback function for each row with the supplied userData.
  81. // Stop parsing and return false if callback returns false.
  82. static bool readFromStream(std::istream &stream,
  83. RowCallback callback,
  84. void *userData,
  85. const std::string * const filePath=0,
  86. char delimiter=',',
  87. char quote='"');
  88. };
  89. #endif //CSVREADER_H