Frobby 0.9.7
IOParameters.cpp
Go to the documentation of this file.
1/* Frobby: Software for monomial ideal computations.
2 Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see http://www.gnu.org/licenses/.
16*/
17#include "stdinc.h"
18#include "IOParameters.h"
19
20#include "IOFacade.h"
21#include "Macaulay2IOHandler.h"
22#include "Scanner.h"
23#include "error.h"
24#include "FrobbyStringStream.h"
25#include "DataType.h"
26
27IOParameters::IOParameters(const DataType& input, const DataType& output):
28 _inputType(input),
29 _outputType(output) {
30
31 string inputFormats;
32 string outputFormats;
33
34 string defaultOutput;
35 if (!_inputType.isNull())
37 else {
39 ASSERT(createIOHandler(defaultOutput)->supportsOutput(_outputType));
40 }
41
42 vector<string> names;
43 getIOHandlerNames(names);
44 for (vector<string>::const_iterator name = names.begin();
45 name != names.end(); ++name) {
46 unique_ptr<IOHandler> handler = createIOHandler(*name);
47 ASSERT(handler.get() != 0);
48
49 if (handler->supportsInput(_inputType)) {
50 inputFormats += ' ';
51 inputFormats += handler->getName();
52 }
53 if (handler->supportsOutput(_outputType)) {
54 outputFormats += ' ';
55 outputFormats += handler->getName();
56 }
57 }
58
59 if (!_inputType.isNull()) {
60 string desc =
61 "The format used to read the input. "
62 "This action supports the formats:\n " + inputFormats + ".\n"
63 "The format \"" +
65 "\" instructs Frobby to guess the format.\n"
66 "Type 'frobby help io' for more information on input formats.";
67
68 _inputFormat.reset
70 ("iformat", desc.c_str(),
73 }
74
75 if (!output.isNull()) {
76 string desc =
77 "The format used to write the output. "
78 "This action supports the formats:\n " + outputFormats + ".\n";
79 if (!_inputType.isNull()) {
80 desc +=
81 "The format \"" +
83 + "\" instructs Frobby to use the input format.\n";
84 }
85 desc += "Type 'frobby help io' for more information on output formats.";
86
87 _outputFormat.reset
88 (new StringParameter("oformat", desc.c_str(), defaultOutput));
90 }
91}
92
93void IOParameters::setOutputFormat(const string& format) {
94 ASSERT(!_inputType.isNull());
95 ASSERT(_outputFormat.get() != 0);
96
97 *_outputFormat = format;
98}
99
100void IOParameters::setInputFormat(const string& format) {
101 ASSERT(!_outputType.isNull());
102
103 *_inputFormat = format;
104}
105
106const string& IOParameters::getInputFormat() const {
107 ASSERT(!_inputType.isNull());
108 ASSERT(_inputFormat.get() != 0);
109
110 return *_inputFormat;
111}
112
113const string& IOParameters::getOutputFormat() const {
114 ASSERT(!_outputType.isNull());
115 ASSERT(_outputFormat.get() != 0);
116
117 if (!_inputType.isNull() &&
118 _outputFormat->getValue() ==
120 ASSERT(_inputFormat.get() != 0);
121 return *_inputFormat;
122 }
123
124 return *_outputFormat;
125}
126
127unique_ptr<IOHandler> IOParameters::createInputHandler() const {
128 unique_ptr<IOHandler> handler(createIOHandler(getInputFormat()));
129 ASSERT(handler.get() != 0);
130 return handler;
131}
132
133unique_ptr<IOHandler> IOParameters::createOutputHandler() const {
134 unique_ptr<IOHandler> handler(createIOHandler(getOutputFormat()));
135 ASSERT(handler.get() != 0);
136 return handler;
137}
138
151
153 IOFacade facade(false);
154
155 if (!_inputType.isNull()) {
156 unique_ptr<IOHandler> handler(createIOHandler(getInputFormat()));
157
158 if (!handler->supportsInput(_inputType)) {
159 FrobbyStringStream errorMsg;
160 errorMsg << "The "
161 << handler->getName()
162 << " format does not support input of "
163 << _inputType.getName()
164 << '.';
165 reportError(errorMsg);
166 }
167 }
168
169 if (!_outputType.isNull()) {
170 unique_ptr<IOHandler> handler(createIOHandler(getOutputFormat()));
171 /*
172 if (!handler->supportsOutput(_outputType)) {
173 FrobbyStringStream errorMsg;
174 errorMsg << "The "
175 << handler->getName()
176 << " format does not support output of "
177 << _outputType.getName()
178 << '.';
179 reportError(errorMsg);
180 }
181 */
182 }
183}
void getIOHandlerNames(vector< string > &names)
Add the name of each fomat to names.
string getFormatNameIndicatingToUseInputFormatAsOutputFormat()
Using the returned string in place of an (output) format name indicates to use the input format as th...
string getFormatNameIndicatingToGuessTheInputFormat()
Using the returned string in place of an (input) format name indicates to guess the format based on w...
unique_ptr< IOHandler > createIOHandler(const string &prefix)
Returns an IOHandler for the format whose name has the given prefix.
string autoDetectFormat(Scanner &in)
Return the format of what in is reading based on the first non-whitespace character.
The intention of this class is to describe the different kinds of mathematical structures that Frobby...
Definition DataType.h:29
bool isNull() const
Returns true if this object was returned by getNullType().
Definition DataType.cpp:28
A replacement for stringstream.
A facade for input and output of mathematical objects.
Definition IOFacade.h:39
void setOutputFormat(const string &format)
unique_ptr< StringParameter > _inputFormat
const string & getOutputFormat() const
void autoDetectInputFormat(Scanner &in)
If using the input format, this must be called before validating the ideals, since the auto detect fo...
unique_ptr< IOHandler > createInputHandler() const
IOParameters(const DataType &input, const DataType &output)
unique_ptr< StringParameter > _outputFormat
const string & getInputFormat() const
unique_ptr< IOHandler > createOutputHandler() const
void setInputFormat(const string &format)
const DataType & _inputType
const DataType & _outputType
void validateFormats() const
static const char * staticGetName()
void addParameter(Parameter *parameter)
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition Scanner.h:50
const string & getFormat() const
Definition Scanner.h:61
void setFormat(const string &format)
Definition Scanner.h:62
void reportError(const string &errorMsg)
Definition error.cpp:23
This header file includes common definitions and is included as the first line of code in every imple...
#define ASSERT(X)
Definition stdinc.h:86