diff --git a/NEWS.md b/NEWS.md index f7eeae3a..2b3dea98 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,7 @@ Red Panda C++ Version 1.0.2 - enhancement: better display when input with IM under column mode - enhancement: better display current lines under column mode - change: test to use utf-8 as the default encoding (prepare to use libclang to implement parser) - - fix: auto syntax check use wrong charset, if a file in editing is not encoded with ANSI encoding + - fix: auto syntax check fail, if the file is not gbk and includes files encoded with utf8 - enhancement: timeout for problem case test - enhancement: slightly reduce start up time - enhancement: use icon to indicate missing project files in the project view diff --git a/RedPandaIDE/compiler/compiler.cpp b/RedPandaIDE/compiler/compiler.cpp index a09f747d..a8d44b47 100644 --- a/RedPandaIDE/compiler/compiler.cpp +++ b/RedPandaIDE/compiler/compiler.cpp @@ -181,6 +181,11 @@ Settings::PCompilerSet Compiler::compilerSet() return pSettings->compilerSets().defaultSet(); } +QByteArray Compiler::pipedText() +{ + return QByteArray(); +} + void Compiler::processOutput(QString &line) { if (line == COMPILE_PROCESS_END) { @@ -276,7 +281,7 @@ void Compiler::stopCompile() mStop = true; } -QString Compiler::getCharsetArgument(const QByteArray& encoding) +QString Compiler::getCharsetArgument(const QByteArray& encoding, bool checkSyntax) { QString result; if (compilerSet()->autoAddCharsetParams() && encoding != ENCODING_ASCII @@ -297,9 +302,13 @@ QString Compiler::getCharsetArgument(const QByteArray& encoding) } else { execEncodingName = compilerSetExecCharset; } - if (encodingName!=execEncodingName) + if (checkSyntax) { + result += QString(" -finput-charset=%1") + .arg(encodingName); + } else if (encodingName!=execEncodingName) { result += QString(" -finput-charset=%1 -fexec-charset=%2") .arg(encodingName, execEncodingName); + } } return result; } @@ -551,7 +560,7 @@ QString Compiler::parseFileIncludesForAutolink( return result; } -void Compiler::runCommand(const QString &cmd, const QString &arguments, const QString &workingDir, const QString& inputText) +void Compiler::runCommand(const QString &cmd, const QString &arguments, const QString &workingDir, const QByteArray& inputText) { QProcess process; mStop = false; @@ -591,11 +600,17 @@ void Compiler::runCommand(const QString &cmd, const QString &arguments, const Q }); process.start(); process.waitForStarted(5000); - if (!inputText.isEmpty()) - process.write(inputText.toLocal8Bit()); - process.closeWriteChannel(); + if (!inputText.isEmpty()) { + process.write(inputText); + process.waitForFinished(0); + } + bool writeChannelClosed = false; while (true) { - process.waitForFinished(1000); + if (process.bytesToWrite()==0 && !writeChannelClosed ) { + writeChannelClosed=true; + process.closeWriteChannel(); + } + process.waitForFinished(100); if (process.state()!=QProcess::Running) { break; } diff --git a/RedPandaIDE/compiler/compiler.h b/RedPandaIDE/compiler/compiler.h index aa5a5c27..df680b74 100644 --- a/RedPandaIDE/compiler/compiler.h +++ b/RedPandaIDE/compiler/compiler.h @@ -62,9 +62,9 @@ protected: protected: virtual Settings::PCompilerSet compilerSet(); virtual bool prepareForCompile() = 0; - virtual QString pipedText() = 0; + virtual QByteArray pipedText(); virtual bool prepareForRebuild() = 0; - virtual QString getCharsetArgument(const QByteArray& encoding); + virtual QString getCharsetArgument(const QByteArray& encoding,bool onlyCheckSyntax); virtual QString getCCompileArguments(bool checkSyntax); virtual QString getCppCompileArguments(bool checkSyntax); virtual QString getCIncludeArguments(); @@ -77,7 +77,7 @@ protected: PCppParser& parser); void log(const QString& msg); void error(const QString& msg); - void runCommand(const QString& cmd, const QString& arguments, const QString& workingDir, const QString& inputText=QString()); + void runCommand(const QString& cmd, const QString& arguments, const QString& workingDir, const QByteArray& inputText=QByteArray()); protected: bool mSilent; diff --git a/RedPandaIDE/compiler/executablerunner.cpp b/RedPandaIDE/compiler/executablerunner.cpp index 77e2843e..23060659 100644 --- a/RedPandaIDE/compiler/executablerunner.cpp +++ b/RedPandaIDE/compiler/executablerunner.cpp @@ -178,13 +178,20 @@ void ExecutableRunner::run() mProcess->waitForStarted(5000); if (mProcess->state()==QProcess::Running && redirectInput()) { mProcess->write(readFileToByteArray(redirectInputFilename())); + mProcess->waitForFinished(0); } bool writeChannelClosed = false; while (true) { + if (mProcess->bytesToWrite()==0 && redirectInput() && !writeChannelClosed) { + writeChannelClosed=true; + mProcess->closeWriteChannel(); + } mProcess->waitForFinished(mWaitForFinishTime); if (mProcess->state()!=QProcess::Running) { break; } + if (errorOccurred) + break; if (mStop) { mProcess->terminate(); if (mProcess->waitForFinished(1000)) { @@ -198,10 +205,6 @@ void ExecutableRunner::run() } break; } - if (mProcess->bytesToWrite()==0 && redirectInput() && !writeChannelClosed) { - writeChannelClosed=true; - mProcess->closeWriteChannel(); - } #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) if (mStartConsole && !mPausing && pBuf) { if (strncmp(pBuf,"FINISHED",sizeof("FINISHED"))==0) { @@ -229,8 +232,6 @@ void ExecutableRunner::run() } } #endif - if (errorOccurred) - break; } #ifdef Q_OS_WIN if (pBuf) diff --git a/RedPandaIDE/compiler/filecompiler.cpp b/RedPandaIDE/compiler/filecompiler.cpp index 4424ad0c..686d4666 100644 --- a/RedPandaIDE/compiler/filecompiler.cpp +++ b/RedPandaIDE/compiler/filecompiler.cpp @@ -54,7 +54,7 @@ bool FileCompiler::prepareForCompile() } } - mArguments += getCharsetArgument(mEncoding); + mArguments += getCharsetArgument(mEncoding, mOnlyCheckSyntax); QString strFileType; switch(fileType) { case FileType::CSource: @@ -90,11 +90,6 @@ bool FileCompiler::prepareForCompile() return true; } -QString FileCompiler::pipedText() -{ - return QString(); -} - bool FileCompiler::prepareForRebuild() { QString exeName = getCompiledExecutableName(mFilename); diff --git a/RedPandaIDE/compiler/filecompiler.h b/RedPandaIDE/compiler/filecompiler.h index 847d6d01..bb78caf7 100644 --- a/RedPandaIDE/compiler/filecompiler.h +++ b/RedPandaIDE/compiler/filecompiler.h @@ -32,10 +32,6 @@ protected: private: QByteArray mEncoding; - // Compiler interface -protected: - QString pipedText() override; - // Compiler interface protected: bool prepareForRebuild() override; diff --git a/RedPandaIDE/compiler/ojproblemcasesrunner.cpp b/RedPandaIDE/compiler/ojproblemcasesrunner.cpp index 22618642..0347980e 100644 --- a/RedPandaIDE/compiler/ojproblemcasesrunner.cpp +++ b/RedPandaIDE/compiler/ojproblemcasesrunner.cpp @@ -92,10 +92,15 @@ void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase) process.write(readFileToByteArray(problemCase->inputFileName)); else process.write(problemCase->input.toUtf8()); + process.waitForFinished(0); } elapsedTimer.start(); while (true) { + if (process.bytesToWrite()==0 && !writeChannelClosed) { + writeChannelClosed = true; + process.closeWriteChannel(); + } process.waitForFinished(mWaitForFinishTime); if (process.state()!=QProcess::Running) { break; @@ -113,10 +118,6 @@ void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase) } if (errorOccurred) break; - if (process.bytesToWrite()==0 && !writeChannelClosed) { - writeChannelClosed = true; - process.closeWriteChannel(); - } readed = process.read(mBufferSize); buffer += readed; if (buffer.length()>=mBufferSize || noOutputTime > mOutputRefreshTime) { diff --git a/RedPandaIDE/compiler/projectcompiler.cpp b/RedPandaIDE/compiler/projectcompiler.cpp index d277d312..6c2cd069 100644 --- a/RedPandaIDE/compiler/projectcompiler.cpp +++ b/RedPandaIDE/compiler/projectcompiler.cpp @@ -461,11 +461,6 @@ void ProjectCompiler::setOnlyClean(bool newOnlyClean) mOnlyClean = newOnlyClean; } -QString ProjectCompiler::pipedText() -{ - return QString(); -} - bool ProjectCompiler::prepareForRebuild() { //we use make argument to clean diff --git a/RedPandaIDE/compiler/projectcompiler.h b/RedPandaIDE/compiler/projectcompiler.h index 53b7f923..a4f82236 100644 --- a/RedPandaIDE/compiler/projectcompiler.h +++ b/RedPandaIDE/compiler/projectcompiler.h @@ -48,7 +48,6 @@ private: bool mOnlyClean; protected: bool prepareForCompile() override; - QString pipedText() override; bool prepareForRebuild() override; }; diff --git a/RedPandaIDE/compiler/runner.cpp b/RedPandaIDE/compiler/runner.cpp index 3771a923..bb1f8c42 100644 --- a/RedPandaIDE/compiler/runner.cpp +++ b/RedPandaIDE/compiler/runner.cpp @@ -24,7 +24,7 @@ Runner::Runner(const QString &filename, const QString &arguments, const QString mFilename(filename), mArguments(arguments), mWorkDir(workDir), - mWaitForFinishTime(1000) + mWaitForFinishTime(100) { } diff --git a/RedPandaIDE/compiler/stdincompiler.cpp b/RedPandaIDE/compiler/stdincompiler.cpp index dbc81d1b..2767933e 100644 --- a/RedPandaIDE/compiler/stdincompiler.cpp +++ b/RedPandaIDE/compiler/stdincompiler.cpp @@ -18,10 +18,11 @@ #include "compilermanager.h" #include #include +#include #include "../platform.h" StdinCompiler::StdinCompiler(const QString &filename,const QByteArray& encoding, const QString& content,bool silent, bool onlyCheckSyntax): - Compiler(filename,silent,onlyCheckSyntax), + Compiler(filename,silent, onlyCheckSyntax), mContent(content), mEncoding(encoding) { @@ -39,8 +40,9 @@ bool StdinCompiler::prepareForCompile() if (fileType == FileType::Other) fileType = FileType::CppSource; QString strFileType; - if (mEncoding!=ENCODING_ASCII && (!mOnlyCheckSyntax || mEncoding != ENCODING_UTF8 )) - mArguments += getCharsetArgument(mEncoding); + if (mEncoding!=ENCODING_ASCII) { + mArguments += getCharsetArgument(mEncoding, mOnlyCheckSyntax); + } switch(fileType) { case FileType::CSource: mArguments += " -x c - "; @@ -78,9 +80,17 @@ bool StdinCompiler::prepareForCompile() return true; } -QString StdinCompiler::pipedText() +QByteArray StdinCompiler::pipedText() { - return mContent; + if (mEncoding == ENCODING_ASCII) + return mContent.toLatin1(); + + QTextCodec* codec = QTextCodec::codecForName(mEncoding); + if (codec) { + return codec->fromUnicode(mContent); + } else { + return mContent.toLocal8Bit(); + } } bool StdinCompiler::prepareForRebuild() diff --git a/RedPandaIDE/compiler/stdincompiler.h b/RedPandaIDE/compiler/stdincompiler.h index b43f3569..e27cf68f 100644 --- a/RedPandaIDE/compiler/stdincompiler.h +++ b/RedPandaIDE/compiler/stdincompiler.h @@ -35,7 +35,7 @@ private: // Compiler interface protected: - QString pipedText() override; + QByteArray pipedText() override; // Compiler interface protected: diff --git a/RedPandaIDE/debugger.cpp b/RedPandaIDE/debugger.cpp index a342185b..99a81f6f 100644 --- a/RedPandaIDE/debugger.cpp +++ b/RedPandaIDE/debugger.cpp @@ -1415,9 +1415,6 @@ void DebugReader::run() break; } if (mStop) { - mProcess->closeReadChannel(QProcess::StandardOutput); - mProcess->closeReadChannel(QProcess::StandardError); - mProcess->closeWriteChannel(); mProcess->terminate(); mProcess->kill(); break; @@ -2401,17 +2398,19 @@ void DebugTarget::run() mStartSemaphore.release(1); if (mProcess->state()==QProcess::Running && !mInputFile.isEmpty()) { mProcess->write(readFileToByteArray(mInputFile)); - mProcess->closeWriteChannel(); + mProcess->waitForFinished(0); } + bool writeChannelClosed = false; while (true) { + if (mProcess->bytesToWrite()==0 && !writeChannelClosed) { + writeChannelClosed = true; + mProcess->closeWriteChannel(); + } mProcess->waitForFinished(1); if (mProcess->state()!=QProcess::Running) { break; } if (mStop) { - mProcess->closeReadChannel(QProcess::StandardOutput); - mProcess->closeReadChannel(QProcess::StandardError); - mProcess->closeWriteChannel(); mProcess->terminate(); mProcess->kill(); break;