SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GenericWriter.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 #ifndef MODULES_DATAIO_GENERIC_WRITER_H
30 #define MODULES_DATAIO_GENERIC_WRITER_H
31 
32 ///
33 ///@author
34 /// Steven G. Parker
35 /// Department of Computer Science
36 /// University of Utah
37 ///@date July 1994
38 ///
39 
40 #include <boost/filesystem.hpp>
43 #include <Core/Datatypes/String.h>
44 
45 namespace SCIRun {
46  namespace Modules {
47  namespace DataIO {
48 
49 template <class HType, class PortTag>
51  public Has2InputPorts<PortTag, StringPortTag>,
52  public HasNoOutputPorts
53 {
54 public:
55  GenericWriter(const std::string &name, const std::string &category, const std::string &package, const std::string& stateFilename);
56  virtual ~GenericWriter() { }
57 
58  virtual void execute();
59 
60  INPUT_PORT(1, Filename, String);
61 
62 protected:
63  HType handle_;
64  std::string filename_;
65  mutable std::string filetype_;
68 
69  //GuiFilename filename_;
70  //GuiString filetype_;
71  //GuiInt confirm_;
72  //GuiInt confirm_once_;
73 
74  virtual bool useCustomExporter(const std::string& filename) const = 0;
75  virtual bool call_exporter(const std::string &filename) { return false; }
76 
77  virtual bool overwrite() { return true; } /// @todo
78 };
79 
80 
81 template <class HType, class PortTag>
82 GenericWriter<HType, PortTag>::GenericWriter(const std::string &name, const std::string &cat, const std::string &pack, const std::string& stateFilename)
83  : SCIRun::Dataflow::Networks::Module(SCIRun::Dataflow::Networks::ModuleLookupInfo(name, cat, pack)),
84  //filename_(get_ctx()->subVar("filename"), ""),
85  //filetype_(get_ctx()->subVar("filetype"), "Binary"),
86  //confirm_(get_ctx()->subVar("confirm"), sci_getenv_p("SCIRUN_CONFIRM_OVERWRITE")),
87  //confirm_once_(get_ctx()->subVar("confirm-once"),0),
88  stateFilename_(stateFilename),
89  objectPortName_(0)
90 {
91  INITIALIZE_PORT(Filename);
92 }
93 
94 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
95 template <class HType, class PortTag>
96 bool
98 {
99  std::string result;
100  TCLInterface::eval(get_id() + " overwrite", result);
101  if (result == std::string("0"))
102  {
103  warning("User chose to not save.");
104  return false;
105  }
106  return true;
107 }
108 #endif
109 
110 template <class HType, class PortTag>
111 void
113 {
114  // If there is an optional input string set the filename to it in the GUI.
115  /// @todo: this will be a common pattern for file loading. Perhaps it will be a base class method someday...
116  auto fileOption = getOptionalInput(Filename);
117  if (!fileOption)
118  filename_ = get_state()->getValue(stateFilename_).getString();
119  else
120  {
121  filename_ = (*fileOption)->value();
122 
123  boost::filesystem::path path(filename_);
124  path = boost::filesystem::absolute(path);
125  if (!boost::filesystem::exists(path.parent_path()))
126  {
127  error("Could not create path to filename");
128  return;
129  }
130  filename_ = path.string();
131  }
132 
133  if (!objectPortName_)
134  {
135  error("Logical error: object port name not specified.");
136  return;
137  }
138  handle_ = getRequiredInput(*objectPortName_);
139 
140  if (filename_.empty())
141  {
142  error("No filename specified.");
143  return;
144  }
145 
146 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
147  update_state(Executing);
148 #endif
149  remark("saving file " + filename_);
150 
151  if (!overwrite()) return;
152 
153  if (useCustomExporter(filename_))
154  {
155  if (!call_exporter(filename_))
156  {
157  error("Export failed.");
158  return;
159  }
160  }
161  else
162  {
163  PiostreamPtr stream;
164  if (filetype_ == "Binary")
165  {
166  stream = auto_ostream(filename_, "Binary", getLogger());
167  }
168  else
169  {
170  stream = auto_ostream(filename_, "Text", getLogger());
171  }
172 
173  if (stream->error())
174  {
175  error("Could not open file for writing" + filename_);
176  }
177  else
178  {
179  Pio(*stream, handle_);
180  }
181  }
182 }
183 
184 }}}
185 
186 #endif
PiostreamPtr auto_ostream(const std::string &filename, const std::string &type, LoggerHandle pr)
Definition: Persistent.cc:478
GenericWriter(const std::string &name, const std::string &category, const std::string &package, const std::string &stateFilename)
Definition: GenericWriter.h:82
virtual bool call_exporter(const std::string &filename)
Definition: GenericWriter.h:75
Definition: AlgorithmBase.h:52
HType handle_
Definition: GenericWriter.h:63
StaticPortName< typename HType::element_type, 0 > * objectPortName_
Definition: GenericWriter.h:67
Definition: Module.h:432
Definition: GenericWriter.h:50
boost::shared_ptr< Piostream > PiostreamPtr
Definition: Persistent.h:80
const char * name[]
Definition: BoostGraphExampleTests.cc:87
Core::Algorithms::AlgorithmParameterName stateFilename_
Definition: GenericWriter.h:66
virtual bool overwrite()
Definition: GenericWriter.h:77
Definition: Module.h:421
virtual ~GenericWriter()
Definition: GenericWriter.h:56
#define INITIALIZE_PORT(nameObj)
Definition: Module.h:674
virtual void execute()
Definition: GenericWriter.h:112
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
Definition: Module.h:53
tuple eval
Definition: eab.py:13
std::string filetype_
Definition: GenericWriter.h:65
Definition: String.h:60
virtual bool useCustomExporter(const std::string &filename) const =0
std::string filename_
Definition: GenericWriter.h:64