aboutsummaryrefslogtreecommitdiffstats
path: root/vbench/src/vbench/core/line_reader.cpp
blob: ac1b97d2924f2b13c638e7973ed4b36526ff6651 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "line_reader.h"

namespace vbench {

namespace {
void stripCR(string &dst) {
    if (!dst.empty() && dst[dst.size() - 1] == '\r') {
        dst.resize(dst.size() - 1);
    }
}
} // namespace vbench::<unnamed>

LineReader::LineReader(Input &input)
    : _input(input)
{
}

bool
LineReader::readLine(string &dst)
{
    dst.clear();
    for (char c = _input.read(); !_input.failed(); c = _input.read()) {
        if (c != '\n') {
            dst.push_back(c);
        } else {
            stripCR(dst);
            return true;
        }
    }
    return !dst.empty();
}

} // namespace vbench