ArmNN
 20.05
CsvReader.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "CsvReader.hpp"
8 
9 #include <boost/tokenizer.hpp>
10 
11 #include <fstream>
12 #include <string>
13 #include <vector>
14 
15 using Tokenizer = boost::tokenizer<boost::escaped_list_separator<char>>;
16 
17 namespace armnnUtils
18 {
19 
20 CsvRow ParseLine(const std::string& csvLine)
21 {
22  Tokenizer tokenizer(csvLine);
23  CsvRow entry;
24 
25  for (const auto &token : tokenizer)
26  {
27  entry.values.push_back(armnn::stringUtils::StringTrimCopy(token));
28  }
29  return entry;
30 }
31 
32 std::vector<CsvRow> CsvReader::ParseFile(const std::string& csvFile)
33 {
34  std::vector<CsvRow> result;
35 
36  std::ifstream in(csvFile.c_str());
37  if (!in.is_open())
38  return result;
39 
40  std::string line;
41  while (getline(in, line))
42  {
43  if(!line.empty())
44  {
45  CsvRow entry = ParseLine(line);
46  result.push_back(entry);
47  }
48  }
49  return result;
50 }
51 
52 std::vector<CsvRow> CsvReader::ParseVector(const std::vector<std::string>& csvVector)
53 {
54  std::vector<CsvRow> result;
55 
56  for (auto const& line: csvVector)
57  {
58  CsvRow entry = ParseLine(line);
59  result.push_back(entry);
60  }
61  return result;
62 }
63 } // namespace armnnUtils
std::vector< std::string > values
Definition: CsvReader.hpp:15
std::string StringTrimCopy(const std::string &str, const std::string &chars="\\\")
Trim from both the start and the end of a string, returns a trimmed copy of the string.
Definition: StringUtils.hpp:85
static std::vector< CsvRow > ParseVector(const std::vector< std::string > &csvVector)
Definition: CsvReader.cpp:52
boost::tokenizer< boost::escaped_list_separator< char > > Tokenizer
Definition: CsvReader.cpp:15
static std::vector< CsvRow > ParseFile(const std::string &csvFile)
Definition: CsvReader.cpp:32
CsvRow ParseLine(const std::string &csvLine)
Definition: CsvReader.cpp:20