Skip to content

Commit

Permalink
Merge pull request #142 from d99kris/integrate-add-setcell-methods
Browse files Browse the repository at this point in the history
fixes #140 - add two SetCell methods for consistency with GetCell
  • Loading branch information
d99kris committed May 13, 2023
2 parents 19946a8 + dd4f601 commit 90f48ab
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ if(RAPIDCSV_BUILD_TESTS)
add_unit_test(test092)
add_unit_test(test093)
add_unit_test(test094)
add_unit_test(test095)

# perf tests
add_perf_test(ptest001)
Expand Down
24 changes: 24 additions & 0 deletions doc/rapidcsv_Document.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,30 @@ Set cell by name.

---

```c++
template<typename T > void SetCell (const size_t pColumnIdx, const std::string & pRowName, const T & pCell)
```
Set cell by column index and row name.
**Parameters**
- `pColumnIdx` zero-based column index.
- `pRowName` row label name.
- `pCell` cell data.
---
```c++
template<typename T > void SetCell (const std::string & pColumnName, const size_t pRowIdx, const T & pCell)
```
Set cell by column name and row index.

**Parameters**
- `pColumnName` column label name.
- `pRowIdx` zero-based row index.
- `pCell` cell data.

---

```c++
template<typename T > void SetColumn (const size_t pColumnIdx, const std::vector< T > & pColumn)
```
Expand Down
38 changes: 37 additions & 1 deletion src/rapidcsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* rapidcsv.h
*
* URL: https://github.com/d99kris/rapidcsv
* Version: 8.76
* Version: 8.77
*
* Copyright (C) 2017-2023 Kristofer Berggren
* All rights reserved.
Expand Down Expand Up @@ -1316,6 +1316,42 @@ namespace rapidcsv
SetCell<T>(static_cast<size_t>(columnIdx), static_cast<size_t>(rowIdx), pCell);
}

/**
* @brief Set cell by column index and row name.
* @param pColumnIdx zero-based column index.
* @param pRowName row label name.
* @param pCell cell data.
*/
template<typename T>
void SetCell(const size_t pColumnIdx, const std::string& pRowName, const T& pCell)
{
const int rowIdx = GetRowIdx(pRowName);
if (rowIdx < 0)
{
throw std::out_of_range("row not found: " + pRowName);
}

SetCell<T>(pColumnIdx, static_cast<size_t>(rowIdx), pCell);
}

/**
* @brief Set cell by column name and row index.
* @param pColumnName column label name.
* @param pRowIdx zero-based row index.
* @param pCell cell data.
*/
template<typename T>
void SetCell(const std::string& pColumnName, const size_t pRowIdx, const T& pCell)
{
const int columnIdx = GetColumnIdx(pColumnName);
if (columnIdx < 0)
{
throw std::out_of_range("column not found: " + pColumnName);
}

SetCell<T>(static_cast<size_t>(columnIdx), pRowIdx, pCell);
}

/**
* @brief Get column name
* @param pColumnIdx zero-based column index.
Expand Down
60 changes: 60 additions & 0 deletions tests/test095.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// test095.cpp - set cell value by mixed name / index

#include <rapidcsv.h>
#include "unittest.h"

int main()
{
int rv = 0;

std::string csvref =
"-,A,B,C\n"
"1,3,9,81\n"
"2,4,16,256\n"
;

std::string csv =
"-,A,B,C\n"
"1,0,0,0\n"
"2,0,0,0\n"
;

std::string path = unittest::TempPath();
unittest::WriteFile(path, csv);

try
{
rapidcsv::Document doc(path, rapidcsv::LabelParams(0, 0));

doc.SetCell<int>("A", 0, 3);
doc.SetCell<int>("B", 0, 9);
doc.SetCell<int>("C", 0, 81);

doc.SetCell<std::string>(0, "2", "4");
doc.SetCell<std::string>(1, "2", "16");
doc.SetCell<std::string>(2, "2", "256");

unittest::ExpectEqual(int, doc.GetCell<int>(0, 0), 3);
unittest::ExpectEqual(int, doc.GetCell<int>(1, 0), 9);
unittest::ExpectEqual(int, doc.GetCell<int>(2, 0), 81);

unittest::ExpectEqual(std::string, doc.GetCell<std::string>("A", "2"), "4");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("B", "2"), "16");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("C", "2"), "256");

doc.Save();

std::string csvread = unittest::ReadFile(path);

unittest::ExpectEqual(std::string, csvref, csvread);
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
rv = 1;
}

unittest::DeleteFile(path);

return rv;
}

0 comments on commit 90f48ab

Please sign in to comment.