2 Aesalon, a tool to visualize a program's behaviour at run-time.
3 Copyright (C) 2010, Aesalon Development Team.
5 Aesalon is distributed under the terms of the GNU GPLv3. For more
6 licensing information, see the file LICENSE included with the distribution.
8 @file include/common/PathSanitizer.h
12 #ifndef AesalonCommon_PathSanitizer_H
13 #define AesalonCommon_PathSanitizer_H
24 static std::string
sanitize(const std::string
&filename
) {
25 if(filename
[0] == '/') return makeReal(filename
);
27 if(filename
[0] == '~') {
28 char *homeDirectory
= std::getenv("HOME");
29 if(homeDirectory
== NULL
) return makeReal(filename
);
30 std::string path
= filename
;
31 path
.replace(0, 1, homeDirectory
);
32 return makeReal(path
);
35 return makeReal(filename
);
37 static std::string
sanitize(const std::string
&filename
, const std::vector
<std::string
> &pathList
) {
38 std::string sanitized
= sanitize(filename
);
39 for(std::vector
<std::string
>::const_iterator i
= pathList
.begin(); i
!= pathList
.end(); ++i
) {
40 std::string fullPath
= *i
+ "/";
41 fullPath
+= sanitized
;
42 struct stat possibleStat
;
43 if(stat(fullPath
.c_str(), &possibleStat
) == 0) return fullPath
;
49 static std::string
makeReal(const std::string
&path
) {
50 char *pathStr
= realpath(path
.c_str(), NULL
);
52 std::string temp
= pathStr
;