src/utilities/filetypes/CSVFile.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #ifndef UTILITIES_FILETYPES_CSVFILE_HPP | ||
| 7 | #define UTILITIES_FILETYPES_CSVFILE_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include "../core/Logger.hpp" | ||
| 12 | #include "../core/Path.hpp" | ||
| 13 | #include "../data/Vector.hpp" | ||
| 14 | |||
| 15 | namespace openstudio { | ||
| 16 | |||
| 17 | class Variant; | ||
| 18 | class TimeSeries; | ||
| 19 | class DateTime; | ||
| 20 | |||
| 21 | namespace detail { | ||
| 22 | class CSVFile_Impl; | ||
| 23 | } | ||
| 24 | |||
| 25 | /** Class for reading and writing CSV files. */ | ||
| 26 | class UTILITIES_API CSVFile | ||
| 27 | { | ||
| 28 | public: | ||
| 29 | /** Create a new, empty CSVFile. */ | ||
| 30 | CSVFile(); | ||
| 31 | |||
| 32 | /** Constructor with string, will throw if string is not a CSVFile. */ | ||
| 33 | CSVFile(const std::string& s); | ||
| 34 | |||
| 35 | /** Constructor with path, will throw if path does not exist or file is incorrect. */ | ||
| 36 | CSVFile(const openstudio::path& p); | ||
| 37 | |||
| 38 | /** Clones this CSVFile into a separate one. */ | ||
| 39 | CSVFile clone() const; | ||
| 40 | |||
| 41 | /** Attempt to load a CSVFile from string */ | ||
| 42 | static boost::optional<CSVFile> load(const std::string& s); | ||
| 43 | |||
| 44 | /** Attempt to load a CSVFile from path */ | ||
| 45 | static boost::optional<CSVFile> load(const openstudio::path& p); | ||
| 46 | |||
| 47 | /** Get the CSVFile as a string. */ | ||
| 48 | std::string string() const; | ||
| 49 | |||
| 50 | /** Saves this file to the current location, fails if no path is set. */ | ||
| 51 | bool save() const; | ||
| 52 | |||
| 53 | /** Saves this file to a new location. Updates the CSVFile path. */ | ||
| 54 | // cppcheck-suppress functionConst | ||
| 55 | bool saveAs(const openstudio::path& p); | ||
| 56 | |||
| 57 | /** Returns the absolute path this CSVFile was loaded from or saved to, empty for new CSVFile. */ | ||
| 58 | boost::optional<openstudio::path> path() const; | ||
| 59 | |||
| 60 | /** Sets the absolute path for this CSVFile. */ | ||
| 61 | // cppcheck-suppress functionConst | ||
| 62 | bool setPath(const openstudio::path& path); | ||
| 63 | |||
| 64 | /** Resets the absolute path for this CSVFile. */ | ||
| 65 | // cppcheck-suppress functionConst | ||
| 66 | void resetPath(); | ||
| 67 | |||
| 68 | /** Returns number of columns, this is the maximum number of columns across all rows. */ | ||
| 69 | unsigned numColumns() const; | ||
| 70 | |||
| 71 | /** Returns number of rows. */ | ||
| 72 | unsigned numRows() const; | ||
| 73 | |||
| 74 | /** Get all rows. */ | ||
| 75 | std::vector<std::vector<Variant>> rows() const; | ||
| 76 | |||
| 77 | /** Add a row. */ | ||
| 78 | // cppcheck-suppress functionConst | ||
| 79 | void addRow(const std::vector<Variant>& row); | ||
| 80 | |||
| 81 | /** Set all rows. */ | ||
| 82 | // cppcheck-suppress functionConst | ||
| 83 | void setRows(const std::vector<std::vector<Variant>>& rows); | ||
| 84 | |||
| 85 | /** Clear all rows. */ | ||
| 86 | // cppcheck-suppress functionConst | ||
| 87 | void clear(); | ||
| 88 | |||
| 89 | /** Add a column of std::vector<DateTime>, returns column index (first column is index 0). */ | ||
| 90 | unsigned addColumn(const std::vector<DateTime>& dateTimes); | ||
| 91 | |||
| 92 | /** Add a column of values in a Vector, returns column index (first column is index 0). */ | ||
| 93 | unsigned addColumn(const Vector& values); | ||
| 94 | |||
| 95 | /** Add a column of values in a std::vector<double>, returns column index (first column is index 0). */ | ||
| 96 | unsigned addColumn(const std::vector<double>& values); | ||
| 97 | |||
| 98 | /** Add a column of values in a std::vector<std::string>, returns column index (first column is index 0). */ | ||
| 99 | unsigned addColumn(const std::vector<std::string>& values); | ||
| 100 | |||
| 101 | /** Get column of DateTime values (first column is index 0). Empty vector is returned if any cell is not a valid DateTime or if column index is invalid.*/ | ||
| 102 | std::vector<DateTime> getColumnAsDateTimes(unsigned columnIndex) const; | ||
| 103 | |||
| 104 | /** Get column as a Vector (first column is index 0). Empty vector is returned if any cell is not a valid number or if column index is invalid.*/ | ||
| 105 | std::vector<double> getColumnAsDoubleVector(unsigned columnIndex) const; | ||
| 106 | |||
| 107 | /** Get column as a Vector (first column is index 0). Numeric cells will be converted to strings. Empty vector is returned if column index is invalid.*/ | ||
| 108 | std::vector<std::string> getColumnAsStringVector(unsigned columnIndex) const; | ||
| 109 | |||
| 110 | protected: | ||
| 111 | // get the impl | ||
| 112 | template <typename T> | ||
| 113 | 54 | std::shared_ptr<T> getImpl() const { | |
| 114 | 54 | return std::dynamic_pointer_cast<T>(m_impl); | |
| 115 | } | ||
| 116 | |||
| 117 | friend class detail::CSVFile_Impl; | ||
| 118 | |||
| 119 | /** Protected constructor from impl. */ | ||
| 120 | CSVFile(std::shared_ptr<detail::CSVFile_Impl> impl); | ||
| 121 | |||
| 122 | private: | ||
| 123 | // configure logging | ||
| 124 | REGISTER_LOGGER("openstudio.CSVFile"); | ||
| 125 | |||
| 126 | // pointer to implementation | ||
| 127 | std::shared_ptr<detail::CSVFile_Impl> m_impl; | ||
| 128 | }; | ||
| 129 | |||
| 130 | UTILITIES_API std::ostream& operator<<(std::ostream& os, const CSVFile& csvFile); | ||
| 131 | |||
| 132 | } // namespace openstudio | ||
| 133 | |||
| 134 | #endif //UTILITIES_FILETYPES_CSVFILE_HPP | ||
| 135 |