From bf245a78424cb76fb7916cc4994bc95c28a1f3fb Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Tue, 2 Apr 2024 19:13:55 +0800 Subject: [PATCH] fix: can't execute bat file --- RedPandaIDE/mainwindow.cpp | 17 +- .../settingsdialog/toolsgeneralwidget.cpp | 5 + .../settingsdialog/toolsgeneralwidget.ui | 187 +++++++++--------- RedPandaIDE/toolsmanager.cpp | 5 +- RedPandaIDE/toolsmanager.h | 1 + RedPandaIDE/translations/RedPandaIDE_pt_BR.ts | 71 +++---- RedPandaIDE/translations/RedPandaIDE_zh_CN.ts | 79 ++++---- RedPandaIDE/translations/RedPandaIDE_zh_TW.ts | 73 +++---- RedPandaIDE/utils.cpp | 24 +++ RedPandaIDE/utils.h | 3 + 10 files changed, 264 insertions(+), 201 deletions(-) diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index df7ba47d..ec529019 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -1233,12 +1233,12 @@ void MainWindow::executeTool(PToolItem item) case ToolItemInputOrigin::CurrentSelection: e=mEditorList->getEditor(); if (e) - inputContent=e->selText().toUtf8(); + inputContent=stringToByteArray(e->selText(), item->isUTF8); break; case ToolItemInputOrigin::WholeDocument: e=mEditorList->getEditor(); if (e) - inputContent=e->text().toUtf8(); + inputContent=stringToByteArray(e->text(), item->isUTF8); break; } QString command; @@ -1253,8 +1253,10 @@ void MainWindow::executeTool(PToolItem item) file.write(escapeCommandForPlatformShell(program, params).toLocal8Bit() + LINE_BREAKER); file.close(); - command = escapeCommandForPlatformShell(file.fileName(), params); - output = runAndGetOutput(file.fileName(), workDir, params, inputContent); + QString cmd="cmd"; + QStringList args{"/C",file.fileName()}; + command = escapeCommandForPlatformShell(cmd, args); + output = runAndGetOutput(cmd, workDir, args, inputContent); } } else { command = escapeCommandForPlatformShell(program, params); @@ -1263,7 +1265,8 @@ void MainWindow::executeTool(PToolItem item) switch(item->outputTarget) { case ToolItemOutputTarget::RedirectToToolsOutputPanel: logToolsOutput(tr(" - Command: %1").arg(command)); - logToolsOutput(QString::fromUtf8(output)); + logToolsOutput(""); + logToolsOutput(byteArrayToString(output, item->isUTF8)); stretchMessagesPanel(true); ui->tabMessages->setCurrentWidget(ui->tabToolsOutput); break; @@ -1272,12 +1275,12 @@ void MainWindow::executeTool(PToolItem item) case ToolItemOutputTarget::RepalceWholeDocument: e=mEditorList->getEditor(); if (e) - e->replaceContent(QString::fromUtf8(output)); + e->replaceContent(byteArrayToString(output, item->isUTF8)); break; case ToolItemOutputTarget::ReplaceCurrentSelection: e=mEditorList->getEditor(); if (e) - e->setSelText(QString::fromUtf8(output)); + e->setSelText(byteArrayToString(output, item->isUTF8)); break; } } diff --git a/RedPandaIDE/settingsdialog/toolsgeneralwidget.cpp b/RedPandaIDE/settingsdialog/toolsgeneralwidget.cpp index 8ce6bedc..3adca48b 100644 --- a/RedPandaIDE/settingsdialog/toolsgeneralwidget.cpp +++ b/RedPandaIDE/settingsdialog/toolsgeneralwidget.cpp @@ -73,6 +73,8 @@ ToolsGeneralWidget::ToolsGeneralWidget(const QString &name, const QString &group this, &ToolsGeneralWidget::onEdited); connect(ui->cbOutput, qOverload(&QComboBox::currentIndexChanged), this, &ToolsGeneralWidget::onEdited); + connect(ui->chkUTF8, &QCheckBox::stateChanged, + this, &ToolsGeneralWidget::onEdited); } ToolsGeneralWidget::~ToolsGeneralWidget() @@ -118,6 +120,7 @@ void ToolsGeneralWidget::finishEditing(bool askSave) item->title = ui->txtTitle->text(); item->inputOrigin = static_cast(ui->cbInput->currentIndex()); item->outputTarget = static_cast(ui->cbOutput->currentIndex()); + item->isUTF8 = ui->chkUTF8->isChecked(); mToolsModel.updateTool(mCurrentEditingRow, item); } @@ -138,6 +141,7 @@ void ToolsGeneralWidget::prepareEdit(int row) ui->txtTitle->setText(item->title); ui->cbInput->setCurrentIndex(static_cast(item->inputOrigin)); ui->cbOutput->setCurrentIndex(static_cast(item->outputTarget)); + ui->chkUTF8->setChecked(item->isUTF8); showEditPanel(true); ui->txtTitle->setFocus(); mEdited = false; @@ -290,6 +294,7 @@ void ToolsGeneralWidget::on_btnAdd_clicked() item->title = tr("untitled"); item->inputOrigin = ToolItemInputOrigin::None; item->outputTarget = ToolItemOutputTarget::RedirectToToolsOutputPanel; + item->isUTF8 = false; mToolsModel.addTool(item); int row = mToolsModel.tools().count() - 1; QModelIndex index=mToolsModel.index(row); diff --git a/RedPandaIDE/settingsdialog/toolsgeneralwidget.ui b/RedPandaIDE/settingsdialog/toolsgeneralwidget.ui index d4186c0e..45c37a6b 100644 --- a/RedPandaIDE/settingsdialog/toolsgeneralwidget.ui +++ b/RedPandaIDE/settingsdialog/toolsgeneralwidget.ui @@ -142,7 +142,95 @@ QFrame::Raised - + + + + Browse + + + + + + + Browse + + + + + + + + + + Program + + + + + + + Output To + + + + + + + + + + Parameters + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Redirect Input + + + + + + + + + + Working Directory + + + + + + + + + + Title + + + + + + + Qt::Horizontal + + + + @@ -183,34 +271,7 @@ - - - - Parameters - - - - - - - Program - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - + @@ -261,66 +322,9 @@ - - - - - - - - - - Working Directory - - - - - - - - - - Output To - - - - - - - Browse - - - - - - - Browse - - - - - - - - - - Redirect Input - - - - - - - - - - Title - - - @@ -331,10 +335,13 @@ + + + - - - Qt::Horizontal + + + Use UTF8 as the encoding @@ -351,6 +358,7 @@ btnAdd + btnEdit btnRemove lstTools txtTitle @@ -362,6 +370,7 @@ txtDemo cbInput cbOutput + chkUTF8 btnInsertMacro cbMacros btnEditOk diff --git a/RedPandaIDE/toolsmanager.cpp b/RedPandaIDE/toolsmanager.cpp index 41f3e74c..7775e8ef 100644 --- a/RedPandaIDE/toolsmanager.cpp +++ b/RedPandaIDE/toolsmanager.cpp @@ -44,9 +44,10 @@ void ToolsManager::load() item->program = "rm"; #endif item->workingDirectory = ""; - item->parameters = ""; + item->parameters = "/q /f "; item->inputOrigin = ToolItemInputOrigin::None; item->outputTarget = ToolItemOutputTarget::RedirectToToolsOutputPanel; + item->isUTF8 = false; mTools.append(item); //#ifdef Q_OS_WIN // item = std::make_shared(); @@ -99,6 +100,7 @@ void ToolsManager::load() item->parameters = object["parameters"].toString(); item->outputTarget = static_cast(object["outputTarget"].toInt(0)); item->inputOrigin= static_cast(object["inputOrigin"].toInt(0)); + item->isUTF8 = object["isUTF8"].toBool(true); mTools.append(item); } } @@ -124,6 +126,7 @@ void ToolsManager::save() object["parameters"]=tool->parameters; object["outputTarget"]=static_cast(tool->outputTarget); object["inputOrigin"]=static_cast(tool->inputOrigin); + object["isUTF8"]=tool->isUTF8; array.append(object); } QJsonDocument doc; diff --git a/RedPandaIDE/toolsmanager.h b/RedPandaIDE/toolsmanager.h index 8d9eb6e3..2afd9c5f 100644 --- a/RedPandaIDE/toolsmanager.h +++ b/RedPandaIDE/toolsmanager.h @@ -41,6 +41,7 @@ struct ToolItem { QString parameters; ToolItemInputOrigin inputOrigin; ToolItemOutputTarget outputTarget; + bool isUTF8; }; using PToolItem = std::shared_ptr; diff --git a/RedPandaIDE/translations/RedPandaIDE_pt_BR.ts b/RedPandaIDE/translations/RedPandaIDE_pt_BR.ts index 3555cbb1..7863476e 100644 --- a/RedPandaIDE/translations/RedPandaIDE_pt_BR.ts +++ b/RedPandaIDE/translations/RedPandaIDE_pt_BR.ts @@ -4276,7 +4276,7 @@ MainWindow - + Red Panda C++ Red Panda C++ @@ -5483,13 +5483,13 @@ Ctrl+Shift+Down - + - + @@ -5501,7 +5501,7 @@ Erro - + New Novo @@ -5527,18 +5527,18 @@ - + Problem Set %1 Conjunto de problemas %1 - + Load Theme Error Erro ao carregar tema - + @@ -5731,12 +5731,12 @@ Propriedades... - + - Command: %1 - + Line: %1/%2 Col: %3 Sel: %4 @@ -7018,7 +7018,7 @@ - + Exact @@ -7033,7 +7033,7 @@ - + Folder Not Empty @@ -8381,7 +8381,7 @@ QFileSystemModel - + <b>The name "%1" cannot be used.</b><p>Try using another name, with fewer characters or no punctuation marks. @@ -8681,7 +8681,7 @@ - + Save Salvar @@ -9000,7 +9000,7 @@ Índice %1 fora dos limites - + bytes bytes @@ -10693,23 +10693,28 @@ Remover - + Output To - + + Use UTF8 as the encoding + + + + Browse Navegar - + Parameters Parâmetros - + Ok Ok @@ -10719,12 +10724,12 @@ Cancelar - + Redirect Input - + Title Título @@ -10733,22 +10738,22 @@ Pausar a console após término do programa - + Program Programa - + Working Directory Pasta de trabalho - + Insert Macro Inserir macro - + Save Changes? Salvar alterações? @@ -10757,7 +10762,7 @@ Quer salvar as alterações na ferramenta atual? - + Choose Folder Escolher pasta @@ -10771,7 +10776,7 @@ Arquivos executáveis (*.exe) - + None @@ -10802,7 +10807,7 @@ - + Do you want to save changes to "%1"? @@ -10817,7 +10822,7 @@ - + untitled sem nome @@ -10873,7 +10878,7 @@ Remover compilado - + Read tools config failed Falha ao ler configurações de ferramentas @@ -10889,18 +10894,18 @@ Falha ao ler arquivo de configurações de ferramentas '%1': %2 - - + + Save tools config failed Falha ao salvar configurações de ferramentas - + Can't open tools config file '%1' for write. Impossível gravar o arquivo de configurações de ferramentas '%1' - + Write to tools config file '%1' failed. Falha ao gravar o arquivo de configurações '%1'. diff --git a/RedPandaIDE/translations/RedPandaIDE_zh_CN.ts b/RedPandaIDE/translations/RedPandaIDE_zh_CN.ts index cbcf97b2..d03ef258 100644 --- a/RedPandaIDE/translations/RedPandaIDE_zh_CN.ts +++ b/RedPandaIDE/translations/RedPandaIDE_zh_CN.ts @@ -4629,7 +4629,7 @@ p, li { white-space: pre-wrap; } MainWindow - + Red Panda C++ 小熊猫C++ @@ -4790,7 +4790,7 @@ p, li { white-space: pre-wrap; } 工具栏2 - + New 新建 @@ -4923,7 +4923,7 @@ p, li { white-space: pre-wrap; } - + Copy @@ -6202,12 +6202,12 @@ p, li { white-space: pre-wrap; } 文件编码 - + Recent Files 文件历史 - + @@ -6529,7 +6529,7 @@ p, li { white-space: pre-wrap; } 清除 - + Export 导出 @@ -6540,7 +6540,7 @@ p, li { white-space: pre-wrap; } - + Problem Set %1 试题集%1 @@ -6599,12 +6599,12 @@ p, li { white-space: pre-wrap; } 删除 - + - Command: %1 - 命令: %1 - + %1 Version %1版 @@ -7496,13 +7496,13 @@ p, li { white-space: pre-wrap; } 试题案例%1 - + - + @@ -7514,7 +7514,7 @@ p, li { white-space: pre-wrap; } 错误 - + Recent Projects 项目历史 @@ -7525,7 +7525,7 @@ p, li { white-space: pre-wrap; } 载入主题失败 - + Clear History 清除历史 @@ -7589,7 +7589,7 @@ p, li { white-space: pre-wrap; } 确认转换 - + Exact 完全一致 @@ -7608,7 +7608,7 @@ p, li { white-space: pre-wrap; } 行: %1 列: %2 (%3个字符) 总行数: %4 - + If you are using the Release compiler set, please use choose the Debug version from toolbar. @@ -9057,7 +9057,7 @@ p, li { white-space: pre-wrap; } QFileSystemModel - + <b>The name "%1" cannot be used.</b><p>Try using another name, with fewer characters or no punctuation marks. <b>文件名 "%1" 无法被使用!</b><p>可能是重名、过长、为空或者是使用了不能出现在文件名里的符号。 @@ -9066,7 +9066,7 @@ p, li { white-space: pre-wrap; } QObject - + Save 保存 @@ -9551,7 +9551,7 @@ p, li { white-space: pre-wrap; } 下标"%1"越界 - + bytes 字节 @@ -11589,23 +11589,28 @@ p, li { white-space: pre-wrap; } 删除 - + Output To 输出到 - + + Use UTF8 as the encoding + 使用UTF8文件编码 + + + Browse 浏览 - + Parameters 参数 - + Ok 确定 @@ -11615,12 +11620,12 @@ p, li { white-space: pre-wrap; } 取消 - + Redirect Input 重定向输入 - + Title 名称 @@ -11629,22 +11634,22 @@ p, li { white-space: pre-wrap; } 程序在主控台中结束运行后暂停 - + Program 程序 - + Working Directory 工作文件夹 - + Insert Macro 插入宏指令 - + Save Changes? 保存修改? @@ -11653,7 +11658,7 @@ p, li { white-space: pre-wrap; } 您需要保存对当前工具的修改吗? - + None @@ -11684,7 +11689,7 @@ p, li { white-space: pre-wrap; } 替换整个文档 - + Do you want to save changes to "%1"? 要保存对"%1"的修改吗? @@ -11699,12 +11704,12 @@ p, li { white-space: pre-wrap; } 标题不可以为空! - + untitled 无标题 - + Choose Folder 选择文件夹 @@ -11777,7 +11782,7 @@ p, li { white-space: pre-wrap; } 在文件管理器中打开编译结果 - + Read tools config failed 读取工具配置失败 @@ -11793,18 +11798,18 @@ p, li { white-space: pre-wrap; } 读取工具配置文件'%1'失败:%2 - - + + Save tools config failed 保存工具配置失败 - + Can't open tools config file '%1' for write. 无法写入工具配置文件'%1'。 - + Write to tools config file '%1' failed. Write to tool config file '%1' failed. 写入工具配置文件'%1'失败。 diff --git a/RedPandaIDE/translations/RedPandaIDE_zh_TW.ts b/RedPandaIDE/translations/RedPandaIDE_zh_TW.ts index 4e87da2a..dc2a8332 100644 --- a/RedPandaIDE/translations/RedPandaIDE_zh_TW.ts +++ b/RedPandaIDE/translations/RedPandaIDE_zh_TW.ts @@ -4061,7 +4061,7 @@ MainWindow - + Red Panda C++ @@ -5255,13 +5255,13 @@ - + - + @@ -5273,7 +5273,7 @@ - + New @@ -5299,18 +5299,18 @@ - + Problem Set %1 - + Load Theme Error - + @@ -5444,12 +5444,12 @@ - + - Command: %1 - + Line: %1/%2 Col: %3 Sel: %4 @@ -6711,7 +6711,7 @@ - + Exact @@ -6726,7 +6726,7 @@ - + Folder Not Empty @@ -7981,7 +7981,7 @@ QFileSystemModel - + <b>The name "%1" cannot be used.</b><p>Try using another name, with fewer characters or no punctuation marks. @@ -8274,7 +8274,7 @@ - + Save @@ -8483,7 +8483,7 @@ - + bytes @@ -9924,23 +9924,28 @@ - + Output To - + + Use UTF8 as the encoding + + + + Browse - + Parameters - + Ok @@ -9950,37 +9955,37 @@ - + Redirect Input - + Title - + Program - + Working Directory - + Insert Macro - + Save Changes? - + Choose Folder @@ -9990,12 +9995,12 @@ - + Do you want to save changes to "%1"? - + None @@ -10026,7 +10031,7 @@ - + Error @@ -10036,7 +10041,7 @@ - + untitled @@ -10092,7 +10097,7 @@ - + Read tools config failed @@ -10108,18 +10113,18 @@ - - + + Save tools config failed - + Can't open tools config file '%1' for write. - + Write to tools config file '%1' failed. diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index d21e0e1d..81641e0a 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -357,6 +357,7 @@ QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const { QProcess process; QByteArray result; + bool errorOccurred = false; if (env.isEmpty()) { if (inheritEnvironment) { process.setProcessEnvironment(QProcessEnvironment::systemEnvironment()); @@ -375,12 +376,19 @@ QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const [&](){ result.append(process.readAllStandardOutput()); }); + process.connect(&process, &QProcess::errorOccurred, + [&](){ + errorOccurred= true; + }); process.start(cmd,arguments); if (!inputContent.isEmpty()) { process.write(inputContent); } process.closeWriteChannel(); process.waitForFinished(); + if (errorOccurred) { + result += process.errorString().toUtf8(); + } return result; } @@ -613,3 +621,19 @@ QString osArch() return QSysInfo::currentCpuArchitecture(); #endif } + +QString byteArrayToString(const QByteArray &content, bool isUTF8) +{ + if (isUTF8) + return QString::fromUtf8(content); + else + return QString::fromLocal8Bit(content); +} + +QByteArray stringToByteArray(const QString &content, bool isUTF8) +{ + if (isUTF8) + return content.toUtf8(); + else + return content.toLocal8Bit(); +} diff --git a/RedPandaIDE/utils.h b/RedPandaIDE/utils.h index f192fd42..d3763d4f 100644 --- a/RedPandaIDE/utils.h +++ b/RedPandaIDE/utils.h @@ -173,6 +173,9 @@ QStringList platformCommandForTerminalArgsPreview(); QString appArch(); QString osArch(); +QString byteArrayToString(const QByteArray &content, bool isUTF8); +QByteArray stringToByteArray(const QString& content, bool isUTF8); + #ifdef _MSC_VER #define __builtin_unreachable() (__assume(0)) #endif