1 /*
2     SPDX-FileCopyrightText: 2016 Anton Anikin <anton.anikin@htower.ru>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <QString>
10 #include <QTextStream>
11 #include <QVector>
12 
13 namespace KDevelop {
14 
15 struct KDevFormatLine
16 {
17     QStringList wildcards;
18     QString command;
19 };
20 
21 class KDevFormatFile
22 {
23 public:
24     KDevFormatFile(const QString& origFilePath, const QString& tempFilePath);
25 
26     bool find();
27     bool read();
28     bool apply();
29 
30 private:
31     bool executeCommand(QString command);
32 
33     /// This is logically a static constant, but since this class is instantiated
34     /// once in non-test code, making it a non-static member is safe and efficient.
35     const QString formatFileName;
36 
37     const QString m_origFilePath;
38     const QString m_tempFilePath;
39 
40     QVector<KDevFormatLine> m_formatLines;
41 };
42 
43 class AutoFlushingQTextStream : public QTextStream
44 {
45 public:
AutoFlushingQTextStream(FILE * f,QIODevice::OpenMode o)46     AutoFlushingQTextStream(FILE* f, QIODevice::OpenMode o)
47         : QTextStream(f, o)
48     {
49     }
50 
51     template<typename T>
52     AutoFlushingQTextStream& operator <<(T&& s)
53     {
54         *(( QTextStream* ) this) << std::forward<T>(s);
55         flush();
56         return *this;
57     }
58 };
59 
qStdOut()60 inline AutoFlushingQTextStream& qStdOut()
61 {
62     static AutoFlushingQTextStream s{stdout, QIODevice::WriteOnly | QIODevice::Text};
63     return s;
64 }
65 
66 }
67 
68 Q_DECLARE_TYPEINFO(KDevelop::KDevFormatLine, Q_MOVABLE_TYPE);
69