1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "ioutputparser.h"
6
7IOutputParser::~IOutputParser()
8{
9 delete outParser;
10}
11
12void IOutputParser::appendOutputParser(IOutputParser *parser)
13{
14 if (!parser)
15 return;
16 if (outParser) {
17 outParser->appendOutputParser(parser);
18 return;
19 }
20
21 outParser = parser;
22 connect(parser, &IOutputParser::addOutput,
23 this, &IOutputParser::outputAdded, Qt::DirectConnection);
24 connect(parser, &IOutputParser::addTask,
25 this, &IOutputParser::taskAdded, Qt::DirectConnection);
26}
27
28IOutputParser *IOutputParser::takeOutputParserChain()
29{
30 IOutputParser *parser = outParser;
31 disconnect(parser, &IOutputParser::addOutput, this, &IOutputParser::outputAdded);
32 disconnect(parser, &IOutputParser::addTask, this, &IOutputParser::taskAdded);
33 outParser = nullptr;
34 return parser;
35}
36
37IOutputParser *IOutputParser::childParser() const
38{
39 return outParser;
40}
41
42void IOutputParser::setChildParser(IOutputParser *parser)
43{
44 if (outParser != parser)
45 delete outParser;
46 outParser = parser;
47 if (parser) {
48 connect(parser, &IOutputParser::addOutput,
49 this, &IOutputParser::outputAdded, Qt::DirectConnection);
50 connect(parser, &IOutputParser::addTask,
51 this, &IOutputParser::taskAdded, Qt::DirectConnection);
52 }
53}
54
55void IOutputParser::stdOutput(const QString &line, OutputPane::OutputFormat format)
56{
57 if (outParser)
58 outParser->stdOutput(line, format);
59}
60
61void IOutputParser::stdError(const QString &line)
62{
63 if (outParser)
64 outParser->stdError(line);
65}
66
67void IOutputParser::outputAdded(const QString &string, OutputPane::OutputFormat format)
68{
69 emit addOutput(string, format);
70}
71
72void IOutputParser::taskAdded(const Task &task, int linkedOutputLines, int skipLines)
73{
74 emit addTask(task, linkedOutputLines, skipLines);
75}
76
77void IOutputParser::doFlush()
78{ }
79
80bool IOutputParser::hasFatalErrors() const
81{
82 return outParser && outParser->hasFatalErrors();
83}
84
85void IOutputParser::setWorkingDirectory(const QString &workingDirectory)
86{
87 if (outParser)
88 outParser->setWorkingDirectory(workingDirectory);
89}
90
91void IOutputParser::flush()
92{
93 doFlush();
94 if (outParser)
95 outParser->flush();
96}
97
98QString IOutputParser::rightTrimmed(const QString &in)
99{
100 int pos = in.length();
101 for (; pos > 0; --pos) {
102 if (!in.at(pos - 1).isSpace())
103 break;
104 }
105 return in.mid(0, pos);
106}
107