Frobby  0.9.0
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 
27 IOParameters::IOParameters(const DataType& input, const DataType& output):
28  _inputType(input),
29  _outputType(output),
30  _inputFormat(0),
31  _outputFormat(0) {
32 
33  string inputFormats;
34  string outputFormats;
35 
36  string defaultOutput;
37  if (!_inputType.isNull())
39  else {
40  defaultOutput = IO::Macaulay2IOHandler::staticGetName();
41  ASSERT(createIOHandler(defaultOutput)->supportsOutput(_outputType));
42  }
43 
44  vector<string> names;
45  getIOHandlerNames(names);
46  for (vector<string>::const_iterator name = names.begin();
47  name != names.end(); ++name) {
48  auto_ptr<IOHandler> handler = createIOHandler(*name);
49  ASSERT(handler.get() != 0);
50 
51  if (handler->supportsInput(_inputType)) {
52  inputFormats += ' ';
53  inputFormats += handler->getName();
54  }
55  if (handler->supportsOutput(_outputType)) {
56  outputFormats += ' ';
57  outputFormats += handler->getName();
58  }
59  }
60 
61  if (!_inputType.isNull()) {
62  string desc =
63  "The format used to read the input. "
64  "This action supports the formats:\n " + inputFormats + ".\n"
65  "The format \"" +
67  "\" instructs Frobby to guess the format.\n"
68  "Type 'frobby help io' for more information on input formats.";
69 
70  _inputFormat.reset
71  (new StringParameter
72  ("iformat", desc.c_str(),
75  }
76 
77  if (!output.isNull()) {
78  string desc =
79  "The format used to write the output. "
80  "This action supports the formats:\n " + outputFormats + ".\n";
81  if (!_inputType.isNull()) {
82  desc +=
83  "The format \"" +
85  + "\" instructs Frobby to use the input format.\n";
86  }
87  desc += "Type 'frobby help io' for more information on output formats.";
88 
89  _outputFormat.reset
90  (new StringParameter("oformat", desc.c_str(), defaultOutput));
92  }
93 }
94 
95 void IOParameters::setOutputFormat(const string& format) {
97  ASSERT(_outputFormat.get() != 0);
98 
99  *_outputFormat = format;
100 }
101 
102 void IOParameters::setInputFormat(const string& format) {
104 
105  *_inputFormat = format;
106 }
107 
108 const string& IOParameters::getInputFormat() const {
110  ASSERT(_inputFormat.get() != 0);
111 
112  return *_inputFormat;
113 }
114 
115 const string& IOParameters::getOutputFormat() const {
117  ASSERT(_outputFormat.get() != 0);
118 
119  if (!_inputType.isNull() &&
120  _outputFormat->getValue() ==
122  ASSERT(_inputFormat.get() != 0);
123  return *_inputFormat;
124  }
125 
126  return *_outputFormat;
127 }
128 
129 auto_ptr<IOHandler> IOParameters::createInputHandler() const {
130  auto_ptr<IOHandler> handler(createIOHandler(getInputFormat()));
131  ASSERT(handler.get() != 0);
132  return handler;
133 }
134 
135 auto_ptr<IOHandler> IOParameters::createOutputHandler() const {
136  auto_ptr<IOHandler> handler(createIOHandler(getOutputFormat()));
137  ASSERT(handler.get() != 0);
138  return handler;
139 }
140 
143  ASSERT(_inputFormat.get() != 0);
144 
145  if (_inputFormat->getValue() ==
148 
149  if (in.getFormat() ==
151  in.setFormat(*_inputFormat);
152 }
153 
155  IOFacade facade(false);
156 
157  if (!_inputType.isNull()) {
158  auto_ptr<IOHandler> handler(createIOHandler(getInputFormat()));
159 
160  if (!handler->supportsInput(_inputType)) {
161  FrobbyStringStream errorMsg;
162  errorMsg << "The "
163  << handler->getName()
164  << " format does not support input of "
165  << _inputType.getName()
166  << '.';
167  reportError(errorMsg);
168  }
169  }
170 
171  if (!_outputType.isNull()) {
172  auto_ptr<IOHandler> handler(createIOHandler(getOutputFormat()));
173  /*
174  if (!handler->supportsOutput(_outputType)) {
175  FrobbyStringStream errorMsg;
176  errorMsg << "The "
177  << handler->getName()
178  << " format does not support output of "
179  << _outputType.getName()
180  << '.';
181  reportError(errorMsg);
182  }
183  */
184  }
185 }
IOParameters(const DataType &input, const DataType &output)
auto_ptr< IOHandler > createInputHandler() const
void validateFormats() const
const string & getOutputFormat() const
const DataType & _inputType
Definition: IOParameters.h:52
#define ASSERT(X)
Definition: stdinc.h:85
const string & getFormat() const
Definition: Scanner.h:61
The intention of this class is to describe the different kinds of mathematical structures that Frobby...
Definition: DataType.h:29
void autoDetectInputFormat(Scanner &in)
If using the input format, this must be called before validating the ideals, since the auto detect fo...
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
const DataType & _outputType
Definition: IOParameters.h:53
auto_ptr< StringParameter > _inputFormat
Definition: IOParameters.h:55
string autoDetectFormat(Scanner &in)
Return the format of what in is reading based on the first non-whitespace character.
Definition: IOHandler.cpp:198
void setInputFormat(const string &format)
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition: Scanner.h:50
void getIOHandlerNames(vector< string > &names)
Add the name of each fomat to names.
Definition: IOHandler.cpp:156
auto_ptr< IOHandler > createOutputHandler() const
bool isNull() const
Returns true if this object was returned by getNullType().
Definition: DataType.cpp:28
const string & getInputFormat() const
void addParameter(Parameter *parameter)
void setFormat(const string &format)
Definition: Scanner.h:62
static const char * staticGetName()
auto_ptr< StringParameter > _outputFormat
Definition: IOParameters.h:56
const char * getName() const
Returns the name of the structure.
Definition: DataType.cpp:24
A facade for input and output of mathematical objects.
Definition: IOFacade.h:39
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
void reportError(const string &errorMsg)
Definition: error.cpp:23
A replacement for stringstream.
auto_ptr< IOHandler > createIOHandler(const string &prefix)
Returns an IOHandler for the format whose name has the given prefix.
Definition: IOHandler.cpp:145
void setOutputFormat(const string &format)