Skip to content

Commit

Permalink
use standard string for program file path
Browse files Browse the repository at this point in the history
changed member and access functions in program class
changed member and access functions in command line class

changed all functions in main window class to use program file path as a
standard string

changed version function of command line class to return standard string
(changed to static function and modified to use simple loop to find
first digit)

modified copyright statement function of command line class to accept
copyright string with default "Copyright" (GUI calls with translated
copyright string) - this allowed the translate functions to be removed
from the command line class

corrected problem in command line constructor preventing program file
argument from being interpreted

modified main window constructor when file to load does not exist (or
can't be loaded) to display a message error box (when the last file
loaded isn't found) or an error to standard error output (when command
line file isn't found)
  • Loading branch information
thunder422 committed Oct 17, 2014
1 parent 2e57346 commit c998b1f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 59 deletions.
35 changes: 19 additions & 16 deletions commandline.cpp
Expand Up @@ -33,20 +33,32 @@


// function to generate the copyright statement
const std::string CommandLine::copyrightStatement()
const std::string CommandLine::copyrightStatement(const char *copyright)
{
std::ostringstream ss;

ss << " " << tr("Copyright").toStdString() << "(C) 2010-"
<< ibcp_COPYRIGHT_YEAR << " Thunder422";
ss << " " << copyright << "(C) 2010-" << ibcp_COPYRIGHT_YEAR
<< " Thunder422";
return ss.str();
}


// function to return the version number string
std::string CommandLine::version(void)
{
const char *p = ibcp_RELEASE_STRING;
while (!isdigit(*p) && *p)
{
++p;
}
return std::string(p);
}


// function to retrieve a base file name from a file path string
std::string CommandLine::baseFileName(const std::string &filePath)
{
return QFileInfo(QString::fromStdString(filePath)).baseName().toStdString();
return QFileInfo(filePath.c_str()).baseName().toStdString();
}


Expand Down Expand Up @@ -100,10 +112,10 @@ CommandLine::CommandLine(std::list<std::string> args) :
}

// check if a possible file name was specified
if (args.size() == 1 && !args.front().front() == '-')
if (args.size() == 1 && args.front().front() != '-')
{
// not an option so assume argument is a file name
m_fileName = QString::fromStdString(args.front());
m_fileName = args.front();
return;
}

Expand All @@ -127,7 +139,7 @@ bool CommandLine::isVersionOption(const std::list<std::string> &args)
{
return false; // not our option or extra/invalid options
}
cout() << m_programName + " version " + version().toStdString() << '\n';
cout() << m_programName + " version " + version() << '\n';
return true;
}

Expand All @@ -139,13 +151,4 @@ bool CommandLine::isHelpOption(const std::list<std::string> &args) const
}


// function to return the version number string
QString CommandLine::version(void) const
{
QString versionString(ibcp_RELEASE_STRING);
int start {versionString.indexOf(QRegExp("\\d"))};
return versionString.mid(start);
}


// end: commandline.cpp
19 changes: 10 additions & 9 deletions commandline.h
Expand Up @@ -29,13 +29,8 @@
#include <list>
#include <string>

#include <QCoreApplication>
#include <QStringList>

class CommandLine
{
Q_DECLARE_TR_FUNCTIONS(CommandLine)

public:
explicit CommandLine(std::list<std::string> args);

Expand All @@ -47,13 +42,19 @@ class CommandLine
{
return m_returnCode;
}
QString version(void) const;
QString fileName(void) const

const std::string programName() const
{
return m_programName;
}
std::string fileName(void) const
{
return m_fileName;
}

static const std::string copyrightStatement();
static const std::string copyrightStatement(const char *copyright
= "Copyright");
static std::string version(void);
static std::string baseFileName(const std::string &filePath);

private:
Expand All @@ -64,7 +65,7 @@ class CommandLine
std::string m_programName;
int m_returnCode;
std::ostream *m_cout;
QString m_fileName;
std::string m_fileName;
};


Expand Down
63 changes: 40 additions & 23 deletions mainwindow.cpp
Expand Up @@ -22,6 +22,9 @@
//
// 2012-11-28 initial version

#include <fstream>
#include <string>

#include <QApplication>
#include <QCloseEvent>
#include <QFileDialog>
Expand All @@ -41,7 +44,8 @@

MainWindow::MainWindow(QWidget *parent) :
QMainWindow {parent},
ui {new Ui::MainWindow}
ui {new Ui::MainWindow},
m_guiActive {false}
{
// convert arguments to standard list of standard strings
std::list<std::string> args;
Expand All @@ -54,7 +58,6 @@ MainWindow::MainWindow(QWidget *parent) :
if (m_commandLine->processed())
{
// don't start GUI and retrieve the return code
m_guiActive = false;
m_returnCode = m_commandLine->returnCode();
return;
}
Expand Down Expand Up @@ -119,7 +122,7 @@ MainWindow::MainWindow(QWidget *parent) :

// if a file name was specified on the command line
// then it overrides the restored program
if (!m_commandLine->fileName().isEmpty())
if (!m_commandLine->fileName().empty())
{
setCurProgram(m_commandLine->fileName());
}
Expand All @@ -130,13 +133,25 @@ MainWindow::MainWindow(QWidget *parent) :
}

// load program if one was saved or specified on command line
if (!m_program.fileName().isEmpty() // load a program?
&& (!QFile::exists(m_program.fileName()) // program not found?
|| !programLoad(m_program.fileName())) // program not loaded?
&& m_commandLine->fileName().isEmpty()) // no program argument
if (!m_program.fileName().empty() // load a program?
&& (!std::ifstream(m_program.fileName()).good() // program not found?
|| !programLoad(m_program.fileName()))) // program not loaded?
{
setCurProgram(""); // clear program path that was restored/set
// TODO should an warning message be issued here?
if (m_commandLine->fileName().empty()) // no program argument
{
QMessageBox::warning(this, tr("IBCP"),
QString(tr("Last used program '%1' no longer exists!"))
.arg(QString(m_program.fileName().c_str())));
setCurProgram(""); // clear program path that was restored/set
}
else // command line program does not exist
{
// don't start GUI and set bad return code
std::cerr << m_commandLine->programName() << ": "
<< m_program.fileName() << ": No such file or directory\n";
m_returnCode = 1;
return;
}
}

m_guiActive = true;
Expand Down Expand Up @@ -264,7 +279,7 @@ void MainWindow::on_actionOpen_triggered(void)
tr("Open Program"), m_curDirectory, tr("Program files (*.*)"))};
if (!programPath.isEmpty())
{
programLoad(programPath);
programLoad(programPath.toStdString());
m_curDirectory = QFileInfo(programPath).path();
}
}
Expand All @@ -286,7 +301,7 @@ void MainWindow::on_actionClearRecent_triggered(void)

bool MainWindow::on_actionSave_triggered(void)
{
if (m_program.fileName().isEmpty())
if (m_program.fileName().empty())
{
return on_actionSaveAs_triggered();
}
Expand All @@ -304,7 +319,7 @@ bool MainWindow::on_actionSave_triggered(void)

bool MainWindow::on_actionSaveAs_triggered(void)
{
QString programPath {m_program.fileName()};
QString programPath {m_program.fileName().c_str()};
if (programPath.isEmpty())
{
programPath = m_curDirectory;
Expand All @@ -316,7 +331,7 @@ bool MainWindow::on_actionSaveAs_triggered(void)
return false;
}
m_curDirectory = QFileInfo(programPath).path();
return programSave(programPath);
return programSave(programPath.toStdString());
}


Expand Down Expand Up @@ -400,8 +415,9 @@ void MainWindow::on_actionAbout_triggered(void)
QString aboutString(tr("<h3>Interactive BASIC Compiler Project</h3>"));

// add version and copyright year to string
aboutString.append(tr("Version ")).append(m_commandLine->version());
aboutString.append(m_commandLine->copyrightStatement().c_str());
aboutString.append(tr("Version ")).append(m_commandLine->version().c_str());
aboutString.append(m_commandLine->copyrightStatement(tr("Copyright")
.toStdString().c_str()).c_str());

// add license statements
aboutString.append(tr("<p>This program is free software: you can "
Expand Down Expand Up @@ -435,13 +451,14 @@ void MainWindow::on_actionAboutQt_triggered(void)


// function the sets the current program path and sets the window title
void MainWindow::setCurProgram(const QString &programPath)
void MainWindow::setCurProgram(const std::string &programPath)
{
m_program.setFileName(programPath);
QString program {tr("Untitled")};
if (!m_program.fileName().isEmpty())
if (!m_program.fileName().empty())
{
program = m_recentPrograms->addFile(m_program.fileName());
program
= m_recentPrograms->addFile(QString(m_program.fileName().c_str()));
}
setWindowTitle(tr("%1[*] - %2").arg(program).arg(tr("IBCP")));
}
Expand Down Expand Up @@ -476,7 +493,7 @@ void MainWindow::programOpen(const QString programPath)
{
if (isOkToContinue())
{
programLoad(programPath);
programLoad(programPath.toStdString());
}
}

Expand All @@ -486,9 +503,9 @@ void MainWindow::programOpen(const QString programPath)
// - for now just reads a text file and puts the text in the edit box
// - eventually it will need to be parsed, translated, encoded and stored

bool MainWindow::programLoad(const QString &programPath)
bool MainWindow::programLoad(const std::string &programPath)
{
QFile file {programPath};
QFile file {programPath.c_str()};

if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
Expand All @@ -515,9 +532,9 @@ bool MainWindow::programLoad(const QString &programPath)
// text representation of the stored program, but may also need to
// collect the text from other edit boxes and/or subroutine/functions

bool MainWindow::programSave(const QString &programPath)
bool MainWindow::programSave(const std::string &programPath)
{
QFile file {programPath};
QFile file {programPath.c_str()};

programCaptureEditChanges();

Expand Down
6 changes: 3 additions & 3 deletions mainwindow.h
Expand Up @@ -89,10 +89,10 @@ private slots:
void editBoxSetActive(EditBox *editBox);

bool isOkToContinue(void);
void setCurProgram(const QString &programPath);
void setCurProgram(const std::string &programPath);

bool programLoad(const QString &programPath);
bool programSave(const QString &programPath);
bool programLoad(const std::string &programPath);
bool programSave(const std::string &programPath);
void programCaptureEditChanges(void);

void settingsRestore(void);
Expand Down
5 changes: 3 additions & 2 deletions program.cpp
Expand Up @@ -51,14 +51,15 @@ void Program::clear(void)
// function to restore the settings saved the last time the program was run
void Program::settingsRestore(QSettings &settings)
{
m_fileName = settings.value(curProgramSettingsName).toString();
m_fileName = settings.value(curProgramSettingsName).toString()
.toStdString();
}


// function to save the settings for the program to be restored later
void Program::settingsSave(QSettings &settings)
{
settings.setValue(curProgramSettingsName, m_fileName);
settings.setValue(curProgramSettingsName, m_fileName.c_str());
}


Expand Down
6 changes: 3 additions & 3 deletions program.h
Expand Up @@ -44,11 +44,11 @@ class Program : public QObject
void settingsSave(QSettings &settings);

// access functions
const QString fileName(void)
const std::string fileName(void)
{
return m_fileName;
}
void setFileName(QString fileName)
void setFileName(const std::string &fileName)
{
m_fileName = fileName;
}
Expand All @@ -62,7 +62,7 @@ class Program : public QObject
public slots:

private:
QString m_fileName; // file name of current program
std::string m_fileName; // file name of current program
ProgramModel *m_unit; // program unit for main routine
};

Expand Down
5 changes: 2 additions & 3 deletions programmodel.cpp
Expand Up @@ -132,7 +132,7 @@ QString ProgramModel::debugText(int lineIndex, bool fullInfo) const
{
const std::string operand {operandText(this, line[++i].operand())};
string += QString(" %1:%2").arg(i)
.arg(line[i].operandDebugText(QString::fromStdString(operand)));
.arg(line[i].operandDebugText(operand.c_str()));
}
}

Expand Down Expand Up @@ -654,8 +654,7 @@ RpnList ProgramModel::decode(const LineInfo &lineInfo)
= m_table.operandTextFunction(token->code());
if (operandText)
{
token->setString(QString::fromStdString(operandText(this,
line[++i].operand())));
token->setString(operandText(this, line[++i].operand()).c_str());
}
rpnList.append(token);
}
Expand Down

0 comments on commit c998b1f

Please sign in to comment.