Parent: [597c93] (diff)

Child: [a93e61] (diff)

Download this file

pathut.cpp    88 lines (64 with data), 1.8 kB

 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
#ifndef lint
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.2 2004-12-14 17:54:16 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
#ifndef TEST_PATHUT
#include <pwd.h>
#include "pathut.h"
std::string path_getfather(const std::string &s) {
std::string father = s;
// ??
if (father.empty())
return "./";
if (father[father.length() - 1] == '/') {
// Input ends with /. Strip it, handle special case for root
if (father.length() == 1)
return father;
father.erase(father.length()-1);
}
std::string::size_type slp = father.rfind('/');
if (slp == std::string::npos)
return "./";
father.erase(slp);
path_catslash(father);
return father;
}
std::string path_getsimple(const std::string &s) {
std::string simple = s;
if (simple.empty())
return simple;
std::string::size_type slp = simple.rfind('/');
if (slp == std::string::npos)
return simple;
simple.erase(0, slp+1);
return simple;
}
std::string path_home()
{
uid_t uid = getuid();
struct passwd *entry = getpwuid(uid);
if (entry == 0)
return "/";
std::string homedir = entry->pw_dir;
path_catslash(homedir);
return homedir;
}
#else // TEST_PATHUT
#include <iostream>
using namespace std;
#include "pathut.h"
const char *tstvec[] = {"", "/", "/dir", "/dir/", "/dir1/dir2",
"/dir1/dir2",
"./dir", "./dir1/", "dir", "../dir", "/dir/toto.c",
"/dir/.c",
};
int main(int argc, const char **argv)
{
for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
cout << tstvec[i] << " FATHER " << path_getfather(tstvec[i]) << endl;
}
for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
cout << tstvec[i] << " SIMPLE " << path_getsimple(tstvec[i]) << endl;
}
return 0;
}
#endif // TEST_PATHUT