SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // CSVWriter.cpp created by Sloan Phillippi @ 2:03pm 3/22/2019 || s:SocketTesting, p:SocketTesting
  2.  
  3. #include "pch.h"
  4. #include "CSVWriter.h"
  5. #include <exception>
  6. #include <iostream>
  7. #include <string>
  8. #include <sstream>
  9.  
  10. struct DataFileAlreadyOpenException : std::exception { const char* what() const noexcept override { return "File already open."; } };
  11.  
  12. #pragma region Writer
  13. struct NoDataForWritingException : std::exception { const char* what() const noexcept override { return "No data to write!"; } };
  14.  
  15. CsvWriter::CsvWriter(const std::string& _fileName)
  16. {
  17.     try
  18.     {
  19.         if (m_dataFile.is_open())
  20.         {
  21.             throw DataFileAlreadyOpenException();
  22.         }
  23.     }
  24.     catch (std::exception& e)
  25.     {
  26.         std::cerr << e.what() << std::endl;
  27.     }
  28.     m_dataFile.open(_fileName, std::ofstream::out | std::ofstream::trunc);
  29. }
  30. CsvWriter::~CsvWriter()
  31. {
  32.     m_dataFile.close();
  33. }
  34.  
  35. void CsvWriter::EmplaceData(std::vector<float> _entry)
  36. {
  37.     m_data.emplace_back(_entry);
  38. }
  39. void CsvWriter::WriteToFile()
  40. {
  41.     try
  42.     {
  43.         if (m_data.empty())
  44.         {
  45.             throw NoDataForWritingException();
  46.         }
  47.     }
  48.     catch (std::exception& e)
  49.     {
  50.         std::cerr << e.what() << std::endl;
  51.     }
  52.  
  53.     for (auto& row : m_data)
  54.     {
  55.         for (auto column : row)
  56.         {
  57.             m_dataFile << column << ",";
  58.         }
  59.         m_dataFile << std::endl;
  60.     }
  61. }
  62. #pragma endregion
  63.  
  64. #pragma region Reader
  65. struct NoDataForReadingException : std::exception { const char* what() const noexcept override { return "Data entry empty!"; } };
  66. CsvReader::CsvReader(const std::string& _fileName)
  67. {
  68.     try
  69.     {
  70.         if (m_dataFile.is_open())
  71.         {
  72.             throw DataFileAlreadyOpenException();
  73.         }
  74.     }
  75.     catch (std::exception& e)
  76.     {
  77.         std::cerr << e.what() << std::endl;
  78.     }
  79.     m_dataFile.open(_fileName);
  80. }
  81. CsvReader::~CsvReader()
  82. {
  83.     m_dataFile.close();
  84. }
  85. void CsvReader::ReadFromFile()
  86. {
  87.     std::string line;
  88.     std::vector<std::vector<std::string>> parsedCsv;
  89.     while (std::getline(m_dataFile, line))
  90.     {
  91.         std::stringstream lineStream(line);
  92.         std::string cell;
  93.         std::vector<std::string> parsedRow;
  94.         while (std::getline(lineStream, cell, ','))
  95.         {
  96.             parsedRow.push_back(cell);
  97.         }
  98.         parsedCsv.push_back(parsedRow);
  99.     }
  100.  
  101.     m_data.clear();
  102.  
  103.     for (auto row : parsedCsv)
  104.     {
  105.         std::vector<float> converted;
  106.         for (const auto& column : row)
  107.         {
  108.             converted.emplace_back(std::stof(column));
  109.         }
  110.         m_data.emplace_back(converted);
  111.     }
  112. }
  113. void CsvReader::GetData(std::vector<std::vector<float>>* _data)
  114. {
  115.     *_data = m_data;
  116. }
  117. std::vector<std::vector<float>> CsvReader::GetData()
  118. {
  119.     CheckData();
  120.  
  121.     return m_data;
  122. }
  123. void CsvReader::CheckData()
  124. {
  125.     try
  126.     {
  127.         if (m_data.empty())
  128.         {
  129.             throw NoDataForReadingException();
  130.         }
  131.     }
  132.     catch (std::exception& e)
  133.     {
  134.         std::cerr << e.what() << std::endl;
  135.     }
  136. }
  137. #pragma endregion
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top