Revert "Started experimenting with perl/python scripts to auto rename incl guards...
[aesalon.git] / include / common / PathSanitizer.h
blobefeca2813903540cf7da034a05775498d6aa7212
1 /**
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
15 #include <string>
16 #include <vector>
17 #include <sys/stat.h>
18 #include <cstdlib>
20 namespace Common {
22 class PathSanitizer {
23 public:
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;
46 return filename;
48 private:
49 static std::string makeReal(const std::string &path) {
50 char *pathStr = realpath(path.c_str(), NULL);
51 if(pathStr != NULL) {
52 std::string temp = pathStr;
53 free(pathStr);
54 return temp;
56 return path;
60 } // namespace Common
62 #endif