File: ready_string_finder.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 89,936 kB
  • sloc: cpp: 201,786; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 272; sql: 72; ruby: 24
file content (52 lines) | stat: -rw-r--r-- 1,402 bytes parent folder | download | duplicates (4)
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
#include "utils/platform/ready_string_finder.h"

#include <sstream>
#include <cstddef>

namespace TangoTest::platform
{
ReadyStringFinder::ReadyStringFinder(const std::string &filename) :
    m_file{filename}
{
    if(m_file.bad())
    {
        std::stringstream ss;
        ss << "failed to open \"" << filename << "\"";
        throw std::runtime_error(ss.str());
    }
}

bool ReadyStringFinder::check_for_ready_string(const std::string &ready_string)
{
    while(true)
    {
        std::string line;
        std::getline(m_file, line);

        // Didn't hit a \n, so "put the line back" so we can try again
        // next time.
        if(m_file.eof())
        {
            auto offset = -static_cast<ptrdiff_t>(line.size());
            // clear needs to happen here.
            // The reason being that on macOS seekg fails when
            // fail bit is set. We hope that it is a libc++ thing
            // and not a implementation detail that could bite us
            // again when implementing the Windows version.
            m_file.clear();
            m_file.seekg(offset, std::ios_base::cur);
            return false;
        }

        if(m_file.fail())
        {
            throw std::runtime_error("getline() failed");
        }

        if(line.find(ready_string) != std::string::npos)
        {
            return true;
        }
    }
}
} // namespace TangoTest::platform