Release 18.08
[platform/upstream/armnn.git] / src / armnnUtils / CsvReader.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5
6 #include "CsvReader.hpp"
7
8 #include <boost/algorithm/string.hpp>
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(boost::trim_copy(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