Sleeping Wombat Common Library  0.50.0
swCommonLibrary
resolver.h
1 /*
2  resolver.h -- A simple class for cross-platform path resolution
3 
4  Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #pragma once
11 
12 #include "path.h"
13 
14 NAMESPACE_BEGIN(filesystem)
15 
16 
23 class resolver {
24 public:
25  typedef std::vector<path_impl>::iterator iterator;
26  typedef std::vector<path_impl>::const_iterator const_iterator;
27 
28  resolver() {
29  m_paths.push_back(path_impl::getcwd());
30  }
31 
32  size_t size() const { return m_paths.size(); }
33 
34  iterator begin() { return m_paths.begin(); }
35  iterator end() { return m_paths.end(); }
36 
37  const_iterator begin() const { return m_paths.begin(); }
38  const_iterator end() const { return m_paths.end(); }
39 
40  void erase(iterator it) { m_paths.erase(it); }
41 
42  void prepend(const path_impl &path) { m_paths.insert(m_paths.begin(), path); }
43  void append(const path_impl &path) { m_paths.push_back(path); }
44  const path_impl &operator[](size_t index) const { return m_paths[index]; }
45  path_impl &operator[](size_t index) { return m_paths[index]; }
46 
47  path_impl resolve(const path_impl &value) const {
48  for (const_iterator it = m_paths.begin(); it != m_paths.end(); ++it) {
49  path_impl combined = *it / value;
50  if (combined.exists())
51  return combined;
52  }
53  return value;
54  }
55 
56  friend std::ostream &operator<<(std::ostream &os, const resolver &r) {
57  os << "resolver[" << std::endl;
58  for (size_t i = 0; i < r.m_paths.size(); ++i) {
59  os << " \"" << r.m_paths[i] << "\"";
60  if (i + 1 < r.m_paths.size())
61  os << ",";
62  os << std::endl;
63  }
64  os << "]";
65  return os;
66  }
67 
68 private:
69  std::vector<path_impl> m_paths;
70 };
71 
72 NAMESPACE_END(filesystem)
Simple class for resolving paths on Linux/Windows/Mac OS.
Definition: resolver.h:23
Definition: Dir.cpp:12
Simple class for manipulating paths on Linux/Windows/Mac OS.
Definition: path.h:33