1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
4 | */ |
---|
5 | |
---|
6 | #include <string.h> |
---|
7 | #ifdef _WIN32 |
---|
8 | #include <direct.h> |
---|
9 | #endif |
---|
10 | #include <stdio.h> |
---|
11 | #include <sys/types.h> |
---|
12 | #include <sys/stat.h> |
---|
13 | #include <unistd.h> |
---|
14 | |
---|
15 | #include "FilePath.h" |
---|
16 | |
---|
17 | #include "Trace.h" |
---|
18 | |
---|
19 | using namespace nv::util; |
---|
20 | |
---|
21 | std::string FilePath::_curDirectory; |
---|
22 | FilePath FilePath::_instance; |
---|
23 | |
---|
24 | static char seps[] = ":"; |
---|
25 | |
---|
26 | FilePath::FilePath() |
---|
27 | { |
---|
28 | char buff[BUFSIZ+2]; |
---|
29 | #ifdef _WIN32 |
---|
30 | _getcwd(buff, sizeof(buff)-1); |
---|
31 | #else |
---|
32 | if (getcwd(buff, sizeof(buff)-1) == NULL) { |
---|
33 | ERROR("can't get current working directory\n"); |
---|
34 | } |
---|
35 | #endif |
---|
36 | size_t length = strlen(buff); |
---|
37 | buff[length] = '/'; |
---|
38 | buff[length + 1] = '\0'; |
---|
39 | _curDirectory = buff; |
---|
40 | } |
---|
41 | |
---|
42 | bool |
---|
43 | FilePath::setPath(const char *filePath) |
---|
44 | { |
---|
45 | char buff[256]; |
---|
46 | |
---|
47 | if (filePath == NULL) { |
---|
48 | return false; |
---|
49 | } |
---|
50 | _pathList.clear(); |
---|
51 | |
---|
52 | char *p; |
---|
53 | strcpy(buff, filePath); |
---|
54 | for (p = strtok(buff, seps); p != NULL; p = strtok(NULL, seps)) { |
---|
55 | char *last; |
---|
56 | struct stat buf; |
---|
57 | if ((stat(p, &buf) != 0) || (!S_ISDIR(buf.st_mode))) { |
---|
58 | return false; |
---|
59 | } |
---|
60 | last = p + strlen(p) - 1; |
---|
61 | if (*p == '/') { |
---|
62 | if (*last == '/' || *last == '\\') { |
---|
63 | _pathList.push_back(std::string(p)); |
---|
64 | } else { |
---|
65 | _pathList.push_back(std::string(p) + "/"); |
---|
66 | } |
---|
67 | } else { |
---|
68 | if (*last == '/' || *last == '\\') { |
---|
69 | _pathList.push_back(_curDirectory + std::string(p)); |
---|
70 | } else { |
---|
71 | _pathList.push_back(_curDirectory + std::string(p) + "/"); |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | return true; |
---|
76 | } |
---|
77 | |
---|
78 | FilePath * |
---|
79 | FilePath::getInstance() |
---|
80 | { |
---|
81 | return &_instance; |
---|
82 | } |
---|
83 | |
---|
84 | std::string |
---|
85 | FilePath::getPath(const char* fileName) |
---|
86 | { |
---|
87 | std::string path; |
---|
88 | |
---|
89 | for (StringListIter iter = _pathList.begin(); |
---|
90 | iter != _pathList.end(); ++iter) { |
---|
91 | path = *iter + std::string(fileName); |
---|
92 | |
---|
93 | if (access(path.c_str(), R_OK) == 0) { |
---|
94 | return path; |
---|
95 | } else { |
---|
96 | path = ""; |
---|
97 | } |
---|
98 | } |
---|
99 | return path; |
---|
100 | } |
---|
101 | |
---|
102 | void |
---|
103 | FilePath::setWorkingDirectory(int argc, const char **argv) |
---|
104 | { |
---|
105 | char buff[256]; |
---|
106 | |
---|
107 | strcpy(buff, argv[0]); |
---|
108 | for (int i = strlen(buff) - 1; i >= 0; --i) { |
---|
109 | if (buff[i] == '\\' || buff[i] == '/') { |
---|
110 | buff[i] = '/'; |
---|
111 | buff[i + 1] = '\0'; |
---|
112 | break; |
---|
113 | } |
---|
114 | } |
---|
115 | _curDirectory = buff; |
---|
116 | } |
---|