1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include "stdincompiler.h"
18#include "compilermanager.h"
19#include <QFile>
20#include <QFileInfo>
21#include <QTextCodec>
22#include "qt_utils/charsetinfo.h"
23
24StdinCompiler::StdinCompiler(const QString &filename,const QByteArray& encoding, const QString& content,bool silent, bool onlyCheckSyntax):
25 Compiler(filename,silent, onlyCheckSyntax),
26 mContent(content),
27 mEncoding(encoding)
28{
29
30}
31
32bool StdinCompiler::prepareForCompile()
33{
34 log(tr("Checking file syntax..."));
35 log("------------------");
36 log(tr("- Filename: %1").arg(mFilename));
37 log(tr("- Compiler Set Name: %1").arg(compilerSet()->name()));
38 log("");
39 FileType fileType = getFileType(mFilename);
40 if (fileType == FileType::Other)
41 fileType = FileType::CppSource;
42 QString strFileType;
43 if (mEncoding!=ENCODING_ASCII) {
44 mArguments += getCharsetArgument(mEncoding,fileType, mOnlyCheckSyntax);
45 }
46 switch(fileType) {
47 case FileType::CSource:
48 mArguments += " -x c - ";
49 mArguments += getCCompileArguments(mOnlyCheckSyntax);
50 mArguments += getCIncludeArguments();
51 mArguments += getProjectIncludeArguments();
52 strFileType = "C";
53 mCompiler = compilerSet()->CCompiler();
54 break;
55 case FileType::CppSource:
56 case FileType::CppHeader:
57 case FileType::CHeader:
58 mArguments += " -x c++ - ";
59 mArguments += getCppCompileArguments(mOnlyCheckSyntax);
60 mArguments += getCppIncludeArguments();
61 mArguments += getProjectIncludeArguments();
62 strFileType = "C++";
63 mCompiler = compilerSet()->cppCompiler();
64 break;
65 default:
66 throw CompileError(tr("Can't find the compiler for file %1").arg(mFilename));
67 }
68 if (!mOnlyCheckSyntax)
69 mArguments += getLibraryArguments(fileType);
70
71 if (!fileExists(mCompiler)) {
72 throw CompileError(tr("The Compiler '%1' doesn't exists!").arg(mCompiler));
73 }
74
75 log(tr("Processing %1 source file:").arg(strFileType));
76 log("------------------");
77 log(tr("%1 Compiler: %2").arg(strFileType).arg(mCompiler));
78 log(tr("Command: %1 %2").arg(extractFileName(mCompiler)).arg(mArguments));
79 mDirectory = extractFileDir(mFilename);
80 return true;
81}
82
83QByteArray StdinCompiler::pipedText()
84{
85 if (mEncoding == ENCODING_ASCII)
86 return mContent.toLatin1();
87
88 QTextCodec* codec = QTextCodec::codecForName(mEncoding);
89 if (codec) {
90 return codec->fromUnicode(mContent);
91 } else {
92 return mContent.toLocal8Bit();
93 }
94}
95
96bool StdinCompiler::prepareForRebuild()
97{
98 return true;
99}
100