Developer Documentation
IOManager.cc
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 
43 
44 
45 //=============================================================================
46 //
47 // Implements the OpenMesh IOManager singleton
48 //
49 //=============================================================================
50 
51 
52 //== INCLUDES =================================================================
53 
54 
55 #include <OpenMesh/Core/IO/IOManager.hh>
56 
57 #include <iostream>
58 
59 
60 //== NAMESPACES ===============================================================
61 
62 
63 namespace OpenMesh {
64 namespace IO {
65 
66 
67 //=============================================================================
68 
69 // Destructor never called. Moved into singleton getter function
70 // _IOManager_ *__IOManager_instance = 0;
71 
73 {
74 
75  static _IOManager_ __IOManager_instance;
76 
77  //if (!__IOManager_instance)
78  // __IOManager_instance = new _IOManager_();
79 
80  return __IOManager_instance;
81 }
82 
83 //-----------------------------------------------------------------------------
84 
85 bool
87 read(const std::string& _filename, BaseImporter& _bi, Options& _opt)
88 {
89  std::set<BaseReader*>::const_iterator it = reader_modules_.begin();
90  std::set<BaseReader*>::const_iterator it_end = reader_modules_.end();
91 
92  if( it == it_end )
93  {
94  omerr() << "[OpenMesh::IO::_IOManager_] No reading modules available!\n";
95  return false;
96  }
97 
98  // Try all registered modules
99  for(; it != it_end; ++it)
100  if ((*it)->can_u_read(_filename))
101  {
102  _bi.prepare();
103  bool ok = (*it)->read(_filename, _bi, _opt);
104  _bi.finish();
105  return ok;
106  }
107 
108  // All modules failed to read
109  return false;
110 }
111 
112 
113 //-----------------------------------------------------------------------------
114 
115 
116 bool
118 read(std::istream& _is, const std::string& _ext, BaseImporter& _bi, Options& _opt)
119 {
120  std::set<BaseReader*>::const_iterator it = reader_modules_.begin();
121  std::set<BaseReader*>::const_iterator it_end = reader_modules_.end();
122 
123  // Try all registered modules
124  for(; it != it_end; ++it)
125  if ((*it)->BaseReader::can_u_read(_ext)) //Use the extension check only (no file existence)
126  {
127  _bi.prepare();
128  bool ok = (*it)->read(_is, _bi, _opt);
129  _bi.finish();
130  return ok;
131  }
132 
133  // All modules failed to read
134  return false;
135 }
136 
137 
138 //-----------------------------------------------------------------------------
139 
140 
141 bool
143 write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision)
144 {
145  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin();
146  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
147 
148  if ( it == it_end )
149  {
150  omerr() << "[OpenMesh::IO::_IOManager_] No writing modules available!\n";
151  return false;
152  }
153 
154  // Try all registered modules
155  for(; it != it_end; ++it)
156  {
157  if ((*it)->can_u_write(_filename))
158  {
159  return (*it)->write(_filename, _be, _opt, _precision);
160  }
161  }
162 
163  // All modules failed to save
164  return false;
165 }
166 
167 //-----------------------------------------------------------------------------
168 
169 
170 bool
172 write(std::ostream& _os,const std::string &_ext, BaseExporter& _be, Options _opt, std::streamsize _precision)
173 {
174  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin();
175  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
176 
177  if ( it == it_end )
178  {
179  omerr() << "[OpenMesh::IO::_IOManager_] No writing modules available!\n";
180  return false;
181  }
182 
183  // Try all registered modules
184  for(; it != it_end; ++it)
185  {
186  if ((*it)->BaseWriter::can_u_write(_ext)) //Restrict test to the extension check
187  {
188  return (*it)->write(_os, _be, _opt, _precision);
189  }
190  }
191 
192  // All modules failed to save
193  return false;
194 }
195 
196 //-----------------------------------------------------------------------------
197 
198 
199 bool
201 can_read( const std::string& _format ) const
202 {
203  std::set<BaseReader*>::const_iterator it = reader_modules_.begin();
204  std::set<BaseReader*>::const_iterator it_end = reader_modules_.end();
205  std::string filename = "dummy." + _format;
206 
207  for(; it != it_end; ++it)
208  if ((*it)->can_u_read(filename))
209  return true;
210 
211  return false;
212 }
213 
214 
215 //-----------------------------------------------------------------------------
216 
217 
218 bool
220 can_write( const std::string& _format ) const
221 {
222  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin();
223  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
224  std::string filename = "dummy." + _format;
225 
226  // Try all registered modules
227  for(; it != it_end; ++it)
228  if ((*it)->can_u_write(filename))
229  return true;
230 
231  return false;
232 }
233 
234 
235 //-----------------------------------------------------------------------------
236 
237 
238 const BaseWriter*
239 _IOManager_::
240 find_writer(const std::string& _format)
241 {
242  using std::string;
243 
244  string::size_type dot = _format.rfind('.');
245 
246  string ext;
247  if (dot == string::npos)
248  ext = _format;
249  else
250  ext = _format.substr(dot+1,_format.length()-(dot+1));
251 
252  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin();
253  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
254  std::string filename = "dummy." + ext;
255 
256  // Try all registered modules
257  for(; it != it_end; ++it)
258  if ((*it)->can_u_write(filename))
259  return *it;
260 
261  return NULL;
262 }
263 
264 
265 //-----------------------------------------------------------------------------
266 
267 
268 void
269 _IOManager_::
270 update_read_filters()
271 {
272  std::set<BaseReader*>::const_iterator it = reader_modules_.begin(),
273  it_end = reader_modules_.end();
274  std::string all = "";
275  std::string filters = "";
276 
277  for(; it != it_end; ++it)
278  {
279  // Initialized with space, as a workaround for debug build with clang on mac
280  // which crashes if tmp is initialized with an empty string ?!
281  std::string tmp = " ";
282 
283  filters += (*it)->get_description() + " (";
284 
285  std::istringstream iss((*it)->get_extensions());
286 
287  while (iss && !iss.eof() && (iss >> tmp) )
288  {
289  tmp = " *." + tmp; filters += tmp; all += tmp;
290  }
291 
292  filters += " );;";
293  }
294 
295  all = "All files ( " + all + " );;";
296 
297  read_filters_ = all + filters;
298 }
299 
300 
301 //-----------------------------------------------------------------------------
302 
303 
304 void
305 _IOManager_::
306 update_write_filters()
307 {
308  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin(),
309  it_end = writer_modules_.end();
310  std::string all;
311  std::string filters;
312 
313  for(; it != it_end; ++it)
314  {
315  // Initialized with space, as a workaround for debug build with clang on mac
316  // which crashes if tmp is initialized with an empty string ?!
317  std::string s = " ";
318 
319  filters += (*it)->get_description() + " (";
320 
321  std::istringstream iss((*it)->get_extensions());
322  while (iss && !iss.eof() && (iss >> s))
323  { s = " *." + s; filters += s; all += s; }
324 
325  filters += " );;";
326  }
327  all = "All files ( " + all + " );;";
328 
329  write_filters_ = all + filters;
330 }
331 
332 
333 //=============================================================================
334 } // namespace IO
335 } // namespace OpenMesh
336 //=============================================================================
bool can_write(const std::string &_format) const
Returns true if the format is supported by one of the writer modules.
Definition: IOManager.cc:220
bool can_read(const std::string &_format) const
Returns true if the format is supported by one of the reader modules.
Definition: IOManager.cc:201
_IOManager_ & IOManager()
Definition: IOManager.cc:72
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
bool read(const std::string &_filename, BaseImporter &_bi, Options &_opt)
Definition: IOManager.cc:87
Set options for reader/writer modules.
Definition: Options.hh:90
bool write(const std::string &_filename, BaseExporter &_be, Options _opt=Options::Default, std::streamsize _precision=6)
Definition: IOManager.cc:143