summaryrefslogtreecommitdiff
path: root/src/cstore/unionfs/fspath.hpp
blob: fbaafd6302be81dd9b134b7ed50bbd0c86d521f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2011 Vyatta, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _FSPATH_HPP_
#define _FSPATH_HPP_
#include <string>

#include <cstore/svector.hpp>

namespace cstore { // begin namespace cstore
namespace unionfs { // begin namespace unionfs

class FsPath {
public:
  FsPath() : _data() {};
  explicit FsPath(const char *full_path) : _data() { operator=(full_path); };
  explicit FsPath(const std::string& full_path) : _data() {
    operator=(full_path);
  };
  FsPath(const FsPath& p) : _data() { operator=(p); };
  ~FsPath() {};

  void push(const char *comp) { _data.push_back(comp); };
  void push(const std::string& comp) { _data.push_back(comp.c_str()); };
  void pop() { _data.pop_back(); };
  void pop(std::string& last) { _data.pop_back(last); };

  FsPath& operator=(const char *full_path) {
    _data = full_path;
    return *this;
  };
  FsPath& operator=(const std::string& full_path) {
    _data = full_path;
    return *this;
  };
  FsPath& operator=(const FsPath& p) {
    _data = p._data;
    return *this;
  };
  FsPath& operator/=(const FsPath& p) {
    _data /= p._data;
    return *this;
  }
  FsPath operator/(const FsPath& rhs) {
    FsPath lhs(*this);
    lhs /= rhs;
    return lhs;
  };

  bool operator==(const FsPath& rhs) const {
    return (_data == rhs._data);
  };

  size_t length() const { return _data.length(); };
  size_t size() const { return _data.size(); };
  bool has_parent_path() const { return (_data.size() > 0); };
  const char *path_cstr() const { return _data.get_cstr(); };
  size_t hash() const { return _data.hash(); };

private:
  struct FsPathParams {
    static const char separator = '/';
    static const size_t static_num_elems = 24;
    static const size_t static_buf_len = 256;
  };

  cstore::svector<FsPathParams> _data;
};

struct FsPathHash {
  inline size_t operator()(const FsPath& p) const {
    return p.hash();
  };
};

} // end namespace unionfs
} // end namespace cstore

#endif /* _FSPATH_HPP_ */