Skip to content

Commit

Permalink
utility: created for base file name function
Browse files Browse the repository at this point in the history
to remove dependency from the command line class of Qt, the base file
name function was moved into a new utility class where only its source
file needs to include Qt headers, which releases any callers of its
member functions from having to include them (the class itself was made
so that it can't be instanced or used as a base class)

to remove dependency from the tester class of the command line class,
the copyright statement string is now passed into the run function as
a standard string
  • Loading branch information
thunder422 committed Oct 18, 2014
1 parent c998b1f commit 3020cd6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 17 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -191,6 +191,7 @@ set(ibcp_SOURCES
token.cpp
translator.cpp
test_ibcp.cpp
utility.cpp
)

# list the header files containing QOBJECT
Expand Down
14 changes: 3 additions & 11 deletions commandline.cpp
Expand Up @@ -25,11 +25,10 @@
#include <iostream>
#include <sstream>

#include <QFileInfo>

#include "ibcp_config.h" // for cmake
#include "commandline.h"
#include "test_ibcp.h"
#include "utility.h"


// function to generate the copyright statement
Expand All @@ -55,18 +54,11 @@ std::string CommandLine::version(void)
}


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


CommandLine::CommandLine(std::list<std::string> args) :
m_cout {nullptr}
{
// get base file name of program from first argument
m_programName = baseFileName(args.front());
m_programName = Utility::baseFileName(args.front());
args.pop_front(); // remove program name

m_returnCode = -1;
Expand Down Expand Up @@ -99,7 +91,7 @@ CommandLine::CommandLine(std::list<std::string> args) :
}
else if (tester.hasOption())
{
if (tester.run(this))
if (tester.run(std::move(copyrightStatement())))
{
m_returnCode = 0;
}
Expand Down
1 change: 0 additions & 1 deletion commandline.h
Expand Up @@ -55,7 +55,6 @@ class CommandLine
static const std::string copyrightStatement(const char *copyright
= "Copyright");
static std::string version(void);
static std::string baseFileName(const std::string &filePath);

private:
std::ostream &cout(std::ostream *stream = &std::cout);
Expand Down
8 changes: 4 additions & 4 deletions test_ibcp.cpp
Expand Up @@ -27,10 +27,10 @@
#include <iostream>

#include "test_ibcp.h"
#include "commandline.h"
#include "table.h"
#include "parser.h"
#include "statusmessage.h"
#include "utility.h"


// overloaded output stream operator for abbreviated contents of a token
Expand Down Expand Up @@ -287,7 +287,7 @@ Tester::Tester(const std::string &programName,
{
// find start of file name less path
m_testFileName = args.back();
std::string baseName {CommandLine::baseFileName(m_testFileName)};
std::string baseName {Utility::baseFileName(m_testFileName)};

for (auto iterator : name)
{
Expand Down Expand Up @@ -353,7 +353,7 @@ std::string Tester::options(void)
}


bool Tester::run(CommandLine *commandLine)
bool Tester::run(std::string copyrightStatement)
{
std::ifstream ifs;

Expand All @@ -371,7 +371,7 @@ bool Tester::run(CommandLine *commandLine)

if (inputMode)
{
m_cout << m_programName << commandLine->copyrightStatement() << "\n\n";
m_cout << m_programName << copyrightStatement << "\n\n";

m_cout << "This program comes with ABSOLUTELY NO WARRANTY.\n";
m_cout << "This is free software, and you are welcome to\n";
Expand Down
2 changes: 1 addition & 1 deletion test_ibcp.h
Expand Up @@ -46,7 +46,7 @@ class Tester
const std::list<std::string> &args, std::ostream &cout);

static std::string options(void);
bool run(CommandLine *commandLine);
bool run(std::string copyrightStatement);
bool hasOption(void) const // has a test option been specified?
{
return m_option != Option{};
Expand Down
37 changes: 37 additions & 0 deletions utility.cpp
@@ -0,0 +1,37 @@
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: utility.cpp - utility class source file
// Copyright (C) 2014 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2014-10-18 initial version (parts removed from commandline.cpp)

#include <QFileInfo>

#include "utility.h"


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


// end: utility.cpp
38 changes: 38 additions & 0 deletions utility.h
@@ -0,0 +1,38 @@
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: utility.h - utility class header file
// Copyright (C) 2014
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2014-10-18 initial version (parts removed from commandline.h)

#ifndef UTILITY_H
#define UTILITY_H

#include <string>


class Utility final // final prevents derived classes
{
Utility() = delete; // prevent instances;
public:
static std::string baseFileName(const std::string &filePath);
};

#endif // UTILITY_H

0 comments on commit 3020cd6

Please sign in to comment.