Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
49 
50 //=============================================================================
51 //
52 // Implements the OpenMesh IOManager singleton
53 //
54 //=============================================================================
55 
56 
57 //== INCLUDES =================================================================
58 
59 
60 #include <OpenMesh/Core/IO/IOManager.hh>
61 
62 #include <iostream>
63 
64 
65 //== NAMESPACES ===============================================================
66 
67 
68 namespace OpenMesh {
69 namespace IO {
70 
71 
72 //=============================================================================
73 
74 // Destructor never called. Moved into singleton getter function
75 // _IOManager_ *__IOManager_instance = 0;
76 
78 {
79 
80  static _IOManager_ __IOManager_instance;
81 
82  //if (!__IOManager_instance)
83  // __IOManager_instance = new _IOManager_();
84 
85  return __IOManager_instance;
86 }
87 
88 //-----------------------------------------------------------------------------
89 
90 bool
92 read(const std::string& _filename, BaseImporter& _bi, Options& _opt)
93 {
94  std::set<BaseReader*>::const_iterator it = reader_modules_.begin();
95  std::set<BaseReader*>::const_iterator it_end = reader_modules_.end();
96 
97  if( it == it_end )
98  {
99  omerr() << "[OpenMesh::IO::_IOManager_] No reading modules available!\n";
100  return false;
101  }
102 
103  // Try all registered modules
104  for(; it != it_end; ++it)
105  if ((*it)->can_u_read(_filename))
106  {
107  _bi.prepare();
108  bool ok = (*it)->read(_filename, _bi, _opt);
109  _bi.finish();
110  return ok;
111  }
112 
113  // All modules failed to read
114  return false;
115 }
116 
117 
118 //-----------------------------------------------------------------------------
119 
120 
121 bool
123 read(std::istream& _is, const std::string& _ext, BaseImporter& _bi, Options& _opt)
124 {
125  std::set<BaseReader*>::const_iterator it = reader_modules_.begin();
126  std::set<BaseReader*>::const_iterator it_end = reader_modules_.end();
127 
128  // Try all registered modules
129  for(; it != it_end; ++it)
130  if ((*it)->BaseReader::can_u_read(_ext)) //Use the extension check only (no file existence)
131  {
132  _bi.prepare();
133  bool ok = (*it)->read(_is, _bi, _opt);
134  _bi.finish();
135  return ok;
136  }
137 
138  // All modules failed to read
139  return false;
140 }
141 
142 
143 //-----------------------------------------------------------------------------
144 
145 
146 bool
148 write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision)
149 {
150  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin();
151  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
152 
153  if ( it == it_end )
154  {
155  omerr() << "[OpenMesh::IO::_IOManager_] No writing modules available!\n";
156  return false;
157  }
158 
159  // Try all registered modules
160  for(; it != it_end; ++it)
161  {
162  if ((*it)->can_u_write(_filename))
163  {
164  return (*it)->write(_filename, _be, _opt, _precision);
165  }
166  }
167 
168  // All modules failed to save
169  return false;
170 }
171 
172 //-----------------------------------------------------------------------------
173 
174 
175 bool
177 write(std::ostream& _os,const std::string &_ext, BaseExporter& _be, Options _opt, std::streamsize _precision)
178 {
179  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin();
180  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
181 
182  if ( it == it_end )
183  {
184  omerr() << "[OpenMesh::IO::_IOManager_] No writing modules available!\n";
185  return false;
186  }
187 
188  // Try all registered modules
189  for(; it != it_end; ++it)
190  {
191  if ((*it)->BaseWriter::can_u_write(_ext)) //Restrict test to the extension check
192  {
193  return (*it)->write(_os, _be, _opt, _precision);
194  }
195  }
196 
197  // All modules failed to save
198  return false;
199 }
200 
201 //-----------------------------------------------------------------------------
202 
203 
204 bool
206 can_read( const std::string& _format ) const
207 {
208  std::set<BaseReader*>::const_iterator it = reader_modules_.begin();
209  std::set<BaseReader*>::const_iterator it_end = reader_modules_.end();
210  std::string filename = "dummy." + _format;
211 
212  for(; it != it_end; ++it)
213  if ((*it)->can_u_read(filename))
214  return true;
215 
216  return false;
217 }
218 
219 
220 //-----------------------------------------------------------------------------
221 
222 
223 bool
225 can_write( const std::string& _format ) const
226 {
227  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin();
228  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
229  std::string filename = "dummy." + _format;
230 
231  // Try all registered modules
232  for(; it != it_end; ++it)
233  if ((*it)->can_u_write(filename))
234  return true;
235 
236  return false;
237 }
238 
239 
240 //-----------------------------------------------------------------------------
241 
242 
243 const BaseWriter*
244 _IOManager_::
245 find_writer(const std::string& _format)
246 {
247  using std::string;
248 
249  string::size_type dot = _format.rfind('.');
250 
251  string ext;
252  if (dot == string::npos)
253  ext = _format;
254  else
255  ext = _format.substr(dot+1,_format.length()-(dot+1));
256 
257  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin();
258  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
259  std::string filename = "dummy." + ext;
260 
261  // Try all registered modules
262  for(; it != it_end; ++it)
263  if ((*it)->can_u_write(filename))
264  return *it;
265 
266  return NULL;
267 }
268 
269 
270 //-----------------------------------------------------------------------------
271 
272 
273 void
274 _IOManager_::
275 update_read_filters()
276 {
277  std::set<BaseReader*>::const_iterator it = reader_modules_.begin(),
278  it_end = reader_modules_.end();
279  std::string all = "";
280  std::string filters = "";
281 
282  for(; it != it_end; ++it)
283  {
284  // Initialized with space, as a workaround for debug build with clang on mac
285  // which crashes if tmp is initialized with an empty string ?!
286  std::string tmp = " ";
287 
288  filters += (*it)->get_description() + " (";
289 
290  std::istringstream iss((*it)->get_extensions());
291 
292  while (iss && !iss.eof() && (iss >> tmp) )
293  {
294  tmp = " *." + tmp; filters += tmp; all += tmp;
295  }
296 
297  filters += " );;";
298  }
299 
300  all = "All files ( " + all + " );;";
301 
302  read_filters_ = all + filters;
303 }
304 
305 
306 //-----------------------------------------------------------------------------
307 
308 
309 void
310 _IOManager_::
311 update_write_filters()
312 {
313  std::set<BaseWriter*>::const_iterator it = writer_modules_.begin(),
314  it_end = writer_modules_.end();
315  std::string all;
316  std::string filters;
317 
318  for(; it != it_end; ++it)
319  {
320  // Initialized with space, as a workaround for debug build with clang on mac
321  // which crashes if tmp is initialized with an empty string ?!
322  std::string s = " ";
323 
324  filters += (*it)->get_description() + " (";
325 
326  std::istringstream iss((*it)->get_extensions());
327  while (iss && !iss.eof() && (iss >> s))
328  { s = " *." + s; filters += s; all += s; }
329 
330  filters += " );;";
331  }
332  all = "All files ( " + all + " );;";
333 
334  write_filters_ = all + filters;
335 }
336 
337 
338 //=============================================================================
339 } // namespace IO
340 } // namespace OpenMesh
341 //=============================================================================
bool can_write(const std::string &_format) const
Returns true if the format is supported by one of the writer modules.
Definition: IOManager.cc:225
Set options for reader/writer modules.
Definition: Options.hh:95
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.
bool write(const std::string &_filename, BaseExporter &_be, Options _opt=Options::Default, std::streamsize _precision=6)
Definition: IOManager.cc:148
bool read(const std::string &_filename, BaseImporter &_bi, Options &_opt)
Definition: IOManager.cc:92
bool can_read(const std::string &_format) const
Returns true if the format is supported by one of the reader modules.
Definition: IOManager.cc:206
_IOManager_ & IOManager()
Definition: IOManager.cc:77