ome-files 0.2.2

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/filesystem/path.hpp>
48 #include <boost/format.hpp>
49 #include <boost/optional.hpp>
50 #include <boost/version.hpp>
51
52 namespace ome
53 {
54 namespace files
55 {
56
63 class FormatHandler
64 {
65 protected:
67 FormatHandler()
68 {}
69
70 private:
72 FormatHandler (const FormatHandler&);
73
75 FormatHandler&
76 operator= (const FormatHandler&);
77
78 public:
80 virtual
81 ~FormatHandler()
82 {}
83
98 virtual
99 bool
100 isThisType(const boost::filesystem::path& name,
101 bool open = true) const = 0;
102
108 virtual
109 const std::string&
110 getFormat() const = 0;
111
117 virtual
118 const std::string&
119 getFormatDescription() const = 0;
120
126 virtual
127 const std::vector<boost::filesystem::path>&
128 getSuffixes() const = 0;
129
135 virtual
136 const std::vector<boost::filesystem::path>&
137 getCompressionSuffixes() const = 0;
138
147 virtual void
148 setId(const boost::filesystem::path& id) = 0;
149
169 virtual
170 void
171 close(bool fileOnly = false) = 0;
172
173 // -- Utility methods --
174
183 static bool
184 checkSuffix(const boost::filesystem::path& name,
185 const boost::filesystem::path& suffix)
186 {
187 bool match = true;
188
189 boost::filesystem::path filename(name);
190 #if !defined(BOOST_VERSION) || BOOST_VERSION >= 105000 // Boost >= 1.50
191 boost::filesystem::path ext;
192 ext.replace_extension(suffix); // Adds leading dot if missing
193 #else // Boost < 1.50
194 // replace_extension doesn't work nicely with older Boost versions.
195 boost::filesystem::path ext(suffix);
196 std::string suffixString(ext.string());
197 if (!suffixString.empty() && suffixString[0] != '.')
198 ext = boost::filesystem::path(std::string(".") + suffixString);
199 #endif // Boost version
200
201 while(true)
202 {
203 boost::filesystem::path filename_ext = filename.extension();
204 boost::filesystem::path current_ext = ext.extension();
205 filename.replace_extension();
206 ext.replace_extension();
207
208 if (filename_ext.empty() && current_ext.empty())
209 break; // End of matches
210 else if (!filename_ext.empty() && !current_ext.empty() &&
211 filename_ext == current_ext) // Match OK
212 continue;
213
214 // Unbalanced or unequal extensions.
215 match = false;
216 break;
217 }
218
219 return match;
220 }
221
230 static bool
231 checkSuffix(const boost::filesystem::path& name,
232 const std::vector<boost::filesystem::path>& suffixes)
233 {
234 for (std::vector<boost::filesystem::path>::const_iterator si = suffixes.begin();
235 si != suffixes.end();
236 ++si)
237 {
238 if (checkSuffix(name, *si))
239 return true;
240 }
241
242 return false;
243 }
244
254 static bool
255 checkSuffix(const boost::filesystem::path& name,
256 const std::vector<boost::filesystem::path>& suffixes,
257 const std::vector<boost::filesystem::path>& compression_suffixes)
258 {
259 if (checkSuffix(name, suffixes))
260 return true;
261
262 for (std::vector<boost::filesystem::path>::const_iterator csi = compression_suffixes.begin();
263 csi != compression_suffixes.end();
264 ++csi)
265 {
266 for (std::vector<boost::filesystem::path>::const_iterator si = suffixes.begin();
267 si != suffixes.end();
268 ++si)
269 {
270 #if !defined(BOOST_VERSION) || BOOST_VERSION >= 105000 // Boost >= 1.50
271 boost::filesystem::path suffix(*si);
272 suffix += boost::filesystem::path(".");
273 suffix += *csi;
274 #else // Boost < 1.50
275 boost::filesystem::path suffix(si->parent_path());
276 suffix /= boost::filesystem::path(si->filename().string() + "." + csi->string());
277 #endif // Boost version
278
279 if (checkSuffix(name, suffix))
280 return true;
281 }
282 }
283 return false;
284 }
285
297 static void
298 assertId(const boost::optional<boost::filesystem::path>& id,
299 bool notNull = true)
300 {
301 if (!id && notNull)
302 {
303 throw std::logic_error("Current file should not be null; call setId() first");
304 }
305 else if (id && !notNull)
306 {
307 boost::format fmt("Current file should be null, but is '%1%'; call close() first");
308 fmt % id.get();
309 throw std::logic_error(fmt.str());
310 }
311 }
312 };
313
314
315 }
316 }
317
318 #endif // OME_FILES_FORMATHANDLER_H
319
320 /*
321 * Local Variables:
322 * mode:C++
323 * End:
324 */
325
ome::files::FormatHandler::isThisType
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.
ome::files::FormatHandler::checkSuffix
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:255
ome::files::FormatHandler::setId
virtual void setId(const boost::filesystem::path &id)=0
Set the current file name.
ome
Open Microscopy Environment C++.
ome::files::FormatHandler::close
virtual void close(bool fileOnly=false)=0
Close the currently open file.
ome::files::FormatHandler::operator=
FormatHandler & operator=(const FormatHandler &)
Assignment operator (deleted).
ome::files::FormatHandler::FormatHandler
FormatHandler()
Constructor.
Definition: FormatHandler.h:67
ome::files::FormatHandler::getCompressionSuffixes
virtual const std::vector< boost::filesystem::path > & getCompressionSuffixes() const =0
Get the default compression suffixes for this file format.
ome::files::FormatHandler::checkSuffix
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:231
ome::files::FormatHandler::~FormatHandler
virtual ~FormatHandler()
Destructor.
Definition: FormatHandler.h:81
ome::files::FormatHandler::getFormatDescription
virtual const std::string & getFormatDescription() const =0
Get the description of this file format.
ome::files::FormatHandler::assertId
static void assertId(const boost::optional< boost::filesystem::path > &id, bool notNull=true)
Assert that the current file is valid.
Definition: FormatHandler.h:298
ome::files::FormatHandler::checkSuffix
static bool checkSuffix(const boost::filesystem::path &name, const boost::filesystem::path &suffix)
Perform suffix matching for the given filename.
Definition: FormatHandler.h:184
ome::files::FormatHandler::getSuffixes
virtual const std::vector< boost::filesystem::path > & getSuffixes() const =0
Get the default file suffixes for this file format.
ome::files::FormatHandler
Interface for all biological file format readers and writers.
Definition: FormatHandler.h:63
ome::files::FormatHandler::getFormat
virtual const std::string & getFormat() const =0
Get the name of this file format.