ome-common  5.5.0
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 
73 # ifdef OME_HAVE_BOOST_FILESYSTEM_ABSOLUTE
75 # else // OME_HAVE_BOOST_FILESYSTEM_ABSOLUTE not defined
76 
83  inline
84  boost::filesystem::path
85  absolute(const boost::filesystem::path& p,
86  const boost::filesystem::path& base = boost::filesystem::current_path())
87  {
88  return boost::filesystem::complete(p, base);
89  }
90 # endif // OME_HAVE_BOOST_FILESYSTEM_ABSOLUTE
91 
93 # ifdef OME_HAVE_BOOST_FILESYSTEM_CANONICAL
95 # else // OME_HAVE_BOOST_FILESYSTEM_CANONICAL not defined
96  // Implementation derived from boost 1.55.
105  inline
106  boost::filesystem::path
107  canonical(const boost::filesystem::path& p,
108  const boost::filesystem::path& base = boost::filesystem::current_path(),
109  boost::system::error_code* ec = 0)
110  {
111  boost::filesystem::path source (absolute(p, base));
112  boost::filesystem::path result;
113 
114  boost::system::error_code local_ec;
115  boost::filesystem::file_status stat (status(source, local_ec));
116 
117  if (stat.type() == boost::filesystem::file_not_found)
118  {
119  if (ec == 0)
120  throw boost::filesystem::filesystem_error
121  ("boost::filesystem::canonical", source,
122  boost::system::error_code(boost::system::errc::no_such_file_or_directory,
123  boost::system::get_generic_category()));
124  ec->assign(boost::system::errc::no_such_file_or_directory,
125  boost::system::get_generic_category());
126  return result;
127  }
128  else if (local_ec)
129  {
130  if (ec == 0)
131  throw(boost::filesystem::filesystem_error
132  ("boost::filesystem::canonical", source, local_ec));
133  *ec = local_ec;
134  return result;
135  }
136  }
137 # endif // OME_HAVE_BOOST_FILESYSTEM_CANONICAL
138 
146  inline
147  boost::filesystem::path
148  make_relative(boost::filesystem::path from,
149  boost::filesystem::path to)
150  {
151  from = absolute(from);
152  to = absolute(to);
153  boost::filesystem::path ret;
154  boost::filesystem::path::const_iterator itrFrom(from.begin());
155  boost::filesystem::path::const_iterator itrTo(to.begin());
156 
157  // Find common base
158  for(boost::filesystem::path::const_iterator toEnd(to.end()), fromEnd(from.end());
159  itrFrom != fromEnd && itrTo != toEnd && *itrFrom == *itrTo;
160  ++itrFrom, ++itrTo);
161 
162  // Navigate backwards in directory to reach previously found base
163  for(boost::filesystem::path::const_iterator fromEnd(from.end());
164  itrFrom != fromEnd;
165  ++itrFrom )
166  {
167  if((*itrFrom) != ".")
168  ret /= "..";
169  }
170  // Now navigate down the directory branch
171  for (boost::filesystem::path::iterator begin = itrTo;
172  begin != to.end();
173  ++begin)
174  ret /= *begin;
175 
176  return ret;
177  }
178 
179  }
180 }
181 
182 #endif // OME_COMMON_FILESYSTEM_H
183 
184 /*
185  * Local Variables:
186  * mode:C++
187  * End:
188  */
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:107
boost::filesystem::path make_relative(boost::filesystem::path from, boost::filesystem::path to)
Make a relative path.
Definition: filesystem.h:148
Open Microscopy Environment C++.
Definition: base64.h:48
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:85