bioformats  5.1.8
filesystem.h
Go to the documentation of this file.
1 /*
2  * #%L
3  * OME-COMMON C++ library for C++ compatibility/portability
4  * %%
5  * Copyright © 2006 - 2016 Open Microscopy Environment:
6  * - Massachusetts Institute of Technology
7  * - National Institutes of Health
8  * - University of Dundee
9  * - Board of Regents of the University of Wisconsin-Madison
10  * - Glencoe Software, Inc.
11  * %%
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * The views and conclusions contained in the software and documentation are
34  * those of the authors and should not be interpreted as representing official
35  * policies, either expressed or implied, of any organization.
36  * #L%
37  */
38 
46 #ifndef OME_COMMON_FILESYSTEM_H
47 # define OME_COMMON_FILESYSTEM_H
48 
49 # include <ome/common/config.h>
50 
51 #include <boost/filesystem/operations.hpp>
52 #include <boost/filesystem/path.hpp>
53 
54 // Older versions use get_generic_category() in place of the new
55 // generic_category()
56 # ifdef OME_HAVE_BOOST_FILESYSTEM_CANONICAL
57 # ifdef BOOST_SYSTEM_NO_DEPRECATED
58 # undef BOOST_SYSTEM_NO_DEPRECATED
59 # endif // BOOST_SYSTEM_NO_DEPRECATED
60 # include <boost/system/error_code.hpp>
61 # endif // OME_HAVE_BOOST_FILESYSTEM_CANONICAL
62 
63 namespace ome
64 {
65 
69  namespace common
70  {
71 
72 # ifdef OME_HAVE_BOOST_FILESYSTEM_ABSOLUTE
74 # else // OME_HAVE_BOOST_FILESYSTEM_ABSOLUTE not defined
75 
82  inline
83  boost::filesystem::path
84  absolute(const boost::filesystem::path& p,
85  const boost::filesystem::path& base = boost::filesystem::current_path())
86  {
87  return boost::filesystem::complete(p, base);
88  }
89 # endif // OME_HAVE_BOOST_FILESYSTEM_ABSOLUTE
90 
91 # ifdef OME_HAVE_BOOST_FILESYSTEM_CANONICAL
93 # else // OME_HAVE_BOOST_FILESYSTEM_CANONICAL not defined
94  // Implementation derived from boost 1.55.
103  inline
104  boost::filesystem::path
105  canonical(const boost::filesystem::path& p,
106  const boost::filesystem::path& base = boost::filesystem::current_path(),
107  boost::system::error_code* ec = 0)
108  {
109  boost::filesystem::path source (absolute(p, base));
110  boost::filesystem::path result;
111 
112  boost::system::error_code local_ec;
113  boost::filesystem::file_status stat (status(source, local_ec));
114 
115  if (stat.type() == boost::filesystem::file_not_found)
116  {
117  if (ec == 0)
118  throw boost::filesystem::filesystem_error
119  ("boost::filesystem::canonical", source,
120  boost::system::error_code(boost::system::errc::no_such_file_or_directory,
121  boost::system::get_generic_category()));
122  ec->assign(boost::system::errc::no_such_file_or_directory,
123  boost::system::get_generic_category());
124  return result;
125  }
126  else if (local_ec)
127  {
128  if (ec == 0)
129  throw(boost::filesystem::filesystem_error
130  ("boost::filesystem::canonical", source, local_ec));
131  *ec = local_ec;
132  return result;
133  }
134  }
135 # endif // OME_HAVE_BOOST_FILESYSTEM_CANONICAL
136 
144  inline
145  boost::filesystem::path
146  make_relative(boost::filesystem::path from,
147  boost::filesystem::path to)
148  {
149  from = absolute(from);
150  to = absolute(to);
151  boost::filesystem::path ret;
152  boost::filesystem::path::const_iterator itrFrom(from.begin());
153  boost::filesystem::path::const_iterator itrTo(to.begin());
154 
155  // Find common base
156  for(boost::filesystem::path::const_iterator toEnd(to.end()), fromEnd(from.end());
157  itrFrom != fromEnd && itrTo != toEnd && *itrFrom == *itrTo;
158  ++itrFrom, ++itrTo);
159 
160  // Navigate backwards in directory to reach previously found base
161  for(boost::filesystem::path::const_iterator fromEnd(from.end());
162  itrFrom != fromEnd;
163  ++itrFrom )
164  {
165  if((*itrFrom) != ".")
166  ret /= "..";
167  }
168  // Now navigate down the directory branch
169  for (boost::filesystem::path::iterator begin = itrTo;
170  begin != to.end();
171  ++begin)
172  ret /= *begin;
173 
174  return ret;
175  }
176 
177  }
178 }
179 
180 #endif // OME_COMMON_FILESYSTEM_H
181 
182 /*
183  * Local Variables:
184  * mode:C++
185  * End:
186  */
boost::filesystem::path canonical(const boost::filesystem::path &p, const boost::filesystem::path &base=boost::filesystem::current_path(), boost::system::error_code *ec=0)
Get a canonical path.
Definition: filesystem.h:105
boost::filesystem::path make_relative(boost::filesystem::path from, boost::filesystem::path to)
Make a relative path.
Definition: filesystem.h:146
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
boost::filesystem::path absolute(const boost::filesystem::path &p, const boost::filesystem::path &base=boost::filesystem::current_path())
Get an absolute path.
Definition: filesystem.h:84