This simple application lets the user specify some text strings, which will be converted to another set of strings. The underlying functions are written in C++17 and the user interface was built with Qt (Qt version 5.13.0, MinGW 7.3.0).
It's a small convenience app, which I mostly use to replace symbols with HTML entities in code snippets for this site (particularly <> brackets and & signs). The app was made for personal use, so it's definitely not perfectly implemented – several aspects of it could be easily expanded for a better user experience. For example, the conversions are not applied all at once, but one after another (which may be desired or not). The words are replaced in the order they are written on the line, i.e. different ordering may yield differing output.
Download(zip) contains the program and source code. The executable is made for Windows OS – more versions could be easily created for other platforms, using the source files. Since the application was built as a standalone file using a static version of Qt, it can be used without any setup and doesn't require any .dll files. However, this also led to the file size being bigger than expected for such a small application (17MB).
//Knowledgedump.org - Text Converter #ifndef TEXT_CONVERTER_H #define TEXT_CONVERTER_H #include <QWidget> #include <QFileDialog> #include <QMessageBox> #include <QTextStream> #include <QClipboard> namespace Ui { class Text_converter; } class Text_converter : public QWidget { Q_OBJECT public: explicit Text_converter(QWidget *parent = nullptr); ~Text_converter(); void init(); private slots: void on_save_button_clicked(); void on_load_button_clicked(); void on_paste_button_clicked(); void on_convert_button_clicked(); void on_copy_button_clicked(); private: Ui::Text_converter *ui; }; #endif // TEXT_CONVERTER_H
//Knowledgedump.org - Text Converter #include "text_converter.h" #include "ui_text_converter.h" Text_converter::Text_converter(QWidget *parent) : QWidget(parent), ui(new Ui::Text_converter) { ui->setupUi(this); ui->input_text->setTabStopDistance(40); ui->output_text->setTabStopDistance(40); init(); } Text_converter::~Text_converter() { delete ui; } void Text_converter::on_save_button_clicked() { //List of "words" to replace, separated by "___". QStringList replace = ui->replace_line->text().split("___"); //The strings in the replace list are replaced with strings in with_this QStringList with_this = ui->with_this_line->text().split("___"); //Check, whether number of strings are the same. If not, show error window and return. if (replace.size() != with_this.size()) { QMessageBox::warning(this, tr("Text Converter"), tr("Failed to save: Number of words in the \"Replace\" line doesn't match " "number of words in the \"with this\" line.\n" "Remember: ___ (3 x \"_\") is used as separator.")); return; } //Open dialog box, to enter filename and confirm saving. QFileDialog dialog(this); dialog.setWindowModality(Qt::WindowModal); dialog.setAcceptMode(QFileDialog::AcceptSave); dialog.setNameFilter("*.txt"); dialog.setDefaultSuffix("txt"); dialog.selectFile("default"); //Sets default filename. if (dialog.exec() != QDialog::Accepted) return; //Filename is set to first selected file, if multiple were specified. QString filename(dialog.selectedFiles().first()); //Create output text-file with replacement pattern. QFile output(filename); //Can file be written? if (!output.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Text Converter"), tr("File creation failed.")); return; } //Create output stream and write to file. File contains the two lines, separated by \n. QTextStream stream(&output); stream << ui->replace_line->text() << "\n" << ui->with_this_line->text(); return; } void Text_converter::on_load_button_clicked() { //Open dialog box to select file for loading and get filename. QString filename(QFileDialog::getOpenFileName(this)); if (filename.isEmpty()) { return; } //File readable? QFile input(filename); if (!input.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Text Converter"), tr("File import failed.")); return; } //Open input stream, save to strings and check, whether text file contains valid pattern. QTextStream stream(&input); //1st line should be content for "Replace" text line. QString replace_raw(stream.readLine()); QStringList replace(replace_raw.split("___")); //2nd line is content for "with_this" text line. QString with_this_raw = stream.readLine(); QStringList with_this(with_this_raw.split("___")); //Pattern valid? if (replace.size() != with_this.size()) { QMessageBox::warning(this, tr("Text Converter"), tr("Failed to load: Invalid pattern.")); return; } //Write pattern to lines. ui->replace_line->setText(replace_raw); ui->with_this_line->setText(with_this_raw); return; } void Text_converter::on_paste_button_clicked() { QClipboard *clipboard = QGuiApplication::clipboard(); ui->input_text->setPlainText(clipboard->text()); } void Text_converter::on_convert_button_clicked() { //Create lists with strings to replace and check for pattern validity first. QStringList replace(ui->replace_line->text().split("___")); QStringList with_this(ui->with_this_line->text().split("___")); if (replace.size() != with_this.size()) { QMessageBox::warning(this, tr("Text Converter"), tr("Conversion failed: Number of words in the \"Replace\" line doesn't match " "number of words in the \"with this\" line.\n" "Remember: ___ (3 x \"_\") is used as separator.")); return; } //Get input from text box and convert. QString converted(ui->input_text->toPlainText()); for (int count = 0; count < replace.size(); ++count) { converted.replace(replace.at(count), with_this.at(count)); } //Write converted text to output box. Keep char format and font. ui->output_text->setPlainText(converted); return; } void Text_converter::on_copy_button_clicked() { QClipboard *clipboard = QGuiApplication::clipboard(); clipboard->setText(ui->output_text->toPlainText()); } void Text_converter::init() { //Load default.txt as pattern on program startup, if possible. QFile input("default.txt"); if (!input.open(QIODevice::ReadOnly | QIODevice::Text)) { return; } //Open input stream, save to strings and check, whether text file contains valid pattern. QTextStream stream(&input); //1st line should be content for "Replace" text line. QString replace_raw(stream.readLine()); QStringList replace(replace_raw.split("___")); //2nd line is content for "with_this" text line. QString with_this_raw = stream.readLine(); QStringList with_this(with_this_raw.split("___")); //Pattern valid? if (replace.size() != with_this.size()) { return; } //Write pattern to lines. ui->replace_line->setText(replace_raw); ui->with_this_line->setText(with_this_raw); return; }
//Knowledgedump.org - Text Converter #include "text_converter.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Text_converter w; w.show(); return a.exec(); }
Add the text strings you want to replace in the "Replace" line, separated by three underscore "_" signs. Similarly, declare what they are to be replaced with in the next line box ("with this"). Since each string needs to be replaced by another, the amount of strings separated by "___" must be identical in both lines.
Patterns can be saved and loaded for future use. When saving a pattern as "default.txt" in the same directory as the executable, it is automatically loaded on the next application startup.