forked from membase/ep-engine
/
pathexpand.hh
61 lines (47 loc) · 1.26 KB
/
pathexpand.hh
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef PATHEXPAND_HH
#define PATHEXPAND_HH 1
#include "config.h"
#include <string>
#include <cassert>
#include <libgen.h>
extern const char* path_separator;
static std::string pe_basename(const char *b) {
assert(b);
std::string s(b);
size_t lastthing(s.find_last_of(path_separator));
if (lastthing == s.npos) {
return s;
}
return s.substr(lastthing + 1);
}
static std::string pe_dirname(const char *b) {
assert(b);
std::string s(b);
size_t lastthing(s.find_last_of(path_separator));
if (lastthing == s.npos) {
return std::string(".");
}
return s.substr(0, lastthing);
}
/**
* Expand paths for DB shards.
*
* Available variables:
*
* %d - the directory containing the base shard
* %b - the basename of the main db
* %i - the shard ID
*/
class PathExpander {
public:
//! Create a path expander with the given main db path.
PathExpander(const char *p) : dir(pe_dirname(p)),
base(pe_basename(p)) { }
//! Expand the path to the given shard ID.
std::string expand(const char *pattern, int shardId);
private:
std::string dir;
std::string base;
};
#endif // PATHEXPAND_HH