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