bioformats  5.1.6
FormatHandler.h
1 /*
2  * #%L
3  * OME-BIOFORMATS 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_BIOFORMATS_FORMATHANDLER_H
39 #define OME_BIOFORMATS_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 bioformats
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 
157  virtual
158  void
159  close(bool fileOnly = false) = 0;
160 
161  // -- Utility methods --
162 
171  static bool
172  checkSuffix(const boost::filesystem::path& name,
173  const boost::filesystem::path& suffix)
174  {
175  bool match = true;
176 
177  boost::filesystem::path filename(name);
178 #if !defined(BOOST_VERSION) || BOOST_VERSION >= 105000 // Boost >= 1.50
179  boost::filesystem::path ext;
180  ext.replace_extension(suffix); // Adds leading dot if missing
181 #else // Boost < 1.50
182  // replace_extension doesn't work nicely with older Boost versions.
183  boost::filesystem::path ext(suffix);
184  std::string suffixString(ext.string());
185  if (!suffixString.empty() && suffixString[0] != '.')
186  ext = boost::filesystem::path(std::string(".") + suffixString);
187 #endif // Boost version
188 
189  while(true)
190  {
191  boost::filesystem::path filename_ext = filename.extension();
192  boost::filesystem::path current_ext = ext.extension();
193  filename.replace_extension();
194  ext.replace_extension();
195 
196  if (filename_ext.empty() && current_ext.empty())
197  break; // End of matches
198  else if (!filename_ext.empty() && !current_ext.empty() &&
199  filename_ext == current_ext) // Match OK
200  continue;
201 
202  // Unbalanced or unequal extensions.
203  match = false;
204  break;
205  }
206 
207  return match;
208  }
209 
218  static bool
219  checkSuffix(const boost::filesystem::path& name,
220  const std::vector<boost::filesystem::path>& suffixes)
221  {
222  for (std::vector<boost::filesystem::path>::const_iterator si = suffixes.begin();
223  si != suffixes.end();
224  ++si)
225  {
226  if (checkSuffix(name, *si))
227  return true;
228  }
229 
230  return false;
231  }
232 
242  static bool
243  checkSuffix(const boost::filesystem::path& name,
244  const std::vector<boost::filesystem::path>& suffixes,
245  const std::vector<boost::filesystem::path>& compression_suffixes)
246  {
247  if (checkSuffix(name, suffixes))
248  return true;
249 
250  for (std::vector<boost::filesystem::path>::const_iterator csi = compression_suffixes.begin();
251  csi != compression_suffixes.end();
252  ++csi)
253  {
254  for (std::vector<boost::filesystem::path>::const_iterator si = suffixes.begin();
255  si != suffixes.end();
256  ++si)
257  {
258 #if !defined(BOOST_VERSION) || BOOST_VERSION >= 105000 // Boost >= 1.50
259  boost::filesystem::path suffix(*si);
260  suffix += boost::filesystem::path(".");
261  suffix += *csi;
262 #else // Boost < 1.50
263  boost::filesystem::path suffix(si->parent_path());
264  suffix /= boost::filesystem::path(si->filename().string() + "." + csi->string());
265 #endif // Boost version
266 
267  if (checkSuffix(name, suffix))
268  return true;
269  }
270  }
271  return false;
272  }
273 
285  static void
286  assertId(const boost::optional<boost::filesystem::path>& id,
287  bool notNull = true)
288  {
289  if (!id && notNull)
290  {
291  throw std::logic_error("Current file should not be null; call setId() first");
292  }
293  else if (id && !notNull)
294  {
295  boost::format fmt("Current file should be null, but is '%1%'; call close() first");
296  fmt % id.get();
297  throw std::logic_error(fmt.str());
298  }
299  }
300  };
301 
302 
303  }
304 }
305 
306 #endif // OME_BIOFORMATS_FORMATHANDLER_H
307 
308 /*
309  * Local Variables:
310  * mode:C++
311  * End:
312  */
313 
virtual const std::vector< boost::filesystem::path > & getCompressionSuffixes() const =0
Get the default compression suffixes for this file format.
virtual void close(bool fileOnly=false)=0
Close the currently open file.
Interface for all biological file format readers and writers.
Definition: FormatHandler.h:64
virtual void setId(const boost::filesystem::path &id)=0
Set the current file name.
static void assertId(const boost::optional< boost::filesystem::path > &id, bool notNull=true)
Assert that the current file is valid.
Definition: FormatHandler.h:286
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:243
FormatHandler & operator=(const FormatHandler &)
Assignment operator (deleted).
virtual const std::string & getFormatDescription() const =0
Get the description of this file format.
Boost.Filesystem compatibility.
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
FormatHandler()
Constructor.
Definition: FormatHandler.h:68
static bool checkSuffix(const boost::filesystem::path &name, const boost::filesystem::path &suffix)
Perform suffix matching for the given filename.
Definition: FormatHandler.h:172
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.
virtual const std::string & getFormat() const =0
Get the name of this file format.
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:219
virtual ~FormatHandler()
Destructor.
Definition: FormatHandler.h:82
virtual const std::vector< boost::filesystem::path > & getSuffixes() const =0
Get the default file suffixes for this file format.