File indexing completed on 2024-05-12 05:46:33

0001 /*
0002     Copyright 2016 Anton Anikin <anton.anikin@htower.ru>
0003 
0004     This program is free software; you can redistribute it and/or modify
0005     it under the terms of the GNU General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or
0007     (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License along
0015     with this program; if not, write to the Free Software Foundation, Inc.,
0016     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0017 */
0018 
0019 #pragma once
0020 
0021 #include <QString>
0022 #include <QTextStream>
0023 #include <QVector>
0024 
0025 namespace KDevelop {
0026 
0027 struct KDevFormatLine
0028 {
0029     QStringList wildcards;
0030     QString command;
0031 };
0032 
0033 class KDevFormatFile
0034 {
0035 public:
0036     KDevFormatFile(const QString& origFilePath, const QString& tempFilePath);
0037 
0038     bool find();
0039     bool read();
0040     bool apply();
0041 
0042 private:
0043     bool executeCommand(QString command);
0044 
0045     /// This is logically a static constant, but since this class is instantiated
0046     /// once in non-test code, making it a non-static member is safe and efficient.
0047     const QString formatFileName;
0048 
0049     const QString m_origFilePath;
0050     const QString m_tempFilePath;
0051 
0052     QVector<KDevFormatLine> m_formatLines;
0053 };
0054 
0055 class AutoFlushingQTextStream : public QTextStream
0056 {
0057 public:
0058     AutoFlushingQTextStream(FILE* f, QIODevice::OpenMode o)
0059         : QTextStream(f, o)
0060     {
0061     }
0062 
0063     template<typename T>
0064     AutoFlushingQTextStream& operator <<(T&& s)
0065     {
0066         *(( QTextStream* ) this) << std::forward<T>(s);
0067         flush();
0068         return *this;
0069     }
0070 };
0071 
0072 inline AutoFlushingQTextStream& qStdOut()
0073 {
0074     static AutoFlushingQTextStream s{stdout, QIODevice::WriteOnly | QIODevice::Text};
0075     return s;
0076 }
0077 
0078 }
0079 
0080 Q_DECLARE_TYPEINFO(KDevelop::KDevFormatLine, Q_MOVABLE_TYPE);