summaryrefslogtreecommitdiff
path: root/src/cstore/cpath.hpp
blob: 05fe82d54b6bf1651646c56232ff53e8f5119475 (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
/*
 * 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 _CPATH_HPP_
#define _CPATH_HPP_
#include <string>

#include <cstore/svector.hpp>

namespace cstore { // begin namespace cstore

class Cpath {
public:
  Cpath() : _data() {};
  Cpath(const Cpath& p) : _data() { operator=(p); };
  Cpath(const char *comps[], size_t num_comps) : _data() {
    for (size_t i = 0; i < num_comps; i++) {
      push(comps[i]);
    }
  };
  ~Cpath() {};

  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); };
  void clear() { _data.assign("", 0); };

  Cpath& operator=(const Cpath& p) {
    _data = p._data;
    return *this;
  };
  Cpath& operator/=(const Cpath& p) {
    _data /= p._data;
    return *this;
  }
  Cpath operator/(const Cpath& rhs) {
    Cpath lhs(*this);
    lhs /= rhs;
    return lhs;
  };

  bool operator==(const Cpath& rhs) const {
    return (_data == rhs._data);
  };
  const char *operator[](size_t idx) const {
    return _data[idx];
  };

  size_t size() const { return _data.size(); };
  size_t hash() const { return _data.hash(); };
  const char *back() const {
    return (size() > 0 ? _data[size() - 1] : NULL);
  };
  std::string to_string() const { return _data.to_string(); };

private:
  struct CpathParams {
    static const char separator = 0;
    static const size_t static_num_elems = 24;
    static const size_t static_buf_len = 256;
  };

  cstore::svector<CpathParams> _data;
};

struct CpathHash {
  inline size_t operator()(const Cpath& p) const {
    return p.hash();
  };
};

} // end namespace cstore

#endif /* _CPATH_HPP_ */