SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GenericReader.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 #include <Dataflow/Network/Ports/StringPort.h>
38 #include <Core/Datatypes/String.h>
39 #include <Dataflow/GuiInterface/GuiVar.h>
40 
42 #include <sys/stat.h>
43 
44 namespace SCIRun {
45 
46 template <class HType>
47 class GenericReader : public Module
48 {
49 public:
50  GenericReader(const std::string &name, GuiContext* ctx,
51  const std::string &category, const std::string &package);
52  virtual ~GenericReader() { }
53 
54  virtual void execute();
55 
56 protected:
57  GuiFilename gui_filename_;
58  GuiString gui_from_env_;
59 
61 
62  bool importing_;
63 
64  virtual bool call_importer(const std::string &filename, HType & handle);
65 };
66 
67 
68 template <class HType>
69 GenericReader<HType>::GenericReader(const std::string &name, GuiContext* ctx,
70  const std::string &cat, const std::string &pack)
71  : Module(name, ctx, Source, cat, pack),
72  gui_filename_(get_ctx()->subVar("filename"), ""),
73  gui_from_env_(get_ctx()->subVar("from-env"),""),
74  old_filemodification_(0),
75  importing_(false)
76 {
77 }
78 
79 
80 template <class HType>
81 bool
82 GenericReader<HType>::call_importer(const std::string &/*filename*/,
83  HType & /*handle*/ )
84 {
85  return false;
86 }
87 
88 
89 template <class HType>
90 void
92 {
93  bool filename_changed = gui_filename_.changed();
94 
95  if (gui_from_env_.get() != "")
96  {
97  std::string filename_from_env = gui_from_env_.get();
98  if (sci_getenv(filename_from_env))
99  {
100  std::string envfilename = sci_getenv(filename_from_env);
101  gui_filename_.set(envfilename);
102  get_ctx()->reset();
103  filename_changed = true;
104  }
105  }
106 
107  // If there is an optional input string set the filename to it in the GUI.
108  StringHandle shandle;
109  if (get_input_handle("Filename",shandle,false))
110  {
111  gui_filename_.set(shandle->get());
112  get_ctx()->reset();
113  filename_changed = true;
114  }
115 
116  const std::string filename(gui_filename_.get());
117 
118  // Read the status of this file so we can compare modification timestamps
119  struct stat buf;
120 
121  if( filename == "" )
122  {
123  error("No file has been selected. Please choose a file.");
124  return;
125  }
126  else if (stat(filename.c_str(), &buf))
127  {
128  if (!importing_)
129  {
130  error("File '" + filename + "' not found.");
131  return;
132  }
133  else
134  {
135  warning("File '" + filename + "' not found. Maybe the plugin can find it?");
136  }
137  }
138 
139  // If we haven't read yet, or if it's a new filename,
140  // or if the datestamp has changed -- then read...
141 
142  time_t new_filemodification = buf.st_mtime;
143 
144  if( inputs_changed_ || filename_changed ||
145  new_filemodification != old_filemodification_ ||
146  !oport_cached(0) ||
147  !oport_cached("Filename") )
148  {
149  update_state(Executing);
150  old_filemodification_ = new_filemodification;
151 
152  HType handle;
153 
154  remark("loading file " +filename);
155 
156  if (importing_)
157  {
158  if (!call_importer(filename, handle))
159  {
160  error("Import failed.");
161  return;
162  }
163  }
164  else
165  {
166  PiostreamPtr stream = auto_istream(filename, 0);
167  if (!stream)
168  {
169  error("Error reading file '" + filename + "'.");
170  return;
171  }
172 
173  // Read the file
174  Pio(*stream, handle);
175 
176  if (!handle.get_rep() || stream->error())
177  {
178  error("Error reading data from file '" + filename +"'.");
179  return;
180  }
181  }
182 
183  shandle = new String(gui_filename_.get());
184  send_output_handle("Filename", shandle);
185 
186  send_output_handle(0, handle);
187  }
188 
189 }
190 
191 } // End namespace SCIRun
LockingHandle< String > StringHandle
Definition: String.h:57
time_t old_filemodification_
Definition: GenericReader.h:60
bool importing_
Definition: GenericReader.h:62
SCISHARE const char * sci_getenv(const std::string &key)
PiostreamPtr auto_istream(const std::string &filename, LoggerHandle pr)
Definition: Persistent.cc:394
boost::shared_ptr< Piostream > PiostreamPtr
Definition: Persistent.h:80
const char * name[]
Definition: BoostGraphExampleTests.cc:87
virtual ~GenericReader()
Definition: GenericReader.h:52
virtual bool call_importer(const std::string &filename, HType &handle)
Definition: GenericReader.h:82
virtual void execute()
Definition: GenericReader.h:91
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
GuiFilename gui_filename_
Definition: GenericReader.h:57
GuiString gui_from_env_
Definition: GenericReader.h:58
GenericReader(const std::string &name, GuiContext *ctx, const std::string &category, const std::string &package)
Definition: GenericReader.h:69
Definition: String.h:60
Definition: GenericReader.h:47