1 | #include <string>
|
---|
2 | #include <iostream>
|
---|
3 | #include <stdexcept>
|
---|
4 | #include <cassert>
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | class PathString_for_ptree
|
---|
9 | {
|
---|
10 | string filename;
|
---|
11 | string::size_type start, size;
|
---|
12 |
|
---|
13 | public:
|
---|
14 | PathString_for_ptree( std::string const& s ) :
|
---|
15 | filename(s), start(0), size(filename.size())
|
---|
16 | {}
|
---|
17 |
|
---|
18 | string dump() const
|
---|
19 | {
|
---|
20 | return filename.substr(start);
|
---|
21 | }
|
---|
22 |
|
---|
23 | bool empty() const
|
---|
24 | {
|
---|
25 | return start == size;
|
---|
26 | }
|
---|
27 |
|
---|
28 | // aka pop_front and return the fragment
|
---|
29 | // modifies the path
|
---|
30 | string reduce()
|
---|
31 | {
|
---|
32 | assert(not empty());
|
---|
33 |
|
---|
34 | // don't crash in production, just in case
|
---|
35 | if (empty())
|
---|
36 | return string();
|
---|
37 |
|
---|
38 | string::size_type next_sep = filename.find('/', start);
|
---|
39 |
|
---|
40 | // no separator, then pretend separator is at the end
|
---|
41 | if (next_sep == string::npos)
|
---|
42 | next_sep = size-1;
|
---|
43 |
|
---|
44 | // include the trailing separator
|
---|
45 | string::size_type old_start = start;
|
---|
46 | start = next_sep+1;
|
---|
47 |
|
---|
48 | return filename.substr(old_start, start-old_start);
|
---|
49 | }
|
---|
50 |
|
---|
51 | bool single() const
|
---|
52 | {
|
---|
53 | // will return true if empty, it should not be asked for empty paths anyway
|
---|
54 |
|
---|
55 | // it is a "single" path if there is a slash BEFORE the last character.
|
---|
56 | // if the last character is /, then it can be a "single" directory.
|
---|
57 | //
|
---|
58 | std::string::size_type idx = filename.find('/', start);
|
---|
59 | return idx == std::string::npos or idx == size-1;
|
---|
60 | }
|
---|
61 |
|
---|
62 | };
|
---|
63 |
|
---|
64 | static void test( string f, string should_frag, string should_remainder )
|
---|
65 | {
|
---|
66 | PathString_for_ptree f2(f);
|
---|
67 | cout << f2.dump() << (f2.single() ? " SINGLE" : "") << " --> ";
|
---|
68 | string frag = f2.reduce();
|
---|
69 | cout << frag << " && " << f2.dump() << endl;
|
---|
70 | if (frag != should_frag)
|
---|
71 | throw logic_error("err frag");
|
---|
72 | if (f2.dump() != should_remainder)
|
---|
73 | throw logic_error("err remainder");
|
---|
74 | }
|
---|
75 |
|
---|
76 | static void descend( string f )
|
---|
77 | {
|
---|
78 | PathString_for_ptree f2(f);
|
---|
79 | while (not f2.empty())
|
---|
80 | {
|
---|
81 | cout << f2.dump() << (f2.single() ? " SINGLE" : " more") << " --> ";
|
---|
82 | string frag = f2.reduce();
|
---|
83 | cout << frag << " && " << f2.dump() << endl;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | int main()
|
---|
89 | {
|
---|
90 | test("hello/there/you.txt", "hello/", "there/you.txt");
|
---|
91 | test("hello/there/you/", "hello/", "there/you/");
|
---|
92 | test("hello/there/", "hello/", "there/");
|
---|
93 | test("hello/there.txt", "hello/", "there.txt");
|
---|
94 | test("there.txt", "there.txt", "");
|
---|
95 | test("a", "a", "");
|
---|
96 | test("a/", "a/", "");
|
---|
97 | test("b/a", "b/", "a");
|
---|
98 | test("b/a/", "b/", "a/");
|
---|
99 | test("/a/", "/", "a/");
|
---|
100 | test("./a/", "./", "a/");
|
---|
101 |
|
---|
102 | descend("hello/there/you.txt");
|
---|
103 |
|
---|
104 | return 0;
|
---|
105 | }
|
---|