aboutsummaryrefslogtreecommitdiff
path: root/src/armnnUtils/CsvReader.cpp
blob: ba6c42c376cd758a63cab4c300f46ac1ef71d4a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "CsvReader.hpp"

#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>

#include <fstream>
#include <string>
#include <vector>

using Tokenizer = boost::tokenizer<boost::escaped_list_separator<char>>;

namespace armnnUtils
{

CsvRow ParseLine(const std::string& csvLine)
{
    Tokenizer tokenizer(csvLine);
    CsvRow entry;

    for (const auto &token : tokenizer)
    {
        entry.values.push_back(boost::trim_copy(token));
    }
    return entry;
}

std::vector<CsvRow> CsvReader::ParseFile(const std::string& csvFile)
{
    std::vector<CsvRow> result;

    std::ifstream in(csvFile.c_str());
    if (!in.is_open())
        return result;

    std::string line;
    while (getline(in, line))
    {
        if(!line.empty())
        {
            CsvRow entry = ParseLine(line);
            result.push_back(entry);
        }
    }
    return result;
}

std::vector<CsvRow> CsvReader::ParseVector(const std::vector<std::string>& csvVector)
{
    std::vector<CsvRow> result;

    for (auto const& line: csvVector)
    {
        CsvRow entry = ParseLine(line);
        result.push_back(entry);
    }
    return result;
}
} // namespace armnnUtils