move helper functions to separate file. add overloads to mystrdup()

This commit is contained in:
Axel Kohlmeyer
2023-09-24 12:53:14 -04:00
parent 6f79918bab
commit c50dcf6129
5 changed files with 115 additions and 54 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(lammps-gui VERSION 1.5.5 LANGUAGES CXX) project(lammps-gui VERSION 1.5.6 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
@ -84,6 +84,7 @@ set(PROJECT_SOURCES
chartviewer.h chartviewer.h
codeeditor.cpp codeeditor.cpp
codeeditor.h codeeditor.h
helpers.cpp
highlighter.cpp highlighter.cpp
highlighter.h highlighter.h
imageviewer.cpp imageviewer.cpp

View File

@ -0,0 +1,71 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "helpers.h"
#include <QFile>
#include <QFileInfo>
#include <QProcess>
#include <QStringList>
// duplicate string, STL version
char *mystrdup(const std::string &text)
{
auto tmp = new char[text.size() + 1];
memcpy(tmp, text.c_str(), text.size() + 1);
return tmp;
}
// duplicate string, pointer version
char *mystrdup(const char *text)
{
return mystrdup(std::string(text));
}
// duplicate string, Qt version
char *mystrdup(const QString &text)
{
return mystrdup(text.toStdString());
}
// find if executable is in path
// https://stackoverflow.com/a/51041497
bool has_exe(const QString &exe)
{
QProcess findProcess;
QStringList arguments;
arguments << exe;
#if defined(_WIN32)
findProcess.start("where", arguments);
#else
findProcess.start("which", arguments);
#endif
findProcess.setReadChannel(QProcess::ProcessChannel::StandardOutput);
if (!findProcess.waitForFinished()) return false; // Not found or which does not work
QString retStr(findProcess.readAll());
retStr = retStr.trimmed();
QFile file(retStr);
QFileInfo check_file(file);
if (check_file.exists() && check_file.isFile())
return true; // Found!
else
return false; // Not found!
}
// Local Variables:
// c-basic-offset: 4
// End:

View File

@ -0,0 +1,31 @@
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef HELPERS_H
#define HELPERS_H
#include <QString>
#include <string>
// duplicate string
extern char *mystrdup(const std::string &text);
extern char *mystrdup(const char *text);
extern char *mystrdup(const QString &text);
// find if executable is in path
extern bool has_exe(const QString &exe);
#endif
// Local Variables:
// c-basic-offset: 4
// End:

View File

@ -14,6 +14,7 @@
#include "lammpsgui.h" #include "lammpsgui.h"
#include "chartviewer.h" #include "chartviewer.h"
#include "helpers.h"
#include "highlighter.h" #include "highlighter.h"
#include "imageviewer.h" #include "imageviewer.h"
#include "lammpsrunner.h" #include "lammpsrunner.h"
@ -59,42 +60,6 @@ static const QString blank(" ");
static constexpr int MAXRECENT = 5; static constexpr int MAXRECENT = 5;
static constexpr int BUFLEN = 128; static constexpr int BUFLEN = 128;
// duplicate string
static char *mystrdup(const std::string &text)
{
auto tmp = new char[text.size() + 1];
memcpy(tmp, text.c_str(), text.size() + 1);
return tmp;
}
// find if executable is in path
// https://stackoverflow.com/a/51041497
static bool has_exe(const QString &exe)
{
QProcess findProcess;
QStringList arguments;
arguments << exe;
#if defined(_WIN32)
findProcess.start("where", arguments);
#else
findProcess.start("which", arguments);
#endif
findProcess.setReadChannel(QProcess::ProcessChannel::StandardOutput);
if (!findProcess.waitForFinished()) return false; // Not found or which does not work
QString retStr(findProcess.readAll());
retStr = retStr.trimmed();
QFile file(retStr);
QFileInfo check_file(file);
if (check_file.exists() && check_file.isFile())
return true; // Found!
else
return false; // Not found!
}
LammpsGui::LammpsGui(QWidget *parent, const char *filename) : LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
QMainWindow(parent), ui(new Ui::LammpsGui), highlighter(nullptr), capturer(nullptr), QMainWindow(parent), ui(new Ui::LammpsGui), highlighter(nullptr), capturer(nullptr),
status(nullptr), logwindow(nullptr), imagewindow(nullptr), chartwindow(nullptr), status(nullptr), logwindow(nullptr), imagewindow(nullptr), chartwindow(nullptr),
@ -1025,11 +990,11 @@ void LammpsGui::do_run(bool use_buffer)
runner = new LammpsRunner(this); runner = new LammpsRunner(this);
is_running = true; is_running = true;
if (use_buffer) { if (use_buffer) {
// always add final newline since the text edit widget does not // always add final newline since the text edit widget does not do it
char *input = mystrdup(ui->textEdit->toPlainText().toStdString() + "\n"); char *input = mystrdup(ui->textEdit->toPlainText() + "\n");
runner->setup_run(&lammps, input, nullptr); runner->setup_run(&lammps, input, nullptr);
} else { } else {
char *fname = mystrdup(current_file.toStdString()); char *fname = mystrdup(current_file);
runner->setup_run(&lammps, nullptr, fname); runner->setup_run(&lammps, nullptr, fname);
} }
@ -1418,9 +1383,9 @@ void LammpsGui::start_lammps()
QString value = var.second; QString value = var.second;
if (!name.isEmpty() && !value.isEmpty()) { if (!name.isEmpty() && !value.isEmpty()) {
lammps_args.push_back(mystrdup("-var")); lammps_args.push_back(mystrdup("-var"));
lammps_args.push_back(mystrdup(name.toStdString())); lammps_args.push_back(mystrdup(name));
for (const auto &v : value.split(' ')) for (const auto &v : value.split(' '))
lammps_args.push_back(mystrdup(v.toStdString())); lammps_args.push_back(mystrdup(v));
} }
} }

View File

@ -13,6 +13,7 @@
#include "preferences.h" #include "preferences.h"
#include "helpers.h"
#include "lammpsgui.h" #include "lammpsgui.h"
#include "lammpswrapper.h" #include "lammpswrapper.h"
#include "ui_lammpsgui.h" #include "ui_lammpsgui.h"
@ -55,14 +56,6 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
// duplicate string
static char *mystrdup(const std::string &text)
{
auto tmp = new char[text.size() + 1];
memcpy(tmp, text.c_str(), text.size() + 1);
return tmp;
}
Preferences::Preferences(LammpsWrapper *_lammps, QWidget *parent) : Preferences::Preferences(LammpsWrapper *_lammps, QWidget *parent) :
QDialog(parent), tabWidget(new QTabWidget), QDialog(parent), tabWidget(new QTabWidget),
buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel)), buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel)),
@ -176,8 +169,8 @@ void Preferences::accept()
"LAMMPS-GUI must be relaunched."), "LAMMPS-GUI must be relaunched."),
QMessageBox::Ok); QMessageBox::Ok);
msg.exec(); msg.exec();
const char *path = mystrdup(QCoreApplication::applicationFilePath().toStdString()); const char *path = mystrdup(QCoreApplication::applicationFilePath());
const char *arg0 = mystrdup(QCoreApplication::arguments().at(0).toStdString()); const char *arg0 = mystrdup(QCoreApplication::arguments().at(0));
execl(path, arg0, (char *)NULL); execl(path, arg0, (char *)NULL);
} }
@ -258,8 +251,8 @@ GeneralTab::GeneralTab(QSettings *_settings, LammpsWrapper *_lammps, QWidget *pa
connect(gettextfont, &QPushButton::released, this, &GeneralTab::newtextfont); connect(gettextfont, &QPushButton::released, this, &GeneralTab::newtextfont);
auto *freqlayout = new QHBoxLayout; auto *freqlayout = new QHBoxLayout;
auto *freqlabel = new QLabel("GUI update interval (ms)"); auto *freqlabel = new QLabel("GUI update interval (ms)");
auto *freqval = new QSpinBox; auto *freqval = new QSpinBox;
freqval->setRange(1, 1000); freqval->setRange(1, 1000);
freqval->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType); freqval->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType);
freqval->setValue(settings->value("updfreq", "100").toInt()); freqval->setValue(settings->value("updfreq", "100").toInt());