IOManager.cc 10.4 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
Jan Möbius's avatar
Jan Möbius committed
2
3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo    
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6
7
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
Jan Möbius's avatar
Jan Möbius committed
8
 *                                                                           *
9
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
 *                                                                           *
 * ========================================================================= */
41
42

/*===========================================================================*\
43
 *                                                                           *
44
45
 *   $Revision$                                                         *
 *   $Date$                   *
Jan Möbius's avatar
Jan Möbius committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 *                                                                           *
\*===========================================================================*/


//=============================================================================
//
//  Implements the OpenMesh IOManager singleton
//
//=============================================================================


//== INCLUDES =================================================================


#include <OpenMesh/Core/IO/IOManager.hh>

62
63
#include <iostream>

Jan Möbius's avatar
Jan Möbius committed
64
65
66
67
68
69
70
71
72
73

//== NAMESPACES ===============================================================


namespace OpenMesh {
namespace IO {


//=============================================================================

74
75
// Destructor never called. Moved into singleton  getter function
// _IOManager_  *__IOManager_instance = 0;
Jan Möbius's avatar
Jan Möbius committed
76
77
78

_IOManager_& IOManager()
{
79

80
  static _IOManager_  __IOManager_instance;
81

82
83
  //if (!__IOManager_instance)
  //  __IOManager_instance = new _IOManager_();
Jan Möbius's avatar
Jan Möbius committed
84

85
86
  return __IOManager_instance;
}
Jan Möbius's avatar
Jan Möbius committed
87
88
89

//-----------------------------------------------------------------------------

90
bool
Jan Möbius's avatar
Jan Möbius committed
91
92
_IOManager_::
read(const std::string& _filename, BaseImporter& _bi, Options& _opt)
93
{
Jan Möbius's avatar
Jan Möbius committed
94
95
  std::set<BaseReader*>::const_iterator it     =  reader_modules_.begin();
  std::set<BaseReader*>::const_iterator it_end =  reader_modules_.end();
96

Jan Möbius's avatar
Jan Möbius committed
97
98
99
100
101
102
103
104
105
  // Try all registered modules
  for(; it != it_end; ++it)
    if ((*it)->can_u_read(_filename))
    {
      _bi.prepare();
      bool ok = (*it)->read(_filename, _bi, _opt);
      _bi.finish();
      return ok;
    }
106

Jan Möbius's avatar
Jan Möbius committed
107
108
109
110
111
112
113
114
  // All modules failed to read
  return false;
}


//-----------------------------------------------------------------------------


115
bool
116
117
_IOManager_::
read(std::istream& _is, const std::string& _ext, BaseImporter& _bi, Options& _opt)
118
{
119
120
  std::set<BaseReader*>::const_iterator it     =  reader_modules_.begin();
  std::set<BaseReader*>::const_iterator it_end =  reader_modules_.end();
121

122
123
124
125
126
127
128
129
130
  // Try all registered modules
  for(; it != it_end; ++it)
    if ((*it)->BaseReader::can_u_read(_ext))  //Use the extension check only (no file existence)
    {
      _bi.prepare();
      bool ok = (*it)->read(_is, _bi, _opt);
      _bi.finish();
      return ok;
    }
131

132
133
134
135
136
137
138
139
  // All modules failed to read
  return false;
}


//-----------------------------------------------------------------------------


Jan Möbius's avatar
Jan Möbius committed
140
141
bool
_IOManager_::
142
write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision)
Jan Möbius's avatar
Jan Möbius committed
143
144
145
{
  std::set<BaseWriter*>::const_iterator it     = writer_modules_.begin();
  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
146

Jan Möbius's avatar
Jan Möbius committed
147
148
149
150
151
152
153
154
155
156
157
  if ( it == it_end )
  {
    omerr() << "[OpenMesh::IO::_IOManager_] No writing modules available!\n";
    return false;
  }

  // Try all registered modules
  for(; it != it_end; ++it)
  {
    if ((*it)->can_u_write(_filename))
    {
158
      return (*it)->write(_filename, _be, _opt, _precision);
Jan Möbius's avatar
Jan Möbius committed
159
160
    }
  }
161

Jan Möbius's avatar
Jan Möbius committed
162
163
164
165
  // All modules failed to save
  return false;
}

166
167
168
169
170
//-----------------------------------------------------------------------------


bool
_IOManager_::
171
write(std::ostream& _os,const std::string &_ext, BaseExporter& _be, Options _opt, std::streamsize _precision)
172
173
174
{
  std::set<BaseWriter*>::const_iterator it     = writer_modules_.begin();
  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
175

176
177
178
179
180
181
182
183
184
185
186
  if ( it == it_end )
  {
    omerr() << "[OpenMesh::IO::_IOManager_] No writing modules available!\n";
    return false;
  }

  // Try all registered modules
  for(; it != it_end; ++it)
  {
    if ((*it)->BaseWriter::can_u_write(_ext)) //Restrict test to the extension check
    {
187
      return (*it)->write(_os, _be, _opt, _precision);
188
189
    }
  }
190

191
192
193
  // All modules failed to save
  return false;
}
Jan Möbius's avatar
Jan Möbius committed
194
195
196
197

//-----------------------------------------------------------------------------


198
bool
Jan Möbius's avatar
Jan Möbius committed
199
200
201
202
203
204
_IOManager_::
can_read( const std::string& _format ) const
{
  std::set<BaseReader*>::const_iterator it     = reader_modules_.begin();
  std::set<BaseReader*>::const_iterator it_end = reader_modules_.end();
  std::string filename = "dummy." + _format;
205

Jan Möbius's avatar
Jan Möbius committed
206
207
208
  for(; it != it_end; ++it)
    if ((*it)->can_u_read(filename))
      return true;
209

Jan Möbius's avatar
Jan Möbius committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  return false;
}


//-----------------------------------------------------------------------------


bool
_IOManager_::
can_write( const std::string& _format ) const
{
  std::set<BaseWriter*>::const_iterator it     = writer_modules_.begin();
  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
  std::string filename = "dummy." + _format;
224

Jan Möbius's avatar
Jan Möbius committed
225
226
227
228
  // Try all registered modules
  for(; it != it_end; ++it)
    if ((*it)->can_u_write(filename))
      return true;
229

Jan Möbius's avatar
Jan Möbius committed
230
231
232
233
234
235
236
237
238
239
240
241
  return false;
}


//-----------------------------------------------------------------------------


const BaseWriter*
_IOManager_::
find_writer(const std::string& _format)
{
  using std::string;
242

Jan Möbius's avatar
Jan Möbius committed
243
244
245
246
247
248
249
  string::size_type dot = _format.rfind('.');

  string ext;
  if (dot == string::npos)
    ext = _format;
  else
    ext = _format.substr(dot+1,_format.length()-(dot+1));
250

Jan Möbius's avatar
Jan Möbius committed
251
252
253
  std::set<BaseWriter*>::const_iterator it     = writer_modules_.begin();
  std::set<BaseWriter*>::const_iterator it_end = writer_modules_.end();
  std::string filename = "dummy." + ext;
254

Jan Möbius's avatar
Jan Möbius committed
255
256
257
258
259
260
261
  // Try all registered modules
  for(; it != it_end; ++it)
    if ((*it)->can_u_write(filename))
      return *it;

  return NULL;
}
262

Jan Möbius's avatar
Jan Möbius committed
263
264
265
266
267
268
269
270
271
272

//-----------------------------------------------------------------------------


void
_IOManager_::
update_read_filters()
{
  std::set<BaseReader*>::const_iterator it     = reader_modules_.begin(),
                                        it_end = reader_modules_.end();
273
274
  std::string all = "";
  std::string filters = "";
275

Jan Möbius's avatar
Jan Möbius committed
276
277
  for(; it != it_end; ++it)
  {
278
279
280
281
    // Initialized with space, as a workaround for debug build with clang on mac
    // which crashes if tmp is initialized with an empty string ?!
    std::string tmp = " ";

Jan Möbius's avatar
Jan Möbius committed
282
    filters += (*it)->get_description() + " (";
283

Jan Möbius's avatar
Jan Möbius committed
284
    std::istringstream iss((*it)->get_extensions());
285
286
287

    while (iss && !iss.eof() && (iss >> tmp) )
    {
288
     tmp = " *." + tmp; filters += tmp; all += tmp;
289
    }
290

Jan Möbius's avatar
Jan Möbius committed
291
292
293
294
    filters += " );;";
  }

  all = "All files ( " + all + " );;";
295

Jan Möbius's avatar
Jan Möbius committed
296
297
298
299
300
301
302
303
304
305
306
307
308
  read_filters_ = all + filters;
}


//-----------------------------------------------------------------------------


void
_IOManager_::
update_write_filters()
{
  std::set<BaseWriter*>::const_iterator it     = writer_modules_.begin(),
                                        it_end = writer_modules_.end();
309
310
  std::string all;
  std::string filters;
311

Jan Möbius's avatar
Jan Möbius committed
312
313
  for(; it != it_end; ++it)
  {
314
315
316
317
    // Initialized with space, as a workaround for debug build with clang on mac
    // which crashes if tmp is initialized with an empty string ?!
    std::string s = " ";

Jan Möbius's avatar
Jan Möbius committed
318
319
320
321
322
323
324
325
326
327
328
    filters += (*it)->get_description() + " (";

    std::istringstream iss((*it)->get_extensions());
    while (iss && !iss.eof() && (iss >> s))
    { s = " *." + s; filters += s; all += s; }

    filters += " );;";
  }
  all = "All files ( " + all + " );;";

  write_filters_ = all + filters;
329
}
Jan Möbius's avatar
Jan Möbius committed
330
331
332
333
334
335


//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================