ome-files  0.1.1
FormatHandler.h
1 /*
2  * #%L
3  * OME-FILES C++ library for image IO.
4  * Copyright © 2006 - 2015 Open Microscopy Environment:
5  * - Massachusetts Institute of Technology
6  * - National Institutes of Health
7  * - University of Dundee
8  * - Board of Regents of the University of Wisconsin-Madison
9  * - Glencoe Software, Inc.
10  * %%
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * The views and conclusions contained in the software and documentation are
33  * those of the authors and should not be interpreted as representing official
34  * policies, either expressed or implied, of any organization.
35  * #L%
36  */
37 
38 #ifndef OME_FILES_FORMATHANDLER_H
39 #define OME_FILES_FORMATHANDLER_H
40 
41 #include <algorithm>
42 #include <iterator>
43 #include <stdexcept>
44 #include <string>
45 #include <vector>
46 
47 #include <boost/format.hpp>
48 #include <boost/optional.hpp>
49 #include <boost/version.hpp>
50 
51 #include <ome/common/filesystem.h>
52 
53 namespace ome
54 {
55  namespace files
56  {
57 
65  {
66  protected:
69  {}
70 
71  private:
74 
77  operator= (const FormatHandler&);
78 
79  public:
81  virtual
83  {}
84 
99  virtual
100  bool
101  isThisType(const boost::filesystem::path& name,
102  bool open = true) const = 0;
103 
109  virtual
110  const std::string&
111  getFormat() const = 0;
112 
118  virtual
119  const std::string&
120  getFormatDescription() const = 0;
121 
127  virtual
128  const std::vector<boost::filesystem::path>&
129  getSuffixes() const = 0;
130 
136  virtual
137  const std::vector<boost::filesystem::path>&
138  getCompressionSuffixes() const = 0;
139 
148  virtual void
149  setId(const boost::filesystem::path& id) = 0;
150 
170  virtual
171  void
172  close(bool fileOnly = false) = 0;
173 
174  // -- Utility methods --
175 
184  static bool
185  checkSuffix(const boost::filesystem::path& name,
186  const boost::filesystem::path& suffix)
187  {
188  bool match = true;
189 
190  boost::filesystem::path filename(name);
191 #if !defined(BOOST_VERSION) || BOOST_VERSION >= 105000 // Boost >= 1.50
192  boost::filesystem::path ext;
193  ext.replace_extension(suffix); // Adds leading dot if missing
194 #else // Boost < 1.50
195  // replace_extension doesn't work nicely with older Boost versions.
196  boost::filesystem::path ext(suffix);
197  std::string suffixString(ext.string());
198  if (!suffixString.empty() && suffixString[0] != '.')
199  ext = boost::filesystem::path(std::string(".") + suffixString);
200 #endif // Boost version
201 
202  while(true)
203  {
204  boost::filesystem::path filename_ext = filename.extension();
205  boost::filesystem::path current_ext = ext.extension();
206  filename.replace_extension();
207  ext.replace_extension();
208 
209  if (filename_ext.empty() && current_ext.empty())
210  break; // End of matches
211  else if (!filename_ext.empty() && !current_ext.empty() &&
212  filename_ext == current_ext) // Match OK
213  continue;
214 
215  // Unbalanced or unequal extensions.
216  match = false;
217  break;
218  }
219 
220  return match;
221  }
222 
231  static bool
232  checkSuffix(const boost::filesystem::path& name,
233  const std::vector<boost::filesystem::path>& suffixes)
234  {
235  for (std::vector<boost::filesystem::path>::const_iterator si = suffixes.begin();
236  si != suffixes.end();
237  ++si)
238  {
239  if (checkSuffix(name, *si))
240  return true;
241  }
242 
243  return false;
244  }
245 
255  static bool
256  checkSuffix(const boost::filesystem::path& name,
257  const std::vector<boost::filesystem::path>& suffixes,
258  const std::vector<boost::filesystem::path>& compression_suffixes)
259  {
260  if (checkSuffix(name, suffixes))
261  return true;
262 
263  for (std::vector<boost::filesystem::path>::const_iterator csi = compression_suffixes.begin();
264  csi != compression_suffixes.end();
265  ++csi)
266  {
267  for (std::vector<boost::filesystem::path>::const_iterator si = suffixes.begin();
268  si != suffixes.end();
269  ++si)
270  {
271 #if !defined(BOOST_VERSION) || BOOST_VERSION >= 105000 // Boost >= 1.50
272  boost::filesystem::path suffix(*si);
273  suffix += boost::filesystem::path(".");
274  suffix += *csi;
275 #else // Boost < 1.50
276  boost::filesystem::path suffix(si->parent_path());
277  suffix /= boost::filesystem::path(si->filename().string() + "." + csi->string());
278 #endif // Boost version
279 
280  if (checkSuffix(name, suffix))
281  return true;
282  }
283  }
284  return false;
285  }
286 
298  static void
299  assertId(const boost::optional<boost::filesystem::path>& id,
300  bool notNull = true)
301  {
302  if (!id && notNull)
303  {
304  throw std::logic_error("Current file should not be null; call setId() first");
305  }
306  else if (id && !notNull)
307  {
308  boost::format fmt("Current file should be null, but is '%1%'; call close() first");
309  fmt % id.get();
310  throw std::logic_error(fmt.str());
311  }
312  }
313  };
314 
315 
316  }
317 }
318 
319 #endif // OME_FILES_FORMATHANDLER_H
320 
321 /*
322  * Local Variables:
323  * mode:C++
324  * End:
325  */
326 
static bool checkSuffix(const boost::filesystem::path &name, const std::vector< boost::filesystem::path > &suffixes, const std::vector< boost::filesystem::path > &compression_suffixes)
Perform suffix matching for the given filename.
Definition: FormatHandler.h:256
virtual const std::vector< boost::filesystem::path > & getSuffixes() const =0
Get the default file suffixes for this file format.
virtual const std::string & getFormatDescription() const =0
Get the description of this file format.
virtual void setId(const boost::filesystem::path &id)=0
Set the current file name.
virtual void close(bool fileOnly=false)=0
Close the currently open file.
virtual bool isThisType(const boost::filesystem::path &name, bool open=true) const =0
Check if the given file is a valid instance of this file format.
FormatHandler & operator=(const FormatHandler &)
Assignment operator (deleted).
FormatHandler()
Constructor.
Definition: FormatHandler.h:68
static bool checkSuffix(const boost::filesystem::path &name, const std::vector< boost::filesystem::path > &suffixes)
Perform suffix matching for the given filename.
Definition: FormatHandler.h:232
virtual ~FormatHandler()
Destructor.
Definition: FormatHandler.h:82
virtual const std::string & getFormat() const =0
Get the name of this file format.
static void assertId(const boost::optional< boost::filesystem::path > &id, bool notNull=true)
Assert that the current file is valid.
Definition: FormatHandler.h:299
static bool checkSuffix(const boost::filesystem::path &name, const boost::filesystem::path &suffix)
Perform suffix matching for the given filename.
Definition: FormatHandler.h:185
virtual const std::vector< boost::filesystem::path > & getCompressionSuffixes() const =0
Get the default compression suffixes for this file format.
Interface for all biological file format readers and writers.
Definition: FormatHandler.h:64