diff options
author | Michele Calgaro <michele.calgaro@yahoo.it> | 2023-07-09 14:17:11 +0900 |
---|---|---|
committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2023-07-09 14:17:11 +0900 |
commit | 48a09796d54081ad7f1c5299b1fd699623034d21 (patch) | |
tree | 219f4e02fa7c43cb0891977133df17a72c12b414 | |
parent | 97a6a7874e936017a833c68701b3adf7f709e675 (diff) | |
download | universal-indent-gui-tqt-48a09796d54081ad7f1c5299b1fd699623034d21.tar.gz universal-indent-gui-tqt-48a09796d54081ad7f1c5299b1fd699623034d21.zip |
Added logic to create the indenter calling script
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/IndentHandler.cpp | 322 | ||||
-rw-r--r-- | src/IndentHandler.h | 4 | ||||
-rw-r--r-- | src/TemplateBatchScript.cpp (renamed from src/__TODO/TemplateBatchScript.cpp) | 2 | ||||
-rw-r--r-- | src/TemplateBatchScript.h (renamed from src/__TODO/TemplateBatchScript.h) | 2 |
5 files changed, 167 insertions, 164 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 09f1b9a..7d5f6b4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,6 +31,7 @@ set( ${target}_SRCS ToolBarWidget.ui SettingsPaths.cpp + TemplateBatchScript.cpp UiGuiErrorMessage.cpp UiGuiIniFileParser.cpp UiGuiVersion.cpp diff --git a/src/IndentHandler.cpp b/src/IndentHandler.cpp index 62745e2..a11ea24 100644 --- a/src/IndentHandler.cpp +++ b/src/IndentHandler.cpp @@ -20,12 +20,14 @@ #include "config.h" #include "IndentHandler.h" +#include <cstdlib> + #include "MainWindow.h" #include "SettingsPaths.h" #include "UiGuiErrorMessage.h" #include "UiGuiIniFileParser.h" #include "UiGuiSettings.h" -//--- #include "TemplateBatchScript.h" +#include "TemplateBatchScript.h" #include <tqapplication.h> #include <tqcheckbox.h> @@ -215,94 +217,94 @@ void IndentHandler::contextMenuEvent(TQContextMenuEvent *event) } } -//--- /* -//--- \brief Creates the content for a shell script that can be used as a external tool call -//--- to indent an as parameter defined file. -//--- */ -//--- TQString IndentHandler::generateShellScript(const TQString &configFilename) -//--- { -//--- TQString indenterCompleteCallString; -//--- TQString parameterInputFile; -//--- TQString parameterOuputFile; -//--- TQString parameterParameterFile; -//--- TQString replaceInputFileCommand; -//--- -//--- // Define the placeholder for parameter variables either in batch or bash programming. -//--- TQString shellParameterPlaceholder = "$1"; -//--- -//--- parameterInputFile = " " + m_inputFileParameter + "\"" + shellParameterPlaceholder + "\""; -//--- -//--- if (m_outputFileParameter != "none" && m_outputFileParameter != "stdout") -//--- { -//--- if (m_outputFileName == m_inputFileName) -//--- { -//--- parameterOuputFile = " " + m_outputFileParameter + "\"" + shellParameterPlaceholder + "\""; -//--- } -//--- else -//--- { -//--- parameterOuputFile = " " + m_outputFileParameter + m_outputFileName + ".tmp"; -//--- } -//--- } -//--- -//--- // If the config file name is empty it is assumed that all parameters are sent via command line -//--- // call -//--- if (m_globalConfigFilename.isEmpty()) -//--- { -//--- parameterParameterFile = " " + getParameterString(); -//--- } -//--- // else if needed add the parameter to the indenter call string where the config file can be -//--- // found. -//--- else if (m_useCfgFileParameter != "none") -//--- { -//--- parameterParameterFile = " " + m_useCfgFileParameter + "\"./" + configFilename + "\""; -//--- } -//--- -//--- // Assemble indenter call string for parameters according to the set order. -//--- if (m_parameterOrder == "ipo") -//--- { -//--- indenterCompleteCallString = parameterInputFile + parameterParameterFile + parameterOuputFile; -//--- } -//--- else if (m_parameterOrder == "pio") -//--- { -//--- indenterCompleteCallString = parameterParameterFile + parameterInputFile + parameterOuputFile; -//--- } -//--- else if (m_parameterOrder == "poi") -//--- { -//--- indenterCompleteCallString = parameterParameterFile + parameterOuputFile + parameterInputFile; -//--- } -//--- else -//--- { -//--- indenterCompleteCallString = parameterInputFile + parameterOuputFile + parameterParameterFile; -//--- } -//--- -//--- // Generate the indenter call string either for win32 or other systems. -//--- indenterCompleteCallString = "#!/bin/bash\n" + m_indenterExecutableCallString + -//--- indenterCompleteCallString; -//--- -//--- // If the indenter writes to stdout pipe the output into a file -//--- if (m_outputFileParameter == "stdout") -//--- { -//--- indenterCompleteCallString = indenterCompleteCallString + " >" + m_outputFileName + ".tmp"; -//--- } -//--- -//--- // If the output filename is not the same as the input filename copy the output over the input. -//--- if (m_outputFileName != m_inputFileName) -//--- { -//--- replaceInputFileCommand = "mv " + m_outputFileName + ".tmp \"" + shellParameterPlaceholder + -//--- "\"\n"; -//--- } -//--- -//--- TQString shellScript(TemplateBatchScript::getTemplateBatchScript()); -//--- shellScript = shellScript.replace("__INDENTERCALLSTRING2__", -//--- indenterCompleteCallString + "\n" + replaceInputFileCommand); -//--- indenterCompleteCallString = indenterCompleteCallString.replace("$1", "$file2indent"); -//--- replaceInputFileCommand = replaceInputFileCommand.replace("$1", "$file2indent"); -//--- shellScript = shellScript.replace("__INDENTERCALLSTRING1__", -//--- indenterCompleteCallString + "\n" + replaceInputFileCommand); -//--- -//--- return shellScript; -//--- } -//--- +/* + \brief Creates the content for a shell script that can be used as a external tool call + to indent an as parameter defined file. + */ +TQString IndentHandler::generateShellScript(const TQString &configFilename) +{ + TQString indenterCompleteCallString; + TQString parameterInputFile; + TQString parameterOuputFile; + TQString parameterParameterFile; + TQString replaceInputFileCommand; + + // Define the placeholder for parameter variables either in batch or bash programming. + TQString shellParameterPlaceholder = "$1"; + + parameterInputFile = " " + m_inputFileParameter + "\"" + shellParameterPlaceholder + "\""; + + if (m_outputFileParameter != "none" && m_outputFileParameter != "stdout") + { + if (m_outputFileName == m_inputFileName) + { + parameterOuputFile = " " + m_outputFileParameter + "\"" + shellParameterPlaceholder + "\""; + } + else + { + parameterOuputFile = " " + m_outputFileParameter + m_outputFileName + ".tmp"; + } + } + + // If the config file name is empty it is assumed that all parameters are sent via command line + // call + if (m_globalConfigFilename.isEmpty()) + { + parameterParameterFile = " " + getParameterString(); + } + // else if needed add the parameter to the indenter call string where the config file can be + // found. + else if (m_useCfgFileParameter != "none") + { + parameterParameterFile = " " + m_useCfgFileParameter + "\"./" + configFilename + "\""; + } + + // Assemble indenter call string for parameters according to the set order. + if (m_parameterOrder == "ipo") + { + indenterCompleteCallString = parameterInputFile + parameterParameterFile + parameterOuputFile; + } + else if (m_parameterOrder == "pio") + { + indenterCompleteCallString = parameterParameterFile + parameterInputFile + parameterOuputFile; + } + else if (m_parameterOrder == "poi") + { + indenterCompleteCallString = parameterParameterFile + parameterOuputFile + parameterInputFile; + } + else + { + indenterCompleteCallString = parameterInputFile + parameterOuputFile + parameterParameterFile; + } + + // Generate the indenter call string either for win32 or other systems. + indenterCompleteCallString = "#!/bin/bash\n" + m_indenterExecutableCallString + + indenterCompleteCallString; + + // If the indenter writes to stdout pipe the output into a file + if (m_outputFileParameter == "stdout") + { + indenterCompleteCallString = indenterCompleteCallString + " >" + m_outputFileName + ".tmp"; + } + + // If the output filename is not the same as the input filename copy the output over the input. + if (m_outputFileName != m_inputFileName) + { + replaceInputFileCommand = "mv " + m_outputFileName + ".tmp \"" + shellParameterPlaceholder + + "\"\n"; + } + + TQString shellScript(TemplateBatchScript::getTemplateBatchScript()); + shellScript = shellScript.replace("__INDENTERCALLSTRING2__", + indenterCompleteCallString + "\n" + replaceInputFileCommand); + indenterCompleteCallString = indenterCompleteCallString.replace("$1", "$file2indent"); + replaceInputFileCommand = replaceInputFileCommand.replace("$1", "$file2indent"); + shellScript = shellScript.replace("__INDENTERCALLSTRING1__", + indenterCompleteCallString + "\n" + replaceInputFileCommand); + + return shellScript; +} + //--- /* //--- \brief Format \a sourceCode by calling the indenter. //--- @@ -669,12 +671,13 @@ TQString IndentHandler::getParameterString() */ void IndentHandler::saveConfigFile(const TQString &filePathName, const TQString ¶mString) { -//--- TQFile::remove(filePathName); -//--- TQFile cfgFile(filePathName); -//--- -//--- cfgFile.open(TQFile::ReadWrite | TQFile::Text); -//--- cfgFile.write(paramString.toAscii()); -//--- cfgFile.close(); + TQFile::remove(filePathName); + TQFile cfgFile(filePathName); + + cfgFile.open(IO_ReadWrite | IO_Translate); + TQCString paramCString = paramString.local8Bit(); + cfgFile.writeBlock(paramCString.data(), paramCString.length()); + cfgFile.close(); } /* @@ -1548,22 +1551,22 @@ void IndentHandler::showIndenterManual() const //--- TQDesktopServices::openUrl(manualReference); } -//--- /* -//--- \brief Returns the name of the currently selected indenter. -//--- */ -//--- TQString IndentHandler::getCurrentIndenterName() -//--- { -//--- TQString currentIndenterName = m_indenterSelectionCombobox->currentText(); -//--- -//--- // Remove the supported programming languages from indenters name, which are set in braces. -//--- if (currentIndenterName.find("(") > 0) -//--- { -//--- // Using index-1 to also leave out the blank before the brace. -//--- currentIndenterName = currentIndenterName.left(currentIndenterName.find("(") - 1); -//--- } -//--- -//--- return currentIndenterName; -//--- } +/* + \brief Returns the name of the currently selected indenter. + */ +TQString IndentHandler::getCurrentIndenterName() +{ + TQString currentIndenterName = m_indenterSelectionCombobox->currentText(); + + // Remove the supported programming languages from indenters name, which are set in braces. + if (currentIndenterName.find("(") > 0) + { + // Using index-1 to also leave out the blank before the brace. + currentIndenterName = currentIndenterName.left(currentIndenterName.find("(") - 1); + } + + return currentIndenterName; +} /* \brief Shows a file open dialog to open an existing config file for the current indenter. @@ -1612,55 +1615,54 @@ void IndentHandler::saveasIndentCfgFileDialog() */ void IndentHandler::createIndenterCallShellScript() { -//--- TQString shellScriptExtension; -//--- shellScriptExtension = "sh"; -//--- -//--- TQString fileExtensions = tr("Shell Script") + " (*." + shellScriptExtension + ");;" + tr( -//--- "All files") + " (*.*)"; -//--- -//--- TQString currentIndenterName = getCurrentIndenterName(); -//--- currentIndenterName = currentIndenterName.replace(" ", "_"); -//--- -//--- TQString shellScriptFileName = TQFileDialog::getSaveFileName(this, tr( -//--- "Save shell script"), "call_" + currentIndenterName + "." + shellScriptExtension, -//--- fileExtensions); -//--- -//--- // Saving has been canceled if the filename is empty -//--- if (shellScriptFileName.isEmpty()) -//--- { -//--- return; -//--- } -//--- -//--- // Delete any old file, write the new contents and set executable permissions. -//--- TQFile::remove(shellScriptFileName); -//--- TQFile outSrcFile(shellScriptFileName); -//--- if (outSrcFile.open(TQFile::ReadWrite | TQFile::Text)) -//--- { -//--- TQString shellScriptConfigFilename = TQFileInfo(shellScriptFileName).baseName() + "." + -//--- TQFileInfo(m_globalConfigFilename).suffix(); -//--- -//--- // Get the content of the shell/batch script. -//--- TQString indenterCallShellScript = generateShellScript(shellScriptConfigFilename); -//--- -//--- // Replace placeholder for script name in script template. -//--- indenterCallShellScript = indenterCallShellScript.replace("__INDENTERCALLSTRINGSCRIPTNAME__", TQFileInfo( -//--- shellScriptFileName).fileName()); -//--- -//--- outSrcFile.write(indenterCallShellScript.toAscii()); -//--- // For non Windows systems set the files executable flag -//--- outSrcFile.setPermissions( -//--- outSrcFile.permissions() | TQFile::ExeOwner | TQFile::ExeUser | TQFile::ExeGroup); -//--- outSrcFile.close(); -//--- -//--- // Save the indenter config file to the same directory, where the shell srcipt was saved to, -//--- // because the script will reference it there via "./". -//--- if (!m_globalConfigFilename.isEmpty()) -//--- { -//--- saveConfigFile(TQFileInfo( -//--- shellScriptFileName).path() + "/" + shellScriptConfigFilename, -//--- getParameterString()); -//--- } -//--- } + TQString shellScriptExtension; + shellScriptExtension = "sh"; + + TQString fileExtensions = tr("Shell Script") + " (*." + shellScriptExtension + ");;" + tr( + "All files") + " (*.*)"; + + TQString currentIndenterName = getCurrentIndenterName(); + currentIndenterName = currentIndenterName.replace(" ", "_"); + + TQString indenterScriptName = "call_" + currentIndenterName + "." + shellScriptExtension; + TQString shellScriptFileName = TQFileDialog::getSaveFileName(indenterScriptName, + fileExtensions, nullptr, nullptr, tr("Save shell script")); + + // Saving has been canceled if the filename is empty + if (shellScriptFileName.isEmpty()) + { + return; + } + + // Delete any old file, write the new contents and set executable permissions. + TQFile::remove(shellScriptFileName); + TQFile outSrcFile(shellScriptFileName); + if (outSrcFile.open(IO_ReadWrite | IO_Translate)) + { + TQString shellScriptConfigFilename = TQFileInfo(shellScriptFileName).baseName() + "." + + TQFileInfo(m_globalConfigFilename).extension(false); + + // Get the content of the shell/batch script. + TQString indenterCallShellScript = generateShellScript(shellScriptConfigFilename); + + // Replace placeholder for script name in script template. + indenterCallShellScript = indenterCallShellScript.replace("__INDENTERCALLSTRINGSCRIPTNAME__", + TQFileInfo(shellScriptFileName).fileName()); + + TQCString indenterString = indenterCallShellScript.local8Bit(); + outSrcFile.writeBlock(indenterString.data(), indenterString.length()); + // For non Windows systems set the files executable flag + outSrcFile.close(); + int _ = std::system("chmod a+x " + shellScriptFileName.local8Bit()); + + // Save the indenter config file to the same directory, where the shell srcipt was saved to, + // because the script will reference it there via "./". + if (!m_globalConfigFilename.isEmpty()) + { + saveConfigFile(TQFileInfo(shellScriptFileName).dirPath(true) + "/" + shellScriptConfigFilename, + getParameterString()); + } + } } /* diff --git a/src/IndentHandler.h b/src/IndentHandler.h index 05c0ce9..9677f6a 100644 --- a/src/IndentHandler.h +++ b/src/IndentHandler.h @@ -46,7 +46,7 @@ class IndentHandler : public TQWidget IndentHandler(int indenterID, MainWindow *mainWindow = nullptr, TQWidget *parent = nullptr); ~IndentHandler(); -///-- TQString generateShellScript(const TQString &configFilename); + TQString generateShellScript(const TQString &configFilename); ///-- TQString callIndenter(TQString sourceCode, TQString inputFileExtension); bool loadConfigFile(const TQString &filePathName); void resetToDefaultValues(); @@ -55,7 +55,7 @@ class IndentHandler : public TQWidget TQString getParameterString(); TQString getIndenterCfgFile(); ///-- TQString getManual(); -///-- TQString getCurrentIndenterName(); + TQString getCurrentIndenterName(); void contextMenuEvent(TQContextMenuEvent *event); signals: diff --git a/src/__TODO/TemplateBatchScript.cpp b/src/TemplateBatchScript.cpp index 56215e1..d3910bd 100644 --- a/src/__TODO/TemplateBatchScript.cpp +++ b/src/TemplateBatchScript.cpp @@ -20,7 +20,7 @@ #include "TemplateBatchScript.h" /* - \brief The only and static function of this class returns a batch or shell script + \brief The only static function of this class returns a batch or shell script as string that can be used to call an indenter with the current settings from the command line. diff --git a/src/__TODO/TemplateBatchScript.h b/src/TemplateBatchScript.h index 6384ff0..642692d 100644 --- a/src/__TODO/TemplateBatchScript.h +++ b/src/TemplateBatchScript.h @@ -29,4 +29,4 @@ class TemplateBatchScript static const char* getTemplateBatchScript(); }; -#endif // TEMPLATEBATCHSCRIPT_H +#endif |