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 ///
30 /// @author
31 /// Steven G. Parker
32 /// Department of Computer Science
33 /// University of Utah
34 /// @date July 1994
35 ///
36 
37 ///@details
38 /// Limitations:
39 /// Uses .tcl file with "filename" and "filetype"
40 /// Input port must be of type SimpleIPort
41 
42 
43 #include <Dataflow/GuiInterface/GuiVar.h>
44 
47 #include <Core/Datatypes/String.h>
48 #include <Core/Util/FullFileName.h>
49 #include <Dataflow/Network/Ports/StringPort.h>
50 
51 namespace SCIRun {
52 
53 
54 template <class HType>
55 class GenericWriter : public Module {
56 protected:
57  HType handle_;
58  GuiFilename filename_;
59  GuiString filetype_;
60  GuiInt confirm_;
61  GuiInt confirm_once_;
62  bool exporting_;
63 
64  virtual bool overwrite();
65  virtual bool call_exporter(const std::string &filename);
66 
67 public:
68  GenericWriter(const std::string &name, GuiContext* ctx,
69  const std::string &category, const std::string &package);
70  virtual ~GenericWriter() { }
71 
72  virtual void execute();
73 };
74 
75 
76 template <class HType>
77 GenericWriter<HType>::GenericWriter(const std::string &name, GuiContext* ctx,
78  const std::string &cat, const std::string &pack)
79  : Module(name, ctx, Sink, cat, pack),
80  filename_(get_ctx()->subVar("filename"), ""),
81  filetype_(get_ctx()->subVar("filetype"), "Binary"),
82  confirm_(get_ctx()->subVar("confirm"), sci_getenv_p("SCIRUN_CONFIRM_OVERWRITE")),
83  confirm_once_(get_ctx()->subVar("confirm-once"),0),
84  exporting_(false)
85 {
86 }
87 
88 
89 template <class HType>
90 bool
92 {
93  std::string result;
94  TCLInterface::eval(get_id() + " overwrite", result);
95  if (result == std::string("0"))
96  {
97  warning("User chose to not save.");
98  return false;
99  }
100  return true;
101 }
102 
103 
104 template <class HType>
105 bool
106 GenericWriter<HType>::call_exporter(const std::string &/*filename*/)
107 {
108  return false;
109 }
110 
111 
112 template <class HType>
113 void
115 {
116  get_input_handle(0,handle_,true);
117 
118  StringHandle filename;
119  if (get_input_handle("Filename",filename,false))
120  {
121  // This piece of code makes sure that the path to the output
122  // file exists and that we use an absolute filename
123  FullFileName ffn(filename->get());
124  if (!(ffn.create_file_path()))
125  {
126  error("Could not create path to filename");
127  return;
128  }
129  filename = new String(ffn.get_abs_filename());
130 
131  filename_.set(filename->get());
132  get_ctx()->reset();
133  }
134 
135  // If no name is provided, return.
136 
137  std::string fn(filename_.get());
138  if (fn == "")
139  {
140  warning("No filename specified.");
141  return;
142  }
143 
144  update_state(Executing);
145  remark("saving file " +fn);
146 
147  if (!overwrite()) return;
148 
149  if (exporting_)
150  {
151  if (!call_exporter(fn))
152  {
153  error("Export failed.");
154  return;
155  }
156  }
157  else
158  {
159  // Open up the output stream
160  PiostreamPtr stream;
161  std::string ft(filetype_.get());
162  if (ft == "Binary")
163  {
164  stream = auto_ostream(fn, "Binary", this);
165  }
166  else
167  {
168  stream = auto_ostream(fn, "Text", this);
169  }
170 
171  if (stream->error())
172  {
173  error("Could not open file for writing" + fn);
174  }
175  else
176  {
177  // Write the file
178  Pio(*stream, handle_);
179  }
180  }
181 }
182 
183 } // End namespace SCIRun
LockingHandle< String > StringHandle
Definition: String.h:57
PiostreamPtr auto_ostream(const std::string &filename, const std::string &type, LoggerHandle pr)
Definition: Persistent.cc:478
SCISHARE bool sci_getenv_p(const std::string &key)
GenericWriter(const std::string &name, GuiContext *ctx, const std::string &category, const std::string &package)
Definition: GenericWriter.h:77
GuiFilename filename_
Definition: GenericWriter.h:58
virtual ~GenericWriter()
Definition: GenericWriter.h:70
HType handle_
Definition: GenericWriter.h:57
void set(const std::string &str)
Definition: String.h:85
boost::shared_ptr< Piostream > PiostreamPtr
Definition: Persistent.h:80
const char * name[]
Definition: BoostGraphExampleTests.cc:87
virtual void execute()
Definition: GenericWriter.h:114
virtual bool overwrite()
Definition: GenericWriter.h:91
bool exporting_
Definition: GenericWriter.h:62
GuiInt confirm_once_
Definition: GenericWriter.h:61
GuiInt confirm_
Definition: GenericWriter.h:60
Definition: GenericWriter.h:55
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
tuple eval
Definition: eab.py:13
Definition: FullFileName.h:44
Definition: String.h:60
GuiString filetype_
Definition: GenericWriter.h:59
virtual bool call_exporter(const std::string &filename)
Definition: GenericWriter.h:106