/
pathexpand.cc
69 lines (56 loc) · 1.42 KB
/
pathexpand.cc
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"
#include <sstream>
#include "pathexpand.hh"
#ifdef WIN32
const char* path_separator("\\/");
#else
const char* path_separator("/");
#endif
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);
}
PathExpander::PathExpander(const char *p) : dir(pe_dirname(p)),
base(pe_basename(p)) {
}
std::string PathExpander::expand(const char *pattern, int shardId) {
std::stringstream ss;
while (*pattern) {
if (*pattern == '%') {
++pattern;
switch (*pattern) {
case 'd':
ss << dir;
break;
case 'b':
ss << base;
break;
case 'i':
ss << shardId;
break;
default:
ss << '%' << *pattern;
}
} else {
ss << *pattern;
}
++pattern;
}
return ss.str();
}