IOParameters.cpp
Go to the documentation of this file.
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
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 2 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include "stdinc.h"
#include "IOParameters.h"
#include "IOFacade.h"
#include "Macaulay2IOHandler.h"
#include "Scanner.h"
#include "error.h"
#include "FrobbyStringStream.h"
#include "DataType.h"
IOParameters::IOParameters(const DataType& input, const DataType& output):
_inputType(input),
_outputType(output) {
string inputFormats;
string outputFormats;
string defaultOutput;
if (!_inputType.isNull())
defaultOutput = getFormatNameIndicatingToUseInputFormatAsOutputFormat();
else {
defaultOutput = IO::Macaulay2IOHandler::staticGetName();
ASSERT(createIOHandler(defaultOutput)->supportsOutput(_outputType));
}
vector<string> names;
getIOHandlerNames(names);
for (vector<string>::const_iterator name = names.begin();
name != names.end(); ++name) {
unique_ptr<IOHandler> handler = createIOHandler(*name);
ASSERT(handler.get() != 0);
if (handler->supportsInput(_inputType)) {
inputFormats += ' ';
inputFormats += handler->getName();
}
if (handler->supportsOutput(_outputType)) {
outputFormats += ' ';
outputFormats += handler->getName();
}
}
if (!_inputType.isNull()) {
string desc =
"The format used to read the input. "
"This action supports the formats:\n " + inputFormats + ".\n"
"The format \"" +
getFormatNameIndicatingToGuessTheInputFormat() +
"\" instructs Frobby to guess the format.\n"
"Type 'frobby help io' for more information on input formats.";
_inputFormat.reset
(new StringParameter
("iformat", desc.c_str(),
getFormatNameIndicatingToGuessTheInputFormat()));
addParameter(_inputFormat.get());
}
if (!output.isNull()) {
string desc =
"The format used to write the output. "
"This action supports the formats:\n " + outputFormats + ".\n";
if (!_inputType.isNull()) {
desc +=
"The format \"" +
getFormatNameIndicatingToUseInputFormatAsOutputFormat()
+ "\" instructs Frobby to use the input format.\n";
}
desc += "Type 'frobby help io' for more information on output formats.";
_outputFormat.reset
(new StringParameter("oformat", desc.c_str(), defaultOutput));
addParameter(_outputFormat.get());
}
}
void IOParameters::setOutputFormat(const string& format) {
ASSERT(!_inputType.isNull());
ASSERT(_outputFormat.get() != 0);
*_outputFormat = format;
}
void IOParameters::setInputFormat(const string& format) {
ASSERT(!_outputType.isNull());
*_inputFormat = format;
}
const string& IOParameters::getInputFormat() const {
ASSERT(!_inputType.isNull());
ASSERT(_inputFormat.get() != 0);
return *_inputFormat;
}
const string& IOParameters::getOutputFormat() const {
ASSERT(!_outputType.isNull());
ASSERT(_outputFormat.get() != 0);
if (!_inputType.isNull() &&
_outputFormat->getValue() ==
getFormatNameIndicatingToUseInputFormatAsOutputFormat()) {
ASSERT(_inputFormat.get() != 0);
return *_inputFormat;
}
return *_outputFormat;
}
unique_ptr<IOHandler> IOParameters::createInputHandler() const {
unique_ptr<IOHandler> handler(createIOHandler(getInputFormat()));
ASSERT(handler.get() != 0);
return handler;
}
unique_ptr<IOHandler> IOParameters::createOutputHandler() const {
unique_ptr<IOHandler> handler(createIOHandler(getOutputFormat()));
ASSERT(handler.get() != 0);
return handler;
}
void IOParameters::autoDetectInputFormat(Scanner& in) {
ASSERT(!_inputType.isNull());
ASSERT(_inputFormat.get() != 0);
if (_inputFormat->getValue() ==
getFormatNameIndicatingToGuessTheInputFormat())
*_inputFormat = autoDetectFormat(in);
if (in.getFormat() ==
getFormatNameIndicatingToGuessTheInputFormat())
in.setFormat(*_inputFormat);
}
void IOParameters::validateFormats() const {
IOFacade facade(false);
if (!_inputType.isNull()) {
unique_ptr<IOHandler> handler(createIOHandler(getInputFormat()));
if (!handler->supportsInput(_inputType)) {
FrobbyStringStream errorMsg;
errorMsg << "The "
<< handler->getName()
<< " format does not support input of "
<< _inputType.getName()
<< '.';
reportError(errorMsg);
}
}
if (!_outputType.isNull()) {
unique_ptr<IOHandler> handler(createIOHandler(getOutputFormat()));
/*
if (!handler->supportsOutput(_outputType)) {
FrobbyStringStream errorMsg;
errorMsg << "The "
<< handler->getName()
<< " format does not support output of "
<< _outputType.getName()
<< '.';
reportError(errorMsg);
}
*/
}
}
DataType.h
FrobbyStringStream.h
IOFacade.h
getIOHandlerNames
void getIOHandlerNames(vector< string > &names)
Add the name of each fomat to names.
Definition IOHandler.cpp:156
getFormatNameIndicatingToUseInputFormatAsOutputFormat
string getFormatNameIndicatingToUseInputFormatAsOutputFormat()
Using the returned string in place of an (output) format name indicates to use the input format as th...
Definition IOHandler.cpp:230
getFormatNameIndicatingToGuessTheInputFormat
string getFormatNameIndicatingToGuessTheInputFormat()
Using the returned string in place of an (input) format name indicates to guess the format based on w...
Definition IOHandler.cpp:234
createIOHandler
unique_ptr< IOHandler > createIOHandler(const string &prefix)
Returns an IOHandler for the format whose name has the given prefix.
Definition IOHandler.cpp:145
autoDetectFormat
string autoDetectFormat(Scanner &in)
Return the format of what in is reading based on the first non-whitespace character.
Definition IOHandler.cpp:198
IOParameters.h
Macaulay2IOHandler.h
Scanner.h
DataType
The intention of this class is to describe the different kinds of mathematical structures that Frobby...
Definition DataType.h:29
DataType::isNull
bool isNull() const
Returns true if this object was returned by getNullType().
Definition DataType.cpp:28
FrobbyStringStream
A replacement for stringstream.
Definition FrobbyStringStream.h:26
IOFacade
A facade for input and output of mathematical objects.
Definition IOFacade.h:39
IOParameters::setOutputFormat
void setOutputFormat(const string &format)
Definition IOParameters.cpp:93
IOParameters::_inputFormat
unique_ptr< StringParameter > _inputFormat
Definition IOParameters.h:55
IOParameters::getOutputFormat
const string & getOutputFormat() const
Definition IOParameters.cpp:113
IOParameters::autoDetectInputFormat
void autoDetectInputFormat(Scanner &in)
If using the input format, this must be called before validating the ideals, since the auto detect fo...
Definition IOParameters.cpp:139
IOParameters::createInputHandler
unique_ptr< IOHandler > createInputHandler() const
Definition IOParameters.cpp:127
IOParameters::IOParameters
IOParameters(const DataType &input, const DataType &output)
Definition IOParameters.cpp:27
IOParameters::_outputFormat
unique_ptr< StringParameter > _outputFormat
Definition IOParameters.h:56
IOParameters::getInputFormat
const string & getInputFormat() const
Definition IOParameters.cpp:106
IOParameters::createOutputHandler
unique_ptr< IOHandler > createOutputHandler() const
Definition IOParameters.cpp:133
IOParameters::setInputFormat
void setInputFormat(const string &format)
Definition IOParameters.cpp:100
IOParameters::_inputType
const DataType & _inputType
Definition IOParameters.h:52
IOParameters::_outputType
const DataType & _outputType
Definition IOParameters.h:53
IOParameters::validateFormats
void validateFormats() const
Definition IOParameters.cpp:152
IO::Macaulay2IOHandler::staticGetName
static const char * staticGetName()
Definition Macaulay2IOHandler.cpp:145
ParameterGroup::addParameter
void addParameter(Parameter *parameter)
Definition ParameterGroup.cpp:31
Scanner
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition Scanner.h:50
Scanner::getFormat
const string & getFormat() const
Definition Scanner.h:61
Scanner::setFormat
void setFormat(const string &format)
Definition Scanner.h:62
StringParameter
Definition StringParameter.h:24
reportError
void reportError(const string &errorMsg)
Definition error.cpp:23
error.h
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...
ASSERT
#define ASSERT(X)
Definition stdinc.h:86