81 template <
template <
class,
class>
class C,
class T =
Cell>
class TCsvGrid final
92 #ifdef USE_EXPLICIT_MOVE_ 96 *
this = std::move(csvGrid);
113 if ((rows == 0) || (cols == 0))
115 BOOST_THROW_EXCEPTION(std::out_of_range(
"rows or cols is 0"));
118 m_grid.resize(rows,
row_type(cols));
132 LoadFromCSVFile(filename, options);
149 #ifdef USE_EXPLICIT_MOVE_ 153 std::swap(m_grid, csvGrid.m_grid);
171 if (row >= GetRowCount())
173 BOOST_THROW_EXCEPTION(std::out_of_range(
"row out of range"));
176 return *std::next(m_grid.begin(), row);
189 if (row >= GetRowCount())
191 BOOST_THROW_EXCEPTION(std::out_of_range(
"row out of range"));
194 return *std::next(m_grid.begin(), row);
202 return m_grid.empty();
210 return std::distance(m_grid.begin(), m_grid.end());
223 if (row >= GetRowCount())
225 BOOST_THROW_EXCEPTION(std::out_of_range(
"row out of range"));
228 auto n = std::next(m_grid.begin(), row);
240 m_grid.resize(rows,
row_type(defaultCols));
250 m_grid.emplace_back(cols);
260 for (
auto& row : m_grid)
275 if (row >= GetRowCount())
277 BOOST_THROW_EXCEPTION(std::out_of_range(
"row out of range"));
280 m_grid.emplace(std::next(m_grid.begin(), row), defaultCols);
292 for (
auto& row : m_grid)
294 if (col < row.GetSize())
296 row.InsertColumn(col);
309 for (
auto& row : m_grid)
336 size_t firstRowToLoad = 0,
337 size_t maxNumRowsToLoad = std::numeric_limits<size_t>::max())
339 std::ifstream csvfile{filename.c_str()};
341 if (!csvfile.is_open())
343 std::string err(
"failed to create file stream for loading: ");
344 err.append(filename);
346 BOOST_THROW_EXCEPTION(std::runtime_error(err));
352 bool rowComplete =
false;
354 while (csvfile.good() && (m_grid.size() < maxNumRowsToLoad))
357 std::getline(csvfile, line);
360 if ((csvfile.tellg() == csvfile.gcount()) || csvfile.eof())
362 if (line.compare(
"") == 0)
379 if (ContainsEndOfRow(row))
384 if ((rowCount >= firstRowToLoad) && rowComplete)
386 m_grid.emplace_back(row, options);
411 std::ofstream csvfile;
415 csvfile.open(filename.c_str(), std::ofstream::app);
419 csvfile.open(filename.c_str(), std::ofstream::trunc);
422 if (!csvfile.is_open())
424 std::string err(
"failed to create file stream for saving: ");
425 err.append(filename);
427 BOOST_THROW_EXCEPTION(std::runtime_error(err));
430 OutputCsvGridToStream(csvfile);
445 for (
const auto& rowItem : m_grid)
447 rowItem.OutputRowToStream(os);
449 if (row++ < GetRowCount() - 1)
467 std::string::difference_type numQuotes = std::count(row.begin(), row.end(),
'"');
468 return (numQuotes % 2) == 0;
479 #endif // CSVGRIDMAIN Append the contents of the grid to the end of the file if it already exists.
void AddRow(size_t cols=0)
Add a new row.
Definition: CsvGridMain.h:248
Grid class with CSV file capabilities.
Definition: CsvGridMain.h:81
void InsertRow(size_t row, size_t defaultCols=0)
Insert a new row.
Definition: CsvGridMain.h:273
Class defining a row of the grid.
Definition: CsvGridRow.h:136
void ResetGrid()
Clear the entire grid.
Definition: CsvGridMain.h:319
size_t GetRowCount() const
Get the number of rows.
Definition: CsvGridMain.h:208
TCsvGrid(const std::string &filename, eCellFormatOptions options)
Initializing constructor.
Definition: CsvGridMain.h:130
void SetRowCount(size_t rows, size_t defaultCols=0)
Resize the grid.
Definition: CsvGridMain.h:238
const row_type & operator[](size_t row) const
Const subscript operator.
Definition: CsvGridMain.h:187
bool Empty() const
Get empty state of grid.
Definition: CsvGridMain.h:200
File containing declarations relating the CSVGridRow class.
void SaveToCsvFile(const std::string &filename, eSaveToFileOptions option=eSaveToFileOptions::truncate) const
Save the grid to a CSV file.
Definition: CsvGridMain.h:408
The core_lib namespace.
Definition: AsioDefines.h:59
Class defining a single cell within a row of the grid.
Definition: CsvGridCell.h:49
Truncate existing file replacing it with the contents of the grid.
void InsertColumnInAllRows(size_t col)
Insert a new column in all rows.
Definition: CsvGridMain.h:290
void LoadFromCSVFile(const std::string &filename, eCellFormatOptions options, size_t firstRowToLoad=0, size_t maxNumRowsToLoad=std::numeric_limits< size_t >::max())
Load a csv file into the grid.
Definition: CsvGridMain.h:335
size_t GetColCount(size_t row) const
Get the number of columns for a given row.
Definition: CsvGridMain.h:221
void AddColumnToAllRows()
Add a column to each row.
Definition: CsvGridMain.h:258
eSaveToFileOptions
Enumeration controlling how file is saved.
Definition: CsvGridMain.h:43
TCsvGrid(size_t rows, size_t cols)
Initializing constructor.
Definition: CsvGridMain.h:111
void OutputCsvGridToStream(std::ostream &os) const
Output the grid to a stream object.
Definition: CsvGridMain.h:441
C< row_type, std::allocator< row_type > > container_type
typedef for container type
Definition: CsvGridMain.h:87
void CORE_LIBRARY_DLL_SHARED_API PackStdString(std::string &line)
Tidy a string obtained from getline function.
Definition: StringUtils.cpp:47
void ClearCells()
Clear the contents of all cells.
Definition: CsvGridMain.h:307
static bool ContainsEndOfRow(const std::string &row)
Check if row string contains the actual end of the CSV row.
Definition: CsvGridMain.h:465
eCellFormatOptions
Cell format options enumeration.
Definition: CsvGridRow.h:61
row_type & operator[](size_t row)
Subscript operator.
Definition: CsvGridMain.h:169
TCsvGrid(std::initializer_list< row_type > rows)
Initializer list constructor.
Definition: CsvGridMain.h:141